Skip to content

Commit

Permalink
fix: Return same computed when injecting reactively many multiple tim…
Browse files Browse the repository at this point in the history
…es for same injection token
  • Loading branch information
jansav committed Jun 15, 2022
1 parent a8fb83a commit 75d8f51
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 10 deletions.
34 changes: 25 additions & 9 deletions packages/injectable-extension-for-mobx/src/computedInjectMany.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,22 +62,37 @@ const invalidateReactiveInstancesOnDeregisterDecorator = getInjectable({
});

export const computedInjectManyInjectable = getInjectable({
id: 'reactive-instances',
id: 'computed-inject-many',

instantiate: di => {
const getMobxAtomForToken = injectionToken =>
di.inject(invalidabilityForReactiveInstances, injectionToken);
const getReactiveInstances = injectionToken =>
di.inject(reactiveInstancesInjectable, injectionToken);

return injectionToken => {
const mobxAtomForToken = getMobxAtomForToken(injectionToken);
return getReactiveInstances(injectionToken);
};
},
});

return computed(() => {
mobxAtomForToken.reportObserved();
const reactiveInstancesInjectable = getInjectable({
id: 'reactive-instances',

return di.injectMany(injectionToken);
});
};
instantiate: (di, injectionToken) => {
const mobxAtomForToken = di.inject(
invalidabilityForReactiveInstances,
injectionToken,
);

return computed(() => {
mobxAtomForToken.reportObserved();

return di.injectMany(injectionToken);
});
},

lifecycle: lifecycleEnum.keyedSingleton({
getInstanceKey: (di, injectionToken) => injectionToken,
}),
});

export const registerMobX = di => {
Expand All @@ -86,5 +101,6 @@ export const registerMobX = di => {
invalidateReactiveInstancesOnDeregisterDecorator,
computedInjectManyInjectable,
invalidabilityForReactiveInstances,
reactiveInstancesInjectable,
);
};
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ describe('registerMobx', () => {
let someFirstInjectionToken;
let someOtherInjectable;
let reactionCountForFirstToken;
let computedInjectMany;

beforeEach(() => {
reactionCountForFirstToken = 0;
Expand All @@ -51,7 +52,7 @@ describe('registerMobx', () => {

registerMobX(di);

const computedInjectMany = di.inject(computedInjectManyInjectable);
computedInjectMany = di.inject(computedInjectManyInjectable);

actual = computedInjectMany(someFirstInjectionToken);

Expand All @@ -74,6 +75,13 @@ describe('registerMobx', () => {
]);
});

it('when injected again, returns same instance of computed', () => {
const actual1 = computedInjectMany(someFirstInjectionToken);
const actual2 = computedInjectMany(someFirstInjectionToken);

expect(actual1).toBe(actual2);
});

it('when a new implementation gets registered, the reactive instances react', () => {
const someIrrelevantInjectable = getInjectable({
id: 'some-irrelevant-injectable',
Expand Down

0 comments on commit 75d8f51

Please sign in to comment.