1
0
mirror of https://github.com/robonen/tools.git synced 2026-03-20 19:04:46 +00:00

feat(web/vue): add useStorage and useStorageAsync, separate all composables by categories

This commit is contained in:
2026-02-14 21:38:29 +07:00
parent 7dce7ed482
commit 6565fa3de8
64 changed files with 1564 additions and 55 deletions

View File

@@ -0,0 +1,62 @@
import { inject as vueInject, provide as vueProvide, type InjectionKey, type App } from 'vue';
import { VueToolsError } from '@/utils';
/**
* @name useContextFactory
* @category State
* @description A composable that provides a factory for creating context with unique key
*
* @param {string} name The name of the context
* @returns {Object} An object with `inject`, `provide`, `appProvide` and `key` properties
* @throws {VueToolsError} when the context is not provided
*
* @example
* const { inject, provide } = useContextFactory('MyContext');
*
* provide('Hello World');
* const value = inject();
*
* @example
* const { inject: injectContext, appProvide } = useContextFactory('MyContext');
*
* // In a plugin
* {
* install(app) {
* appProvide(app)('Hello World');
* }
* }
*
* // In a component
* const value = injectContext();
*
* @since 0.0.1
*/
export function useContextFactory<ContextValue>(name: string) {
const injectionKey: InjectionKey<ContextValue> = Symbol(name);
const inject = <Fallback extends ContextValue = ContextValue>(fallback?: Fallback) => {
const context = vueInject(injectionKey, fallback);
if (context !== undefined)
return context;
throw new VueToolsError(`useContextFactory: '${name}' context is not provided`);
};
const provide = (context: ContextValue) => {
vueProvide(injectionKey, context);
return context;
};
const appProvide = (app: App) => (context: ContextValue) => {
app.provide(injectionKey, context);
return context;
};
return {
inject,
provide,
appProvide,
key: injectionKey,
}
}