diff --git a/packages/injectable-extension-for-mobx/index.d.ts b/packages/injectable-extension-for-mobx/index.d.ts index 440ff790..0cd08de3 100644 --- a/packages/injectable-extension-for-mobx/index.d.ts +++ b/packages/injectable-extension-for-mobx/index.d.ts @@ -1,3 +1,21 @@ -import { DiContainer } from '@ogre-tools/injectable'; +import { + DiContainer, + Injectable, + InjectionToken, +} from '@ogre-tools/injectable'; +import { IComputedValue } from 'mobx'; export function registerMobX(di: DiContainer): void; + +export const computedInjectManyInjectable: Injectable< + < + TInjectionToken extends InjectionToken, + TInstance = TInjectionToken extends InjectionToken + ? T + : never, + >( + injectionToken: TInjectionToken, + ) => IComputedValue, + unknown, + void +>; diff --git a/packages/injectable-extension-for-mobx/index.js b/packages/injectable-extension-for-mobx/index.js index 75c0336b..40b1e9d3 100644 --- a/packages/injectable-extension-for-mobx/index.js +++ b/packages/injectable-extension-for-mobx/index.js @@ -1,3 +1,6 @@ -import { computedInjectMany, registerMobX } from './src/computedInjectMany'; +import { + computedInjectManyInjectable, + registerMobX, +} from './src/computedInjectMany'; -export { registerMobX, computedInjectMany }; +export { registerMobX, computedInjectManyInjectable }; diff --git a/packages/injectable-extension-for-mobx/src/computedInjectMany.js b/packages/injectable-extension-for-mobx/src/computedInjectMany.js index 3baa4ca4..0e0cdacb 100644 --- a/packages/injectable-extension-for-mobx/src/computedInjectMany.js +++ b/packages/injectable-extension-for-mobx/src/computedInjectMany.js @@ -29,7 +29,7 @@ const getInvalidatorInstance = runInAction(() => { pipeline( injectables, - filter('injectionToken'), + filter(injectable => !!injectable.injectionToken), forEach(({ injectionToken }) => { const mobxAtomForToken = di.inject( @@ -61,32 +61,30 @@ const invalidateReactiveInstancesOnDeregisterDecorator = getInjectable({ injectionToken: deregistrationDecoratorToken, }); -export const computedInjectMany = getInjectable({ +export const computedInjectManyInjectable = getInjectable({ id: 'reactive-instances', - instantiate: (di, { injectionToken }) => { - const mobxAtomForToken = di.inject( - invalidabilityForReactiveInstances, - injectionToken, - ); + instantiate: di => { + const getMobxAtomForToken = injectionToken => + di.inject(invalidabilityForReactiveInstances, injectionToken); - return computed(() => { - mobxAtomForToken.reportObserved(); + return injectionToken => { + const mobxAtomForToken = getMobxAtomForToken(injectionToken); - return di.injectMany(injectionToken); - }); - }, + return computed(() => { + mobxAtomForToken.reportObserved(); - lifecycle: lifecycleEnum.keyedSingleton({ - getInstanceKey: (_, injectable) => injectable, - }), + return di.injectMany(injectionToken); + }); + }; + }, }); export const registerMobX = di => { di.register( invalidateReactiveInstancesOnRegisterDecorator, invalidateReactiveInstancesOnDeregisterDecorator, - computedInjectMany, + computedInjectManyInjectable, invalidabilityForReactiveInstances, ); }; diff --git a/packages/injectable-extension-for-mobx/src/computedInjectMany.test.js b/packages/injectable-extension-for-mobx/src/computedInjectMany.test.js index c834f7e0..d30d3642 100644 --- a/packages/injectable-extension-for-mobx/src/computedInjectMany.test.js +++ b/packages/injectable-extension-for-mobx/src/computedInjectMany.test.js @@ -4,7 +4,10 @@ import { getInjectionToken, } from '@ogre-tools/injectable'; -import { computedInjectMany, registerMobX } from './computedInjectMany'; +import { + computedInjectManyInjectable, + registerMobX, +} from './computedInjectMany'; import { configure, observe } from 'mobx'; @@ -48,9 +51,9 @@ describe('registerMobx', () => { registerMobX(di); - actual = di.inject(computedInjectMany, { - injectionToken: someFirstInjectionToken, - }); + const computedInjectMany = di.inject(computedInjectManyInjectable); + + actual = computedInjectMany(someFirstInjectionToken); observe( actual, @@ -139,9 +142,9 @@ describe('registerMobx', () => { injectionToken: someSecondInjectionToken, }); - const actual = di.inject(computedInjectMany, { - injectionToken: someSecondInjectionToken, - }); + const computedInjectMany = di.inject(computedInjectManyInjectable); + + const actual = computedInjectMany(someSecondInjectionToken); observe( actual,