Skip to content

Commit

Permalink
feat: Expose deregister to instantiate
Browse files Browse the repository at this point in the history
  • Loading branch information
jansav committed Apr 8, 2022
1 parent 1365d5b commit eea8bcc
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 0 deletions.
2 changes: 2 additions & 0 deletions packages/injectable/ogre-tools-injectable.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ export interface DiContainer extends DiContainerForInjection {
InstantiationParam
>[]
): void;

deregister(...injectables: Injectable<any, any, any>[]): void;
preventSideEffects: () => void;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,4 +75,31 @@ describe('createContainer.deregistration', () => {
const actual = di.inject(someInjectable);
expect(actual).toBe('some-instance');
});

it('given injectable and injectable which can deregister and first injectable is deregistered, when first injectable is injected, throws', () => {
const di = createContainer();

const someInjectable = getInjectable({
id: 'some-injectable',
instantiate: () => 'some-instance',
});

const deregisterInjectable = getInjectable({
id: 'deregister',

instantiate: di => injectable => {
di.deregister(injectable);
},
});

di.register(someInjectable, deregisterInjectable);

const deregister = di.inject(deregisterInjectable);

deregister(someInjectable);

expect(() => {
di.inject(someInjectable);
}).toThrow('Tried to inject non-registered injectable "some-injectable".');
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -279,6 +279,8 @@ const getInstance = ({
context: newContext,

register: di.register,

deregister: di.deregister,
};

const instanceKey = injectable.lifecycle.getInstanceKey(
Expand Down

0 comments on commit eea8bcc

Please sign in to comment.