@wirestate/core 0.7.0-experimental.1 → 0.7.0-experimental.3
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 +8 -2
- package/README.md +2 -2
- package/cjs/development/index.js +28 -26
- package/cjs/development/index.js.map +1 -1
- package/cjs/development/lib.js +234 -288
- package/cjs/development/lib.js.map +1 -1
- package/cjs/development/test-utils.js +22 -38
- 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/bind/bind-dynamic-value.js +1 -1
- package/esm/development/bind/bind-dynamic-value.js.map +1 -1
- package/esm/development/bind/bind-entry.js +1 -4
- package/esm/development/bind/bind-entry.js.map +1 -1
- package/esm/development/bind/bind-service.js +40 -54
- package/esm/development/bind/bind-service.js.map +1 -1
- package/esm/development/commands/command-bus.js +33 -42
- package/esm/development/commands/command-bus.js.map +1 -1
- package/esm/development/commands/get-command-handler-metadata.js +3 -3
- package/esm/development/commands/get-command-handler-metadata.js.map +1 -1
- package/esm/development/commands/on-command.js +4 -4
- package/esm/development/commands/on-command.js.map +1 -1
- package/esm/development/container/create-base-container.js +2 -3
- package/esm/development/container/create-base-container.js.map +1 -1
- package/esm/development/container/create-container.js +10 -19
- package/esm/development/container/create-container.js.map +1 -1
- package/esm/development/container/wire-scope.js +41 -42
- package/esm/development/container/wire-scope.js.map +1 -1
- package/esm/development/error/error-code.js +8 -8
- package/esm/development/error/error-code.js.map +1 -1
- package/esm/development/error/wirestate-error.js +18 -17
- package/esm/development/error/wirestate-error.js.map +1 -1
- package/esm/development/events/build-event-dispatcher.js +5 -7
- package/esm/development/events/build-event-dispatcher.js.map +1 -1
- package/esm/development/events/emit-event.js +3 -3
- package/esm/development/events/emit-event.js.map +1 -1
- package/esm/development/events/event-bus.js +16 -23
- package/esm/development/events/event-bus.js.map +1 -1
- package/esm/development/events/get-event-handler-metadata.js +3 -3
- package/esm/development/events/get-event-handler-metadata.js.map +1 -1
- package/esm/development/events/on-event.js +4 -5
- package/esm/development/events/on-event.js.map +1 -1
- package/esm/development/queries/get-query-handler-metadata.js +3 -3
- package/esm/development/queries/get-query-handler-metadata.js.map +1 -1
- package/esm/development/queries/on-query.js +4 -4
- package/esm/development/queries/on-query.js.map +1 -1
- package/esm/development/queries/query-bus.js +29 -35
- package/esm/development/queries/query-bus.js.map +1 -1
- package/esm/development/registry.js +10 -10
- package/esm/development/registry.js.map +1 -1
- package/esm/development/seeds/apply-seeds.js +2 -5
- package/esm/development/seeds/apply-seeds.js.map +1 -1
- package/esm/development/seeds/tokens.js +2 -2
- package/esm/development/seeds/tokens.js.map +1 -1
- package/esm/development/seeds/unapply-seeds.js +2 -3
- package/esm/development/seeds/unapply-seeds.js.map +1 -1
- package/esm/development/service/get-activated-handler-metadata.js +3 -3
- package/esm/development/service/get-activated-handler-metadata.js.map +1 -1
- package/esm/development/service/get-deactivation-handler-metadata.js +3 -3
- package/esm/development/service/get-deactivation-handler-metadata.js.map +1 -1
- package/esm/development/service/on-activated.js +3 -3
- package/esm/development/service/on-activated.js.map +1 -1
- package/esm/development/service/on-deactivation.js +3 -3
- package/esm/development/service/on-deactivation.js.map +1 -1
- package/esm/development/test-utils/mock-bind-entry.js +4 -5
- package/esm/development/test-utils/mock-bind-entry.js.map +1 -1
- package/esm/development/test-utils/mock-bind-service.js +4 -5
- package/esm/development/test-utils/mock-bind-service.js.map +1 -1
- package/esm/development/test-utils/mock-container.js +13 -21
- package/esm/development/test-utils/mock-container.js.map +1 -1
- package/esm/development/test-utils/mock-service.js +1 -7
- package/esm/development/test-utils/mock-service.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 +1 -1
- package/esm/production/bind/bind-service.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/get-command-handler-metadata.js +1 -1
- package/esm/production/commands/get-command-handler-metadata.js.map +1 -1
- package/esm/production/commands/on-command.js +1 -1
- package/esm/production/commands/on-command.js.map +1 -1
- package/esm/production/container/create-base-container.js +1 -1
- package/esm/production/container/create-base-container.js.map +1 -1
- package/esm/production/container/create-container.js +1 -1
- package/esm/production/container/create-container.js.map +1 -1
- 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 +1 -1
- package/esm/production/error/wirestate-error.js.map +1 -1
- package/esm/production/events/build-event-dispatcher.js +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 +1 -1
- package/esm/production/events/get-event-handler-metadata.js.map +1 -1
- package/esm/production/events/on-event.js +1 -1
- package/esm/production/events/on-event.js.map +1 -1
- package/esm/production/queries/get-query-handler-metadata.js +1 -1
- package/esm/production/queries/get-query-handler-metadata.js.map +1 -1
- package/esm/production/queries/on-query.js +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/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/tokens.js +1 -1
- package/esm/production/seeds/tokens.js.map +1 -1
- 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 +1 -1
- package/esm/production/service/get-activated-handler-metadata.js.map +1 -1
- package/esm/production/service/get-deactivation-handler-metadata.js +1 -1
- package/esm/production/service/get-deactivation-handler-metadata.js.map +1 -1
- package/esm/production/service/on-activated.js +1 -1
- package/esm/production/service/on-activated.js.map +1 -1
- package/esm/production/service/on-deactivation.js +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 +1 -1
- package/esm/production/test-utils/mock-service.js.map +1 -1
- package/index.d.ts +1 -1
- package/package.json +1 -1
package/cjs/development/lib.js
CHANGED
|
@@ -3,14 +3,14 @@
|
|
|
3
3
|
var inversify = require('inversify');
|
|
4
4
|
var tslib = require('tslib');
|
|
5
5
|
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
6
|
+
const ERROR_CODE_GENERIC = 1;
|
|
7
|
+
const ERROR_CODE_VALIDATION_ERROR = 50;
|
|
8
|
+
const ERROR_CODE_INVALID_ARGUMENTS = 51;
|
|
9
|
+
const ERROR_CODE_BINDING_SCOPE = 52;
|
|
10
|
+
const ERROR_CODE_FAILED_TO_RESOLVE_QUERY_HANDLER = 101;
|
|
11
|
+
const ERROR_CODE_FAILED_TO_RESOLVE_COMMAND_HANDLER = 102;
|
|
12
|
+
const ERROR_CODE_ACCESS_BEFORE_ACTIVATION = 200;
|
|
13
|
+
const ERROR_CODE_ACCESS_AFTER_DISPOSAL = 201;
|
|
14
14
|
|
|
15
15
|
/**
|
|
16
16
|
* Base error class for all Wirestate-related exceptions.
|
|
@@ -33,29 +33,31 @@ var ERROR_CODE_ACCESS_AFTER_DISPOSAL = 201;
|
|
|
33
33
|
* }
|
|
34
34
|
* ```
|
|
35
35
|
*/
|
|
36
|
-
|
|
37
|
-
|
|
36
|
+
class WirestateError extends Error {
|
|
37
|
+
/**
|
|
38
|
+
* The name of the error class, useful for identification in minified environments.
|
|
39
|
+
*/
|
|
40
|
+
name = "WirestateError";
|
|
41
|
+
/**
|
|
42
|
+
* Numeric error code identifying the specific failure type.
|
|
43
|
+
*/
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
* Human-readable description of the error.
|
|
47
|
+
*/
|
|
48
|
+
|
|
38
49
|
/**
|
|
39
50
|
* Creates a new instance of WirestateError.
|
|
40
51
|
*
|
|
41
52
|
* @param code - Numeric identifier for the error (defaults to ERROR_CODE_GENERIC).
|
|
42
53
|
* @param detail - Optional descriptive message.
|
|
43
54
|
*/
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
* The name of the error class, useful for identification in minified environments.
|
|
51
|
-
*/
|
|
52
|
-
_this.name = "WirestateError";
|
|
53
|
-
_this.code = code;
|
|
54
|
-
_this.message = detail || "Wirestate error.";
|
|
55
|
-
return _this;
|
|
56
|
-
}
|
|
57
|
-
return WirestateError;
|
|
58
|
-
}(Error);
|
|
55
|
+
constructor(code = ERROR_CODE_GENERIC, detail) {
|
|
56
|
+
super();
|
|
57
|
+
this.code = code;
|
|
58
|
+
this.message = detail || "Wirestate error.";
|
|
59
|
+
}
|
|
60
|
+
}
|
|
59
61
|
|
|
60
62
|
/**
|
|
61
63
|
* Binds a constant value to a service identifier in the container.
|
|
@@ -118,7 +120,7 @@ function bindConstant(container, entry) {
|
|
|
118
120
|
* ```
|
|
119
121
|
*/
|
|
120
122
|
function bindDynamicValue(container, entry) {
|
|
121
|
-
|
|
123
|
+
const binding = container.bind(entry.id).toDynamicValue(() => {
|
|
122
124
|
if (entry.factory) {
|
|
123
125
|
return entry.factory();
|
|
124
126
|
}
|
|
@@ -160,20 +162,18 @@ exports.CommandStatus = void 0;
|
|
|
160
162
|
*
|
|
161
163
|
* @group Commands
|
|
162
164
|
*/
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
this.handlers = new Map();
|
|
170
|
-
}
|
|
165
|
+
class CommandBus {
|
|
166
|
+
/**
|
|
167
|
+
* Internal handler storage.
|
|
168
|
+
* Uses a stack for each command type to support shadowing.
|
|
169
|
+
*/
|
|
170
|
+
handlers = new Map();
|
|
171
171
|
/**
|
|
172
172
|
* Removes all registered command handlers from the bus.
|
|
173
173
|
*/
|
|
174
|
-
|
|
174
|
+
clear() {
|
|
175
175
|
this.handlers.clear();
|
|
176
|
-
}
|
|
176
|
+
}
|
|
177
177
|
/**
|
|
178
178
|
* Dispatches a command to the last registered handler.
|
|
179
179
|
*
|
|
@@ -196,27 +196,25 @@ var CommandBus = /** @class */function () {
|
|
|
196
196
|
* const user: User = await descriptor.task;
|
|
197
197
|
* ```
|
|
198
198
|
*/
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
if (!
|
|
202
|
-
throw new WirestateError(ERROR_CODE_FAILED_TO_RESOLVE_COMMAND_HANDLER,
|
|
199
|
+
command(type, data) {
|
|
200
|
+
const stack = this.handlers.get(type);
|
|
201
|
+
if (!stack?.length) {
|
|
202
|
+
throw new WirestateError(ERROR_CODE_FAILED_TO_RESOLVE_COMMAND_HANDLER, `No command handler registered in container for type: '${String(type)}'.`);
|
|
203
203
|
}
|
|
204
|
-
|
|
205
|
-
|
|
204
|
+
const handler = stack[stack.length - 1];
|
|
205
|
+
const descriptor = {
|
|
206
206
|
task: null,
|
|
207
207
|
status: exports.CommandStatus.PENDING
|
|
208
208
|
};
|
|
209
|
-
descriptor.task = Promise.resolve().then(
|
|
210
|
-
return handler(data);
|
|
211
|
-
}).then(function (result) {
|
|
209
|
+
descriptor.task = Promise.resolve().then(() => handler(data)).then(result => {
|
|
212
210
|
descriptor.status = exports.CommandStatus.SETTLED;
|
|
213
211
|
return result;
|
|
214
|
-
}).catch(
|
|
212
|
+
}).catch(error => {
|
|
215
213
|
descriptor.status = exports.CommandStatus.ERROR;
|
|
216
214
|
throw error;
|
|
217
215
|
});
|
|
218
216
|
return descriptor;
|
|
219
|
-
}
|
|
217
|
+
}
|
|
220
218
|
/**
|
|
221
219
|
* Dispatches a command if a handler exists, otherwise returns null.
|
|
222
220
|
*
|
|
@@ -227,20 +225,19 @@ var CommandBus = /** @class */function () {
|
|
|
227
225
|
* @param data - Optional payload for the handler.
|
|
228
226
|
* @returns A command descriptor, or `null` if no handler is found.
|
|
229
227
|
*/
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
return
|
|
233
|
-
}
|
|
228
|
+
commandOptional(type, data) {
|
|
229
|
+
const stack = this.handlers.get(type);
|
|
230
|
+
return stack?.length ? this.command(type, data) : null;
|
|
231
|
+
}
|
|
234
232
|
/**
|
|
235
233
|
* Checks if at least one handler is registered for the given command type.
|
|
236
234
|
*
|
|
237
235
|
* @param type - Command identifier.
|
|
238
236
|
* @returns `true` if a handler is available, `false` otherwise.
|
|
239
237
|
*/
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
};
|
|
238
|
+
has(type) {
|
|
239
|
+
return Boolean(this.handlers.get(type)?.length);
|
|
240
|
+
}
|
|
244
241
|
/**
|
|
245
242
|
* Registers a handler for a specific command type.
|
|
246
243
|
*
|
|
@@ -262,18 +259,15 @@ var CommandBus = /** @class */function () {
|
|
|
262
259
|
* });
|
|
263
260
|
* ```
|
|
264
261
|
*/
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
var stack = this.handlers.get(type);
|
|
262
|
+
register(type, handler) {
|
|
263
|
+
let stack = this.handlers.get(type);
|
|
268
264
|
if (!stack) {
|
|
269
265
|
stack = [];
|
|
270
266
|
this.handlers.set(type, stack);
|
|
271
267
|
}
|
|
272
268
|
stack.push(handler);
|
|
273
|
-
return
|
|
274
|
-
|
|
275
|
-
};
|
|
276
|
-
};
|
|
269
|
+
return () => this.unregister(type, handler);
|
|
270
|
+
}
|
|
277
271
|
/**
|
|
278
272
|
* Removes a previously registered command handler.
|
|
279
273
|
*
|
|
@@ -286,12 +280,12 @@ var CommandBus = /** @class */function () {
|
|
|
286
280
|
* @param type - Command identifier.
|
|
287
281
|
* @param handler - The handler function instance to remove.
|
|
288
282
|
*/
|
|
289
|
-
|
|
290
|
-
|
|
283
|
+
unregister(type, handler) {
|
|
284
|
+
const current = this.handlers.get(type);
|
|
291
285
|
if (!current) {
|
|
292
286
|
return;
|
|
293
287
|
}
|
|
294
|
-
|
|
288
|
+
const index = current.indexOf(handler);
|
|
295
289
|
if (index >= 0) {
|
|
296
290
|
current.splice(index, 1);
|
|
297
291
|
}
|
|
@@ -299,9 +293,8 @@ var CommandBus = /** @class */function () {
|
|
|
299
293
|
if (current.length === 0) {
|
|
300
294
|
this.handlers.delete(type);
|
|
301
295
|
}
|
|
302
|
-
}
|
|
303
|
-
|
|
304
|
-
}();
|
|
296
|
+
}
|
|
297
|
+
}
|
|
305
298
|
|
|
306
299
|
/**
|
|
307
300
|
* Registry of class constructors to their declared query handlers.
|
|
@@ -313,7 +306,7 @@ var CommandBus = /** @class */function () {
|
|
|
313
306
|
* @group Queries
|
|
314
307
|
* @internal
|
|
315
308
|
*/
|
|
316
|
-
|
|
309
|
+
const QUERY_HANDLER_METADATA = new WeakMap();
|
|
317
310
|
/**
|
|
318
311
|
* Registry of class constructors to their declared command handlers.
|
|
319
312
|
*
|
|
@@ -324,7 +317,7 @@ var QUERY_HANDLER_METADATA = new WeakMap();
|
|
|
324
317
|
* @group Commands
|
|
325
318
|
* @internal
|
|
326
319
|
*/
|
|
327
|
-
|
|
320
|
+
const COMMAND_HANDLER_METADATA = new WeakMap();
|
|
328
321
|
/**
|
|
329
322
|
* Registry of class constructors to their `@OnActivated`-decorated method names.
|
|
330
323
|
*
|
|
@@ -335,7 +328,7 @@ var COMMAND_HANDLER_METADATA = new WeakMap();
|
|
|
335
328
|
* @group Service
|
|
336
329
|
* @internal
|
|
337
330
|
*/
|
|
338
|
-
|
|
331
|
+
const ACTIVATED_HANDLER_METADATA = new WeakMap();
|
|
339
332
|
/**
|
|
340
333
|
* Registry of class constructors to their `@OnDeactivation`-decorated method names.
|
|
341
334
|
*
|
|
@@ -346,7 +339,7 @@ var ACTIVATED_HANDLER_METADATA = new WeakMap();
|
|
|
346
339
|
* @group Service
|
|
347
340
|
* @internal
|
|
348
341
|
*/
|
|
349
|
-
|
|
342
|
+
const DEACTIVATION_HANDLER_METADATA = new WeakMap();
|
|
350
343
|
/**
|
|
351
344
|
* Registry of class constructors to their declared event handlers.
|
|
352
345
|
*
|
|
@@ -357,7 +350,7 @@ var DEACTIVATION_HANDLER_METADATA = new WeakMap();
|
|
|
357
350
|
* @group Events
|
|
358
351
|
* @internal
|
|
359
352
|
*/
|
|
360
|
-
|
|
353
|
+
const EVENT_HANDLER_METADATA = new WeakMap();
|
|
361
354
|
/**
|
|
362
355
|
* Internal storage for mapping service instances to their originating Inversify containers.
|
|
363
356
|
*
|
|
@@ -368,7 +361,7 @@ var EVENT_HANDLER_METADATA = new WeakMap();
|
|
|
368
361
|
* @group Bind
|
|
369
362
|
* @internal
|
|
370
363
|
*/
|
|
371
|
-
|
|
364
|
+
const CONTAINER_REFS_BY_SERVICE = new WeakMap();
|
|
372
365
|
/**
|
|
373
366
|
* Internal storage for managing injected {@link WireScope} instances per service.
|
|
374
367
|
*
|
|
@@ -379,7 +372,7 @@ var CONTAINER_REFS_BY_SERVICE = new WeakMap();
|
|
|
379
372
|
* @group Container
|
|
380
373
|
* @internal
|
|
381
374
|
*/
|
|
382
|
-
|
|
375
|
+
const WIRE_SCOPES_BY_SERVICE = new WeakMap();
|
|
383
376
|
/**
|
|
384
377
|
* Internal storage for service event unsubscribers.
|
|
385
378
|
*
|
|
@@ -390,7 +383,7 @@ var WIRE_SCOPES_BY_SERVICE = new WeakMap();
|
|
|
390
383
|
* @group Events
|
|
391
384
|
* @internal
|
|
392
385
|
*/
|
|
393
|
-
|
|
386
|
+
const EVENT_UNSUBSCRIBERS_BY_SERVICE = new WeakMap();
|
|
394
387
|
/**
|
|
395
388
|
* Internal storage for service query unregisters.
|
|
396
389
|
*
|
|
@@ -401,7 +394,7 @@ var EVENT_UNSUBSCRIBERS_BY_SERVICE = new WeakMap();
|
|
|
401
394
|
* @group Queries
|
|
402
395
|
* @internal
|
|
403
396
|
*/
|
|
404
|
-
|
|
397
|
+
const QUERY_UNREGISTERS_BY_SERVICE = new WeakMap();
|
|
405
398
|
/**
|
|
406
399
|
* Internal storage for service command unregisters.
|
|
407
400
|
*
|
|
@@ -412,7 +405,7 @@ var QUERY_UNREGISTERS_BY_SERVICE = new WeakMap();
|
|
|
412
405
|
* @group Commands
|
|
413
406
|
* @internal
|
|
414
407
|
*/
|
|
415
|
-
|
|
408
|
+
const COMMAND_UNREGISTERS_BY_SERVICE = new WeakMap();
|
|
416
409
|
|
|
417
410
|
/**
|
|
418
411
|
* Retrieves `@OnCommand` metadata from the class hierarchy.
|
|
@@ -428,11 +421,11 @@ var COMMAND_UNREGISTERS_BY_SERVICE = new WeakMap();
|
|
|
428
421
|
* @returns A read-only array of metadata for all discovered command handlers.
|
|
429
422
|
*/
|
|
430
423
|
function getCommandHandlerMetadata(instance) {
|
|
431
|
-
|
|
432
|
-
|
|
424
|
+
let constructor = instance.constructor;
|
|
425
|
+
const chain = [];
|
|
433
426
|
// Traverse prototype chain up to Object/Function
|
|
434
427
|
while (typeof constructor === "function" && constructor !== Object && constructor !== Function.prototype) {
|
|
435
|
-
|
|
428
|
+
const own = COMMAND_HANDLER_METADATA.get(constructor);
|
|
436
429
|
if (own && own.length > 0) {
|
|
437
430
|
chain.push(own);
|
|
438
431
|
}
|
|
@@ -452,10 +445,8 @@ function getCommandHandlerMetadata(instance) {
|
|
|
452
445
|
*
|
|
453
446
|
* @group Events
|
|
454
447
|
*/
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
this.handlers = new Set();
|
|
458
|
-
}
|
|
448
|
+
class EventBus {
|
|
449
|
+
handlers = new Set();
|
|
459
450
|
/**
|
|
460
451
|
* Broadcasts an event to all registered subscribers.
|
|
461
452
|
*
|
|
@@ -478,11 +469,10 @@ var EventBus = /** @class */function () {
|
|
|
478
469
|
* });
|
|
479
470
|
* ```
|
|
480
471
|
*/
|
|
481
|
-
|
|
472
|
+
emit(event) {
|
|
482
473
|
// Snapshot prevents concurrent modification errors if handlers sub/unsub during emit.
|
|
483
|
-
|
|
484
|
-
for (
|
|
485
|
-
var handler = snapshot_1[_i];
|
|
474
|
+
const snapshot = Array.from(this.handlers);
|
|
475
|
+
for (const handler of snapshot) {
|
|
486
476
|
try {
|
|
487
477
|
handler(event);
|
|
488
478
|
} catch (error) {
|
|
@@ -490,7 +480,7 @@ var EventBus = /** @class */function () {
|
|
|
490
480
|
console.error("[wirestate] Event handler threw:", error);
|
|
491
481
|
}
|
|
492
482
|
}
|
|
493
|
-
}
|
|
483
|
+
}
|
|
494
484
|
/**
|
|
495
485
|
* Registers a handler to receive all broadcasted events.
|
|
496
486
|
*
|
|
@@ -504,13 +494,10 @@ var EventBus = /** @class */function () {
|
|
|
504
494
|
* });
|
|
505
495
|
* ```
|
|
506
496
|
*/
|
|
507
|
-
|
|
508
|
-
var _this = this;
|
|
497
|
+
subscribe(handler) {
|
|
509
498
|
this.handlers.add(handler);
|
|
510
|
-
return
|
|
511
|
-
|
|
512
|
-
};
|
|
513
|
-
};
|
|
499
|
+
return () => this.unsubscribe(handler);
|
|
500
|
+
}
|
|
514
501
|
/**
|
|
515
502
|
* Removes a previously registered event handler.
|
|
516
503
|
*
|
|
@@ -519,27 +506,26 @@ var EventBus = /** @class */function () {
|
|
|
519
506
|
*
|
|
520
507
|
* @param handler - The handler function instance to remove.
|
|
521
508
|
*/
|
|
522
|
-
|
|
509
|
+
unsubscribe(handler) {
|
|
523
510
|
this.handlers.delete(handler);
|
|
524
|
-
}
|
|
511
|
+
}
|
|
525
512
|
/**
|
|
526
513
|
* Checks if the bus has any active subscribers.
|
|
527
514
|
*
|
|
528
515
|
* @returns `true` if at least one handler is registered, `false` otherwise.
|
|
529
516
|
*/
|
|
530
|
-
|
|
517
|
+
has() {
|
|
531
518
|
return this.handlers.size > 0;
|
|
532
|
-
}
|
|
519
|
+
}
|
|
533
520
|
/**
|
|
534
521
|
* Removes all registered handlers from the bus.
|
|
535
522
|
*
|
|
536
523
|
* @internal
|
|
537
524
|
*/
|
|
538
|
-
|
|
525
|
+
clear() {
|
|
539
526
|
this.handlers.clear();
|
|
540
|
-
}
|
|
541
|
-
|
|
542
|
-
}();
|
|
527
|
+
}
|
|
528
|
+
}
|
|
543
529
|
|
|
544
530
|
/**
|
|
545
531
|
* Orchestrates query dispatching and handler registration.
|
|
@@ -552,14 +538,12 @@ var EventBus = /** @class */function () {
|
|
|
552
538
|
*
|
|
553
539
|
* @group Queries
|
|
554
540
|
*/
|
|
555
|
-
|
|
556
|
-
|
|
557
|
-
|
|
558
|
-
|
|
559
|
-
|
|
560
|
-
|
|
561
|
-
this.handlers = new Map();
|
|
562
|
-
}
|
|
541
|
+
class QueryBus {
|
|
542
|
+
/**
|
|
543
|
+
* Internal handler storage.
|
|
544
|
+
* Uses a stack for each query type to support shadowing (e.g., component-level vs service-level).
|
|
545
|
+
*/
|
|
546
|
+
handlers = new Map();
|
|
563
547
|
/**
|
|
564
548
|
* Registers a handler for a specific query type.
|
|
565
549
|
*
|
|
@@ -579,18 +563,15 @@ var QueryBus = /** @class */function () {
|
|
|
579
563
|
* const unregister: QueryUnregister = queryBus.register("GET_NOW", () => Date.now());
|
|
580
564
|
* ```
|
|
581
565
|
*/
|
|
582
|
-
|
|
583
|
-
|
|
584
|
-
var stack = this.handlers.get(type);
|
|
566
|
+
register(type, handler) {
|
|
567
|
+
let stack = this.handlers.get(type);
|
|
585
568
|
if (!stack) {
|
|
586
569
|
stack = [];
|
|
587
570
|
this.handlers.set(type, stack);
|
|
588
571
|
}
|
|
589
572
|
stack.push(handler);
|
|
590
|
-
return
|
|
591
|
-
|
|
592
|
-
};
|
|
593
|
-
};
|
|
573
|
+
return () => this.unregister(type, handler);
|
|
574
|
+
}
|
|
594
575
|
/**
|
|
595
576
|
* Removes a previously registered query handler.
|
|
596
577
|
*
|
|
@@ -603,12 +584,12 @@ var QueryBus = /** @class */function () {
|
|
|
603
584
|
* @param type - Unique query identifier.
|
|
604
585
|
* @param handler - The handler function instance to remove.
|
|
605
586
|
*/
|
|
606
|
-
|
|
607
|
-
|
|
587
|
+
unregister(type, handler) {
|
|
588
|
+
const current = this.handlers.get(type);
|
|
608
589
|
if (!current) {
|
|
609
590
|
return;
|
|
610
591
|
}
|
|
611
|
-
|
|
592
|
+
const index = current.indexOf(handler);
|
|
612
593
|
if (index >= 0) {
|
|
613
594
|
current.splice(index, 1);
|
|
614
595
|
}
|
|
@@ -616,7 +597,7 @@ var QueryBus = /** @class */function () {
|
|
|
616
597
|
if (current.length === 0) {
|
|
617
598
|
this.handlers.delete(type);
|
|
618
599
|
}
|
|
619
|
-
}
|
|
600
|
+
}
|
|
620
601
|
/**
|
|
621
602
|
* Dispatches a query to the last registered handler and returns the result.
|
|
622
603
|
*
|
|
@@ -639,14 +620,14 @@ var QueryBus = /** @class */function () {
|
|
|
639
620
|
* const user: User = await queryBus.query<User, string>("FIND_USER", "user-id-123");
|
|
640
621
|
* ```
|
|
641
622
|
*/
|
|
642
|
-
|
|
643
|
-
|
|
623
|
+
query(type, data) {
|
|
624
|
+
const stack = this.handlers.get(type);
|
|
644
625
|
// Always use the top of the stack (most recent registration) if handlers are available.
|
|
645
|
-
if (stack
|
|
626
|
+
if (stack?.length) {
|
|
646
627
|
return stack[stack.length - 1](data);
|
|
647
628
|
}
|
|
648
|
-
throw new WirestateError(ERROR_CODE_FAILED_TO_RESOLVE_QUERY_HANDLER,
|
|
649
|
-
}
|
|
629
|
+
throw new WirestateError(ERROR_CODE_FAILED_TO_RESOLVE_QUERY_HANDLER, `No query handler registered in container for type: '${String(type)}'.`);
|
|
630
|
+
}
|
|
650
631
|
/**
|
|
651
632
|
* Dispatches a query if a handler exists, otherwise returns null.
|
|
652
633
|
*
|
|
@@ -658,33 +639,32 @@ var QueryBus = /** @class */function () {
|
|
|
658
639
|
* @param data - Optional input data for the handler.
|
|
659
640
|
* @returns The query result, or `null` if no handler is found.
|
|
660
641
|
*/
|
|
661
|
-
|
|
662
|
-
|
|
663
|
-
if (stack
|
|
642
|
+
queryOptional(type, data) {
|
|
643
|
+
const stack = this.handlers.get(type);
|
|
644
|
+
if (stack?.length) {
|
|
664
645
|
return stack[stack.length - 1](data);
|
|
665
646
|
}
|
|
666
647
|
return null;
|
|
667
|
-
}
|
|
648
|
+
}
|
|
668
649
|
/**
|
|
669
650
|
* Checks if at least one handler is registered for the given query type.
|
|
670
651
|
*
|
|
671
652
|
* @param type - Unique query identifier.
|
|
672
653
|
* @returns `true` if a handler is available, `false` otherwise.
|
|
673
654
|
*/
|
|
674
|
-
|
|
675
|
-
|
|
655
|
+
has(type) {
|
|
656
|
+
const stack = this.handlers.get(type);
|
|
676
657
|
return Boolean(stack && stack.length);
|
|
677
|
-
}
|
|
658
|
+
}
|
|
678
659
|
/**
|
|
679
660
|
* Removes all registered query handlers from the bus.
|
|
680
661
|
*
|
|
681
662
|
* @internal
|
|
682
663
|
*/
|
|
683
|
-
|
|
664
|
+
clear() {
|
|
684
665
|
this.handlers.clear();
|
|
685
|
-
}
|
|
686
|
-
|
|
687
|
-
}();
|
|
666
|
+
}
|
|
667
|
+
}
|
|
688
668
|
|
|
689
669
|
/**
|
|
690
670
|
* Unique symbol used as a token for the container-scoped seeds map.
|
|
@@ -699,7 +679,7 @@ var QueryBus = /** @class */function () {
|
|
|
699
679
|
* const seedsMap: SeedsMap = container.get(SEEDS_TOKEN);
|
|
700
680
|
* ```
|
|
701
681
|
*/
|
|
702
|
-
|
|
682
|
+
const SEEDS_TOKEN = Symbol("@wirestate/core/seeds");
|
|
703
683
|
/**
|
|
704
684
|
* Unique symbol used as a token for the container-scoped shared seed object.
|
|
705
685
|
*
|
|
@@ -713,7 +693,7 @@ var SEEDS_TOKEN = Symbol("@wirestate/core/seeds");
|
|
|
713
693
|
* const sharedSeed: AnyObject = container.get(SEED_TOKEN);
|
|
714
694
|
* ```
|
|
715
695
|
*/
|
|
716
|
-
|
|
696
|
+
const SEED_TOKEN = Symbol("@wirestate/core/seed");
|
|
717
697
|
|
|
718
698
|
/**
|
|
719
699
|
* A transient bridge providing services with access to Wirestate buses, lazy resolution, and seeds.
|
|
@@ -726,13 +706,13 @@ var SEED_TOKEN = Symbol("@wirestate/core/seed");
|
|
|
726
706
|
*
|
|
727
707
|
* @group Container
|
|
728
708
|
*/
|
|
729
|
-
|
|
730
|
-
|
|
709
|
+
exports.WireScope = class WireScope {
|
|
710
|
+
/**
|
|
711
|
+
* Whether the scope was deactivated and disposed from the container.
|
|
712
|
+
*/
|
|
713
|
+
isDisposed = false;
|
|
714
|
+
constructor(container) {
|
|
731
715
|
this.container = container;
|
|
732
|
-
/**
|
|
733
|
-
* Whether the scope was deactivated and disposed from the container.
|
|
734
|
-
*/
|
|
735
|
-
this.isDisposed = false;
|
|
736
716
|
}
|
|
737
717
|
/**
|
|
738
718
|
* Provides direct access to the underlying Inversify {@link Container}.
|
|
@@ -748,7 +728,7 @@ var WireScope = /** @class */function () {
|
|
|
748
728
|
* container.bind("TOKEN").toConstantValue(42);
|
|
749
729
|
* ```
|
|
750
730
|
*/
|
|
751
|
-
|
|
731
|
+
getContainer() {
|
|
752
732
|
if (this.container) {
|
|
753
733
|
return this.container;
|
|
754
734
|
}
|
|
@@ -757,7 +737,7 @@ var WireScope = /** @class */function () {
|
|
|
757
737
|
} else {
|
|
758
738
|
throw new WirestateError(ERROR_CODE_ACCESS_BEFORE_ACTIVATION, "WireScope::container accessed before activation. " + "Ensure service is bound to container and is properly resolved.");
|
|
759
739
|
}
|
|
760
|
-
}
|
|
740
|
+
}
|
|
761
741
|
/**
|
|
762
742
|
* Lazily resolves a service or value from the container.
|
|
763
743
|
*
|
|
@@ -777,9 +757,9 @@ var WireScope = /** @class */function () {
|
|
|
777
757
|
* const service: MyService = scope.resolve(MyService);
|
|
778
758
|
* ```
|
|
779
759
|
*/
|
|
780
|
-
|
|
760
|
+
resolve(injectionId) {
|
|
781
761
|
return this.getContainer().get(injectionId);
|
|
782
|
-
}
|
|
762
|
+
}
|
|
783
763
|
/**
|
|
784
764
|
* Lazily resolves a service if it is bound, otherwise returns null.
|
|
785
765
|
*
|
|
@@ -797,10 +777,10 @@ var WireScope = /** @class */function () {
|
|
|
797
777
|
* logger?.info("Resolved optionally");
|
|
798
778
|
* ```
|
|
799
779
|
*/
|
|
800
|
-
|
|
801
|
-
|
|
780
|
+
resolveOptional(injectionId) {
|
|
781
|
+
const container = this.getContainer();
|
|
802
782
|
return container.isBound(injectionId) ? container.get(injectionId) : null;
|
|
803
|
-
}
|
|
783
|
+
}
|
|
804
784
|
/**
|
|
805
785
|
* Dispatches an event to the {@link EventBus}.
|
|
806
786
|
*
|
|
@@ -818,13 +798,13 @@ var WireScope = /** @class */function () {
|
|
|
818
798
|
* scope.emitEvent("VALUE_CHANGED", { value: "abcd" });
|
|
819
799
|
* ```
|
|
820
800
|
*/
|
|
821
|
-
|
|
801
|
+
emitEvent(type, payload, from) {
|
|
822
802
|
this.getContainer().get(EventBus).emit({
|
|
823
|
-
type
|
|
824
|
-
payload
|
|
803
|
+
type,
|
|
804
|
+
payload,
|
|
825
805
|
from: from === undefined ? this : from
|
|
826
806
|
});
|
|
827
|
-
}
|
|
807
|
+
}
|
|
828
808
|
/**
|
|
829
809
|
* Subscribes to all events on the {@link EventBus}.
|
|
830
810
|
*
|
|
@@ -840,9 +820,9 @@ var WireScope = /** @class */function () {
|
|
|
840
820
|
* });
|
|
841
821
|
* ```
|
|
842
822
|
*/
|
|
843
|
-
|
|
823
|
+
subscribeToEvent(handler) {
|
|
844
824
|
return this.getContainer().get(EventBus).subscribe(handler);
|
|
845
|
-
}
|
|
825
|
+
}
|
|
846
826
|
/**
|
|
847
827
|
* Unsubscribes a specific handler from the {@link EventBus}.
|
|
848
828
|
*
|
|
@@ -855,9 +835,9 @@ var WireScope = /** @class */function () {
|
|
|
855
835
|
* scope.unsubscribeFromEvent(this.onEvent);
|
|
856
836
|
* ```
|
|
857
837
|
*/
|
|
858
|
-
|
|
838
|
+
unsubscribeFromEvent(handler) {
|
|
859
839
|
this.getContainer().get(EventBus).unsubscribe(handler);
|
|
860
|
-
}
|
|
840
|
+
}
|
|
861
841
|
/**
|
|
862
842
|
* Dispatches a query and waits for the result.
|
|
863
843
|
*
|
|
@@ -877,9 +857,9 @@ var WireScope = /** @class */function () {
|
|
|
877
857
|
* const user: User = await scope.queryData("GET_USER", { id: 1 });
|
|
878
858
|
* ```
|
|
879
859
|
*/
|
|
880
|
-
|
|
860
|
+
queryData(type, data) {
|
|
881
861
|
return this.getContainer().get(QueryBus).query(type, data);
|
|
882
|
-
}
|
|
862
|
+
}
|
|
883
863
|
/**
|
|
884
864
|
* Dispatches a query and returns the result, or null if no handler is registered.
|
|
885
865
|
*
|
|
@@ -898,9 +878,9 @@ var WireScope = /** @class */function () {
|
|
|
898
878
|
* const config: Config | null = await scope.queryOptionalData("GET_CONFIG");
|
|
899
879
|
* ```
|
|
900
880
|
*/
|
|
901
|
-
|
|
881
|
+
queryOptionalData(type, data) {
|
|
902
882
|
return this.getContainer().get(QueryBus).queryOptional(type, data);
|
|
903
|
-
}
|
|
883
|
+
}
|
|
904
884
|
/**
|
|
905
885
|
* Registers a handler for a specific query type.
|
|
906
886
|
*
|
|
@@ -918,9 +898,9 @@ var WireScope = /** @class */function () {
|
|
|
918
898
|
* scope.registerQueryHandler("GET_DATE_NOW", () => new Date());
|
|
919
899
|
* ```
|
|
920
900
|
*/
|
|
921
|
-
|
|
901
|
+
registerQueryHandler(type, handler) {
|
|
922
902
|
return this.getContainer().get(QueryBus).register(type, handler);
|
|
923
|
-
}
|
|
903
|
+
}
|
|
924
904
|
/**
|
|
925
905
|
* Removes a specific query handler registration.
|
|
926
906
|
*
|
|
@@ -937,9 +917,9 @@ var WireScope = /** @class */function () {
|
|
|
937
917
|
* scope.unregisterQueryHandler("GET_DATE_NOW", this.onGetDateNow);
|
|
938
918
|
* ```
|
|
939
919
|
*/
|
|
940
|
-
|
|
920
|
+
unregisterQueryHandler(type, handler) {
|
|
941
921
|
this.getContainer().get(QueryBus).unregister(type, handler);
|
|
942
|
-
}
|
|
922
|
+
}
|
|
943
923
|
/**
|
|
944
924
|
* Dispatches a command and returns a descriptor to track its progress.
|
|
945
925
|
*
|
|
@@ -961,9 +941,9 @@ var WireScope = /** @class */function () {
|
|
|
961
941
|
* await descriptor.task;
|
|
962
942
|
* ```
|
|
963
943
|
*/
|
|
964
|
-
|
|
944
|
+
executeCommand(type, data) {
|
|
965
945
|
return this.getContainer().get(CommandBus).command(type, data);
|
|
966
|
-
}
|
|
946
|
+
}
|
|
967
947
|
/**
|
|
968
948
|
* Dispatches a command if a handler is registered, otherwise returns null.
|
|
969
949
|
*
|
|
@@ -986,9 +966,9 @@ var WireScope = /** @class */function () {
|
|
|
986
966
|
* }
|
|
987
967
|
* ```
|
|
988
968
|
*/
|
|
989
|
-
|
|
969
|
+
executeOptionalCommand(type, data) {
|
|
990
970
|
return this.getContainer().get(CommandBus).commandOptional(type, data);
|
|
991
|
-
}
|
|
971
|
+
}
|
|
992
972
|
/**
|
|
993
973
|
* Registers a handler for a specific command type.
|
|
994
974
|
*
|
|
@@ -1008,9 +988,9 @@ var WireScope = /** @class */function () {
|
|
|
1008
988
|
* });
|
|
1009
989
|
* ```
|
|
1010
990
|
*/
|
|
1011
|
-
|
|
991
|
+
registerCommandHandler(type, handler) {
|
|
1012
992
|
return this.getContainer().get(CommandBus).register(type, handler);
|
|
1013
|
-
}
|
|
993
|
+
}
|
|
1014
994
|
/**
|
|
1015
995
|
* Removes a specific command handler registration.
|
|
1016
996
|
*
|
|
@@ -1027,15 +1007,14 @@ var WireScope = /** @class */function () {
|
|
|
1027
1007
|
* scope.unregisterCommandHandler("LOG_ERROR", this.handleLogError);
|
|
1028
1008
|
* ```
|
|
1029
1009
|
*/
|
|
1030
|
-
|
|
1010
|
+
unregisterCommandHandler(type, handler) {
|
|
1031
1011
|
this.getContainer().get(CommandBus).unregister(type, handler);
|
|
1032
|
-
}
|
|
1033
|
-
|
|
1012
|
+
}
|
|
1013
|
+
getSeed(seed) {
|
|
1034
1014
|
return seed ? this.getContainer().get(SEEDS_TOKEN).get(seed) || null : this.getContainer().get(SEED_TOKEN);
|
|
1035
|
-
}
|
|
1036
|
-
|
|
1037
|
-
|
|
1038
|
-
}();
|
|
1015
|
+
}
|
|
1016
|
+
};
|
|
1017
|
+
exports.WireScope = tslib.__decorate([inversify.injectable(), tslib.__metadata("design:paramtypes", [Object])], exports.WireScope);
|
|
1039
1018
|
|
|
1040
1019
|
/**
|
|
1041
1020
|
* Retrieves event handler metadata for a service instance by traversing its prototype chain.
|
|
@@ -1061,11 +1040,11 @@ var WireScope = /** @class */function () {
|
|
|
1061
1040
|
* ```
|
|
1062
1041
|
*/
|
|
1063
1042
|
function getEventHandlerMetadata(instance) {
|
|
1064
|
-
|
|
1065
|
-
|
|
1043
|
+
let constructor = instance.constructor;
|
|
1044
|
+
const chain = [];
|
|
1066
1045
|
// Traverse prototype chain up to Object/Function
|
|
1067
1046
|
while (typeof constructor === "function" && constructor !== Object && constructor !== Function.prototype) {
|
|
1068
|
-
|
|
1047
|
+
const own = EVENT_HANDLER_METADATA.get(constructor);
|
|
1069
1048
|
if (own && own.length > 0) {
|
|
1070
1049
|
chain.push(own);
|
|
1071
1050
|
}
|
|
@@ -1101,11 +1080,10 @@ function getEventHandlerMetadata(instance) {
|
|
|
1101
1080
|
* ```
|
|
1102
1081
|
*/
|
|
1103
1082
|
function buildEventDispatcher(instance) {
|
|
1104
|
-
|
|
1083
|
+
const entries = [];
|
|
1105
1084
|
// Register methods decorated with @OnEvent.
|
|
1106
|
-
for (
|
|
1107
|
-
|
|
1108
|
-
var method = instance[meta.methodName];
|
|
1085
|
+
for (const meta of getEventHandlerMetadata(instance)) {
|
|
1086
|
+
const method = instance[meta.methodName];
|
|
1109
1087
|
if (typeof method === "function") {
|
|
1110
1088
|
entries.push({
|
|
1111
1089
|
types: meta.types,
|
|
@@ -1114,10 +1092,9 @@ function buildEventDispatcher(instance) {
|
|
|
1114
1092
|
}
|
|
1115
1093
|
}
|
|
1116
1094
|
if (entries.length) {
|
|
1117
|
-
return
|
|
1095
|
+
return event => {
|
|
1118
1096
|
// Fan out events to all matching handlers.
|
|
1119
|
-
for (
|
|
1120
|
-
var entry = entries_1[_i];
|
|
1097
|
+
for (const entry of entries) {
|
|
1121
1098
|
if (entry.types === null || entry.types.includes(event.type)) {
|
|
1122
1099
|
entry.handler(event);
|
|
1123
1100
|
}
|
|
@@ -1153,11 +1130,11 @@ function buildEventDispatcher(instance) {
|
|
|
1153
1130
|
* ```
|
|
1154
1131
|
*/
|
|
1155
1132
|
function getQueryHandlerMetadata(instance) {
|
|
1156
|
-
|
|
1157
|
-
|
|
1133
|
+
let constructor = instance.constructor;
|
|
1134
|
+
const chain = [];
|
|
1158
1135
|
// Traverse prototype chain up to Object/Function
|
|
1159
1136
|
while (typeof constructor === "function" && constructor !== Object && constructor !== Function.prototype) {
|
|
1160
|
-
|
|
1137
|
+
const own = QUERY_HANDLER_METADATA.get(constructor);
|
|
1161
1138
|
if (own && own.length > 0) {
|
|
1162
1139
|
chain.push(own);
|
|
1163
1140
|
}
|
|
@@ -1188,11 +1165,11 @@ function getQueryHandlerMetadata(instance) {
|
|
|
1188
1165
|
* ```
|
|
1189
1166
|
*/
|
|
1190
1167
|
function getActivatedHandlerMetadata(instance) {
|
|
1191
|
-
|
|
1192
|
-
|
|
1168
|
+
let constructor = instance.constructor;
|
|
1169
|
+
const chain = [];
|
|
1193
1170
|
// Traverse prototype chain up to Object/Function.
|
|
1194
1171
|
while (typeof constructor === "function" && constructor !== Object && constructor !== Function.prototype) {
|
|
1195
|
-
|
|
1172
|
+
const own = ACTIVATED_HANDLER_METADATA.get(constructor);
|
|
1196
1173
|
if (own && own.length > 0) {
|
|
1197
1174
|
chain.push(own);
|
|
1198
1175
|
}
|
|
@@ -1223,11 +1200,11 @@ function getActivatedHandlerMetadata(instance) {
|
|
|
1223
1200
|
* ```
|
|
1224
1201
|
*/
|
|
1225
1202
|
function getDeactivationHandlerMetadata(instance) {
|
|
1226
|
-
|
|
1227
|
-
|
|
1203
|
+
let constructor = instance.constructor;
|
|
1204
|
+
const chain = [];
|
|
1228
1205
|
// Traverse prototype chain up to Object/Function.
|
|
1229
1206
|
while (typeof constructor === "function" && constructor !== Object && constructor !== Function.prototype) {
|
|
1230
|
-
|
|
1207
|
+
const own = DEACTIVATION_HANDLER_METADATA.get(constructor);
|
|
1231
1208
|
if (own && own.length > 0) {
|
|
1232
1209
|
chain.push(own);
|
|
1233
1210
|
}
|
|
@@ -1294,73 +1271,66 @@ function bindService(container, entry, options) {
|
|
|
1294
1271
|
// `.onDeactivation` call per chain, so we register them on the container
|
|
1295
1272
|
// itself instead — this also works correctly if a later call rebinds the
|
|
1296
1273
|
// same token.
|
|
1297
|
-
|
|
1298
|
-
whenBind.onActivation(
|
|
1274
|
+
const whenBind = container.bind(entry).to(entry).inSingletonScope();
|
|
1275
|
+
whenBind.onActivation((context, instance) => {
|
|
1299
1276
|
// Ensure flag is initialized on activation.
|
|
1300
1277
|
instance.IS_DISPOSED = false;
|
|
1301
1278
|
CONTAINER_REFS_BY_SERVICE.set(instance, container);
|
|
1302
1279
|
attachWireScopes(instance, entry);
|
|
1303
1280
|
// Compose all events listeners into a single bus subscription so we only
|
|
1304
1281
|
// pay one Set lookup per emitted event.
|
|
1305
|
-
|
|
1282
|
+
const dispatcher = buildEventDispatcher(instance);
|
|
1306
1283
|
if (dispatcher) {
|
|
1307
1284
|
attachEventsSubscription(instance, dispatcher);
|
|
1308
1285
|
}
|
|
1309
1286
|
// Register every `@OnQuery` handler on the container's QueryBus, and
|
|
1310
1287
|
// remember the unregister functions so we can roll them back when the
|
|
1311
1288
|
// service is deactivated.
|
|
1312
|
-
|
|
1313
|
-
for (
|
|
1314
|
-
|
|
1315
|
-
var method = instance[meta.methodName];
|
|
1289
|
+
const queryBus = container.get(QueryBus);
|
|
1290
|
+
for (const meta of getQueryHandlerMetadata(instance)) {
|
|
1291
|
+
const method = instance[meta.methodName];
|
|
1316
1292
|
if (typeof method !== "function") {
|
|
1317
1293
|
continue;
|
|
1318
1294
|
}
|
|
1319
|
-
|
|
1295
|
+
const unregister = queryBus.register(meta.type, method.bind(instance));
|
|
1320
1296
|
attachQueryUnregister(instance, unregister);
|
|
1321
1297
|
}
|
|
1322
1298
|
// Register every `@OnCommand` handler on the container's CommandBus, and
|
|
1323
1299
|
// remember the unregister functions so we can roll them back when the
|
|
1324
1300
|
// service is deactivated.
|
|
1325
|
-
|
|
1326
|
-
for (
|
|
1327
|
-
|
|
1328
|
-
var method = instance[meta.methodName];
|
|
1301
|
+
const commandBus = container.get(CommandBus);
|
|
1302
|
+
for (const meta of getCommandHandlerMetadata(instance)) {
|
|
1303
|
+
const method = instance[meta.methodName];
|
|
1329
1304
|
if (typeof method !== "function") {
|
|
1330
1305
|
continue;
|
|
1331
1306
|
}
|
|
1332
|
-
|
|
1307
|
+
const unregister = commandBus.register(meta.type, method.bind(instance));
|
|
1333
1308
|
attachCommandUnregister(instance, unregister);
|
|
1334
1309
|
}
|
|
1335
|
-
if (options
|
|
1336
|
-
|
|
1337
|
-
|
|
1310
|
+
if (options?.isWithIgnoreLifecycle) ; else {
|
|
1311
|
+
// Call every `@OnActivated`-decorated method in base-to-derived order.
|
|
1312
|
+
// Fire-and-forget any async init so we stay synchronous from the
|
|
1313
|
+
// container's point of view.
|
|
1314
|
+
for (const methodName of getActivatedHandlerMetadata(instance)) {
|
|
1315
|
+
const method = instance[methodName];
|
|
1338
1316
|
if (typeof method !== "function") {
|
|
1339
|
-
|
|
1317
|
+
continue;
|
|
1340
1318
|
}
|
|
1341
|
-
|
|
1319
|
+
const result = method.call(instance);
|
|
1342
1320
|
if (result && typeof result.then === "function") {
|
|
1343
|
-
result.catch(
|
|
1321
|
+
result.catch(error => {
|
|
1344
1322
|
console.error("[wirestate] @OnActivated rejected for:", entry.name, String(methodName), error);
|
|
1345
1323
|
});
|
|
1346
1324
|
}
|
|
1347
|
-
};
|
|
1348
|
-
// Call every `@OnActivated`-decorated method in base-to-derived order.
|
|
1349
|
-
// Fire-and-forget any async init so we stay synchronous from the
|
|
1350
|
-
// container's point of view.
|
|
1351
|
-
for (var _d = 0, _e = getActivatedHandlerMetadata(instance); _d < _e.length; _d++) {
|
|
1352
|
-
var methodName = _e[_d];
|
|
1353
|
-
_loop_1(methodName);
|
|
1354
1325
|
}
|
|
1355
1326
|
}
|
|
1356
1327
|
return instance;
|
|
1357
1328
|
});
|
|
1358
|
-
whenBind.onDeactivation(
|
|
1359
|
-
if (options
|
|
1329
|
+
whenBind.onDeactivation(instance => {
|
|
1330
|
+
if (options?.isWithIgnoreLifecycle) ; else {
|
|
1360
1331
|
// Call every `@OnDeactivation`-decorated method in base-to-derived order.
|
|
1361
|
-
for (
|
|
1362
|
-
|
|
1363
|
-
var method = instance[methodName];
|
|
1332
|
+
for (const methodName of getDeactivationHandlerMetadata(instance)) {
|
|
1333
|
+
const method = instance[methodName];
|
|
1364
1334
|
if (typeof method === "function") {
|
|
1365
1335
|
method.call(instance);
|
|
1366
1336
|
}
|
|
@@ -1387,8 +1357,7 @@ function bindService(container, entry, options) {
|
|
|
1387
1357
|
* @param handler - Event handler.
|
|
1388
1358
|
*/
|
|
1389
1359
|
function attachEventsSubscription(service, handler) {
|
|
1390
|
-
|
|
1391
|
-
var bus = (_a = CONTAINER_REFS_BY_SERVICE.get(service)) === null || _a === void 0 ? void 0 : _a.get(EventBus);
|
|
1360
|
+
const bus = CONTAINER_REFS_BY_SERVICE.get(service)?.get(EventBus);
|
|
1392
1361
|
if (bus) {
|
|
1393
1362
|
EVENT_UNSUBSCRIBERS_BY_SERVICE.set(service, bus.subscribe(handler));
|
|
1394
1363
|
}
|
|
@@ -1401,7 +1370,7 @@ function attachEventsSubscription(service, handler) {
|
|
|
1401
1370
|
* @param service - Service instance.
|
|
1402
1371
|
*/
|
|
1403
1372
|
function detachEventSubscription(service) {
|
|
1404
|
-
|
|
1373
|
+
const unsubscribe = EVENT_UNSUBSCRIBERS_BY_SERVICE.get(service);
|
|
1405
1374
|
if (unsubscribe) {
|
|
1406
1375
|
unsubscribe();
|
|
1407
1376
|
EVENT_UNSUBSCRIBERS_BY_SERVICE.delete(service);
|
|
@@ -1416,7 +1385,7 @@ function detachEventSubscription(service) {
|
|
|
1416
1385
|
* @param unregister - Query unregister function.
|
|
1417
1386
|
*/
|
|
1418
1387
|
function attachQueryUnregister(service, unregister) {
|
|
1419
|
-
|
|
1388
|
+
let list = QUERY_UNREGISTERS_BY_SERVICE.get(service);
|
|
1420
1389
|
if (!list) {
|
|
1421
1390
|
list = [];
|
|
1422
1391
|
QUERY_UNREGISTERS_BY_SERVICE.set(service, list);
|
|
@@ -1431,12 +1400,11 @@ function attachQueryUnregister(service, unregister) {
|
|
|
1431
1400
|
* @param service - Service instance.
|
|
1432
1401
|
*/
|
|
1433
1402
|
function detachQueryUnregister(service) {
|
|
1434
|
-
|
|
1403
|
+
const list = QUERY_UNREGISTERS_BY_SERVICE.get(service);
|
|
1435
1404
|
if (!list) {
|
|
1436
1405
|
return;
|
|
1437
1406
|
}
|
|
1438
|
-
for (
|
|
1439
|
-
var unregister = list_1[_i];
|
|
1407
|
+
for (const unregister of list) {
|
|
1440
1408
|
unregister();
|
|
1441
1409
|
}
|
|
1442
1410
|
QUERY_UNREGISTERS_BY_SERVICE.delete(service);
|
|
@@ -1450,7 +1418,7 @@ function detachQueryUnregister(service) {
|
|
|
1450
1418
|
* @param unregister - Command unregister function.
|
|
1451
1419
|
*/
|
|
1452
1420
|
function attachCommandUnregister(service, unregister) {
|
|
1453
|
-
|
|
1421
|
+
let list = COMMAND_UNREGISTERS_BY_SERVICE.get(service);
|
|
1454
1422
|
if (!list) {
|
|
1455
1423
|
list = [];
|
|
1456
1424
|
COMMAND_UNREGISTERS_BY_SERVICE.set(service, list);
|
|
@@ -1465,12 +1433,11 @@ function attachCommandUnregister(service, unregister) {
|
|
|
1465
1433
|
* @param service - Service instance.
|
|
1466
1434
|
*/
|
|
1467
1435
|
function detachCommandUnregister(service) {
|
|
1468
|
-
|
|
1436
|
+
const list = COMMAND_UNREGISTERS_BY_SERVICE.get(service);
|
|
1469
1437
|
if (!list) {
|
|
1470
1438
|
return;
|
|
1471
1439
|
}
|
|
1472
|
-
for (
|
|
1473
|
-
var unregister = list_2[_i];
|
|
1440
|
+
for (const unregister of list) {
|
|
1474
1441
|
unregister();
|
|
1475
1442
|
}
|
|
1476
1443
|
COMMAND_UNREGISTERS_BY_SERVICE.delete(service);
|
|
@@ -1488,17 +1455,14 @@ function detachCommandUnregister(service) {
|
|
|
1488
1455
|
* @param Service - Service constructor.
|
|
1489
1456
|
*/
|
|
1490
1457
|
function attachWireScopes(service, Service) {
|
|
1491
|
-
|
|
1492
|
-
if (!(
|
|
1493
|
-
return type === WireScope;
|
|
1494
|
-
}))) {
|
|
1458
|
+
const paramTypes = Reflect.getMetadata("design:paramtypes", Service);
|
|
1459
|
+
if (!paramTypes?.some(type => type === exports.WireScope)) {
|
|
1495
1460
|
return;
|
|
1496
1461
|
}
|
|
1497
|
-
|
|
1498
|
-
for (
|
|
1499
|
-
|
|
1500
|
-
|
|
1501
|
-
if ((value === null || value === void 0 ? void 0 : value.constructor) === WireScope) {
|
|
1462
|
+
const scopes = [];
|
|
1463
|
+
for (const key of Object.getOwnPropertyNames(service)) {
|
|
1464
|
+
const value = service[key];
|
|
1465
|
+
if (value?.constructor === exports.WireScope) {
|
|
1502
1466
|
scopes.push(value);
|
|
1503
1467
|
}
|
|
1504
1468
|
}
|
|
@@ -1517,12 +1481,11 @@ function attachWireScopes(service, Service) {
|
|
|
1517
1481
|
* @param service - Service instance.
|
|
1518
1482
|
*/
|
|
1519
1483
|
function detachWireScopes(service) {
|
|
1520
|
-
|
|
1484
|
+
const scopes = WIRE_SCOPES_BY_SERVICE.get(service);
|
|
1521
1485
|
if (!scopes) {
|
|
1522
1486
|
return;
|
|
1523
1487
|
}
|
|
1524
|
-
for (
|
|
1525
|
-
var scope = scopes_1[_i];
|
|
1488
|
+
for (const scope of scopes) {
|
|
1526
1489
|
scope.isDisposed = true;
|
|
1527
1490
|
scope.container = null;
|
|
1528
1491
|
}
|
|
@@ -1574,10 +1537,7 @@ function detachWireScopes(service) {
|
|
|
1574
1537
|
* });
|
|
1575
1538
|
* ```
|
|
1576
1539
|
*/
|
|
1577
|
-
function bindEntry(container, entry, options) {
|
|
1578
|
-
if (options === void 0) {
|
|
1579
|
-
options = {};
|
|
1580
|
-
}
|
|
1540
|
+
function bindEntry(container, entry, options = {}) {
|
|
1581
1541
|
if (typeof entry === "function") {
|
|
1582
1542
|
bindService(container, entry, options);
|
|
1583
1543
|
return;
|
|
@@ -1644,11 +1604,8 @@ function getEntryToken(entry) {
|
|
|
1644
1604
|
* ```
|
|
1645
1605
|
*/
|
|
1646
1606
|
function applySeeds(container, seeds) {
|
|
1647
|
-
|
|
1648
|
-
for (
|
|
1649
|
-
var _a = seeds_1[_i],
|
|
1650
|
-
key = _a[0],
|
|
1651
|
-
state = _a[1];
|
|
1607
|
+
const existing = container.get(SEEDS_TOKEN);
|
|
1608
|
+
for (const [key, state] of seeds) {
|
|
1652
1609
|
existing.set(key, state);
|
|
1653
1610
|
}
|
|
1654
1611
|
}
|
|
@@ -1685,8 +1642,7 @@ function applySeeds(container, seeds) {
|
|
|
1685
1642
|
* ```
|
|
1686
1643
|
*/
|
|
1687
1644
|
function createBaseContainer(options) {
|
|
1688
|
-
|
|
1689
|
-
var container = new inversify.Container({
|
|
1645
|
+
const container = new inversify.Container({
|
|
1690
1646
|
parent: options.parent,
|
|
1691
1647
|
defaultScope: "Singleton"
|
|
1692
1648
|
});
|
|
@@ -1694,7 +1650,7 @@ function createBaseContainer(options) {
|
|
|
1694
1650
|
container.bind(QueryBus).toConstantValue(new QueryBus());
|
|
1695
1651
|
container.bind(CommandBus).toConstantValue(new CommandBus());
|
|
1696
1652
|
container.bind(SEEDS_TOKEN).toConstantValue(new Map());
|
|
1697
|
-
container.bind(SEED_TOKEN).toConstantValue(
|
|
1653
|
+
container.bind(SEED_TOKEN).toConstantValue(options.seed ?? {});
|
|
1698
1654
|
if (options.seeds) {
|
|
1699
1655
|
applySeeds(container, options.seeds);
|
|
1700
1656
|
}
|
|
@@ -1718,7 +1674,7 @@ function createBaseContainer(options) {
|
|
|
1718
1674
|
*
|
|
1719
1675
|
* @example
|
|
1720
1676
|
* ```typescript
|
|
1721
|
-
* const container: Container =
|
|
1677
|
+
* const container: Container = createContainer({
|
|
1722
1678
|
* seeds: [
|
|
1723
1679
|
* [CounterService, { count: 1000 }],
|
|
1724
1680
|
* ["SOME_KEY", "VALUE"],
|
|
@@ -1730,39 +1686,30 @@ function createBaseContainer(options) {
|
|
|
1730
1686
|
* bindService(container, MyService);
|
|
1731
1687
|
* ```
|
|
1732
1688
|
*/
|
|
1733
|
-
function createContainer(options) {
|
|
1734
|
-
var _a;
|
|
1735
|
-
if (options === void 0) {
|
|
1736
|
-
options = {};
|
|
1737
|
-
}
|
|
1689
|
+
function createContainer(options = {}) {
|
|
1738
1690
|
if (options.activate && options.activate.length) {
|
|
1739
|
-
if (!
|
|
1691
|
+
if (!options.entries?.length) {
|
|
1740
1692
|
throw new WirestateError(ERROR_CODE_VALIDATION_ERROR, "Supplied activation list while entries for binding are not provided.");
|
|
1741
1693
|
}
|
|
1742
|
-
|
|
1743
|
-
for (
|
|
1744
|
-
var eager = _b[_i];
|
|
1694
|
+
const entryTokens = options.entries.map(getEntryToken);
|
|
1695
|
+
for (const eager of options.activate) {
|
|
1745
1696
|
if (!entryTokens.includes(eager)) {
|
|
1746
|
-
throw new WirestateError(ERROR_CODE_VALIDATION_ERROR,
|
|
1697
|
+
throw new WirestateError(ERROR_CODE_VALIDATION_ERROR, `createInjectablesProvider: '${String(eager)}' is listed in 'activate' but was not provided in 'entries'.`);
|
|
1747
1698
|
}
|
|
1748
1699
|
}
|
|
1749
1700
|
}
|
|
1750
|
-
|
|
1701
|
+
const container = new inversify.Container({
|
|
1751
1702
|
defaultScope: "Singleton",
|
|
1752
1703
|
parent: createBaseContainer(options)
|
|
1753
1704
|
});
|
|
1754
|
-
container.bind(WireScope).toResolvedValue(
|
|
1755
|
-
return new WireScope(container);
|
|
1756
|
-
}).inTransientScope();
|
|
1705
|
+
container.bind(exports.WireScope).toResolvedValue(() => new exports.WireScope(container)).inTransientScope();
|
|
1757
1706
|
if (options.entries) {
|
|
1758
|
-
for (
|
|
1759
|
-
var entry = _d[_c];
|
|
1707
|
+
for (const entry of options.entries) {
|
|
1760
1708
|
bindEntry(container, entry);
|
|
1761
1709
|
}
|
|
1762
1710
|
}
|
|
1763
1711
|
if (options.activate) {
|
|
1764
|
-
for (
|
|
1765
|
-
var entry = _f[_e];
|
|
1712
|
+
for (const entry of options.activate) {
|
|
1766
1713
|
container.get(entry);
|
|
1767
1714
|
}
|
|
1768
1715
|
}
|
|
@@ -1780,7 +1727,6 @@ exports.QUERY_HANDLER_METADATA = QUERY_HANDLER_METADATA;
|
|
|
1780
1727
|
exports.QueryBus = QueryBus;
|
|
1781
1728
|
exports.SEEDS_TOKEN = SEEDS_TOKEN;
|
|
1782
1729
|
exports.SEED_TOKEN = SEED_TOKEN;
|
|
1783
|
-
exports.WireScope = WireScope;
|
|
1784
1730
|
exports.WirestateError = WirestateError;
|
|
1785
1731
|
exports.applySeeds = applySeeds;
|
|
1786
1732
|
exports.bindConstant = bindConstant;
|