@wirestate/core 0.6.1 → 0.7.0-experimental.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +29 -1
- package/README.md +186 -232
- package/cjs/development/index.js +325 -91
- package/cjs/development/index.js.map +1 -1
- package/cjs/development/lib.js +1033 -248
- package/cjs/development/lib.js.map +1 -1
- package/cjs/development/test-utils.js +95 -32
- package/cjs/development/test-utils.js.map +1 -1
- package/cjs/production/index.js +1 -1
- package/cjs/production/index.js.map +1 -1
- package/cjs/production/lib.js +1 -1
- package/cjs/production/lib.js.map +1 -1
- package/cjs/production/test-utils.js +1 -1
- package/cjs/production/test-utils.js.map +1 -1
- package/esm/development/alias.js +10 -1
- package/esm/development/alias.js.map +1 -1
- package/esm/development/bind/bind-constant.js +25 -4
- package/esm/development/bind/bind-constant.js.map +1 -1
- package/esm/development/bind/bind-dynamic-value.js +27 -7
- package/esm/development/bind/bind-dynamic-value.js.map +1 -1
- package/esm/development/bind/bind-entry.js +50 -17
- package/esm/development/bind/bind-entry.js.map +1 -1
- package/esm/development/bind/bind-service.js +71 -19
- package/esm/development/bind/bind-service.js.map +1 -1
- package/esm/development/bind/get-entry-token.js +21 -5
- package/esm/development/bind/get-entry-token.js.map +1 -1
- package/esm/development/commands/command-bus.js +93 -46
- package/esm/development/commands/command-bus.js.map +1 -1
- package/esm/development/commands/command-optional.js +28 -5
- package/esm/development/commands/command-optional.js.map +1 -1
- package/esm/development/commands/command.js +26 -5
- package/esm/development/commands/command.js.map +1 -1
- package/esm/development/commands/get-command-handler-metadata.js +8 -3
- package/esm/development/commands/get-command-handler-metadata.js.map +1 -1
- package/esm/development/commands/on-command.js +19 -3
- package/esm/development/commands/on-command.js.map +1 -1
- package/esm/development/container/create-base-container.js +57 -0
- package/esm/development/container/create-base-container.js.map +1 -0
- package/esm/development/container/create-container.js +78 -0
- package/esm/development/container/create-container.js.map +1 -0
- package/esm/development/container/wire-scope.js +236 -54
- package/esm/development/container/wire-scope.js.map +1 -1
- package/esm/development/error/error-code.js +2 -1
- package/esm/development/error/error-code.js.map +1 -1
- package/esm/development/error/wirestate-error.js +25 -4
- package/esm/development/error/wirestate-error.js.map +1 -1
- package/esm/development/events/build-event-dispatcher.js +20 -2
- package/esm/development/events/build-event-dispatcher.js.map +1 -1
- package/esm/development/events/emit-event.js +18 -5
- package/esm/development/events/emit-event.js.map +1 -1
- package/esm/development/events/event-bus.js +58 -9
- package/esm/development/events/event-bus.js.map +1 -1
- package/esm/development/events/get-event-handler-metadata.js +19 -4
- package/esm/development/events/get-event-handler-metadata.js.map +1 -1
- package/esm/development/events/on-event.js +31 -2
- package/esm/development/events/on-event.js.map +1 -1
- package/esm/development/index.js +5 -4
- package/esm/development/index.js.map +1 -1
- package/esm/development/queries/get-query-handler-metadata.js +20 -4
- package/esm/development/queries/get-query-handler-metadata.js.map +1 -1
- package/esm/development/queries/on-query.js +24 -2
- package/esm/development/queries/on-query.js.map +1 -1
- package/esm/development/queries/query-bus.js +82 -31
- package/esm/development/queries/query-bus.js.map +1 -1
- package/esm/development/queries/query-optional.js +19 -5
- package/esm/development/queries/query-optional.js.map +1 -1
- package/esm/development/queries/query.js +25 -5
- package/esm/development/queries/query.js.map +1 -1
- package/esm/development/registry.js +81 -24
- package/esm/development/registry.js.map +1 -1
- package/esm/development/seeds/apply-seeds.js +19 -5
- package/esm/development/seeds/apply-seeds.js.map +1 -1
- package/esm/development/seeds/apply-shared-seed.js +16 -4
- package/esm/development/seeds/apply-shared-seed.js.map +1 -1
- package/esm/development/seeds/tokens.js +31 -0
- package/esm/development/seeds/tokens.js.map +1 -0
- package/esm/development/seeds/unapply-seeds.js +16 -5
- package/esm/development/seeds/unapply-seeds.js.map +1 -1
- package/esm/development/service/get-activated-handler-metadata.js +16 -4
- package/esm/development/service/get-activated-handler-metadata.js.map +1 -1
- package/esm/development/service/get-deactivation-handler-metadata.js +16 -4
- package/esm/development/service/get-deactivation-handler-metadata.js.map +1 -1
- package/esm/development/service/on-activated.js +22 -2
- package/esm/development/service/on-activated.js.map +1 -1
- package/esm/development/service/on-deactivation.js +22 -2
- package/esm/development/service/on-deactivation.js.map +1 -1
- package/esm/development/test-utils/mock-bind-entry.js +17 -7
- package/esm/development/test-utils/mock-bind-entry.js.map +1 -1
- package/esm/development/test-utils/mock-bind-service.js +17 -7
- package/esm/development/test-utils/mock-bind-service.js.map +1 -1
- package/esm/development/test-utils/mock-container.js +25 -8
- package/esm/development/test-utils/mock-container.js.map +1 -1
- package/esm/development/test-utils/mock-service.js +18 -5
- package/esm/development/test-utils/mock-service.js.map +1 -1
- package/esm/development/test-utils/mock-unbind-service.js +16 -3
- package/esm/development/test-utils/mock-unbind-service.js.map +1 -1
- package/esm/development/types/commands.js +6 -1
- package/esm/development/types/commands.js.map +1 -1
- package/esm/production/alias.js +1 -1
- package/esm/production/alias.js.map +1 -1
- package/esm/production/bind/bind-constant.js +1 -1
- package/esm/production/bind/bind-constant.js.map +1 -1
- package/esm/production/bind/bind-dynamic-value.js +1 -1
- package/esm/production/bind/bind-dynamic-value.js.map +1 -1
- package/esm/production/bind/bind-entry.js +1 -1
- package/esm/production/bind/bind-entry.js.map +1 -1
- package/esm/production/bind/bind-service.js.map +1 -1
- package/esm/production/bind/get-entry-token.js.map +1 -1
- package/esm/production/commands/command-bus.js +1 -1
- package/esm/production/commands/command-bus.js.map +1 -1
- package/esm/production/commands/command-optional.js.map +1 -1
- package/esm/production/commands/command.js.map +1 -1
- package/esm/production/commands/get-command-handler-metadata.js.map +1 -1
- package/esm/production/commands/on-command.js.map +1 -1
- package/esm/production/container/create-base-container.js +1 -0
- package/esm/production/container/create-base-container.js.map +1 -0
- package/esm/production/container/create-container.js +1 -0
- package/esm/production/container/create-container.js.map +1 -0
- package/esm/production/container/wire-scope.js +1 -1
- package/esm/production/container/wire-scope.js.map +1 -1
- package/esm/production/error/error-code.js +1 -1
- package/esm/production/error/error-code.js.map +1 -1
- package/esm/production/error/wirestate-error.js.map +1 -1
- package/esm/production/events/build-event-dispatcher.js.map +1 -1
- package/esm/production/events/emit-event.js.map +1 -1
- package/esm/production/events/event-bus.js +1 -1
- package/esm/production/events/event-bus.js.map +1 -1
- package/esm/production/events/get-event-handler-metadata.js.map +1 -1
- package/esm/production/events/on-event.js.map +1 -1
- package/esm/production/index.js +1 -1
- package/esm/production/queries/get-query-handler-metadata.js.map +1 -1
- package/esm/production/queries/on-query.js.map +1 -1
- package/esm/production/queries/query-bus.js +1 -1
- package/esm/production/queries/query-bus.js.map +1 -1
- package/esm/production/queries/query-optional.js.map +1 -1
- package/esm/production/queries/query.js.map +1 -1
- package/esm/production/registry.js +1 -1
- package/esm/production/registry.js.map +1 -1
- package/esm/production/seeds/apply-seeds.js +1 -1
- package/esm/production/seeds/apply-seeds.js.map +1 -1
- package/esm/production/seeds/apply-shared-seed.js +1 -1
- package/esm/production/seeds/apply-shared-seed.js.map +1 -1
- package/esm/production/seeds/tokens.js +1 -0
- package/esm/production/seeds/tokens.js.map +1 -0
- package/esm/production/seeds/unapply-seeds.js +1 -1
- package/esm/production/seeds/unapply-seeds.js.map +1 -1
- package/esm/production/service/get-activated-handler-metadata.js.map +1 -1
- package/esm/production/service/get-deactivation-handler-metadata.js.map +1 -1
- package/esm/production/service/on-activated.js.map +1 -1
- package/esm/production/service/on-deactivation.js.map +1 -1
- package/esm/production/test-utils/mock-bind-entry.js +1 -1
- package/esm/production/test-utils/mock-bind-entry.js.map +1 -1
- package/esm/production/test-utils/mock-bind-service.js +1 -1
- package/esm/production/test-utils/mock-bind-service.js.map +1 -1
- package/esm/production/test-utils/mock-container.js +1 -1
- package/esm/production/test-utils/mock-container.js.map +1 -1
- package/esm/production/test-utils/mock-service.js.map +1 -1
- package/esm/production/test-utils/mock-unbind-service.js.map +1 -1
- package/esm/production/types/commands.js.map +1 -1
- package/index.d.ts +1159 -229
- package/lib.d.ts +128 -9
- package/package.json +1 -2
- package/test-utils.d.ts +140 -40
- package/esm/development/container/create-ioc-container.js +0 -35
- package/esm/development/container/create-ioc-container.js.map +0 -1
- package/esm/production/container/create-ioc-container.js +0 -1
- package/esm/production/container/create-ioc-container.js.map +0 -1
package/index.d.ts
CHANGED
|
@@ -1,129 +1,414 @@
|
|
|
1
|
-
import { ServiceIdentifier, LazyServiceIdentifier, Container, Newable } from 'inversify';
|
|
2
|
-
export { bindingTypeValues as BindingType, Container, ContainerModule, inject as Inject, injectable as Injectable, LazyServiceIdentifier, multiInject as MultiInject, named as Named, Newable, optional as Optional, postConstruct as PostConstruct, preDestroy as PreDestroy, bindingScopeValues as ScopeBindingType, ServiceIdentifier, tagged as Tagged } from 'inversify';
|
|
3
|
-
import { I as InjectableDescriptor } from './lib.js';
|
|
1
|
+
import { ServiceIdentifier, LazyServiceIdentifier, Container, BindWhenOnFluentSyntax, Newable } from 'inversify';
|
|
2
|
+
export { AbstractNewable, Bind, BindInFluentSyntax, BindInWhenOnFluentSyntax, BindOnFluentSyntax, BindToFluentSyntax, BindWhenFluentSyntax, BindWhenOnFluentSyntax, BindingActivation, BindingConstraints, BindingDeactivation, BindingIdentifier, BindingScope, bindingTypeValues as BindingType, BoundServiceSyntax, Container, ContainerModule, ContainerModuleLoadOptions, ContainerOptions, DynamicValueBuilder, Factory, GetAllOptions, GetOptions, GetOptionsTagConstraint, inject as Inject, injectFromBase as InjectFromBase, InjectFromBaseOptions, InjectFromBaseOptionsLifecycle, injectFromHierarchy as InjectFromHierarchy, InjectFromHierarchyOptions, InjectFromHierarchyOptionsLifecycle, injectable as Injectable, IsBound, IsBoundOptions, LazyServiceIdentifier, MapToResolvedValueInjectOptions, MetadataName, MetadataTag, multiInject as MultiInject, MultiInjectOptions, named as Named, Newable, optional as Optional, OptionalGetOptions, postConstruct as PostConstruct, preDestroy as PreDestroy, Rebind, RebindSync, ResolutionContext, ResolvedValueInjectOptions, ResolvedValueMetadataInjectOptions, ResolvedValueMetadataInjectTagOptions, bindingScopeValues as ScopeBindingType, ServiceIdentifier, tagged as Tagged, Unbind, UnbindSync, unmanaged as Unmanaged, bindingScopeValues, bindingTypeValues } from 'inversify';
|
|
3
|
+
import { I as InjectableDescriptor, M as MaybePromise, O as Optional, A as AnyObject, S as SeedEntries, a as SeedKey } from './lib.js';
|
|
4
|
+
export { b as SeedEntry, c as SeedsMap } from './lib.js';
|
|
4
5
|
|
|
6
|
+
/**
|
|
7
|
+
* Util to resolve circular dependencies by wrapping the service identifier in a lazy identifier.
|
|
8
|
+
*
|
|
9
|
+
* @group External-inversify
|
|
10
|
+
* @see {@link https://inversify.io/}
|
|
11
|
+
*
|
|
12
|
+
* @param forward - A function that returns the service identifier.
|
|
13
|
+
* @returns A lazy service identifier.
|
|
14
|
+
*/
|
|
5
15
|
declare function forwardRef<TInstance = unknown>(forward: () => ServiceIdentifier<TInstance>): LazyServiceIdentifier<TInstance>;
|
|
6
16
|
|
|
17
|
+
/**
|
|
18
|
+
* Binds a constant value to a service identifier in the container.
|
|
19
|
+
*
|
|
20
|
+
* @remarks
|
|
21
|
+
* Use this to register configuration values, primitive constants, or pre-instantiated objects.
|
|
22
|
+
* Constant values are bound with a singleton scope by default.
|
|
23
|
+
*
|
|
24
|
+
* @group Bind
|
|
25
|
+
*
|
|
26
|
+
* @template T - Type of the service being bound.
|
|
27
|
+
*
|
|
28
|
+
* @param container - Target Inversify {@link Container}.
|
|
29
|
+
* @param entry - Descriptor containing `id` (token) and `value` (constant).
|
|
30
|
+
* @returns Inversify fluent syntax for additional constraints.
|
|
31
|
+
*
|
|
32
|
+
* @throws {@link WirestateError} If `entry.scopeBindingType` is not `Singleton`.
|
|
33
|
+
*
|
|
34
|
+
* @example
|
|
35
|
+
* ```typescript
|
|
36
|
+
* const API_URL: unique symbol = Symbol("API_URL");
|
|
37
|
+
*
|
|
38
|
+
* bindConstant(container, {
|
|
39
|
+
* id: API_URL,
|
|
40
|
+
* value: "https://api.example.com"
|
|
41
|
+
* });
|
|
42
|
+
* ```
|
|
43
|
+
*/
|
|
44
|
+
declare function bindConstant<T>(container: Container, entry: InjectableDescriptor): BindWhenOnFluentSyntax<T>;
|
|
45
|
+
|
|
46
|
+
/**
|
|
47
|
+
* Binds a dynamic value (factory-based) to an identifier in the container.
|
|
48
|
+
*
|
|
49
|
+
* @remarks
|
|
50
|
+
* Use this when the value depends on runtime state or requires logic during resolution.
|
|
51
|
+
* The binding uses `entry.factory` if provided; otherwise, it falls back to `entry.value`.
|
|
52
|
+
* Supports custom scoping via `entry.scopeBindingType`.
|
|
53
|
+
*
|
|
54
|
+
* @group Bind
|
|
55
|
+
*
|
|
56
|
+
* @template T - Type of the value being bound.
|
|
57
|
+
*
|
|
58
|
+
* @param container - Target Inversify {@link Container}.
|
|
59
|
+
* @param entry - Descriptor containing `id`, `factory` or `value`, and optional `scopeBindingType`.
|
|
60
|
+
* @returns Inversify fluent syntax for additional constraints.
|
|
61
|
+
*
|
|
62
|
+
* @example
|
|
63
|
+
* ```typescript
|
|
64
|
+
* const DATE_NOW: unique symbol = Symbol("DATE_NOW");
|
|
65
|
+
*
|
|
66
|
+
* bindDynamicValue(container, {
|
|
67
|
+
* id: DATE_NOW,
|
|
68
|
+
* factory: () => new Date()
|
|
69
|
+
* });
|
|
70
|
+
* ```
|
|
71
|
+
*/
|
|
72
|
+
declare function bindDynamicValue<T>(container: Container, entry: InjectableDescriptor): BindWhenOnFluentSyntax<T>;
|
|
73
|
+
|
|
74
|
+
/**
|
|
75
|
+
* Represents options for {@link bindService}.
|
|
76
|
+
*
|
|
77
|
+
* @group Bind
|
|
78
|
+
*/
|
|
7
79
|
interface BindServiceOptions {
|
|
8
|
-
|
|
80
|
+
/**
|
|
81
|
+
* If true, suppresses execution of `@OnActivated` and `@OnDeactivation` methods.
|
|
82
|
+
* Messaging registrations (commands, queries, events) are still processed.
|
|
83
|
+
*
|
|
84
|
+
* @default false
|
|
85
|
+
*/
|
|
86
|
+
readonly isWithIgnoreLifecycle?: boolean;
|
|
9
87
|
}
|
|
10
88
|
/**
|
|
11
|
-
*
|
|
12
|
-
*
|
|
89
|
+
* Binds a service class in the {@link Container} with full lifecycle and messaging integration.
|
|
90
|
+
*
|
|
91
|
+
* @remarks
|
|
92
|
+
* Binds the class in singleton scope and configures Inversify activation/deactivation hooks to:
|
|
93
|
+
* - Manage the `IS_DISPOSED` lifecycle state flag.
|
|
94
|
+
* - Trigger `@OnActivated` and `@OnDeactivation` decorated methods.
|
|
95
|
+
* - Register/unregister `@OnCommand`, `@OnEvent` and `@OnQuery` handlers.
|
|
96
|
+
* - Set up event dispatching and bus subscriptions.
|
|
97
|
+
* - Track and dispose injected {@link WireScope} instances.
|
|
98
|
+
*
|
|
99
|
+
* @group Bind
|
|
100
|
+
*
|
|
101
|
+
* @template T - Type of the service instance.
|
|
102
|
+
*
|
|
103
|
+
* @param container - Target Inversify {@link Container}.
|
|
104
|
+
* @param entry - Service class constructor.
|
|
105
|
+
* @param options - Configuration options for the binding.
|
|
106
|
+
*
|
|
107
|
+
* @example
|
|
108
|
+
* ```typescript
|
|
109
|
+
* @Injectable()
|
|
110
|
+
* class UserService {
|
|
111
|
+
* @OnActivated()
|
|
112
|
+
* public onActivated(): void {
|
|
113
|
+
* console.log("UserService activated");
|
|
114
|
+
* }
|
|
115
|
+
*
|
|
116
|
+
* @OnDeactivation()
|
|
117
|
+
* public onDeactivation(): void {
|
|
118
|
+
* console.log("UserService deactivating");
|
|
119
|
+
* }
|
|
120
|
+
*
|
|
121
|
+
* @OnEvent("USER_LOGGED_IN")
|
|
122
|
+
* private onUserLoggedIn(event: UserLoggedInEvent) {
|
|
123
|
+
* console.log('User logged in:', event.payload);
|
|
124
|
+
* }
|
|
125
|
+
*
|
|
126
|
+
* @OnCommand("LOG_DATE_NOW")
|
|
127
|
+
* private onLogDateNow(): void {
|
|
128
|
+
* console.log("Date now:", new Date());
|
|
129
|
+
* }
|
|
130
|
+
*
|
|
131
|
+
* @OnQuery("DATE_NOW")
|
|
132
|
+
* private onQueryDateNow(): void {
|
|
133
|
+
* return new Date();
|
|
134
|
+
* }
|
|
135
|
+
* }
|
|
13
136
|
*
|
|
14
|
-
*
|
|
15
|
-
*
|
|
16
|
-
* @param options - options object to control binding flow
|
|
137
|
+
* bindService(container, UserService);
|
|
138
|
+
* ```
|
|
17
139
|
*/
|
|
18
140
|
declare function bindService<T extends object>(container: Container, entry: Newable<T>, options?: BindServiceOptions): void;
|
|
19
141
|
|
|
20
142
|
/**
|
|
21
|
-
*
|
|
143
|
+
* Represents options for {@link bindEntry}.
|
|
144
|
+
*
|
|
145
|
+
* @group Bind
|
|
22
146
|
*/
|
|
23
147
|
interface BindEntryOptions extends BindServiceOptions {
|
|
24
|
-
|
|
148
|
+
/**
|
|
149
|
+
* If true, the service's lifecycle methods (like `@OnActivated`) will be ignored
|
|
150
|
+
* during the binding process.
|
|
151
|
+
*
|
|
152
|
+
* @default `false`
|
|
153
|
+
*/
|
|
154
|
+
readonly isWithIgnoreLifecycle?: boolean;
|
|
25
155
|
}
|
|
26
156
|
/**
|
|
27
|
-
* Binds
|
|
28
|
-
*
|
|
157
|
+
* Binds an entry to the Inversify {@link Container} using the appropriate strategy.
|
|
158
|
+
*
|
|
159
|
+
* @remarks
|
|
160
|
+
* This is a high-level dispatching function that selects the binding method based on the `entry` type:
|
|
161
|
+
* - **Class Constructor**: Binds as a singleton service via {@link bindService}.
|
|
162
|
+
* - **ConstantValue**: Binds a fixed value via {@link bindConstant}.
|
|
163
|
+
* - **DynamicValue**: Binds a factory-generated value via {@link bindDynamicValue}.
|
|
164
|
+
* - **Instance**: Binds a value as a class instance via {@link bindService}.
|
|
165
|
+
*
|
|
166
|
+
* @group Bind
|
|
167
|
+
*
|
|
168
|
+
* @template T - Type of the object being bound.
|
|
169
|
+
*
|
|
170
|
+
* @param container - Target Inversify {@link Container}.
|
|
171
|
+
* @param entry - Class constructor or {@link InjectableDescriptor} describing the service.
|
|
172
|
+
* @param options - Optional binding configuration (primarily used for class-based services).
|
|
173
|
+
*
|
|
174
|
+
* @throws {@link WirestateError} If `entry.scopeBindingType` is not `Singleton` for constant values.
|
|
175
|
+
*
|
|
176
|
+
* @example
|
|
177
|
+
* ```typescript
|
|
178
|
+
* // Binding a class constructor (defaults to singleton)
|
|
179
|
+
* class MyService {}
|
|
29
180
|
*
|
|
30
|
-
*
|
|
31
|
-
* - Service classes (function entries) - bound as singleton
|
|
32
|
-
* - Constant values - bound via `bindConstant`
|
|
33
|
-
* - Dynamic values - bound via `toDynamicValue` with optional scope
|
|
34
|
-
* - Instance bindings - bound as generic singleton service
|
|
181
|
+
* bindEntry(container, MyService);
|
|
35
182
|
*
|
|
36
|
-
*
|
|
37
|
-
*
|
|
38
|
-
*
|
|
39
|
-
*
|
|
183
|
+
* // Binding a constant value
|
|
184
|
+
* const API_URL: unique symbol = Symbol("API_URL");
|
|
185
|
+
*
|
|
186
|
+
* bindEntry(container, {
|
|
187
|
+
* id: API_URL,
|
|
188
|
+
* value: "https://api.example.com"
|
|
189
|
+
* });
|
|
190
|
+
*
|
|
191
|
+
* // Binding a dynamic value (factory)
|
|
192
|
+
* const CURRENT_TIME: unique symbol = Symbol("CURRENT_TIME");
|
|
193
|
+
*
|
|
194
|
+
* bindEntry(container, {
|
|
195
|
+
* id: CURRENT_TIME,
|
|
196
|
+
* bindingType: "DynamicValue",
|
|
197
|
+
* factory: () => new Date()
|
|
198
|
+
* });
|
|
199
|
+
* ```
|
|
40
200
|
*/
|
|
41
201
|
declare function bindEntry<T extends object = object>(container: Container, entry: Newable<T> | InjectableDescriptor, options?: BindEntryOptions): void;
|
|
42
202
|
|
|
43
203
|
/**
|
|
44
|
-
*
|
|
204
|
+
* Resolves the identifier for a given entry.
|
|
45
205
|
*
|
|
46
|
-
* @
|
|
47
|
-
*
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
*
|
|
53
|
-
*
|
|
54
|
-
*
|
|
206
|
+
* @remarks
|
|
207
|
+
* Handles both plain service classes and injectable descriptors:
|
|
208
|
+
* - If `entry` is a class constructor, it is returned as the identifier.
|
|
209
|
+
* - If `entry` is an {@link InjectableDescriptor}, its `id` field is returned.
|
|
210
|
+
*
|
|
211
|
+
* @group Bind
|
|
212
|
+
*
|
|
213
|
+
* @template T - Type of the injectable object.
|
|
214
|
+
*
|
|
215
|
+
* @param entry - Class constructor or descriptor to get the identifier for.
|
|
216
|
+
* @returns Identifier token for Inversify.
|
|
55
217
|
*
|
|
56
|
-
* @
|
|
57
|
-
*
|
|
218
|
+
* @example
|
|
219
|
+
* ```typescript
|
|
220
|
+
* class MyService {}
|
|
221
|
+
*
|
|
222
|
+
* getEntryToken(MyService); // returns MyService
|
|
223
|
+
*
|
|
224
|
+
* getEntryToken({ id: "my-service" }); // returns "my-service"
|
|
225
|
+
* ```
|
|
58
226
|
*/
|
|
59
227
|
declare function getEntryToken<T extends object = object>(entry: Newable<T> | InjectableDescriptor): ServiceIdentifier;
|
|
60
228
|
|
|
61
|
-
type AnyObject = Record<string, any>;
|
|
62
|
-
type Optional<T> = T | null;
|
|
63
|
-
type MaybePromise<T> = T | Promise<T>;
|
|
64
|
-
|
|
65
229
|
/**
|
|
66
|
-
*
|
|
230
|
+
* Represents identifier used to dispatch and handle commands.
|
|
231
|
+
*
|
|
232
|
+
* @remarks
|
|
233
|
+
* Use strings for public commands and symbols for private/scoped commands to avoid name collisions.
|
|
234
|
+
*
|
|
235
|
+
* @group Commands
|
|
236
|
+
*
|
|
237
|
+
* @example
|
|
238
|
+
* ```typescript
|
|
239
|
+
* const PUBLIC_COMMAND: CommandType = "USER/LOGIN";
|
|
240
|
+
*
|
|
241
|
+
* const PRIVATE_COMMAND: CommandType = Symbol("INTERNAL/SYNC");
|
|
242
|
+
*
|
|
243
|
+
* enum CounterCommand {
|
|
244
|
+
* INCREMENT = "COUNTER/INCREMENT",
|
|
245
|
+
* DECREMENT = "COUNTER/DECREMENT",
|
|
246
|
+
* }
|
|
247
|
+
* ```
|
|
67
248
|
*/
|
|
68
249
|
type CommandType = string | symbol;
|
|
69
250
|
/**
|
|
70
|
-
*
|
|
251
|
+
* Represents function signature for handling command execution.
|
|
252
|
+
*
|
|
253
|
+
* @group Commands
|
|
254
|
+
*
|
|
255
|
+
* @template D - Type of the input payload (data) for the command.
|
|
256
|
+
* @template R - Type of the result returned by the handler (can be wrapped in a Promise).
|
|
257
|
+
*
|
|
258
|
+
* @example
|
|
259
|
+
* ```typescript
|
|
260
|
+
* const loginHandler: CommandHandler<Credentials, Session> = (payload) => {
|
|
261
|
+
* return auth.login(payload);
|
|
262
|
+
* };
|
|
263
|
+
* ```
|
|
71
264
|
*/
|
|
72
|
-
type CommandHandler<D = unknown, R = unknown> = (
|
|
265
|
+
type CommandHandler<D = unknown, R = unknown> = (payload: D) => MaybePromise<R>;
|
|
73
266
|
/**
|
|
74
|
-
*
|
|
267
|
+
* Represents function returned when a command handler is registered.
|
|
268
|
+
* Calling this function removes the handler from the command bus.
|
|
269
|
+
*
|
|
270
|
+
* @group Commands
|
|
271
|
+
*
|
|
272
|
+
* @example
|
|
273
|
+
* ```typescript
|
|
274
|
+
* const unregister: CommandUnregister = commandBus.register("MY_COMMAND", handler);
|
|
275
|
+
*
|
|
276
|
+
* unregister();
|
|
277
|
+
* ```
|
|
75
278
|
*/
|
|
76
279
|
type CommandUnregister = () => void;
|
|
77
280
|
/**
|
|
78
|
-
*
|
|
281
|
+
* Represents the current state of a command execution.
|
|
282
|
+
*
|
|
283
|
+
* @group Commands
|
|
79
284
|
*/
|
|
80
285
|
declare enum CommandStatus {
|
|
286
|
+
/** The command task has started but not yet completed. */
|
|
81
287
|
PENDING = "pending",
|
|
288
|
+
/** The command task has successfully completed. */
|
|
82
289
|
SETTLED = "settled",
|
|
290
|
+
/** The command task failed with an error. */
|
|
83
291
|
ERROR = "error"
|
|
84
292
|
}
|
|
85
293
|
/**
|
|
86
|
-
*
|
|
87
|
-
*
|
|
294
|
+
* Represents a handle to an executing command.
|
|
295
|
+
*
|
|
296
|
+
* @remarks
|
|
297
|
+
* Returned by the command bus when a command is dispatched. It allows tracking
|
|
298
|
+
* the progress and outcome of the command execution.
|
|
299
|
+
*
|
|
300
|
+
* @group Commands
|
|
301
|
+
*
|
|
302
|
+
* @template R - Type of the result produced by the command.
|
|
88
303
|
*/
|
|
89
304
|
interface CommandDescriptor<R = unknown> {
|
|
305
|
+
/**
|
|
306
|
+
* A promise that resolves with the command result or rejects with an error.
|
|
307
|
+
*/
|
|
90
308
|
readonly task: Promise<R>;
|
|
309
|
+
/**
|
|
310
|
+
* The current execution status of the command.
|
|
311
|
+
*/
|
|
91
312
|
readonly status: CommandStatus;
|
|
92
313
|
}
|
|
93
314
|
|
|
94
315
|
/**
|
|
95
|
-
* Dispatches a command
|
|
316
|
+
* Dispatches a command through the {@link CommandBus} resolved from the container.
|
|
317
|
+
*
|
|
318
|
+
* @remarks
|
|
319
|
+
* This is a convenience wrapper around the `CommandBus.command` method.
|
|
320
|
+
* Commands allow for decoupled communication between services.
|
|
96
321
|
*
|
|
97
|
-
* @
|
|
98
|
-
*
|
|
99
|
-
* @
|
|
100
|
-
* @
|
|
322
|
+
* @group Commands
|
|
323
|
+
*
|
|
324
|
+
* @template R - Type of the expected result from the command execution.
|
|
325
|
+
* @template D - Type of the data (payload) passed to the command.
|
|
326
|
+
* @template T - Type of the command identifier.
|
|
327
|
+
*
|
|
328
|
+
* @param container - Inversify {@link Container} to resolve the {@link CommandBus} from.
|
|
329
|
+
* @param type - Unique identifier of the command to dispatch.
|
|
330
|
+
* @param data - Optional payload for the command handler.
|
|
331
|
+
* @returns A descriptor to track the command execution and result.
|
|
332
|
+
*
|
|
333
|
+
* @example
|
|
334
|
+
* ```typescript
|
|
335
|
+
* const descriptor = command<User, UserFindParameters>(
|
|
336
|
+
* container,
|
|
337
|
+
* "FIND_USER",
|
|
338
|
+
* { id: "123" }
|
|
339
|
+
* );
|
|
340
|
+
*
|
|
341
|
+
* const user: User = await descriptor.task;
|
|
342
|
+
* ```
|
|
101
343
|
*/
|
|
102
344
|
declare function command<R = unknown, D = unknown, T extends CommandType = CommandType>(container: Container, type: T, data?: D): CommandDescriptor<R>;
|
|
103
345
|
|
|
104
346
|
/**
|
|
105
|
-
* Dispatches a command
|
|
347
|
+
* Dispatches a command through the {@link CommandBus} resolved from the container, returning null if no handler exists.
|
|
348
|
+
*
|
|
349
|
+
* @remarks
|
|
350
|
+
* This is a convenience wrapper around the `CommandBus.commandOptional` method.
|
|
351
|
+
* Unlike {@link command}, it does not throw if no handler is registered.
|
|
106
352
|
*
|
|
107
|
-
* @
|
|
108
|
-
*
|
|
109
|
-
* @
|
|
110
|
-
* @
|
|
353
|
+
* @group Commands
|
|
354
|
+
*
|
|
355
|
+
* @template R - Type of the expected result from the command execution.
|
|
356
|
+
* @template D - Type of the data (payload) passed to the command.
|
|
357
|
+
* @template T - Type of the command identifier.
|
|
358
|
+
*
|
|
359
|
+
* @param container - Inversify {@link Container} to resolve the {@link CommandBus} from.
|
|
360
|
+
* @param type - Unique identifier of the command to dispatch.
|
|
361
|
+
* @param data - Optional payload for the command handler.
|
|
362
|
+
* @returns A {@link CommandDescriptor} if a handler was found, or `null` otherwise.
|
|
363
|
+
*
|
|
364
|
+
* @example
|
|
365
|
+
* ```typescript
|
|
366
|
+
* const descriptor = commandOptional<User, FindUserOptions>(
|
|
367
|
+
* container,
|
|
368
|
+
* "FIND_USER",
|
|
369
|
+
* { id: "123" }
|
|
370
|
+
* );
|
|
371
|
+
*
|
|
372
|
+
* if (descriptor) {
|
|
373
|
+
* const user: User = await descriptor.task;
|
|
374
|
+
* }
|
|
375
|
+
* ```
|
|
111
376
|
*/
|
|
112
377
|
declare function commandOptional<R = unknown, D = unknown, T extends CommandType = CommandType>(container: Container, type: T, data?: D): Optional<CommandDescriptor<R>>;
|
|
113
378
|
|
|
114
379
|
/**
|
|
115
|
-
* Decorator for service methods that handle a command.
|
|
380
|
+
* Decorator for service methods that handle a specific command.
|
|
381
|
+
*
|
|
382
|
+
* @remarks
|
|
383
|
+
* Methods decorated with `@OnCommand` are automatically registered as command handlers
|
|
384
|
+
* when the service is bound via {@link bindService}.
|
|
116
385
|
*
|
|
117
|
-
* @
|
|
118
|
-
*
|
|
386
|
+
* @group Commands
|
|
387
|
+
*
|
|
388
|
+
* @param type - Unique identifier of the command to handle.
|
|
389
|
+
* @returns A method decorator function.
|
|
390
|
+
*
|
|
391
|
+
* @example
|
|
392
|
+
* ```typescript
|
|
393
|
+
* class UserService {
|
|
394
|
+
* @OnCommand("USER_LOGIN")
|
|
395
|
+
* private onUserLogin(credentials: Credentials): Promise<Session> {
|
|
396
|
+
* return auth.login(credentials);
|
|
397
|
+
* }
|
|
398
|
+
* }
|
|
399
|
+
* ```
|
|
119
400
|
*/
|
|
120
401
|
declare function OnCommand(type: CommandType): MethodDecorator;
|
|
121
402
|
|
|
122
403
|
/**
|
|
123
|
-
*
|
|
404
|
+
* Orchestrates command dispatching and handler registration.
|
|
124
405
|
*
|
|
125
|
-
*
|
|
126
|
-
*
|
|
406
|
+
* @remarks
|
|
407
|
+
* The `CommandBus` provides a way to decouple command dispatchers from their handlers.
|
|
408
|
+
* It supports handler shadowing: when multiple handlers are registered for the same type,
|
|
409
|
+
* the last registered one takes priority.
|
|
410
|
+
*
|
|
411
|
+
* @group Commands
|
|
127
412
|
*/
|
|
128
413
|
declare class CommandBus {
|
|
129
414
|
/**
|
|
@@ -132,70 +417,163 @@ declare class CommandBus {
|
|
|
132
417
|
*/
|
|
133
418
|
private readonly handlers;
|
|
134
419
|
/**
|
|
135
|
-
*
|
|
136
|
-
* Returns an unregister function.
|
|
137
|
-
*
|
|
138
|
-
* @param type - command type
|
|
139
|
-
* @param handler - handler function
|
|
140
|
-
* @returns unregister function
|
|
420
|
+
* Removes all registered command handlers from the bus.
|
|
141
421
|
*/
|
|
142
|
-
|
|
422
|
+
clear(): void;
|
|
143
423
|
/**
|
|
144
424
|
* Dispatches a command to the last registered handler.
|
|
145
|
-
* Wraps the handler execution in a promise and returns a descriptor.
|
|
146
425
|
*
|
|
147
|
-
* @
|
|
148
|
-
*
|
|
149
|
-
* @
|
|
426
|
+
* @remarks
|
|
427
|
+
* Execution is always asynchronous. The handler's return value is wrapped in a Promise.
|
|
428
|
+
* Returns a {@link CommandDescriptor} that tracks the execution status and task.
|
|
429
|
+
*
|
|
430
|
+
* @template R - Type of the command result.
|
|
431
|
+
* @template D - Type of the command payload data.
|
|
432
|
+
*
|
|
433
|
+
* @param type - Command identifier.
|
|
434
|
+
* @param data - Optional payload for the handler.
|
|
435
|
+
* @returns A descriptor for the executing command.
|
|
436
|
+
*
|
|
437
|
+
* @throws {@link WirestateError} If no handler is registered.
|
|
150
438
|
*
|
|
151
|
-
* @
|
|
439
|
+
* @example
|
|
440
|
+
* ```typescript
|
|
441
|
+
* const descriptor: CommandDescriptor<User> = commandBus.command<User, string>("GET_USER", "id-123");
|
|
442
|
+
* const user: User = await descriptor.task;
|
|
443
|
+
* ```
|
|
152
444
|
*/
|
|
153
445
|
command<R = unknown, D = unknown>(type: CommandType, data?: D): CommandDescriptor<R>;
|
|
154
446
|
/**
|
|
155
|
-
* Dispatches a command
|
|
447
|
+
* Dispatches a command if a handler exists, otherwise returns null.
|
|
156
448
|
*
|
|
157
|
-
* @
|
|
158
|
-
* @
|
|
159
|
-
*
|
|
449
|
+
* @template R - Type of the command result.
|
|
450
|
+
* @template D - Type of the command payload data.
|
|
451
|
+
*
|
|
452
|
+
* @param type - Command identifier.
|
|
453
|
+
* @param data - Optional payload for the handler.
|
|
454
|
+
* @returns A command descriptor, or `null` if no handler is found.
|
|
160
455
|
*/
|
|
161
456
|
commandOptional<R = unknown, D = unknown>(type: CommandType, data?: D): Optional<CommandDescriptor<R>>;
|
|
162
457
|
/**
|
|
163
|
-
* Checks if
|
|
458
|
+
* Checks if at least one handler is registered for the given command type.
|
|
164
459
|
*
|
|
165
|
-
* @param type -
|
|
166
|
-
* @returns true if handler
|
|
460
|
+
* @param type - Command identifier.
|
|
461
|
+
* @returns `true` if a handler is available, `false` otherwise.
|
|
167
462
|
*/
|
|
168
463
|
has(type: CommandType): boolean;
|
|
169
464
|
/**
|
|
170
|
-
*
|
|
465
|
+
* Registers a handler for a specific command type.
|
|
466
|
+
*
|
|
467
|
+
* @remarks
|
|
468
|
+
* If multiple handlers are registered for the same type, they are stored in a stack.
|
|
469
|
+
* The most recently registered handler will be used for dispatching.
|
|
470
|
+
*
|
|
471
|
+
* @template D - Type of the command payload data.
|
|
472
|
+
* @template R - Type of the command execution result.
|
|
473
|
+
*
|
|
474
|
+
* @param type - Command identifier.
|
|
475
|
+
* @param handler - Function to execute when the command is dispatched.
|
|
476
|
+
* @returns A function to unregister the handler.
|
|
477
|
+
*
|
|
478
|
+
* @example
|
|
479
|
+
* ```typescript
|
|
480
|
+
* const unregister: CommandUnregister = commandBus.register("LOG_MESSAGE", (message: string) => {
|
|
481
|
+
* console.log(message);
|
|
482
|
+
* });
|
|
483
|
+
* ```
|
|
484
|
+
*/
|
|
485
|
+
register<D = unknown, R = unknown>(type: CommandType, handler: CommandHandler<D, R>): CommandUnregister;
|
|
486
|
+
/**
|
|
487
|
+
* Removes a previously registered command handler.
|
|
488
|
+
*
|
|
489
|
+
* @remarks
|
|
490
|
+
* If the handler was not registered for the given type, this operation does nothing.
|
|
491
|
+
*
|
|
492
|
+
* @template D - Type of the command payload data.
|
|
493
|
+
* @template R - Type of the command execution result.
|
|
494
|
+
*
|
|
495
|
+
* @param type - Command identifier.
|
|
496
|
+
* @param handler - The handler function instance to remove.
|
|
171
497
|
*/
|
|
172
|
-
|
|
498
|
+
unregister<D = unknown, R = unknown>(type: CommandType, handler: CommandHandler<D, R>): void;
|
|
173
499
|
}
|
|
174
500
|
|
|
175
|
-
|
|
501
|
+
/**
|
|
502
|
+
* Represents configuration options for {@link createContainer}.
|
|
503
|
+
*
|
|
504
|
+
* @group Container
|
|
505
|
+
*/
|
|
506
|
+
interface CreateContainerOptions {
|
|
176
507
|
/**
|
|
177
|
-
*
|
|
508
|
+
* Optional parent container.
|
|
509
|
+
* Enables hierarchical resolution and sharing of bindings.
|
|
178
510
|
*/
|
|
179
511
|
readonly parent?: Container;
|
|
180
512
|
/**
|
|
181
|
-
*
|
|
513
|
+
* Initial data for the root seed.
|
|
514
|
+
* Accessible via {@link WireScope.getSeed}() in services.
|
|
182
515
|
*/
|
|
183
516
|
readonly seed?: AnyObject;
|
|
517
|
+
/**
|
|
518
|
+
* Targeted seeds bound to specific injectables or tokens.
|
|
519
|
+
*/
|
|
520
|
+
readonly seeds?: SeedEntries;
|
|
521
|
+
/**
|
|
522
|
+
* Injectables to be bound to the container.
|
|
523
|
+
*
|
|
524
|
+
* @remarks
|
|
525
|
+
* Supports class constructors and {@link InjectableDescriptor} configurations.
|
|
526
|
+
*/
|
|
527
|
+
readonly entries?: ReadonlyArray<Newable<object> | InjectableDescriptor>;
|
|
528
|
+
/**
|
|
529
|
+
* Services to resolve immediately.
|
|
530
|
+
*
|
|
531
|
+
* @remarks
|
|
532
|
+
* Listed services must also be present in the `entries` array.
|
|
533
|
+
*/
|
|
534
|
+
readonly activate?: ReadonlyArray<ServiceIdentifier>;
|
|
184
535
|
}
|
|
185
536
|
/**
|
|
186
|
-
* Creates an IoC container with
|
|
537
|
+
* Creates an Inversify IoC container pre-configured with Wirestate essentials.
|
|
187
538
|
*
|
|
188
|
-
* @
|
|
189
|
-
*
|
|
539
|
+
* @remarks
|
|
540
|
+
* The container is initialized with:
|
|
541
|
+
* - State management tokens: `SEEDS_TOKEN` and `SEED_TOKEN`.
|
|
542
|
+
* - Messaging buses: {@link EventBus}, {@link QueryBus}, {@link CommandBus}.
|
|
543
|
+
* - Service bridge: {@link WireScope} (bound in transient scope).
|
|
544
|
+
* - Default scope set to `Singleton`.
|
|
545
|
+
*
|
|
546
|
+
* @group Container
|
|
547
|
+
*
|
|
548
|
+
* @param options - {@link CreateContainerOptions} configuration.
|
|
549
|
+
* @returns A new Inversify {@link Container} instance.
|
|
550
|
+
*
|
|
551
|
+
* @example
|
|
552
|
+
* ```typescript
|
|
553
|
+
* const container: Container = createIocContainer({
|
|
554
|
+
* seeds: [
|
|
555
|
+
* [CounterService, { count: 1000 }],
|
|
556
|
+
* ["SOME_KEY", "VALUE"],
|
|
557
|
+
* ],
|
|
558
|
+
* entries: [CounterService, LoggerService],
|
|
559
|
+
* activate: [LoggerService]
|
|
560
|
+
* });
|
|
561
|
+
*
|
|
562
|
+
* bindService(container, MyService);
|
|
563
|
+
* ```
|
|
190
564
|
*/
|
|
191
|
-
declare function
|
|
565
|
+
declare function createContainer(options?: CreateContainerOptions): Container;
|
|
192
566
|
|
|
193
567
|
/**
|
|
194
|
-
* Event identifier.
|
|
568
|
+
* Represents an Event identifier.
|
|
569
|
+
*
|
|
570
|
+
* @group Events
|
|
195
571
|
*/
|
|
196
572
|
type EventType = string | symbol;
|
|
197
573
|
/**
|
|
198
|
-
*
|
|
574
|
+
* Represents an event object.
|
|
575
|
+
*
|
|
576
|
+
* @group Events
|
|
199
577
|
*/
|
|
200
578
|
interface Event<P = unknown, T extends EventType = EventType, F = unknown> {
|
|
201
579
|
readonly type: T;
|
|
@@ -203,44 +581,75 @@ interface Event<P = unknown, T extends EventType = EventType, F = unknown> {
|
|
|
203
581
|
readonly from?: F;
|
|
204
582
|
}
|
|
205
583
|
/**
|
|
206
|
-
*
|
|
584
|
+
* Represents event handler signature.
|
|
585
|
+
*
|
|
586
|
+
* @group Events
|
|
207
587
|
*/
|
|
208
588
|
type EventHandler<E extends Event = Event> = (event: E) => void;
|
|
209
589
|
/**
|
|
210
|
-
*
|
|
590
|
+
* Represents event bus unsubscribing function, part of events subscription lifecycle.
|
|
591
|
+
*
|
|
592
|
+
* @group Events
|
|
211
593
|
*/
|
|
212
594
|
type EventUnsubscriber = () => void;
|
|
213
595
|
|
|
214
596
|
/**
|
|
215
|
-
*
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
*
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
*
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
/**
|
|
228
|
-
* Query identifier. Use symbols for private queries.
|
|
597
|
+
* Represents unique identifier for a query.
|
|
598
|
+
*
|
|
599
|
+
* @remarks
|
|
600
|
+
* Queries use a request-response pattern. Using symbols is recommended for
|
|
601
|
+
* private or internal queries to avoid naming collisions.
|
|
602
|
+
*
|
|
603
|
+
* @group Queries
|
|
604
|
+
*
|
|
605
|
+
* @example
|
|
606
|
+
* ```typescript
|
|
607
|
+
* const GET_USER_QUERY: QueryType = Symbol("GET_USER");
|
|
608
|
+
* ```
|
|
229
609
|
*/
|
|
230
610
|
type QueryType = string | symbol;
|
|
231
611
|
/**
|
|
232
|
-
*
|
|
612
|
+
* Represents signature for a function that handles queries of a specific type.
|
|
613
|
+
*
|
|
614
|
+
* @remarks
|
|
615
|
+
* Query handlers can be synchronous or asynchronous. They receive a payload
|
|
616
|
+
* and must return a result (or a Promise resolving to it).
|
|
617
|
+
*
|
|
618
|
+
* @group Queries
|
|
619
|
+
*
|
|
620
|
+
* @template D - Type of the input data (payload).
|
|
621
|
+
* @template R - Type of the returned result.
|
|
622
|
+
*
|
|
623
|
+
* @example
|
|
624
|
+
* ```typescript
|
|
625
|
+
* const handler: QueryHandler<string, User> = (userId) => userRepository.find(userId);
|
|
626
|
+
* ```
|
|
233
627
|
*/
|
|
234
628
|
type QueryHandler<D = unknown, R = unknown> = (data: D) => MaybePromise<R>;
|
|
235
629
|
/**
|
|
236
|
-
*
|
|
630
|
+
* Represents function returned by registration methods to remove a query handler.
|
|
631
|
+
*
|
|
632
|
+
* @group Queries
|
|
633
|
+
*
|
|
634
|
+
* @example
|
|
635
|
+
* ```typescript
|
|
636
|
+
* const unregister: QueryUnregister = queryBus.register("QUERY_TYPE", handler);
|
|
637
|
+
*
|
|
638
|
+
* unregister(); // Handler is no longer active
|
|
639
|
+
* ```
|
|
237
640
|
*/
|
|
238
641
|
type QueryUnregister = () => void;
|
|
239
642
|
|
|
240
643
|
/**
|
|
241
|
-
*
|
|
242
|
-
*
|
|
243
|
-
*
|
|
644
|
+
* A transient bridge providing services with access to Wirestate buses, lazy resolution, and seeds.
|
|
645
|
+
*
|
|
646
|
+
* @remarks
|
|
647
|
+
* Every service bound via {@link bindService} receives its own unique `WireScope` instance.
|
|
648
|
+
* It acts as a facade to the IoC container while enforcing lifecycle safety.
|
|
649
|
+
*
|
|
650
|
+
* Methods are available only while the scope is "active" (after service activation and before deactivation).
|
|
651
|
+
*
|
|
652
|
+
* @group Container
|
|
244
653
|
*/
|
|
245
654
|
declare class WireScope {
|
|
246
655
|
private readonly container;
|
|
@@ -250,152 +659,471 @@ declare class WireScope {
|
|
|
250
659
|
readonly isDisposed: boolean;
|
|
251
660
|
constructor(container: Optional<Container>);
|
|
252
661
|
/**
|
|
253
|
-
*
|
|
254
|
-
*
|
|
662
|
+
* Provides direct access to the underlying Inversify {@link Container}.
|
|
663
|
+
*
|
|
664
|
+
* @returns The active {@link Container}.
|
|
665
|
+
*
|
|
666
|
+
* @throws {@link WirestateError} If accessed before activation or after disposal.
|
|
255
667
|
*
|
|
256
|
-
* @
|
|
668
|
+
* @example
|
|
669
|
+
* ```typescript
|
|
670
|
+
* const container: Container = scope.getContainer();
|
|
257
671
|
*
|
|
258
|
-
*
|
|
672
|
+
* container.bind("TOKEN").toConstantValue(42);
|
|
673
|
+
* ```
|
|
259
674
|
*/
|
|
260
675
|
getContainer(): Container;
|
|
261
676
|
/**
|
|
262
|
-
*
|
|
263
|
-
*
|
|
264
|
-
*
|
|
677
|
+
* Lazily resolves a service or value from the container.
|
|
678
|
+
*
|
|
679
|
+
* @remarks
|
|
680
|
+
* Use this to break circular dependencies or for services that are not needed immediately.
|
|
681
|
+
*
|
|
682
|
+
* @template T - Type of the service or value to resolve.
|
|
265
683
|
*
|
|
266
|
-
* @param injectionId -
|
|
267
|
-
* @returns resolved
|
|
684
|
+
* @param injectionId - Service token (class constructor, symbol, or string).
|
|
685
|
+
* @returns The resolved instance or value.
|
|
268
686
|
*
|
|
269
|
-
* @throws WirestateError
|
|
687
|
+
* @throws {@link WirestateError} If accessed before activation or after disposal.
|
|
688
|
+
* @throws {Error} If the service cannot be resolved from the container.
|
|
689
|
+
*
|
|
690
|
+
* @example
|
|
691
|
+
* ```typescript
|
|
692
|
+
* const service: MyService = scope.resolve(MyService);
|
|
693
|
+
* ```
|
|
270
694
|
*/
|
|
271
695
|
resolve<T>(injectionId: ServiceIdentifier<T>): T;
|
|
272
696
|
/**
|
|
273
|
-
*
|
|
274
|
-
*
|
|
275
|
-
*
|
|
697
|
+
* Lazily resolves a service if it is bound, otherwise returns null.
|
|
698
|
+
*
|
|
699
|
+
* @template T - Type of the service or value to resolve.
|
|
700
|
+
*
|
|
701
|
+
* @param injectionId - Service token (class constructor, symbol, or string).
|
|
702
|
+
* @returns The resolved instance, value, or `null` if not bound.
|
|
276
703
|
*
|
|
277
|
-
* @
|
|
278
|
-
* @returns resolved injection, service instance, generic value, or null if it is not bound
|
|
704
|
+
* @throws {@link WirestateError} If accessed before activation or after disposal.
|
|
279
705
|
*
|
|
280
|
-
* @
|
|
706
|
+
* @example
|
|
707
|
+
* ```typescript
|
|
708
|
+
* const logger: Logger | null = scope.resolveOptional(Logger);
|
|
709
|
+
*
|
|
710
|
+
* logger?.info("Resolved optionally");
|
|
711
|
+
* ```
|
|
281
712
|
*/
|
|
282
713
|
resolveOptional<T>(injectionId: ServiceIdentifier<T>): Optional<T>;
|
|
283
714
|
/**
|
|
284
|
-
*
|
|
285
|
-
*
|
|
715
|
+
* Dispatches an event to the {@link EventBus}.
|
|
716
|
+
*
|
|
717
|
+
* @template P - Type of the event payload.
|
|
718
|
+
* @template T - Type of the event identifier.
|
|
286
719
|
*
|
|
287
|
-
* @param type -
|
|
288
|
-
* @param payload -
|
|
289
|
-
* @param from -
|
|
720
|
+
* @param type - Event identifier.
|
|
721
|
+
* @param payload - Optional data associated with the event.
|
|
722
|
+
* @param from - Optional source identifier (defaults to current scope).
|
|
290
723
|
*
|
|
291
|
-
* @throws WirestateError
|
|
724
|
+
* @throws {@link WirestateError} If accessed before activation or after disposal.
|
|
725
|
+
*
|
|
726
|
+
* @example
|
|
727
|
+
* ```typescript
|
|
728
|
+
* scope.emitEvent("VALUE_CHANGED", { value: "abcd" });
|
|
729
|
+
* ```
|
|
292
730
|
*/
|
|
293
731
|
emitEvent<P, T extends EventType = EventType>(type: T, payload?: P, from?: unknown): void;
|
|
294
732
|
/**
|
|
295
|
-
*
|
|
296
|
-
*
|
|
733
|
+
* Subscribes to all events on the {@link EventBus}.
|
|
734
|
+
*
|
|
735
|
+
* @param handler - Function called for every emitted event.
|
|
736
|
+
* @returns A function to unsubscribe.
|
|
737
|
+
*
|
|
738
|
+
* @throws {@link WirestateError} If accessed before activation or after disposal.
|
|
739
|
+
*
|
|
740
|
+
* @example
|
|
741
|
+
* ```typescript
|
|
742
|
+
* const unsubscribe: EventUnsubscriber = scope.subscribeToEvent((event) => {
|
|
743
|
+
* console.log("Event received:", event);
|
|
744
|
+
* });
|
|
745
|
+
* ```
|
|
746
|
+
*/
|
|
747
|
+
subscribeToEvent(handler: EventHandler): EventUnsubscriber;
|
|
748
|
+
/**
|
|
749
|
+
* Unsubscribes a specific handler from the {@link EventBus}.
|
|
750
|
+
*
|
|
751
|
+
* @param handler - The handler instance to remove.
|
|
752
|
+
*
|
|
753
|
+
* @throws {@link WirestateError} If accessed before activation or after disposal.
|
|
754
|
+
*
|
|
755
|
+
* @example
|
|
756
|
+
* ```typescript
|
|
757
|
+
* scope.unsubscribeFromEvent(this.onEvent);
|
|
758
|
+
* ```
|
|
759
|
+
*/
|
|
760
|
+
unsubscribeFromEvent(handler: EventHandler): void;
|
|
761
|
+
/**
|
|
762
|
+
* Dispatches a query and waits for the result.
|
|
763
|
+
*
|
|
764
|
+
* @template R - Type of the query result.
|
|
765
|
+
* @template D - Type of the query data (payload).
|
|
766
|
+
* @template T - Type of the query identifier.
|
|
297
767
|
*
|
|
298
|
-
* @param type -
|
|
299
|
-
* @param data - query
|
|
300
|
-
* @returns query result
|
|
768
|
+
* @param type - Query identifier.
|
|
769
|
+
* @param data - Input data for the query handler.
|
|
770
|
+
* @returns The query result (can be a Promise).
|
|
301
771
|
*
|
|
302
|
-
* @throws WirestateError
|
|
772
|
+
* @throws {@link WirestateError} If accessed before activation or after disposal.
|
|
773
|
+
* @throws {@link WirestateError} If no query handler is registered.
|
|
774
|
+
*
|
|
775
|
+
* @example
|
|
776
|
+
* ```typescript
|
|
777
|
+
* const user: User = await scope.queryData("GET_USER", { id: 1 });
|
|
778
|
+
* ```
|
|
303
779
|
*/
|
|
304
780
|
queryData<R = unknown, D = unknown, T extends QueryType = QueryType>(type: T, data?: D): MaybePromise<R>;
|
|
305
781
|
/**
|
|
306
|
-
* Dispatches a query and returns the result.
|
|
307
|
-
*
|
|
782
|
+
* Dispatches a query and returns the result, or null if no handler is registered.
|
|
783
|
+
*
|
|
784
|
+
* @template R - Type of the query result.
|
|
785
|
+
* @template D - Type of the query data (payload).
|
|
786
|
+
* @template T - Type of the query identifier.
|
|
308
787
|
*
|
|
309
|
-
* @param type -
|
|
310
|
-
* @param data - query
|
|
311
|
-
* @returns query result or null
|
|
788
|
+
* @param type - Query identifier.
|
|
789
|
+
* @param data - Input data for the query handler.
|
|
790
|
+
* @returns The query result or `null`.
|
|
791
|
+
*
|
|
792
|
+
* @throws {@link WirestateError} If accessed before activation or after disposal.
|
|
793
|
+
*
|
|
794
|
+
* @example
|
|
795
|
+
* ```typescript
|
|
796
|
+
* const config: Config | null = await scope.queryOptionalData("GET_CONFIG");
|
|
797
|
+
* ```
|
|
312
798
|
*/
|
|
313
799
|
queryOptionalData<R = unknown, D = unknown, T extends QueryType = QueryType>(type: T, data?: D): Optional<MaybePromise<R>>;
|
|
314
800
|
/**
|
|
315
|
-
*
|
|
316
|
-
*
|
|
801
|
+
* Registers a handler for a specific query type.
|
|
802
|
+
*
|
|
803
|
+
* @template D - Type of the query data (payload).
|
|
804
|
+
* @template R - Type of the query result.
|
|
317
805
|
*
|
|
318
|
-
* @param type -
|
|
319
|
-
* @param
|
|
320
|
-
* @returns
|
|
806
|
+
* @param type - Query identifier.
|
|
807
|
+
* @param handler - The handler function.
|
|
808
|
+
* @returns A function to unregister the handler.
|
|
321
809
|
*
|
|
322
|
-
* @throws WirestateError
|
|
810
|
+
* @throws {@link WirestateError} If accessed before activation or after disposal.
|
|
811
|
+
*
|
|
812
|
+
* @example
|
|
813
|
+
* ```typescript
|
|
814
|
+
* scope.registerQueryHandler("GET_DATE_NOW", () => new Date());
|
|
815
|
+
* ```
|
|
816
|
+
*/
|
|
817
|
+
registerQueryHandler<D = unknown, R = unknown>(type: QueryType, handler: QueryHandler<D, R>): QueryUnregister;
|
|
818
|
+
/**
|
|
819
|
+
* Removes a specific query handler registration.
|
|
820
|
+
*
|
|
821
|
+
* @template D - Type of the query data (payload).
|
|
822
|
+
* @template R - Type of the query result.
|
|
823
|
+
*
|
|
824
|
+
* @param type - Query identifier.
|
|
825
|
+
* @param handler - The handler instance to remove.
|
|
826
|
+
*
|
|
827
|
+
* @throws {@link WirestateError} If accessed before activation or after disposal.
|
|
828
|
+
*
|
|
829
|
+
* @example
|
|
830
|
+
* ```typescript
|
|
831
|
+
* scope.unregisterQueryHandler("GET_DATE_NOW", this.onGetDateNow);
|
|
832
|
+
* ```
|
|
833
|
+
*/
|
|
834
|
+
unregisterQueryHandler<D = unknown, R = unknown>(type: QueryType, handler: QueryHandler<D, R>): void;
|
|
835
|
+
/**
|
|
836
|
+
* Dispatches a command and returns a descriptor to track its progress.
|
|
837
|
+
*
|
|
838
|
+
* @template R - Type of the command result.
|
|
839
|
+
* @template D - Type of the command payload.
|
|
840
|
+
* @template T - Type of the command identifier.
|
|
841
|
+
*
|
|
842
|
+
* @param type - Command identifier.
|
|
843
|
+
* @param data - Payload for the command.
|
|
844
|
+
* @returns A {@link CommandDescriptor}.
|
|
845
|
+
*
|
|
846
|
+
* @throws {@link WirestateError} If accessed before activation or after disposal.
|
|
847
|
+
* @throws {@link WirestateError} If no command handler is registered.
|
|
848
|
+
*
|
|
849
|
+
* @example
|
|
850
|
+
* ```typescript
|
|
851
|
+
* const descriptor: CommandDescriptor = scope.executeCommand("LOGOUT");
|
|
852
|
+
*
|
|
853
|
+
* await descriptor.task;
|
|
854
|
+
* ```
|
|
323
855
|
*/
|
|
324
856
|
executeCommand<R = unknown, D = unknown, T extends CommandType = CommandType>(type: T, data?: D): CommandDescriptor<R>;
|
|
325
857
|
/**
|
|
326
|
-
* Dispatches a command
|
|
327
|
-
*
|
|
858
|
+
* Dispatches a command if a handler is registered, otherwise returns null.
|
|
859
|
+
*
|
|
860
|
+
* @template R - Type of the command result.
|
|
861
|
+
* @template D - Type of the command payload.
|
|
862
|
+
* @template T - Type of the command identifier.
|
|
863
|
+
*
|
|
864
|
+
* @param type - Command identifier.
|
|
865
|
+
* @param data - Payload for the command.
|
|
866
|
+
* @returns A {@link CommandDescriptor} or `null`.
|
|
328
867
|
*
|
|
329
|
-
* @
|
|
330
|
-
*
|
|
331
|
-
* @
|
|
868
|
+
* @throws {@link WirestateError} If accessed before activation or after disposal.
|
|
869
|
+
*
|
|
870
|
+
* @example
|
|
871
|
+
* ```typescript
|
|
872
|
+
* const descriptor: CommandDescriptor | null = scope.executeOptionalCommand("CLEANUP_CACHE");
|
|
873
|
+
*
|
|
874
|
+
* if (descriptor) {
|
|
875
|
+
* await descriptor.task;
|
|
876
|
+
* }
|
|
877
|
+
* ```
|
|
332
878
|
*/
|
|
333
879
|
executeOptionalCommand<R = unknown, D = unknown, T extends CommandType = CommandType>(type: T, data?: D): Optional<CommandDescriptor<R>>;
|
|
334
|
-
|
|
335
|
-
|
|
880
|
+
/**
|
|
881
|
+
* Registers a handler for a specific command type.
|
|
882
|
+
*
|
|
883
|
+
* @template D - Type of the command payload.
|
|
884
|
+
* @template R - Type of the command result.
|
|
885
|
+
*
|
|
886
|
+
* @param type - Command identifier.
|
|
887
|
+
* @param handler - The handler function.
|
|
888
|
+
* @returns A function to unregister the handler.
|
|
889
|
+
*
|
|
890
|
+
* @throws {@link WirestateError} If accessed before activation or after disposal.
|
|
891
|
+
*
|
|
892
|
+
* @example
|
|
893
|
+
* ```typescript
|
|
894
|
+
* scope.registerCommandHandler("LOG_ERROR", (error) => {
|
|
895
|
+
* console.error(error);
|
|
896
|
+
* });
|
|
897
|
+
* ```
|
|
898
|
+
*/
|
|
899
|
+
registerCommandHandler<D = unknown, R = unknown>(type: CommandType, handler: CommandHandler<D, R>): CommandUnregister;
|
|
900
|
+
/**
|
|
901
|
+
* Removes a specific command handler registration.
|
|
902
|
+
*
|
|
903
|
+
* @template D - Type of the command payload.
|
|
904
|
+
* @template R - Type of the command result.
|
|
905
|
+
*
|
|
906
|
+
* @param type - Command identifier.
|
|
907
|
+
* @param handler - The handler instance to remove.
|
|
908
|
+
*
|
|
909
|
+
* @throws {@link WirestateError} If accessed before activation or after disposal.
|
|
910
|
+
*
|
|
911
|
+
* @example
|
|
912
|
+
* ```typescript
|
|
913
|
+
* scope.unregisterCommandHandler("LOG_ERROR", this.handleLogError);
|
|
914
|
+
* ```
|
|
915
|
+
*/
|
|
916
|
+
unregisterCommandHandler<D = unknown, R = unknown>(type: CommandType, handler: CommandHandler<D, R>): void;
|
|
917
|
+
/**
|
|
918
|
+
* Retrieves the global seed object (initial state) from the container.
|
|
919
|
+
*
|
|
920
|
+
* @remarks
|
|
921
|
+
* Use this to access the entire seed object when no specific key is provided.
|
|
922
|
+
*
|
|
923
|
+
* @template T - Expected type of the global seed object.
|
|
924
|
+
* @returns The global seed object.
|
|
925
|
+
*
|
|
926
|
+
* @throws {@link WirestateError} If accessed before activation or after disposal.
|
|
927
|
+
*
|
|
928
|
+
* @example
|
|
929
|
+
* ```typescript
|
|
930
|
+
* interface GlobalSeed {
|
|
931
|
+
* apiUrl: string;
|
|
932
|
+
* }
|
|
933
|
+
*
|
|
934
|
+
* const seeds: GlobalSeed = scope.getSeed();
|
|
935
|
+
* ```
|
|
936
|
+
*/
|
|
937
|
+
getSeed<T extends AnyObject>(): T;
|
|
938
|
+
/**
|
|
939
|
+
* Retrieves a specific seed value by key from the container's seed map.
|
|
940
|
+
*
|
|
941
|
+
* @remarks
|
|
942
|
+
* Use this to retrieve individual values registered in the seed map.
|
|
943
|
+
*
|
|
944
|
+
* @template T - Expected type of the seed value.
|
|
945
|
+
* @param seed - Lookup key (identifier or token) for the seed.
|
|
946
|
+
* @returns The seed value or `null` if not found.
|
|
947
|
+
*
|
|
948
|
+
* @throws {@link WirestateError} If accessed before activation or after disposal.
|
|
949
|
+
*
|
|
950
|
+
* @example
|
|
951
|
+
* ```typescript
|
|
952
|
+
* const apiUrl: string = scope.getSeed("API_URL");
|
|
953
|
+
* ```
|
|
954
|
+
*/
|
|
955
|
+
getSeed<T>(seed: SeedKey): Optional<T>;
|
|
336
956
|
}
|
|
337
957
|
|
|
338
958
|
/**
|
|
339
|
-
*
|
|
959
|
+
* Base error class for all Wirestate-related exceptions.
|
|
960
|
+
*
|
|
961
|
+
* @remarks
|
|
962
|
+
* `WirestateError` provides structured error information, including a numeric error code
|
|
963
|
+
* and a descriptive message. It is used throughout the library to signal lifecycle
|
|
964
|
+
* violations, messaging failures, and configuration issues.
|
|
340
965
|
*
|
|
341
|
-
*
|
|
342
|
-
*
|
|
966
|
+
* @group Error
|
|
967
|
+
*
|
|
968
|
+
* @example
|
|
969
|
+
* ```typescript
|
|
970
|
+
* try {
|
|
971
|
+
* scope.getContainer();
|
|
972
|
+
* } catch (error) {
|
|
973
|
+
* if (error instanceof WirestateError) {
|
|
974
|
+
* console.error(`Error code: ${error.code}`);
|
|
975
|
+
* }
|
|
976
|
+
* }
|
|
977
|
+
* ```
|
|
343
978
|
*/
|
|
344
979
|
declare class WirestateError extends Error {
|
|
345
980
|
/**
|
|
346
|
-
*
|
|
981
|
+
* The name of the error class, useful for identification in minified environments.
|
|
347
982
|
*/
|
|
348
983
|
readonly name: string;
|
|
349
984
|
/**
|
|
350
|
-
*
|
|
985
|
+
* Numeric error code identifying the specific failure type.
|
|
351
986
|
*/
|
|
352
987
|
readonly code: number;
|
|
353
988
|
/**
|
|
354
|
-
*
|
|
989
|
+
* Human-readable description of the error.
|
|
355
990
|
*/
|
|
356
991
|
readonly message: string;
|
|
992
|
+
/**
|
|
993
|
+
* Creates a new instance of WirestateError.
|
|
994
|
+
*
|
|
995
|
+
* @param code - Numeric identifier for the error (defaults to ERROR_CODE_GENERIC).
|
|
996
|
+
* @param detail - Optional descriptive message.
|
|
997
|
+
*/
|
|
357
998
|
constructor(code?: number, detail?: string);
|
|
358
999
|
}
|
|
359
1000
|
|
|
360
1001
|
/**
|
|
361
|
-
*
|
|
1002
|
+
* Broadcasts an event to all subscribers via the {@link EventBus} resolved from the container.
|
|
362
1003
|
*
|
|
363
|
-
* @
|
|
364
|
-
* @
|
|
365
|
-
*
|
|
366
|
-
* @
|
|
1004
|
+
* @remarks
|
|
1005
|
+
* Use this utility to emit events from outside a service's {@link WireScope} (e.g., from a bootstrap script or external controller).
|
|
1006
|
+
*
|
|
1007
|
+
* @group Events
|
|
1008
|
+
*
|
|
1009
|
+
* @template P - Type of the event payload.
|
|
1010
|
+
* @template T - Type of the event identifier.
|
|
1011
|
+
*
|
|
1012
|
+
* @param container - Inversify {@link Container} to resolve the {@link EventBus} from.
|
|
1013
|
+
* @param type - Unique event identifier.
|
|
1014
|
+
* @param payload - Optional data associated with the event.
|
|
1015
|
+
* @param from - Optional source identifier.
|
|
1016
|
+
*
|
|
1017
|
+
* @example
|
|
1018
|
+
* ```typescript
|
|
1019
|
+
* emitEvent(container, "SYSTEM_READY", { version: "1.0.0" });
|
|
1020
|
+
* ```
|
|
367
1021
|
*/
|
|
368
1022
|
declare function emitEvent<P, T extends EventType>(container: Container, type: T, payload?: P, from?: unknown): void;
|
|
369
1023
|
|
|
370
1024
|
/**
|
|
371
1025
|
* Decorator for service methods that respond to events.
|
|
372
1026
|
*
|
|
373
|
-
* @
|
|
374
|
-
*
|
|
1027
|
+
* @remarks
|
|
1028
|
+
* Methods decorated with `@OnEvent` are automatically registered as subscribers
|
|
1029
|
+
* when the service is bound via {@link bindService}.
|
|
1030
|
+
*
|
|
1031
|
+
* You can specify one or more event types to handle. If `types` is omitted,
|
|
1032
|
+
* the method acts as a catch-all handler for all events broadcasted to the {@link EventBus}.
|
|
1033
|
+
*
|
|
1034
|
+
* @group Events
|
|
1035
|
+
*
|
|
1036
|
+
* @param types - Event identifier(s) to handle. If omitted, handles all events.
|
|
1037
|
+
* @returns Method decorator.
|
|
1038
|
+
*
|
|
1039
|
+
* @example
|
|
1040
|
+
* ```typescript
|
|
1041
|
+
* class MyService {
|
|
1042
|
+
* @OnEvent("USER_LOGGED_IN")
|
|
1043
|
+
* private onLogin(event: Event<User>): void {
|
|
1044
|
+
* console.log("User logged in:", event);
|
|
1045
|
+
* }
|
|
1046
|
+
*
|
|
1047
|
+
* @OnEvent(["LOGOUT", "SESSION_EXPIRED"])
|
|
1048
|
+
* private onSessionEnd(event: Event): void {
|
|
1049
|
+
* console.log("Specific event received:", event);
|
|
1050
|
+
* }
|
|
1051
|
+
*
|
|
1052
|
+
* @OnEvent()
|
|
1053
|
+
* private onAnyEvent(event: Event): void {
|
|
1054
|
+
* // Catch-all handler
|
|
1055
|
+
* }
|
|
1056
|
+
* }
|
|
1057
|
+
* ```
|
|
375
1058
|
*/
|
|
376
1059
|
declare function OnEvent(types?: EventType | ReadonlyArray<EventType>): MethodDecorator;
|
|
377
1060
|
|
|
378
1061
|
/**
|
|
379
|
-
*
|
|
1062
|
+
* Orchestrates event broadcasting to multiple subscribers.
|
|
1063
|
+
*
|
|
1064
|
+
* @remarks
|
|
1065
|
+
* The `EventBus` facilitates decoupled, many-to-many communication.
|
|
1066
|
+
* Unlike commands or queries, which are dispatched to a single handler,
|
|
1067
|
+
* events are broadcast to all registered subscribers.
|
|
1068
|
+
*
|
|
1069
|
+
* @group Events
|
|
380
1070
|
*/
|
|
381
1071
|
declare class EventBus {
|
|
382
1072
|
private readonly handlers;
|
|
383
1073
|
/**
|
|
384
|
-
* Broadcasts an event to all subscribers.
|
|
1074
|
+
* Broadcasts an event to all registered subscribers.
|
|
1075
|
+
*
|
|
1076
|
+
* @remarks
|
|
1077
|
+
* Handlers are executed in a try-catch block to ensure that a single
|
|
1078
|
+
* failing subscriber does not prevent others from receiving the event.
|
|
1079
|
+
*
|
|
1080
|
+
* @template P - Type of the event payload.
|
|
1081
|
+
* @template T - Type of the event identifier.
|
|
1082
|
+
* @template F - Type of the event source.
|
|
1083
|
+
*
|
|
1084
|
+
* @param event - The event object to broadcast.
|
|
385
1085
|
*
|
|
386
|
-
* @
|
|
1086
|
+
* @example
|
|
1087
|
+
* ```typescript
|
|
1088
|
+
* eventBus.emit({
|
|
1089
|
+
* type: "USER_LOGGED_IN",
|
|
1090
|
+
* payload: { userId: "123" },
|
|
1091
|
+
* from: AuthService
|
|
1092
|
+
* });
|
|
1093
|
+
* ```
|
|
387
1094
|
*/
|
|
388
1095
|
emit<P = unknown, T extends EventType = EventType, F = unknown>(event: Event<P, T, F>): void;
|
|
389
1096
|
/**
|
|
390
|
-
*
|
|
391
|
-
* Returns an unsubscribe function.
|
|
1097
|
+
* Registers a handler to receive all broadcasted events.
|
|
392
1098
|
*
|
|
393
|
-
* @param handler -
|
|
394
|
-
* @returns
|
|
1099
|
+
* @param handler - Function invoked for every emitted event.
|
|
1100
|
+
* @returns An {@link EventUnsubscriber} function to remove the subscription.
|
|
1101
|
+
*
|
|
1102
|
+
* @example
|
|
1103
|
+
* ```typescript
|
|
1104
|
+
* const unsubscribe: EventUnsubscriber = eventBus.subscribe((event) => {
|
|
1105
|
+
* console.log('Received event:', event);
|
|
1106
|
+
* });
|
|
1107
|
+
* ```
|
|
395
1108
|
*/
|
|
396
1109
|
subscribe(handler: EventHandler): EventUnsubscriber;
|
|
397
1110
|
/**
|
|
398
|
-
* Removes
|
|
1111
|
+
* Removes a previously registered event handler.
|
|
1112
|
+
*
|
|
1113
|
+
* @remarks
|
|
1114
|
+
* If the handler was not subscribed, this operation does nothing.
|
|
1115
|
+
*
|
|
1116
|
+
* @param handler - The handler function instance to remove.
|
|
1117
|
+
*/
|
|
1118
|
+
unsubscribe(handler: EventHandler): void;
|
|
1119
|
+
/**
|
|
1120
|
+
* Checks if the bus has any active subscribers.
|
|
1121
|
+
*
|
|
1122
|
+
* @returns `true` if at least one handler is registered, `false` otherwise.
|
|
1123
|
+
*/
|
|
1124
|
+
has(): boolean;
|
|
1125
|
+
/**
|
|
1126
|
+
* Removes all registered handlers from the bus.
|
|
399
1127
|
*
|
|
400
1128
|
* @internal
|
|
401
1129
|
*/
|
|
@@ -403,27 +1131,69 @@ declare class EventBus {
|
|
|
403
1131
|
}
|
|
404
1132
|
|
|
405
1133
|
/**
|
|
406
|
-
* Dispatches a query
|
|
1134
|
+
* Dispatches a query through the {@link QueryBus} resolved from the container.
|
|
1135
|
+
*
|
|
1136
|
+
* @remarks
|
|
1137
|
+
* This is a convenience wrapper around the `QueryBus.query` method.
|
|
1138
|
+
* Queries allow for decoupled request-response communication between services.
|
|
1139
|
+
*
|
|
1140
|
+
* @group Queries
|
|
1141
|
+
*
|
|
1142
|
+
* @template R - Type of the expected query result.
|
|
1143
|
+
* @template D - Type of the input data (payload).
|
|
1144
|
+
*
|
|
1145
|
+
* @param container - Inversify {@link Container} to resolve the {@link QueryBus} from.
|
|
1146
|
+
* @param type - Unique query identifier.
|
|
1147
|
+
* @param data - Optional input data for the query handler.
|
|
1148
|
+
* @returns The query result (can be a Promise).
|
|
1149
|
+
*
|
|
1150
|
+
* @throws {@link WirestateError} If no query handler is registered.
|
|
407
1151
|
*
|
|
408
|
-
* @
|
|
409
|
-
*
|
|
410
|
-
*
|
|
411
|
-
*
|
|
1152
|
+
* @example
|
|
1153
|
+
* ```typescript
|
|
1154
|
+
* const result: string = await query<string, FindUserParameters>(
|
|
1155
|
+
* container,
|
|
1156
|
+
* "GET_USER_NAME",
|
|
1157
|
+
* { id: 123 }
|
|
1158
|
+
* );
|
|
1159
|
+
* ```
|
|
412
1160
|
*/
|
|
413
1161
|
declare function query<R = unknown, D = unknown>(container: Container, type: QueryType, data?: D): MaybePromise<R>;
|
|
414
1162
|
|
|
415
1163
|
/**
|
|
416
|
-
* Dispatches a query
|
|
1164
|
+
* Dispatches a query through the {@link QueryBus}, returning null if no handler is registered.
|
|
417
1165
|
*
|
|
418
|
-
* @
|
|
419
|
-
*
|
|
420
|
-
*
|
|
421
|
-
*
|
|
1166
|
+
* @remarks
|
|
1167
|
+
* This is a convenience wrapper around the `QueryBus.queryOptional` method.
|
|
1168
|
+
* Use this when the query resolution is optional and you want to avoid catching errors.
|
|
1169
|
+
*
|
|
1170
|
+
* @group Queries
|
|
1171
|
+
*
|
|
1172
|
+
* @template R - Type of the expected query result.
|
|
1173
|
+
* @template D - Type of the input data (payload).
|
|
1174
|
+
*
|
|
1175
|
+
* @param container - Inversify {@link Container} to resolve the {@link QueryBus} from.
|
|
1176
|
+
* @param type - Unique query identifier.
|
|
1177
|
+
* @param data - Optional input data for the query handler.
|
|
1178
|
+
* @returns The query result or `null` if no handler exists.
|
|
1179
|
+
*
|
|
1180
|
+
* @example
|
|
1181
|
+
* ```typescript
|
|
1182
|
+
* const config: Config | null = await queryOptional<Config>(container, "GET_OPTIONAL_CONFIG");
|
|
1183
|
+
* ```
|
|
422
1184
|
*/
|
|
423
1185
|
declare function queryOptional<R = unknown, D = unknown>(container: Container, type: QueryType, data?: D): Optional<MaybePromise<R>>;
|
|
424
1186
|
|
|
425
1187
|
/**
|
|
426
|
-
*
|
|
1188
|
+
* Orchestrates query dispatching and handler registration.
|
|
1189
|
+
*
|
|
1190
|
+
* @remarks
|
|
1191
|
+
* The `QueryBus` provides a request-response mechanism for decoupled communication.
|
|
1192
|
+
* It supports handler shadowing: when multiple handlers are registered for the same type,
|
|
1193
|
+
* the last registered one (e.g., at the component level) takes priority over earlier ones
|
|
1194
|
+
* (e.g., at the global service level).
|
|
1195
|
+
*
|
|
1196
|
+
* @group Queries
|
|
427
1197
|
*/
|
|
428
1198
|
declare class QueryBus {
|
|
429
1199
|
/**
|
|
@@ -432,41 +1202,82 @@ declare class QueryBus {
|
|
|
432
1202
|
*/
|
|
433
1203
|
private readonly handlers;
|
|
434
1204
|
/**
|
|
435
|
-
* Registers a query
|
|
436
|
-
*
|
|
1205
|
+
* Registers a handler for a specific query type.
|
|
1206
|
+
*
|
|
1207
|
+
* @remarks
|
|
1208
|
+
* If multiple handlers are registered for the same type, they are stored in a stack.
|
|
1209
|
+
* The most recently registered handler will be used for resolution.
|
|
1210
|
+
*
|
|
1211
|
+
* @template D - Type of the query input data.
|
|
1212
|
+
* @template R - Type of the query result.
|
|
1213
|
+
*
|
|
1214
|
+
* @param type - Unique query identifier.
|
|
1215
|
+
* @param handler - Function to execute when the query is dispatched.
|
|
1216
|
+
* @returns A function to unregister the handler.
|
|
437
1217
|
*
|
|
438
|
-
* @
|
|
439
|
-
*
|
|
440
|
-
*
|
|
1218
|
+
* @example
|
|
1219
|
+
* ```typescript
|
|
1220
|
+
* const unregister: QueryUnregister = queryBus.register("GET_NOW", () => Date.now());
|
|
1221
|
+
* ```
|
|
441
1222
|
*/
|
|
442
1223
|
register<D = unknown, R = unknown>(type: QueryType, handler: QueryHandler<D, R>): QueryUnregister;
|
|
443
1224
|
/**
|
|
444
|
-
*
|
|
1225
|
+
* Removes a previously registered query handler.
|
|
445
1226
|
*
|
|
446
|
-
* @
|
|
447
|
-
*
|
|
448
|
-
* @returns query result
|
|
1227
|
+
* @remarks
|
|
1228
|
+
* If the handler was not registered for the given type, this operation does nothing.
|
|
449
1229
|
*
|
|
450
|
-
* @
|
|
1230
|
+
* @template D - Type of the query input data.
|
|
1231
|
+
* @template R - Type of the query result.
|
|
1232
|
+
*
|
|
1233
|
+
* @param type - Unique query identifier.
|
|
1234
|
+
* @param handler - The handler function instance to remove.
|
|
1235
|
+
*/
|
|
1236
|
+
unregister<D = unknown, R = unknown>(type: QueryType, handler: QueryHandler<D, R>): void;
|
|
1237
|
+
/**
|
|
1238
|
+
* Dispatches a query to the last registered handler and returns the result.
|
|
1239
|
+
*
|
|
1240
|
+
* @remarks
|
|
1241
|
+
* Query handlers can be synchronous or asynchronous. The result is returned as-is
|
|
1242
|
+
* (or as a Promise if the handler is async).
|
|
1243
|
+
*
|
|
1244
|
+
* @template R - Type of the expected query result.
|
|
1245
|
+
* @template D - Type of the data (payload) passed to the query.
|
|
1246
|
+
* @template T - Type of the query identifier.
|
|
1247
|
+
*
|
|
1248
|
+
* @param type - Unique query identifier.
|
|
1249
|
+
* @param data - Optional input data for the handler.
|
|
1250
|
+
* @returns The result of the query execution.
|
|
1251
|
+
*
|
|
1252
|
+
* @throws {@link WirestateError} If no handler is registered for the given type.
|
|
1253
|
+
*
|
|
1254
|
+
* @example
|
|
1255
|
+
* ```typescript
|
|
1256
|
+
* const user: User = await queryBus.query<User, string>("FIND_USER", "user-id-123");
|
|
1257
|
+
* ```
|
|
451
1258
|
*/
|
|
452
1259
|
query<R = unknown, D = unknown, T extends QueryType = QueryType>(type: T, data?: D): MaybePromise<R>;
|
|
453
1260
|
/**
|
|
454
|
-
* Dispatches a query
|
|
1261
|
+
* Dispatches a query if a handler exists, otherwise returns null.
|
|
455
1262
|
*
|
|
456
|
-
* @
|
|
457
|
-
* @
|
|
458
|
-
* @
|
|
1263
|
+
* @template R - Type of the expected query result.
|
|
1264
|
+
* @template D - Type of the data (payload) passed to the query.
|
|
1265
|
+
* @template T - Type of the query identifier.
|
|
1266
|
+
*
|
|
1267
|
+
* @param type - Unique query identifier.
|
|
1268
|
+
* @param data - Optional input data for the handler.
|
|
1269
|
+
* @returns The query result, or `null` if no handler is found.
|
|
459
1270
|
*/
|
|
460
1271
|
queryOptional<R = unknown, D = unknown, T extends QueryType = QueryType>(type: T, data?: D): Optional<MaybePromise<R>>;
|
|
461
1272
|
/**
|
|
462
|
-
* Checks if
|
|
1273
|
+
* Checks if at least one handler is registered for the given query type.
|
|
463
1274
|
*
|
|
464
|
-
* @param type - query
|
|
465
|
-
* @returns true if handler
|
|
1275
|
+
* @param type - Unique query identifier.
|
|
1276
|
+
* @returns `true` if a handler is available, `false` otherwise.
|
|
466
1277
|
*/
|
|
467
1278
|
has(type: QueryType): boolean;
|
|
468
1279
|
/**
|
|
469
|
-
* Removes all registered handlers.
|
|
1280
|
+
* Removes all registered query handlers from the bus.
|
|
470
1281
|
*
|
|
471
1282
|
* @internal
|
|
472
1283
|
*/
|
|
@@ -476,59 +1287,178 @@ declare class QueryBus {
|
|
|
476
1287
|
/**
|
|
477
1288
|
* Decorator for service methods that respond to a query.
|
|
478
1289
|
*
|
|
479
|
-
* @
|
|
480
|
-
*
|
|
1290
|
+
* @remarks
|
|
1291
|
+
* Methods decorated with `@OnQuery` are automatically registered as query handlers
|
|
1292
|
+
* when the service is bound via {@link bindService}.
|
|
1293
|
+
*
|
|
1294
|
+
* Unlike events, queries MUST be handled by exactly one handler. If multiple handlers
|
|
1295
|
+
* are registered for the same query type, the most recent one (usually the most
|
|
1296
|
+
* specific in terms of class hierarchy or registration order) will shadow the others.
|
|
1297
|
+
*
|
|
1298
|
+
* @group Queries
|
|
1299
|
+
*
|
|
1300
|
+
* @param type - Unique query identifier to handle.
|
|
1301
|
+
* @returns Method decorator.
|
|
1302
|
+
*
|
|
1303
|
+
* @example
|
|
1304
|
+
* ```typescript
|
|
1305
|
+
* class UserProfileService {
|
|
1306
|
+
* @OnQuery("GET_USER_AVATAR")
|
|
1307
|
+
* private async onGetUserAvatar(userId: string): Promise<string> {
|
|
1308
|
+
* const user: User = await this.userRepository.findById(userId);
|
|
1309
|
+
*
|
|
1310
|
+
* return user.avatarUrl;
|
|
1311
|
+
* }
|
|
1312
|
+
* }
|
|
1313
|
+
* ```
|
|
481
1314
|
*/
|
|
482
1315
|
declare function OnQuery(type: QueryType): MethodDecorator;
|
|
483
1316
|
|
|
484
1317
|
/**
|
|
485
|
-
*
|
|
1318
|
+
* Unique symbol used as a token for the container-scoped seeds map.
|
|
1319
|
+
*
|
|
1320
|
+
* @remarks
|
|
1321
|
+
* This token is used to bind and resolve the {@link SeedsMap} in the Inversify {@link Container}.
|
|
1322
|
+
*
|
|
1323
|
+
* @group Seeds
|
|
1324
|
+
*
|
|
1325
|
+
* @example
|
|
1326
|
+
* ```typescript
|
|
1327
|
+
* const seedsMap: SeedsMap = container.get(SEEDS_TOKEN);
|
|
1328
|
+
* ```
|
|
486
1329
|
*/
|
|
487
1330
|
declare const SEEDS_TOKEN: unique symbol;
|
|
488
1331
|
/**
|
|
489
|
-
*
|
|
1332
|
+
* Unique symbol used as a token for the container-scoped shared seed object.
|
|
1333
|
+
*
|
|
1334
|
+
* @remarks
|
|
1335
|
+
* This token is used to bind and resolve the global shared seed object in the Inversify {@link Container}.
|
|
1336
|
+
*
|
|
1337
|
+
* @group Seeds
|
|
1338
|
+
*
|
|
1339
|
+
* @example
|
|
1340
|
+
* ```typescript
|
|
1341
|
+
* const sharedSeed: AnyObject = container.get(SEED_TOKEN);
|
|
1342
|
+
* ```
|
|
490
1343
|
*/
|
|
491
1344
|
declare const SEED_TOKEN: unique symbol;
|
|
492
1345
|
|
|
493
1346
|
/**
|
|
494
|
-
* Applies seeds to the container
|
|
495
|
-
*
|
|
1347
|
+
* Applies targeted seeds to the container's internal seed map.
|
|
1348
|
+
*
|
|
1349
|
+
* @remarks
|
|
1350
|
+
* This function updates the existing {@link SeedsMap} instance instead of replacing it.
|
|
1351
|
+
* This ensures that multiple providers can co-exist and contribute their own seeds
|
|
1352
|
+
* without overwriting each other's data.
|
|
1353
|
+
*
|
|
1354
|
+
* @group Seeds
|
|
496
1355
|
*
|
|
497
|
-
* @param container -
|
|
498
|
-
* @param seeds -
|
|
1356
|
+
* @param container - The Inversify {@link Container} where seeds should be applied.
|
|
1357
|
+
* @param seeds - An array of {@link SeedEntries} to add to the container.
|
|
1358
|
+
*
|
|
1359
|
+
* @example
|
|
1360
|
+
* ```typescript
|
|
1361
|
+
* applySeeds(container, [
|
|
1362
|
+
* [UserService, { initialUser: "admin" }],
|
|
1363
|
+
* ["API_KEY", "12345"]
|
|
1364
|
+
* ]);
|
|
1365
|
+
* ```
|
|
499
1366
|
*/
|
|
500
1367
|
declare function applySeeds(container: Container, seeds: SeedEntries): void;
|
|
501
1368
|
|
|
502
1369
|
/**
|
|
503
|
-
*
|
|
1370
|
+
* Rebinds the global shared seed object in the container.
|
|
1371
|
+
*
|
|
1372
|
+
* @remarks
|
|
1373
|
+
* Unlike targeted seeds, there is only one shared seed object per container.
|
|
1374
|
+
* This function uses `rebind` to ensure the new shared seed replaces the previous one.
|
|
1375
|
+
* The shared seed is typically used for global configuration or common state.
|
|
504
1376
|
*
|
|
505
|
-
* @
|
|
506
|
-
*
|
|
1377
|
+
* @group Seeds
|
|
1378
|
+
*
|
|
1379
|
+
* @param container - The Inversify {@link Container} to update.
|
|
1380
|
+
* @param seed - The new shared seed object.
|
|
1381
|
+
*
|
|
1382
|
+
* @example
|
|
1383
|
+
* ```typescript
|
|
1384
|
+
* applySharedSeed(container, { theme: "dark", lang: "en" });
|
|
1385
|
+
* ```
|
|
507
1386
|
*/
|
|
508
1387
|
declare function applySharedSeed(container: Container, seed: AnyObject): void;
|
|
509
1388
|
|
|
510
1389
|
/**
|
|
511
|
-
* Removes specific seeds from the container.
|
|
512
|
-
*
|
|
1390
|
+
* Removes specific targeted seeds from the container's internal seed map.
|
|
1391
|
+
*
|
|
1392
|
+
* @remarks
|
|
1393
|
+
* This is typically called during provider unmounting to ensure that only
|
|
1394
|
+
* the seeds owned by that specific provider are removed, leaving other
|
|
1395
|
+
* providers' seeds intact.
|
|
1396
|
+
*
|
|
1397
|
+
* @group Seeds
|
|
513
1398
|
*
|
|
514
|
-
* @param container -
|
|
515
|
-
* @param seeds - targeted
|
|
1399
|
+
* @param container - The Inversify {@link Container} to clean up.
|
|
1400
|
+
* @param seeds - The targeted {@link SeedEntries} to remove.
|
|
1401
|
+
*
|
|
1402
|
+
* @example
|
|
1403
|
+
* ```typescript
|
|
1404
|
+
* unapplySeeds(container, [[UserService, { initialUser: "admin" }]]);
|
|
1405
|
+
* ```
|
|
516
1406
|
*/
|
|
517
1407
|
declare function unapplySeeds(container: Container, seeds: SeedEntries): void;
|
|
518
1408
|
|
|
519
1409
|
/**
|
|
520
|
-
* Decorator for service methods that
|
|
1410
|
+
* Decorator for service methods that should be executed after the service instance is activated.
|
|
1411
|
+
*
|
|
1412
|
+
* @remarks
|
|
1413
|
+
* Methods decorated with `@OnActivated` are automatically invoked when the service
|
|
1414
|
+
* is resolved from the container and its activation lifecycle hook is triggered.
|
|
1415
|
+
*
|
|
1416
|
+
* It is commonly used for initial setup, subscribing to events, or starting background tasks.
|
|
1417
|
+
* Multiple `@OnActivated` methods can exist in the same class hierarchy; they are executed
|
|
1418
|
+
* in parent-to-child order.
|
|
1419
|
+
*
|
|
1420
|
+
* @group Service
|
|
1421
|
+
*
|
|
1422
|
+
* @returns A method decorator function.
|
|
521
1423
|
*
|
|
522
|
-
* @
|
|
1424
|
+
* @example
|
|
1425
|
+
* ```typescript
|
|
1426
|
+
* class MyService {
|
|
1427
|
+
* @OnActivated()
|
|
1428
|
+
* public onActivated(): void {
|
|
1429
|
+
* console.log("Service activated!");
|
|
1430
|
+
* }
|
|
1431
|
+
* }
|
|
1432
|
+
* ```
|
|
523
1433
|
*/
|
|
524
1434
|
declare function OnActivated(): MethodDecorator;
|
|
525
1435
|
|
|
526
1436
|
/**
|
|
527
|
-
* Decorator for service methods that
|
|
1437
|
+
* Decorator for service methods that should be executed before the service instance is deactivated.
|
|
1438
|
+
*
|
|
1439
|
+
* @remarks
|
|
1440
|
+
* Methods decorated with `@OnDeactivation` are automatically invoked when the service
|
|
1441
|
+
* is being removed from the container or when the container itself is being disposed.
|
|
1442
|
+
*
|
|
1443
|
+
* It is commonly used for cleanup, unsubscribing from events, or stopping background tasks.
|
|
1444
|
+
* Multiple `@OnDeactivation` methods can exist in the same class hierarchy; they are executed
|
|
1445
|
+
* in parent-to-child order.
|
|
1446
|
+
*
|
|
1447
|
+
* @group Service
|
|
1448
|
+
*
|
|
1449
|
+
* @returns A method decorator function.
|
|
528
1450
|
*
|
|
529
|
-
* @
|
|
1451
|
+
* @example
|
|
1452
|
+
* ```typescript
|
|
1453
|
+
* class MyService {
|
|
1454
|
+
* @OnDeactivation()
|
|
1455
|
+
* public onDeactivation(): void {
|
|
1456
|
+
* console.log("Service deactivating!");
|
|
1457
|
+
* }
|
|
1458
|
+
* }
|
|
1459
|
+
* ```
|
|
530
1460
|
*/
|
|
531
1461
|
declare function OnDeactivation(): MethodDecorator;
|
|
532
1462
|
|
|
533
|
-
export { CommandBus, CommandStatus, EventBus, InjectableDescriptor, OnActivated, OnCommand, OnDeactivation, OnEvent, OnQuery, QueryBus, SEED_TOKEN as SEED, SEEDS_TOKEN as SEEDS, WireScope, WirestateError, applySeeds, applySharedSeed, bindConstant, bindEntry, bindService, command, commandOptional,
|
|
534
|
-
export type { CommandDescriptor, CommandHandler, CommandType, CommandUnregister, Event, EventHandler, EventType, EventUnsubscriber, QueryHandler, QueryType, QueryUnregister
|
|
1463
|
+
export { CommandBus, CommandStatus, EventBus, InjectableDescriptor, OnActivated, OnCommand, OnDeactivation, OnEvent, OnQuery, QueryBus, SEED_TOKEN as SEED, SEEDS_TOKEN as SEEDS, SeedEntries, SeedKey, WireScope, WirestateError, applySeeds, applySharedSeed, bindConstant, bindDynamicValue, bindEntry, bindService, command, commandOptional, createContainer, emitEvent, forwardRef, getEntryToken, query, queryOptional, unapplySeeds };
|
|
1464
|
+
export type { BindEntryOptions, BindServiceOptions, CommandDescriptor, CommandHandler, CommandType, CommandUnregister, CreateContainerOptions, Event, EventHandler, EventType, EventUnsubscriber, QueryHandler, QueryType, QueryUnregister };
|