Skip to content

Commit

Permalink
feat: Make injectable-mobx not require MobX-transaction
Browse files Browse the repository at this point in the history
This will remove a lot of boilerplate in using code, and not impose MobX in libraries
using injectable, which don't need to know about MobX.
  • Loading branch information
Iku-turso committed Jun 19, 2023
1 parent f1c7b8f commit 45a2712
Show file tree
Hide file tree
Showing 3 changed files with 3 additions and 131 deletions.
14 changes: 3 additions & 11 deletions packages/injectable/extension-for-mobx/src/computedInjectMany.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import {
registrationCallbackToken,
deregistrationCallbackToken,
getInjectable,
lifecycleEnum,
deregistrationCallbackToken,
registrationCallbackToken,
} from '@ogre-tools/injectable';

import { computed, createAtom, runInAction, _getGlobalState } from 'mobx';
import { computed, createAtom, runInAction } from 'mobx';

export const isInternalOfComputedInjectMany = Symbol(
'isInternalOfComputedInjectMany',
Expand All @@ -27,14 +27,6 @@ const invalidabilityForReactiveInstances = getInjectable({
});

const getInvalidatorInstance = registerOrDeregister => di => injectable => {
const { inBatch } = _getGlobalState();

if (inBatch === 0 && !!injectable.injectionToken) {
throw new Error(
`Tried to ${registerOrDeregister} injectable "${injectable.id}" having an injection token outside of MobX-transaction. Transaction is required, as otherwise usages of computedInjectMany could observe untimely invalidations.`,
);
}

if (!injectable.injectionToken) {
return;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -238,66 +238,6 @@ describe('registerMobx', () => {
});
});

it('given not in reactive context, when an injectable with injection token is registered, throws', () => {
expect(() => {
di.register(someInjectable);
}).toThrow(
'Tried to register injectable "some-injectable" having an injection token outside of MobX-transaction. Transaction is required, as otherwise usages of computedInjectMany could observe untimely invalidations.',
);
});

it('given not in reactive context, when an injectable without injection token is late-registered, does not throw', () => {
const lateRegistratorInjectable = getInjectable({
id: 'some-late-registrator',
instantiate: di => injectable => di.register(injectable),
});

runInAction(() => {
di.register(lateRegistratorInjectable);
});

const lateRegister = di.inject(lateRegistratorInjectable);

expect(() => {
delete someInjectable.injectionToken;
lateRegister(someInjectable);
}).not.toThrow();
});

it('given not in reactive context, when an injectable with injection token is late-registered, throws', () => {
const injectionToken = getInjectionToken({ id: 'some-injection-token' });

const lateRegistratorInjectable = getInjectable({
id: 'some-late-registrator',
instantiate: di => injectable => di.register(injectable),
injectionToken,
});

runInAction(() => {
di.register(lateRegistratorInjectable);
});

const lateRegister = di.inject(lateRegistratorInjectable);

expect(() => {
lateRegister(someInjectable);
}).toThrow(
'Tried to register injectable "some-injectable" having an injection token outside of MobX-transaction. Transaction is required, as otherwise usages of computedInjectMany could observe untimely invalidations.',
);
});

it('given registered injectable, and no longer in reactive context, when the injectable is deregistered, throws', () => {
runInAction(() => {
di.register(someInjectable);
});

expect(() => {
di.deregister(someInjectable);
}).toThrow(
'Tried to deregister injectable "some-injectable" having an injection token outside of MobX-transaction. Transaction is required, as otherwise usages of computedInjectMany could observe untimely invalidations.',
);
});

it('given an injection decorator, when an injectable is registered and deregistered, does not decorate internals of computedInjectMany because injects between registrations can happen too early', () => {
const someDecorator = getInjectable({
id: 'some-decorator',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -259,66 +259,6 @@ describe('registerMobx', () => {
});
});

it('given not in reactive context, when an injectable with injection token is registered, throws', () => {
expect(() => {
di.register(someInjectable);
}).toThrow(
'Tried to register injectable "some-injectable" having an injection token outside of MobX-transaction. Transaction is required, as otherwise usages of computedInjectMany could observe untimely invalidations.',
);
});

it('given not in reactive context, when an injectable without injection token is late-registered, does not throw', () => {
const lateRegistratorInjectable = getInjectable({
id: 'some-late-registrator',
instantiate: di => injectable => di.register(injectable),
});

runInAction(() => {
di.register(lateRegistratorInjectable);
});

const lateRegister = di.inject(lateRegistratorInjectable);

expect(() => {
delete someInjectable.injectionToken;
lateRegister(someInjectable);
}).not.toThrow();
});

it('given not in reactive context, when an injectable with injection token is late-registered, throws', () => {
const injectionToken = getInjectionToken({ id: 'some-injection-token' });

const lateRegistratorInjectable = getInjectable({
id: 'some-late-registrator',
instantiate: di => injectable => di.register(injectable),
injectionToken,
});

runInAction(() => {
di.register(lateRegistratorInjectable);
});

const lateRegister = di.inject(lateRegistratorInjectable);

expect(() => {
lateRegister(someInjectable);
}).toThrow(
'Tried to register injectable "some-injectable" having an injection token outside of MobX-transaction. Transaction is required, as otherwise usages of computedInjectMany could observe untimely invalidations.',
);
});

it('given registered injectable, and no longer in reactive context, when the injectable is deregistered, throws', () => {
runInAction(() => {
di.register(someInjectable);
});

expect(() => {
di.deregister(someInjectable);
}).toThrow(
'Tried to deregister injectable "some-injectable" having an injection token outside of MobX-transaction. Transaction is required, as otherwise usages of computedInjectMany could observe untimely invalidations.',
);
});

it('given an injection decorator, when an injectable is registered and deregistered, does not decorate internals of computedInjectMany because injects between registrations can happen too early', () => {
const someDecorator = getInjectable({
id: 'some-decorator',
Expand Down

0 comments on commit 45a2712

Please sign in to comment.