Skip to content

Interface: WirestatePlugin

Defined in: src/wirestate-core/plugin/plugin.ts:38

A container lifecycle plugin.

Remarks

Register plugins on a Container via config.plugins. A plugin is a class instance, so it can hold per-instance state, and every hook is optional.

Plugins bracket the user layer (@OnActivated / @OnProvision): setup hooks run before the matching user hook, teardown hooks run after it. Setup hooks (install, onActivate, onContainerProvision, onProvision) are atomic, so a throw unwinds the activation/provision cycle. Teardown hooks (onDeactivate, onDeprovision, onContainerDeprovision) and disposers are failsafe, so a throw is swallowed and teardown continues. Plugin teardown failures are not reported through the container error handler.

A plugin reaches its container and every descendant (plugins resolve up the parent chain, nearest first), so one registered on the root observes the whole subtree unless a nearer plugin shadows a handled kind.

Example

typescript
import { Container, WirestatePlugin } from "@wirestate/core";

class LogPlugin implements WirestatePlugin {
  public onActivate(instance: object): void {
    console.log("activated", instance.constructor.name);
  }
}

new Container({ plugins: [new LogPlugin()] });

Methods

install()?

ts
optional install(container: Container): void;

Defined in: src/wirestate-core/plugin/plugin.ts:48

Contributes bindings (or other one-time setup) when the plugin is registered.

Parameters

ParameterTypeDescription
containerContainerContainer the plugin is registered on.

Returns

void

Remarks

Runs once, on the container the plugin is registered on (not on inheriting children), before any binding activates.


onActivate()?

ts
optional onActivate(instance: object, container: Container): void;

Defined in: src/wirestate-core/plugin/plugin.ts:95

Runs after a service instance is activated, before its @OnActivated.

Parameters

ParameterTypeDescription
instanceobjectThe activated instance.
containerContainerContainer that activated it.

Returns

void


onContainerDeprovision()?

ts
optional onContainerDeprovision(container: Container): void;

Defined in: src/wirestate-core/plugin/plugin.ts:87

Runs once at the end of a container deprovision cycle, after all teardown.

Parameters

ParameterTypeDescription
containerContainerContainer being deprovisioned.

Returns

void


onContainerProvision()?

ts
optional onContainerProvision(container: Container): void;

Defined in: src/wirestate-core/plugin/plugin.ts:80

Runs once at the start of a container provision cycle, before instance wiring.

Parameters

ParameterTypeDescription
containerContainerContainer being provisioned.

Returns

void


onDeactivate()?

ts
optional onDeactivate(instance: object, container: Container): void;

Defined in: src/wirestate-core/plugin/plugin.ts:103

Runs as a service instance is deactivated, after its @OnDeactivation.

Parameters

ParameterTypeDescription
instanceobjectThe instance being deactivated.
containerContainerContainer that owns it.

Returns

void


onDeprovision()?

ts
optional onDeprovision(instance: object, container: Container): void;

Defined in: src/wirestate-core/plugin/plugin.ts:124

Runs as a provisioned instance is deprovisioned, after its @OnDeprovision.

Parameters

ParameterTypeDescription
instanceobjectThe instance being deprovisioned.
containerContainerContainer that owns it.

Returns

void


onProvision()?

ts
optional onProvision(
   instance: object, 
   container: Container, 
   addDisposer: (dispose: () => void) => void): void;

Defined in: src/wirestate-core/plugin/plugin.ts:116

Wires a provisioned instance, before any user @OnProvision.

Parameters

ParameterTypeDescription
instanceobjectThe provisioned instance.
containerContainerContainer being provisioned.
addDisposer(dispose: () => void) => voidRegisters a teardown callback for this provision cycle.

Returns

void

Remarks

Register teardown with addDisposer. Disposers run (reverse order, failsafe) at deprovision. A throw here unwinds the whole provision cycle.


participates()?

ts
optional participates(token: ServiceToken): boolean;

Defined in: src/wirestate-core/plugin/plugin.ts:62

Declares whether a binding token is a participant this plugin wires.

Parameters

ParameterTypeDescription
tokenServiceTokenBinding token to inspect.

Returns

boolean

Whether the plugin participates in this token.

Remarks

Token/class-level so it can drive force-activation: a token this returns true for is resolved (activated) at provision even if nothing injected it, and the instance is then delivered to WirestatePlugin.onProvision. Omit for a pure observer that force-activates nothing.

Properties

handles?

ts
readonly optional handles?: readonly symbol[];

Defined in: src/wirestate-core/plugin/plugin.ts:73

Messaging-handler kinds this plugin owns (advanced).

Remarks

Used by the built-in messaging plugins. Provision matches declared handler metadata against the union of registered plugins' kinds and throws on a kind no plugin handles. A nearer plugin owning a kind shadows an ancestor's of the same kind. Observer plugins usually omit this field.