|
1 |
| -import { provide, getCurrentInstance, onUnmounted, reactive, ref } from 'vue'; |
2 |
| - |
3 |
| -import { Container } from 'inversify'; |
4 |
| - |
5 |
| -const CONTAINER_KEY = 'USE_VUE_SERVICE_CONTAINER_KEY'; |
| 1 | +import { |
| 2 | + provide, |
| 3 | + inject, |
| 4 | + getCurrentInstance, |
| 5 | + onUnmounted, |
| 6 | + reactive, |
| 7 | + ref, |
| 8 | + hasInjectionContext, |
| 9 | + type Component, |
| 10 | +} from 'vue'; |
| 11 | +import { Container, ContainerModule, type interfaces } from 'inversify'; |
6 | 12 |
|
| 13 | +const CONTAINER_TOKEN = 'USE_VUE_SERVICE_CONTAINER_TOKEN'; |
| 14 | +export function createToken<T>(desc: string): interfaces.ServiceIdentifier<T> { |
| 15 | + return Symbol.for(desc); |
| 16 | +} |
| 17 | +export const COMPONENT_TOKEN = createToken<Component>( |
| 18 | + 'USE_VUE_SERVICE_COMPONENT_TOKEN' |
| 19 | +); |
7 | 20 | const DEFAULT_CONTAINER_OPTIONS = {
|
| 21 | + reactive: true, |
8 | 22 | autoBindInjectable: false,
|
9 | 23 | defaultScope: 'Singleton',
|
10 | 24 | skipBaseClassChecks: false,
|
11 | 25 | };
|
12 |
| - |
13 |
| -function createContainer(parent?: Container, options?: any) { |
| 26 | +function getOptions(options: any) { |
| 27 | + return Object.assign({}, DEFAULT_CONTAINER_OPTIONS, options); |
| 28 | +} |
| 29 | +function makeReactiveObject(_: any, obj: any) { |
| 30 | + if (typeof obj === 'object') { |
| 31 | + return reactive(obj); |
| 32 | + } else { |
| 33 | + return ref(obj); |
| 34 | + } |
| 35 | +} |
| 36 | +function createContainer(parent?: Container, opts?: any) { |
| 37 | + let container: Container; |
| 38 | + const options = getOptions(opts); |
14 | 39 | if (parent) {
|
15 |
| - return parent.createChild(options); |
| 40 | + container = parent.createChild(options); |
| 41 | + } else { |
| 42 | + container = new Container(options); |
16 | 43 | }
|
17 |
| - return new Container(options); |
| 44 | + if (opts?.instance) { |
| 45 | + container.bind(COMPONENT_TOKEN).toConstantValue(opts.instance); |
| 46 | + } |
| 47 | + return options?.reactive ? reactiveContainer(container) : container; |
| 48 | +} |
| 49 | +function reactiveContainer(container: Container) { |
| 50 | + const originalBind = container.bind; |
| 51 | + const newBind = (serviceIdentifier: any) => { |
| 52 | + const bindingToSyntax = originalBind.call(container, serviceIdentifier); |
| 53 | + const protos = Object.getPrototypeOf(bindingToSyntax); |
| 54 | + const methods = Object.getOwnPropertyNames(protos).filter( |
| 55 | + p => typeof protos[p] === 'function' && p.indexOf('to') === 0 |
| 56 | + ); |
| 57 | + for (let i = 0; i < methods.length; i++) { |
| 58 | + const method = methods[i]; |
| 59 | + const originalMethod = (protos as any)[method]; |
| 60 | + (bindingToSyntax as any)[method] = (...args: any[]) => { |
| 61 | + const result = originalMethod.call(bindingToSyntax, ...args); |
| 62 | + if (result?.onActivation) { |
| 63 | + result.onActivation(makeReactiveObject); |
| 64 | + } |
| 65 | + return result; |
| 66 | + }; |
| 67 | + } |
| 68 | + return bindingToSyntax; |
| 69 | + }; |
| 70 | + container.bind = newBind as any; |
| 71 | + return container; |
18 | 72 | }
|
19 | 73 | function bindContainer(container: Container, providers: any) {
|
20 |
| - if (typeof providers === 'function') { |
| 74 | + if (providers instanceof ContainerModule) { |
| 75 | + container.load(providers); |
| 76 | + } else if (typeof providers === 'function') { |
21 | 77 | providers(container);
|
22 | 78 | } else {
|
23 | 79 | for (let i = 0; i < providers.length; i++) {
|
24 | 80 | const s = providers[i];
|
25 | 81 | container.bind(s).toSelf();
|
26 | 82 | }
|
27 | 83 | }
|
28 |
| - container.onActivation('_', (_: any, instance: any) => { |
29 |
| - return typeof instance === 'object' ? reactive(instance) : ref(instance); |
30 |
| - }); |
31 | 84 | }
|
32 |
| - |
33 |
| -const DEFAULT_CONTAINER = createContainer(); |
34 |
| - |
35 |
| -function getServiceFromContainer(container: Container, token: any) { |
| 85 | +function getServiceFromContainer<T>( |
| 86 | + container: Container, |
| 87 | + token: interfaces.ServiceIdentifier<T> |
| 88 | +) { |
36 | 89 | return container.get(token);
|
37 | 90 | }
|
38 |
| - |
39 |
| -function injectFromSelf(key: any, defaultValue: any) { |
| 91 | +function getCurrentContainer() { |
40 | 92 | const instance: any = getCurrentInstance();
|
41 | 93 | if (instance) {
|
| 94 | + const token = CONTAINER_TOKEN; |
42 | 95 | const provides = instance.provides;
|
43 |
| - return provides[key] || defaultValue; |
| 96 | + if (provides.hasOwnProperty(token)) { |
| 97 | + return provides[token]; |
| 98 | + } |
44 | 99 | } else {
|
45 | 100 | console.warn(
|
46 |
| - `inject() can only be used inside setup() or functional components.` |
| 101 | + `declareProviders can only be used inside setup() or functional components.` |
| 102 | + ); |
| 103 | + } |
| 104 | +} |
| 105 | +function getContextContainer() { |
| 106 | + if (hasInjectionContext()) { |
| 107 | + const token = CONTAINER_TOKEN; |
| 108 | + const defaultValue = DEFAULT_CONTAINER; |
| 109 | + const instance: any = getCurrentInstance(); |
| 110 | + if (instance) { |
| 111 | + const provides = instance.provides; |
| 112 | + return provides[token] || defaultValue; |
| 113 | + } else { |
| 114 | + return inject(token, defaultValue); |
| 115 | + } |
| 116 | + } else { |
| 117 | + console.warn( |
| 118 | + `declareProviders and useService can only be used inside setup() or functional components.` |
47 | 119 | );
|
48 | 120 | }
|
49 | 121 | }
|
50 | 122 |
|
51 |
| -export function useService(token: any) { |
52 |
| - const currentContainer = injectFromSelf(CONTAINER_KEY, DEFAULT_CONTAINER); |
53 |
| - return getServiceFromContainer(currentContainer, token); |
| 123 | +const DEFAULT_CONTAINER = createContainer(); |
| 124 | +export function useService<T>(token: interfaces.ServiceIdentifier<T>) { |
| 125 | + const container = getContextContainer(); |
| 126 | + return getServiceFromContainer(container, token); |
54 | 127 | }
|
55 |
| -export function useRootService(token: any) { |
| 128 | +export function useRootService<T>(token: interfaces.ServiceIdentifier<T>) { |
56 | 129 | return getServiceFromContainer(DEFAULT_CONTAINER, token);
|
57 | 130 | }
|
58 |
| - |
59 | 131 | export function declareProviders(providers: any, options?: any) {
|
60 |
| - const instance = getCurrentInstance(); |
61 |
| - if (!instance) { |
62 |
| - throw new Error('declareProviders can only be used inside setup function.'); |
63 |
| - } |
64 |
| - const parentContainer = injectFromSelf(CONTAINER_KEY, DEFAULT_CONTAINER); |
65 |
| - if (parentContainer.uid === instance.uid) { |
66 |
| - throw new Error('declareProviders can only be called once.'); |
| 132 | + const currentContainer = getCurrentContainer(); |
| 133 | + if (currentContainer) { |
| 134 | + bindContainer(currentContainer, providers); |
| 135 | + } else { |
| 136 | + const parent = getContextContainer(); |
| 137 | + if (parent) { |
| 138 | + const instance = getCurrentInstance(); |
| 139 | + const container = createContainer(parent, { instance, ...options }); |
| 140 | + bindContainer(container, providers); |
| 141 | + onUnmounted(() => { |
| 142 | + container.unbindAll(); |
| 143 | + }); |
| 144 | + provide(CONTAINER_TOKEN, container); |
| 145 | + } |
67 | 146 | }
|
68 |
| - const finalOptions = Object.assign({}, DEFAULT_CONTAINER_OPTIONS, options); |
69 |
| - const currentContainer = createContainer(parentContainer, finalOptions); |
70 |
| - bindContainer(currentContainer, providers); |
71 |
| - |
72 |
| - onUnmounted(() => { |
73 |
| - currentContainer.unbindAll(); |
74 |
| - }); |
75 |
| - |
76 |
| - (currentContainer as any).uid = instance.uid; |
77 |
| - provide(CONTAINER_KEY, currentContainer); |
78 | 147 | }
|
79 |
| - |
80 | 148 | export function declareRootProviders(providers: any) {
|
81 | 149 | bindContainer(DEFAULT_CONTAINER, providers);
|
82 | 150 | }
|
| 151 | +export function declareAppProviders(providers: any, options?: any) { |
| 152 | + return (app: any) => { |
| 153 | + const appContainer = getContextContainer(); |
| 154 | + if (appContainer) { |
| 155 | + bindContainer(appContainer, providers); |
| 156 | + } else { |
| 157 | + const container = createContainer(DEFAULT_CONTAINER, options); |
| 158 | + bindContainer(container, providers); |
| 159 | + app.onUnmounted(() => { |
| 160 | + container.unbindAll(); |
| 161 | + }); |
| 162 | + app.provide(CONTAINER_TOKEN, container); |
| 163 | + } |
| 164 | + }; |
| 165 | +} |
0 commit comments