Skip to content

Commit

Permalink
feat!: Expose function for injecting reactively many instead of using…
Browse files Browse the repository at this point in the history
… instantiation parameter
  • Loading branch information
jansav committed Jun 15, 2022
1 parent 6823839 commit b572b32
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 26 deletions.
20 changes: 19 additions & 1 deletion packages/injectable-extension-for-mobx/index.d.ts
Original file line number Diff line number Diff line change
@@ -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<any, any>,
TInstance = TInjectionToken extends InjectionToken<infer T, any>
? T
: never,
>(
injectionToken: TInjectionToken,
) => IComputedValue<TInstance[]>,
unknown,
void
>;
7 changes: 5 additions & 2 deletions packages/injectable-extension-for-mobx/index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
import { computedInjectMany, registerMobX } from './src/computedInjectMany';
import {
computedInjectManyInjectable,
registerMobX,
} from './src/computedInjectMany';

export { registerMobX, computedInjectMany };
export { registerMobX, computedInjectManyInjectable };
30 changes: 14 additions & 16 deletions packages/injectable-extension-for-mobx/src/computedInjectMany.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ const getInvalidatorInstance =
runInAction(() => {
pipeline(
injectables,
filter('injectionToken'),
filter(injectable => !!injectable.injectionToken),

forEach(({ injectionToken }) => {
const mobxAtomForToken = di.inject(
Expand Down Expand Up @@ -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,
);
};
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand Down Expand Up @@ -48,9 +51,9 @@ describe('registerMobx', () => {

registerMobX(di);

actual = di.inject(computedInjectMany, {
injectionToken: someFirstInjectionToken,
});
const computedInjectMany = di.inject(computedInjectManyInjectable);

actual = computedInjectMany(someFirstInjectionToken);

observe(
actual,
Expand Down Expand Up @@ -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,
Expand Down

0 comments on commit b572b32

Please sign in to comment.