From 45a27123452ec200c5997cb91e7408344ae139dd Mon Sep 17 00:00:00 2001 From: Iku-turso Date: Mon, 19 Jun 2023 13:29:33 +0300 Subject: [PATCH] feat: Make injectable-mobx not require MobX-transaction 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. --- .../src/computedInjectMany.js | 14 +---- .../src/computedInjectMany.test.js | 60 ------------------- .../src/computedInjectManyWithMeta.test.js | 60 ------------------- 3 files changed, 3 insertions(+), 131 deletions(-) diff --git a/packages/injectable/extension-for-mobx/src/computedInjectMany.js b/packages/injectable/extension-for-mobx/src/computedInjectMany.js index 4dc22998..41f416a1 100644 --- a/packages/injectable/extension-for-mobx/src/computedInjectMany.js +++ b/packages/injectable/extension-for-mobx/src/computedInjectMany.js @@ -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', @@ -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; } diff --git a/packages/injectable/extension-for-mobx/src/computedInjectMany.test.js b/packages/injectable/extension-for-mobx/src/computedInjectMany.test.js index eaa16ee4..f74bc8d1 100644 --- a/packages/injectable/extension-for-mobx/src/computedInjectMany.test.js +++ b/packages/injectable/extension-for-mobx/src/computedInjectMany.test.js @@ -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', diff --git a/packages/injectable/extension-for-mobx/src/computedInjectManyWithMeta.test.js b/packages/injectable/extension-for-mobx/src/computedInjectManyWithMeta.test.js index 462e81e3..31ba35dc 100644 --- a/packages/injectable/extension-for-mobx/src/computedInjectManyWithMeta.test.js +++ b/packages/injectable/extension-for-mobx/src/computedInjectManyWithMeta.test.js @@ -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',