Skip to content

Commit

Permalink
Revert "fix: New factory binding doesn't reset a cached value"
Browse files Browse the repository at this point in the history
This reverts commit 7a33dc6.
  • Loading branch information
mnasyrov committed Jul 12, 2024
1 parent 261eb23 commit ee7bfb8
Show file tree
Hide file tree
Showing 2 changed files with 1 addition and 51 deletions.
40 changes: 0 additions & 40 deletions packages/ditox/src/container.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -272,46 +272,6 @@ describe('Container', () => {
expect(container.get(CONTAINER)).toBe(container);
expect(container.get(PARENT_CONTAINER)).toBe(parent);
});

it('should rebind a value binding by the same token', () => {
const container = createContainer();

container.bindValue(NUMBER, 1);
container.bindFactory(NUMBER, () => 2);
expect(container.get(NUMBER)).toBe(2);
});

it('should rebind a factory binding by the same token', () => {
const container = createContainer();

container.bindFactory(NUMBER, () => 1);
container.bindFactory(NUMBER, () => 2);
expect(container.get(NUMBER)).toBe(2);
});

it('should reset a cached value during rebinding a factory', () => {
const container = createContainer();

container.bindFactory(NUMBER, () => 1);
expect(container.get(NUMBER)).toBe(1);

container.bindFactory(NUMBER, () => 2);
expect(container.get(NUMBER)).toBe(2);
});

it('should not reset a cached value if the factory is the same', () => {
const container = createContainer();

let value = 1;
const factory = () => value;

container.bindFactory(NUMBER, factory);
expect(container.get(NUMBER)).toBe(1);

value = 2;
container.bindFactory(NUMBER, factory);
expect(container.get(NUMBER)).toBe(1);
});
});

describe('remove()', () => {
Expand Down
12 changes: 1 addition & 11 deletions packages/ditox/src/container.ts
Original file line number Diff line number Diff line change
Expand Up @@ -164,17 +164,7 @@ export function createContainer(parentContainer?: Container): Container {
return;
}

const prev = factories.get(token.symbol);
const next = {factory, options};

const isSame =
prev && prev.factory === next.factory && prev.options === options;

if (!isSame) {
values.delete(token.symbol);
}

factories.set(token.symbol, next);
factories.set(token.symbol, {factory, options});
},

remove<T>(token: Token<T>): void {
Expand Down

0 comments on commit ee7bfb8

Please sign in to comment.