1 | "use strict";
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 | exports.BusIndexed = void 0;
|
4 | const Bus_1 = require("./Bus");
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 |
|
18 |
|
19 |
|
20 |
|
21 | class BusIndexed extends Bus_1.Bus {
|
22 | |
23 |
|
24 |
|
25 |
|
26 |
|
27 |
|
28 |
|
29 | constructor(args) {
|
30 | super(args);
|
31 | this.actorsIndex = {};
|
32 | }
|
33 | subscribe(actor) {
|
34 | const actorId = this.getActorIdentifier(actor) || '_undefined_';
|
35 | let actors = this.actorsIndex[actorId];
|
36 | if (!actors) {
|
37 | actors = this.actorsIndex[actorId] = [];
|
38 | }
|
39 | actors.push(actor);
|
40 | super.subscribe(actor);
|
41 | }
|
42 | unsubscribe(actor) {
|
43 | const actorId = this.getActorIdentifier(actor) || '_undefined_';
|
44 | const actors = this.actorsIndex[actorId];
|
45 | if (actors) {
|
46 | const i = actors.indexOf(actor);
|
47 | if (i >= 0) {
|
48 | actors.splice(i, 1);
|
49 | }
|
50 | if (actors.length === 0) {
|
51 | delete this.actorsIndex[actorId];
|
52 | }
|
53 | }
|
54 | return super.unsubscribe(actor);
|
55 | }
|
56 | publish(action) {
|
57 | const actionId = this.getActionIdentifier(action);
|
58 | if (actionId) {
|
59 | const actors = [...this.actorsIndex[actionId] || [], ...this.actorsIndex._undefined_ || []];
|
60 | return actors.map((actor) => ({ actor, reply: actor.test(action) }));
|
61 | }
|
62 | return super.publish(action);
|
63 | }
|
64 | getActorIdentifier(actor) {
|
65 | return this.actorIdentifierFields.reduce((object, field) => object[field], actor);
|
66 | }
|
67 | getActionIdentifier(action) {
|
68 | return this.actionIdentifierFields.reduce((object, field) => object[field], action);
|
69 | }
|
70 | }
|
71 | exports.BusIndexed = BusIndexed;
|
72 |
|
\ | No newline at end of file |