Skip to content

Commit

Permalink
fix: clear cache
Browse files Browse the repository at this point in the history
  • Loading branch information
exuanbo committed Oct 17, 2024
1 parent 9ed4230 commit bf1fad0
Show file tree
Hide file tree
Showing 4 changed files with 8 additions and 7 deletions.
1 change: 1 addition & 0 deletions eslint.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ export default tseslint.config(
"@typescript-eslint/no-unused-vars": ["error", {
argsIgnorePattern: "^_",
varsIgnorePattern: "^_",
ignoreRestSiblings: true,
}],
"@typescript-eslint/no-empty-object-type": "off",
"@typescript-eslint/no-explicit-any": "off",
Expand Down
10 changes: 5 additions & 5 deletions src/container.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,8 @@ export class Container {

clearCache(): void {
for (const registrations of this.registry.values()) {
registrations.forEach(({provider}, i) => {
registrations[i] = {provider};
registrations.forEach(({instance, ...registration}, i) => {
registrations[i] = registration;
});
}
}
Expand Down Expand Up @@ -197,11 +197,11 @@ export class Container {
});
try {
if (resolvedScope == Scope.Container) {
if (registration.cache) {
return registration.cache.current;
if (registration.instance) {
return registration.instance.current;
}
const instance = instantiate();
registration.cache = {current: instance};
registration.instance = {current: instance};
return instance;
}
else if (resolvedScope == Scope.Resolution) {
Expand Down
2 changes: 1 addition & 1 deletion src/registry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {type Token, Type} from "./token";

export interface Registration<T = any> {
options?: RegistrationOptions;
cache?: InstanceRef<T>;
instance?: InstanceRef<T>;
provider: Provider<T>;
}

Expand Down
2 changes: 1 addition & 1 deletion src/token.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ export interface Constructor<Instance extends object> {
}

export interface Type<T> {
name: string;
readonly name: string;
inter<I>(typeName: string, I: Type<I>): Type<T & I>;
union<U>(typeName: string, U: Type<U>): Type<T | U>;
}
Expand Down

0 comments on commit bf1fad0

Please sign in to comment.