mirror of
https://github.com/robonen/tools.git
synced 2026-03-20 02:44:45 +00:00
refactor(packages/vue): return object instead of tuple for useContextFactory
This commit is contained in:
@@ -9,7 +9,7 @@ function testFactory<Data>(
|
||||
context: ReturnType<typeof useContextFactory<Data>>,
|
||||
fallback?: Data,
|
||||
) {
|
||||
const [inject, provide] = context;
|
||||
const { inject, provide } = context;
|
||||
|
||||
const Child = defineComponent({
|
||||
setup() {
|
||||
@@ -72,7 +72,7 @@ describe('useContextFactory', () => {
|
||||
|
||||
const childComponent = mount(Child, {
|
||||
global: {
|
||||
plugins: [app => context[1]('test', app)],
|
||||
plugins: [app => context.provide('test', app)],
|
||||
},
|
||||
});
|
||||
|
||||
|
||||
@@ -7,17 +7,17 @@ import { VueToolsError } from '../..';
|
||||
* @description A composable that provides a factory for creating context with unique key
|
||||
*
|
||||
* @param {string} name The name of the context
|
||||
* @returns {readonly [injectContext, provideContext]} The context factory
|
||||
* @returns {Object} An object with `inject`, `provide` and `key` properties
|
||||
* @throws {VueToolsError} when the context is not provided
|
||||
*
|
||||
* @example
|
||||
* const [injectContext, provideContext] = useContextFactory('MyContext');
|
||||
* const { inject, provide } = useContextFactory('MyContext');
|
||||
*
|
||||
* const context = provideContext('Hello World');
|
||||
* const value = injectContext();
|
||||
* provide('Hello World');
|
||||
* const value = inject();
|
||||
*
|
||||
* @example
|
||||
* const [injectContext, provideContext] = useContextFactory('MyContext');
|
||||
* const { inject: injectContext, provide: provideContext } = useContextFactory('MyContext');
|
||||
*
|
||||
* // In a plugin
|
||||
* {
|
||||
@@ -44,9 +44,13 @@ export function useContextFactory<ContextValue>(name: string) {
|
||||
};
|
||||
|
||||
const provideContext = (context: ContextValue, app?: App) => {
|
||||
(app ? app.provide : provide)(injectionKey, context);
|
||||
(app?.provide ?? provide)(injectionKey, context);
|
||||
return context;
|
||||
};
|
||||
|
||||
return [injectContext, provideContext] as const;
|
||||
return {
|
||||
inject: injectContext,
|
||||
provide: provideContext,
|
||||
key: injectionKey,
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user