mirror of
https://github.com/sern-handler/tools
synced 2026-06-06 01:16:59 +00:00
chore: well the book was obviously wrong.
permalink: http://whatthecommit.com/3f3725962c1b9d44b824736db7579209
This commit is contained in:
@@ -4,7 +4,7 @@
|
||||
"description": "Dependency Injection system",
|
||||
"main": "dist/index.js",
|
||||
"scripts": {
|
||||
"test": "vitest --run",
|
||||
"test": "vitest --watch",
|
||||
"tdd": "vitest",
|
||||
"build": "tsc"
|
||||
},
|
||||
|
||||
@@ -82,14 +82,16 @@ export class Container {
|
||||
|
||||
const existing = this.__singletons.get(key);
|
||||
if (!existing) {
|
||||
// @jacoobes should we return false if no existing key?
|
||||
throw Error("No existing key to swap for " + key);
|
||||
return false;
|
||||
}
|
||||
// check if there's dispose hook, and call it
|
||||
if (hasCallableMethod(existing, 'dispose')) {
|
||||
this.disposeAll().then(() => {;
|
||||
this.registerHooks('dispose', swp);
|
||||
});
|
||||
existing.dispose();
|
||||
// get the index of the existing singleton, now delete the dispose hook at that index
|
||||
const hookIndex = this.hooks.get('dispose')!.indexOf(existing.dispose);
|
||||
if (hookIndex > -1) {
|
||||
this.hooks.get('dispose')!.splice(hookIndex, 1);
|
||||
}
|
||||
}
|
||||
|
||||
this.__singletons.set(key, swp);
|
||||
|
||||
@@ -125,8 +125,9 @@ describe('CoreContainer Tests', () => {
|
||||
expect(singletonWInit.init).toHaveBeenCalledOnce();
|
||||
});
|
||||
|
||||
it('should throw because not swapping anything', () => {
|
||||
expect(() => coreContainer.swap('singletonKeyWithInit', singletonWInit)).toThrow()
|
||||
it('should be false because not swapping anything', () => {
|
||||
const swap = coreContainer.swap('singletonKeyWithInit', singletonWInit);
|
||||
expect(swap).toBe(false);
|
||||
})
|
||||
|
||||
it('should swap object with another', () => {
|
||||
@@ -146,11 +147,18 @@ describe('CoreContainer Tests', () => {
|
||||
dispose: vi.fn()
|
||||
};
|
||||
|
||||
const singletonWithDispose3 = {
|
||||
value: 'singletonValueWithDispose3',
|
||||
dispose: vi.fn()
|
||||
};
|
||||
|
||||
coreContainer.addSingleton('singletonWithDispose3', singletonWithDispose3);
|
||||
const swapped = coreContainer.swap('singleton', singletonWithDispose2);
|
||||
|
||||
expect(singletonWDispose.dispose).toHaveBeenCalledOnce();
|
||||
expect(coreContainer.get<Record<string, unknown>>('singleton')).toBe(singletonWithDispose2);
|
||||
expect(singletonWithDispose2.dispose).not.toHaveBeenCalledOnce();
|
||||
expect(singletonWithDispose3.dispose).not.toHaveBeenCalledOnce();
|
||||
expect(swapped).toBe(true);
|
||||
})
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user