Skip to content

Commit

Permalink
Update module activation store interface (#1641)
Browse files Browse the repository at this point in the history
* refactor: update container
update ModuleActivationStore
update _getContainerModuleHelpersFactory to avoid private property access

* docs: update changelog
  • Loading branch information
notaphplover authored Nov 18, 2024
1 parent 3e19129 commit d4cd32b
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 62 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,10 +64,11 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

### Changed
- Updated `ServiceIdentifier` to rely on `Function` instead of `Abstract<T>`.
- `injectable` decorator is no longer required.

### Fixed
- Fixed `Target.getNameTag` with the right type: `number | string | symbol`.
- Fixed `interfaces.ModuleActivationStore.addDeactivation` to enforce `serviceIdentifier` and `onDeactivation` are consistent.
- Fixed `interfaces.ModuleActivationStore.addActivation` to enforce `serviceIdentifier` and `onDeactivation` are consistent.

## [6.0.3]

Expand Down
94 changes: 47 additions & 47 deletions src/container/container.ts
Original file line number Diff line number Diff line change
Expand Up @@ -138,8 +138,8 @@ class Container implements interfaces.Container {
containerModuleHelpers.isboundFunction,
containerModuleHelpers.rebindFunction,
containerModuleHelpers.unbindAsyncFunction,
containerModuleHelpers.onActivationFunction as interfaces.Container['onActivation'],
containerModuleHelpers.onDeactivationFunction as interfaces.Container['onDeactivation'],
containerModuleHelpers.onActivationFunction,
containerModuleHelpers.onDeactivationFunction,
);
}
}
Expand Down Expand Up @@ -192,11 +192,7 @@ class Container implements interfaces.Container {
public bind<T>(
serviceIdentifier: interfaces.ServiceIdentifier<T>,
): interfaces.BindingToSyntax<T> {
const scope: interfaces.BindingScope =
this.options.defaultScope || BindingScopeEnum.Transient;
const binding: Binding<T> = new Binding<T>(serviceIdentifier, scope);
this._bindingDictionary.add(serviceIdentifier, binding as Binding<unknown>);
return new BindingToSyntax<T>(binding);
return this._bind(this._buildBinding(serviceIdentifier));
}

public rebind<T>(
Expand Down Expand Up @@ -684,32 +680,15 @@ class Container implements interfaces.Container {
}

private _getContainerModuleHelpersFactory() {
const setModuleId: (
bindingToSyntax: interfaces.BindingToSyntax<unknown>,
moduleId: interfaces.ContainerModuleBase['id'],
) => void = (
bindingToSyntax: interfaces.BindingToSyntax<unknown>,
moduleId: interfaces.ContainerModuleBase['id'],
) => {
// TODO: Implement an internal type `_BindingToSyntax<T>` wherein this member
// can be public. Let `BindingToSyntax<T>` be the presentational type that
// depends on it, and does not expose this member as public.
(
bindingToSyntax as unknown as {
_binding: { moduleId: interfaces.ContainerModuleBase['id'] };
}
)._binding.moduleId = moduleId;
};

const getBindFunction: (
moduleId: interfaces.ContainerModuleBase['id'],
) => interfaces.Bind =
(moduleId: interfaces.ContainerModuleBase['id']) =>
<T>(serviceIdentifier: interfaces.ServiceIdentifier<T>) => {
const bindingToSyntax: interfaces.BindingToSyntax<T> =
this.bind<T>(serviceIdentifier);
setModuleId(bindingToSyntax, moduleId);
return bindingToSyntax;
const binding: Binding<T> = this._buildBinding(serviceIdentifier);
binding.moduleId = moduleId;

return this._bind(binding);
};

const getUnbindFunction: () => (
Expand Down Expand Up @@ -738,25 +717,29 @@ class Container implements interfaces.Container {

const getRebindFunction: (
moduleId: interfaces.ContainerModuleBase['id'],
) => interfaces.Rebind =
(moduleId: interfaces.ContainerModuleBase['id']) =>
<T = unknown>(serviceIdentifier: interfaces.ServiceIdentifier<T>) => {
const bindingToSyntax: interfaces.BindingToSyntax<T> =
this.rebind(serviceIdentifier);
setModuleId(bindingToSyntax, moduleId);
return bindingToSyntax;
) => interfaces.Rebind = (
moduleId: interfaces.ContainerModuleBase['id'],
) => {
const bind: interfaces.Bind = getBindFunction(moduleId);

return <T = unknown>(
serviceIdentifier: interfaces.ServiceIdentifier<T>,
) => {
this.unbind(serviceIdentifier);
return bind(serviceIdentifier);
};
};

const getOnActivationFunction: (
moduleId: interfaces.ContainerModuleBase['id'],
) => (
serviceIdentifier: interfaces.ServiceIdentifier,
onActivation: interfaces.BindingActivation,
) => <T>(
serviceIdentifier: interfaces.ServiceIdentifier<T>,
onActivation: interfaces.BindingActivation<T>,
) => void =
(moduleId: interfaces.ContainerModuleBase['id']) =>
(
serviceIdentifier: interfaces.ServiceIdentifier,
onActivation: interfaces.BindingActivation,
<T>(
serviceIdentifier: interfaces.ServiceIdentifier<T>,
onActivation: interfaces.BindingActivation<T>,
) => {
this._moduleActivationStore.addActivation(
moduleId,
Expand All @@ -768,14 +751,14 @@ class Container implements interfaces.Container {

const getOnDeactivationFunction: (
moduleId: interfaces.ContainerModuleBase['id'],
) => (
serviceIdentifier: interfaces.ServiceIdentifier,
onDeactivation: interfaces.BindingDeactivation,
) => <T>(
serviceIdentifier: interfaces.ServiceIdentifier<T>,
onDeactivation: interfaces.BindingDeactivation<T>,
) => void =
(moduleId: interfaces.ContainerModuleBase['id']) =>
(
serviceIdentifier: interfaces.ServiceIdentifier,
onDeactivation: interfaces.BindingDeactivation,
<T>(
serviceIdentifier: interfaces.ServiceIdentifier<T>,
onDeactivation: interfaces.BindingDeactivation<T>,
) => {
this._moduleActivationStore.addDeactivation(
moduleId,
Expand All @@ -795,6 +778,23 @@ class Container implements interfaces.Container {
unbindFunction: getUnbindFunction(),
});
}

private _bind<T>(binding: Binding<T>): BindingToSyntax<T> {
this._bindingDictionary.add(
binding.serviceIdentifier,
binding as Binding<unknown>,
);
return new BindingToSyntax<T>(binding);
}

private _buildBinding<T>(
serviceIdentifier: interfaces.ServiceIdentifier<T>,
): Binding<T> {
const scope: interfaces.BindingScope =
this.options.defaultScope || BindingScopeEnum.Transient;
return new Binding<T>(serviceIdentifier, scope);
}

private async _getAll<T>(getArgs: GetArgs<T>): Promise<T[]> {
return Promise.all(this._get<T>(getArgs) as (Promise<T> | T)[]);
}
Expand Down
16 changes: 8 additions & 8 deletions src/container/module_activation_store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,25 +18,25 @@ export class ModuleActivationStore implements interfaces.ModuleActivationStore {
return handlers;
}

public addDeactivation(
public addDeactivation<T>(
moduleId: number,
serviceIdentifier: interfaces.ServiceIdentifier<unknown>,
onDeactivation: interfaces.BindingDeactivation<unknown>,
serviceIdentifier: interfaces.ServiceIdentifier<T>,
onDeactivation: interfaces.BindingDeactivation<T>,
) {
this._getModuleActivationHandlers(moduleId).onDeactivations.add(
serviceIdentifier,
onDeactivation,
onDeactivation as interfaces.BindingDeactivation<unknown>,
);
}

public addActivation(
public addActivation<T>(
moduleId: number,
serviceIdentifier: interfaces.ServiceIdentifier<unknown>,
onActivation: interfaces.BindingActivation<unknown>,
serviceIdentifier: interfaces.ServiceIdentifier<T>,
onActivation: interfaces.BindingActivation<T>,
) {
this._getModuleActivationHandlers(moduleId).onActivations.add(
serviceIdentifier,
onActivation,
onActivation as interfaces.BindingActivation<unknown>,
);
}

Expand Down
12 changes: 6 additions & 6 deletions src/interfaces/interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -340,15 +340,15 @@ namespace interfaces {

export interface ModuleActivationStore
extends Clonable<ModuleActivationStore> {
addDeactivation(
addDeactivation<T>(
moduleId: ContainerModuleBase['id'],
serviceIdentifier: ServiceIdentifier<unknown>,
onDeactivation: interfaces.BindingDeactivation<unknown>,
serviceIdentifier: ServiceIdentifier<T>,
onDeactivation: interfaces.BindingDeactivation<T>,
): void;
addActivation(
addActivation<T>(
moduleId: ContainerModuleBase['id'],
serviceIdentifier: ServiceIdentifier<unknown>,
onActivation: interfaces.BindingActivation<unknown>,
serviceIdentifier: ServiceIdentifier<T>,
onActivation: interfaces.BindingActivation<T>,
): void;
remove(moduleId: ContainerModuleBase['id']): ModuleActivationHandlers;
}
Expand Down

0 comments on commit d4cd32b

Please sign in to comment.