@zeltjs/eventbus 0.11.0 → 0.12.0-canary.0
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/README.md +58 -10
- package/dist/adaptor-memory/index.cjs +1 -1
- package/dist/adaptor-memory/index.d.cts +1 -1
- package/dist/adaptor-memory/index.d.ts +1 -1
- package/dist/adaptor-memory/index.js +1 -1
- package/dist/{adaptor-memory-BG1Xyr7_.js → adaptor-memory-WYgtTbUx.js} +5 -6
- package/dist/adaptor-memory-WYgtTbUx.js.map +1 -0
- package/dist/{adaptor-memory-Obg3l9mF.cjs → adaptor-memory-zYqB6KAT.cjs} +4 -5
- package/dist/adaptor-redis/index.cjs +38 -12
- package/dist/adaptor-redis/index.d.cts +10 -3
- package/dist/adaptor-redis/index.d.cts.map +1 -1
- package/dist/adaptor-redis/index.d.ts +10 -3
- package/dist/adaptor-redis/index.d.ts.map +1 -1
- package/dist/adaptor-redis/index.js +38 -12
- package/dist/adaptor-redis/index.js.map +1 -1
- package/dist/{eventbus.types-Cn8xuI2B.d.ts → eventbus.types-Dk9jl4Xp.d.cts} +3 -3
- package/dist/eventbus.types-Dk9jl4Xp.d.cts.map +1 -0
- package/dist/{eventbus.types-kl1yXTK8.d.cts → eventbus.types-DvL3Ux3o.d.ts} +3 -3
- package/dist/eventbus.types-DvL3Ux3o.d.ts.map +1 -0
- package/dist/{index-Buq5DkB1.d.ts → index-C2cRFzub.d.cts} +4 -4
- package/dist/index-C2cRFzub.d.cts.map +1 -0
- package/dist/{index-qecFavvB.d.cts → index-CcpSMqqG.d.ts} +4 -4
- package/dist/index-CcpSMqqG.d.ts.map +1 -0
- package/dist/index.cjs +1 -1
- package/dist/index.d.cts +4 -4
- package/dist/index.d.cts.map +1 -1
- package/dist/index.d.ts +4 -4
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +1 -1
- package/dist/index.js.map +1 -1
- package/package.json +7 -5
- package/dist/adaptor-memory-BG1Xyr7_.js.map +0 -1
- package/dist/eventbus.types-Cn8xuI2B.d.ts.map +0 -1
- package/dist/eventbus.types-kl1yXTK8.d.cts.map +0 -1
- package/dist/index-Buq5DkB1.d.ts.map +0 -1
- package/dist/index-qecFavvB.d.cts.map +0 -1
package/README.md
CHANGED
|
@@ -14,20 +14,68 @@ npm install @zeltjs/eventbus @zeltjs/core
|
|
|
14
14
|
|
|
15
15
|
## Usage
|
|
16
16
|
|
|
17
|
+
Declare your event schema by augmenting `EventBusSchema`:
|
|
18
|
+
|
|
17
19
|
```typescript
|
|
18
|
-
|
|
19
|
-
|
|
20
|
+
declare module '@zeltjs/eventbus' {
|
|
21
|
+
interface EventBusSchema {
|
|
22
|
+
'order:created': { orderId: number; userId: number };
|
|
23
|
+
}
|
|
24
|
+
}
|
|
20
25
|
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
'user.deleted': { id: string };
|
|
24
|
-
}>;
|
|
26
|
+
export {};
|
|
27
|
+
```
|
|
25
28
|
|
|
26
|
-
|
|
29
|
+
Register the `eventbus` feature with an adaptor in `createApp`:
|
|
30
|
+
|
|
31
|
+
```typescript
|
|
32
|
+
import { createApp } from '@zeltjs/core';
|
|
33
|
+
import { eventbus, MemoryEventBusAdaptor } from '@zeltjs/eventbus';
|
|
27
34
|
|
|
28
|
-
eventbus
|
|
29
|
-
|
|
35
|
+
const app = createApp([eventbus({ adaptor: MemoryEventBusAdaptor })]);
|
|
36
|
+
const runtime = await app.createRuntime();
|
|
37
|
+
|
|
38
|
+
runtime.eventbus.on('order:created', (data) => {
|
|
39
|
+
console.log('Order created:', data.orderId);
|
|
30
40
|
});
|
|
31
41
|
|
|
32
|
-
eventbus.emit('
|
|
42
|
+
await runtime.eventbus.emit('order:created', { orderId: 1, userId: 2 });
|
|
33
43
|
```
|
|
44
|
+
|
|
45
|
+
Subscribers are ordinary `Lifecycle` classes: subscribe in `startup()`, unsubscribe in `shutdown()`, and pass them as `handlers` so the feature resolves and starts them:
|
|
46
|
+
|
|
47
|
+
```typescript
|
|
48
|
+
import type { Lifecycle } from '@zeltjs/core';
|
|
49
|
+
import { Injectable, inject, LifecycleManager } from '@zeltjs/core';
|
|
50
|
+
import { MemoryEventBusAdaptor } from '@zeltjs/eventbus';
|
|
51
|
+
|
|
52
|
+
@Injectable()
|
|
53
|
+
class OrderHandlers implements Lifecycle {
|
|
54
|
+
private unsubscribes: Array<() => void> = [];
|
|
55
|
+
|
|
56
|
+
constructor(
|
|
57
|
+
private readonly eventBus = inject(MemoryEventBusAdaptor),
|
|
58
|
+
lifecycle = inject(LifecycleManager),
|
|
59
|
+
) {
|
|
60
|
+
lifecycle.register(this);
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
async startup(): Promise<void> {
|
|
64
|
+
const unsub = this.eventBus.on('order:created', (data) => {
|
|
65
|
+
// handle event
|
|
66
|
+
});
|
|
67
|
+
this.unsubscribes.push(unsub);
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
async shutdown(): Promise<void> {
|
|
71
|
+
for (const unsub of this.unsubscribes) unsub();
|
|
72
|
+
this.unsubscribes = [];
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
const app = createApp([
|
|
77
|
+
eventbus({ adaptor: MemoryEventBusAdaptor, handlers: [OrderHandlers] }),
|
|
78
|
+
]);
|
|
79
|
+
```
|
|
80
|
+
|
|
81
|
+
Use `RedisEventBusAdaptor` from `@zeltjs/eventbus/adaptor-redis` in place of `MemoryEventBusAdaptor` to fan events out across processes via Redis pub/sub.
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
2
|
-
const require_adaptor_memory = require("../adaptor-memory-
|
|
2
|
+
const require_adaptor_memory = require("../adaptor-memory-zYqB6KAT.cjs");
|
|
3
3
|
Object.defineProperty(exports, "MemoryEventBusAdaptor", {
|
|
4
4
|
enumerable: true,
|
|
5
5
|
get: function() {
|
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
import { t as MemoryEventBusAdaptor } from "../index-
|
|
1
|
+
import { t as MemoryEventBusAdaptor } from "../index-C2cRFzub.cjs";
|
|
2
2
|
export { MemoryEventBusAdaptor };
|
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
import { t as MemoryEventBusAdaptor } from "../index-
|
|
1
|
+
import { t as MemoryEventBusAdaptor } from "../index-CcpSMqqG.js";
|
|
2
2
|
export { MemoryEventBusAdaptor };
|
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
import { t as MemoryEventBusAdaptor } from "../adaptor-memory-
|
|
1
|
+
import { t as MemoryEventBusAdaptor } from "../adaptor-memory-WYgtTbUx.js";
|
|
2
2
|
export { MemoryEventBusAdaptor };
|
|
@@ -11,17 +11,16 @@ var MemoryEventBusAdaptor = class {
|
|
|
11
11
|
async emit(event, data) {
|
|
12
12
|
this.emitter.emit(event, data);
|
|
13
13
|
}
|
|
14
|
-
on(event, handler) {
|
|
14
|
+
async on(event, handler) {
|
|
15
15
|
this.emitter.on(event, handler);
|
|
16
16
|
return () => this.emitter.off(event, handler);
|
|
17
17
|
}
|
|
18
|
-
once(event, handler) {
|
|
18
|
+
async once(event, handler) {
|
|
19
19
|
const wrappedHandler = (data) => {
|
|
20
|
-
|
|
20
|
+
this.emitter.off(event, wrappedHandler);
|
|
21
21
|
handler(data);
|
|
22
22
|
};
|
|
23
|
-
|
|
24
|
-
return unsubscribe;
|
|
23
|
+
return this.on(event, wrappedHandler);
|
|
25
24
|
}
|
|
26
25
|
constructor() {
|
|
27
26
|
this.emitter = mitt_default();
|
|
@@ -31,4 +30,4 @@ MemoryEventBusAdaptor = _ts_decorate([Injectable()], MemoryEventBusAdaptor);
|
|
|
31
30
|
//#endregion
|
|
32
31
|
export { MemoryEventBusAdaptor as t };
|
|
33
32
|
|
|
34
|
-
//# sourceMappingURL=adaptor-memory-
|
|
33
|
+
//# sourceMappingURL=adaptor-memory-WYgtTbUx.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"adaptor-memory-WYgtTbUx.js","names":["Injectable","mitt","MemoryEventBusAdaptor","emit","event","data","emitter","on","handler","off","once","wrappedHandler"],"sources":["../src/adaptor-memory/memory-event-bus.adaptor.ts"],"sourcesContent":["import { Injectable } from '@zeltjs/core';\nimport mitt from 'mitt';\n\nimport type { EventBusAdaptor, EventBusSchema } from '../eventbus.types';\n\n@Injectable()\nexport class MemoryEventBusAdaptor implements EventBusAdaptor {\n private readonly emitter = mitt<EventBusSchema>();\n\n async emit<K extends string & keyof EventBusSchema>(\n event: K,\n data: EventBusSchema[K],\n ): Promise<void> {\n this.emitter.emit(event, data);\n }\n\n async on<K extends string & keyof EventBusSchema>(\n event: K,\n handler: (data: EventBusSchema[K]) => void,\n ): Promise<() => void> {\n this.emitter.on(event, handler);\n return () => this.emitter.off(event, handler);\n }\n\n async once<K extends string & keyof EventBusSchema>(\n event: K,\n handler: (data: EventBusSchema[K]) => void,\n ): Promise<() => void> {\n // Unsubscribes itself directly instead of going through on()'s returned\n // function, since that function only resolves after this handler is\n // already registered.\n const wrappedHandler = (data: EventBusSchema[K]) => {\n this.emitter.off(event, wrappedHandler);\n handler(data);\n };\n return this.on(event, wrappedHandler);\n }\n}\n"],"mappings":";;;;;;;;;AAMA,IAAaE,wBAAb,MAAaA;CAGX,MAAMC,KACJC,OACAC,MACe;AACf,OAAKC,QAAQH,KAAKC,OAAOC,KAAAA;;CAG3B,MAAME,GACJH,OACAI,SACqB;AACrB,OAAKF,QAAQC,GAAGH,OAAOI,QAAAA;AACvB,eAAa,KAAKF,QAAQG,IAAIL,OAAOI,QAAAA;;CAGvC,MAAME,KACJN,OACAI,SACqB;EAIrB,MAAMG,kBAAkBN,SAAAA;AACtB,QAAKC,QAAQG,IAAIL,OAAOO,eAAAA;AACxBH,WAAQH,KAAAA;;AAEV,SAAO,KAAKE,GAAGH,OAAOO,eAAAA;;;OA5BPL,UAAUL,cAAAA"}
|
|
@@ -11,17 +11,16 @@ var MemoryEventBusAdaptor = class {
|
|
|
11
11
|
async emit(event, data) {
|
|
12
12
|
this.emitter.emit(event, data);
|
|
13
13
|
}
|
|
14
|
-
on(event, handler) {
|
|
14
|
+
async on(event, handler) {
|
|
15
15
|
this.emitter.on(event, handler);
|
|
16
16
|
return () => this.emitter.off(event, handler);
|
|
17
17
|
}
|
|
18
|
-
once(event, handler) {
|
|
18
|
+
async once(event, handler) {
|
|
19
19
|
const wrappedHandler = (data) => {
|
|
20
|
-
|
|
20
|
+
this.emitter.off(event, wrappedHandler);
|
|
21
21
|
handler(data);
|
|
22
22
|
};
|
|
23
|
-
|
|
24
|
-
return unsubscribe;
|
|
23
|
+
return this.on(event, wrappedHandler);
|
|
25
24
|
}
|
|
26
25
|
constructor() {
|
|
27
26
|
this.emitter = require_mitt.mitt_default();
|
|
@@ -13,42 +13,68 @@ function _ts_metadata(k, v) {
|
|
|
13
13
|
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
|
|
14
14
|
}
|
|
15
15
|
var RedisEventBusAdaptor = class {
|
|
16
|
-
async startup() {
|
|
16
|
+
async startup() {
|
|
17
|
+
try {
|
|
18
|
+
await this.sub.connect();
|
|
19
|
+
if (this.subscriptions.size > 0) await this.sub.subscribe(...this.subscriptions);
|
|
20
|
+
} catch (error) {
|
|
21
|
+
this.sub.disconnect();
|
|
22
|
+
throw error;
|
|
23
|
+
}
|
|
24
|
+
}
|
|
17
25
|
async shutdown() {
|
|
18
26
|
this.sub.disconnect();
|
|
19
27
|
}
|
|
20
28
|
async emit(event, data) {
|
|
21
29
|
await this.pub.publish(event, JSON.stringify(data));
|
|
22
30
|
}
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
31
|
+
/**
|
|
32
|
+
* @throws {Error} When the channel's SUBSCRIBE command to Redis fails,
|
|
33
|
+
* whether this call sent it or joined another in-flight on() for the same
|
|
34
|
+
* channel; the handler is not registered in that case.
|
|
35
|
+
*/ async on(event, handler) {
|
|
28
36
|
this.localEmitter.on(event, handler);
|
|
37
|
+
if (!(this.sub.status !== "wait" && this.sub.status !== "end")) this.subscriptions.add(event);
|
|
38
|
+
else if (!this.subscriptions.has(event)) {
|
|
39
|
+
let subscribing = this.subscribing.get(event);
|
|
40
|
+
if (!subscribing) {
|
|
41
|
+
subscribing = this.sub.subscribe(event).then(() => {
|
|
42
|
+
this.subscriptions.add(event);
|
|
43
|
+
}).finally(() => {
|
|
44
|
+
this.subscribing.delete(event);
|
|
45
|
+
});
|
|
46
|
+
this.subscribing.set(event, subscribing);
|
|
47
|
+
}
|
|
48
|
+
try {
|
|
49
|
+
await subscribing;
|
|
50
|
+
} catch (error) {
|
|
51
|
+
this.localEmitter.off(event, handler);
|
|
52
|
+
throw error;
|
|
53
|
+
}
|
|
54
|
+
}
|
|
29
55
|
return () => {
|
|
30
56
|
this.localEmitter.off(event, handler);
|
|
31
57
|
};
|
|
32
58
|
}
|
|
33
|
-
once(event, handler) {
|
|
59
|
+
/** @throws {Error} Same as on(), which this delegates to. */ async once(event, handler) {
|
|
34
60
|
const wrappedHandler = (data) => {
|
|
35
|
-
|
|
61
|
+
this.localEmitter.off(event, wrappedHandler);
|
|
36
62
|
handler(data);
|
|
37
63
|
};
|
|
38
|
-
|
|
39
|
-
return unsubscribe;
|
|
64
|
+
return this.on(event, wrappedHandler);
|
|
40
65
|
}
|
|
41
66
|
constructor(redis = (0, _zeltjs_core.inject)(_zeltjs_redis.RedisService), lifecycle = (0, _zeltjs_core.inject)(_zeltjs_core.LifecycleManager)) {
|
|
42
67
|
this.localEmitter = require_mitt.mitt_default();
|
|
43
68
|
this.subscriptions = /* @__PURE__ */ new Set();
|
|
69
|
+
this.subscribing = /* @__PURE__ */ new Map();
|
|
44
70
|
this.pub = redis.client;
|
|
45
|
-
this.sub =
|
|
46
|
-
lifecycle.register(this);
|
|
71
|
+
this.sub = this.pub.duplicate();
|
|
47
72
|
this.sub.on("message", (channel, message) => {
|
|
48
73
|
if (!this.subscriptions.has(channel)) return;
|
|
49
74
|
const data = JSON.parse(message);
|
|
50
75
|
this.localEmitter.emit(channel, data);
|
|
51
76
|
});
|
|
77
|
+
lifecycle.register(this);
|
|
52
78
|
}
|
|
53
79
|
};
|
|
54
80
|
RedisEventBusAdaptor = _ts_decorate([
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { n as EventBusSchema, t as EventBusAdaptor } from "../eventbus.types-
|
|
1
|
+
import { n as EventBusSchema, t as EventBusAdaptor } from "../eventbus.types-Dk9jl4Xp.cjs";
|
|
2
2
|
import { Lifecycle, LifecycleManager } from "@zeltjs/core";
|
|
3
3
|
import { RedisService } from "@zeltjs/redis";
|
|
4
4
|
|
|
@@ -8,12 +8,19 @@ declare class RedisEventBusAdaptor implements EventBusAdaptor, Lifecycle {
|
|
|
8
8
|
private readonly sub;
|
|
9
9
|
private readonly localEmitter;
|
|
10
10
|
private readonly subscriptions;
|
|
11
|
+
private readonly subscribing;
|
|
11
12
|
constructor(redis?: RedisService, lifecycle?: LifecycleManager);
|
|
12
13
|
startup(): Promise<void>;
|
|
13
14
|
shutdown(): Promise<void>;
|
|
14
15
|
emit<K extends string & keyof EventBusSchema>(event: K, data: EventBusSchema[K]): Promise<void>;
|
|
15
|
-
|
|
16
|
-
|
|
16
|
+
/**
|
|
17
|
+
* @throws {Error} When the channel's SUBSCRIBE command to Redis fails,
|
|
18
|
+
* whether this call sent it or joined another in-flight on() for the same
|
|
19
|
+
* channel; the handler is not registered in that case.
|
|
20
|
+
*/
|
|
21
|
+
on<K extends string & keyof EventBusSchema>(event: K, handler: (data: EventBusSchema[K]) => void): Promise<() => void>;
|
|
22
|
+
/** @throws {Error} Same as on(), which this delegates to. */
|
|
23
|
+
once<K extends string & keyof EventBusSchema>(event: K, handler: (data: EventBusSchema[K]) => void): Promise<() => void>;
|
|
17
24
|
}
|
|
18
25
|
//#endregion
|
|
19
26
|
export { RedisEventBusAdaptor };
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.cts","names":[],"sources":["../../src/adaptor-redis/redis-event-bus.adaptor.ts"],"mappings":";;;;;cASa,oBAAA,YAAgC,eAAA,EAAiB,SAAA;EAAA,iBAC3C,GAAA;EAAA,iBACA,GAAA;EAAA,iBACA,YAAA;EAAA,iBACA,aAAA;cAEL,KAAA,GAAK,YAAA,EAAyB,SAAA,GAAS,gBAAA;
|
|
1
|
+
{"version":3,"file":"index.d.cts","names":[],"sources":["../../src/adaptor-redis/redis-event-bus.adaptor.ts"],"mappings":";;;;;cASa,oBAAA,YAAgC,eAAA,EAAiB,SAAA;EAAA,iBAC3C,GAAA;EAAA,iBACA,GAAA;EAAA,iBACA,YAAA;EAAA,iBACA,aAAA;EAAA,iBAIA,WAAA;cAEL,KAAA,GAAK,YAAA,EAAyB,SAAA,GAAS,gBAAA;EAa7C,OAAA,CAAA,GAAW,OAAA;EAgBX,QAAA,CAAA,GAAY,OAAA;EAIZ,IAAA,0BAA8B,cAAA,CAAA,CAClC,KAAA,EAAO,CAAA,EACP,IAAA,EAAM,cAAA,CAAe,CAAA,IACpB,OAAA;EADK;;;;;EAUF,EAAA,0BAA4B,cAAA,CAAA,CAChC,KAAA,EAAO,CAAA,EACP,OAAA,GAAU,IAAA,EAAM,cAAA,CAAe,CAAA,aAC9B,OAAA;EAD8B;EAmD3B,IAAA,0BAA8B,cAAA,CAAA,CAClC,KAAA,EAAO,CAAA,EACP,OAAA,GAAU,IAAA,EAAM,cAAA,CAAe,CAAA,aAC9B,OAAA;AAAA"}
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { n as EventBusSchema, t as EventBusAdaptor } from "../eventbus.types-
|
|
1
|
+
import { n as EventBusSchema, t as EventBusAdaptor } from "../eventbus.types-DvL3Ux3o.js";
|
|
2
2
|
import { Lifecycle, LifecycleManager } from "@zeltjs/core";
|
|
3
3
|
import { RedisService } from "@zeltjs/redis";
|
|
4
4
|
|
|
@@ -8,12 +8,19 @@ declare class RedisEventBusAdaptor implements EventBusAdaptor, Lifecycle {
|
|
|
8
8
|
private readonly sub;
|
|
9
9
|
private readonly localEmitter;
|
|
10
10
|
private readonly subscriptions;
|
|
11
|
+
private readonly subscribing;
|
|
11
12
|
constructor(redis?: RedisService, lifecycle?: LifecycleManager);
|
|
12
13
|
startup(): Promise<void>;
|
|
13
14
|
shutdown(): Promise<void>;
|
|
14
15
|
emit<K extends string & keyof EventBusSchema>(event: K, data: EventBusSchema[K]): Promise<void>;
|
|
15
|
-
|
|
16
|
-
|
|
16
|
+
/**
|
|
17
|
+
* @throws {Error} When the channel's SUBSCRIBE command to Redis fails,
|
|
18
|
+
* whether this call sent it or joined another in-flight on() for the same
|
|
19
|
+
* channel; the handler is not registered in that case.
|
|
20
|
+
*/
|
|
21
|
+
on<K extends string & keyof EventBusSchema>(event: K, handler: (data: EventBusSchema[K]) => void): Promise<() => void>;
|
|
22
|
+
/** @throws {Error} Same as on(), which this delegates to. */
|
|
23
|
+
once<K extends string & keyof EventBusSchema>(event: K, handler: (data: EventBusSchema[K]) => void): Promise<() => void>;
|
|
17
24
|
}
|
|
18
25
|
//#endregion
|
|
19
26
|
export { RedisEventBusAdaptor };
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","names":[],"sources":["../../src/adaptor-redis/redis-event-bus.adaptor.ts"],"mappings":";;;;;cASa,oBAAA,YAAgC,eAAA,EAAiB,SAAA;EAAA,iBAC3C,GAAA;EAAA,iBACA,GAAA;EAAA,iBACA,YAAA;EAAA,iBACA,aAAA;cAEL,KAAA,GAAK,YAAA,EAAyB,SAAA,GAAS,gBAAA;
|
|
1
|
+
{"version":3,"file":"index.d.ts","names":[],"sources":["../../src/adaptor-redis/redis-event-bus.adaptor.ts"],"mappings":";;;;;cASa,oBAAA,YAAgC,eAAA,EAAiB,SAAA;EAAA,iBAC3C,GAAA;EAAA,iBACA,GAAA;EAAA,iBACA,YAAA;EAAA,iBACA,aAAA;EAAA,iBAIA,WAAA;cAEL,KAAA,GAAK,YAAA,EAAyB,SAAA,GAAS,gBAAA;EAa7C,OAAA,CAAA,GAAW,OAAA;EAgBX,QAAA,CAAA,GAAY,OAAA;EAIZ,IAAA,0BAA8B,cAAA,CAAA,CAClC,KAAA,EAAO,CAAA,EACP,IAAA,EAAM,cAAA,CAAe,CAAA,IACpB,OAAA;EADK;;;;;EAUF,EAAA,0BAA4B,cAAA,CAAA,CAChC,KAAA,EAAO,CAAA,EACP,OAAA,GAAU,IAAA,EAAM,cAAA,CAAe,CAAA,aAC9B,OAAA;EAD8B;EAmD3B,IAAA,0BAA8B,cAAA,CAAA,CAClC,KAAA,EAAO,CAAA,EACP,OAAA,GAAU,IAAA,EAAM,cAAA,CAAe,CAAA,aAC9B,OAAA;AAAA"}
|
|
@@ -12,42 +12,68 @@ function _ts_metadata(k, v) {
|
|
|
12
12
|
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
|
|
13
13
|
}
|
|
14
14
|
var RedisEventBusAdaptor = class {
|
|
15
|
-
async startup() {
|
|
15
|
+
async startup() {
|
|
16
|
+
try {
|
|
17
|
+
await this.sub.connect();
|
|
18
|
+
if (this.subscriptions.size > 0) await this.sub.subscribe(...this.subscriptions);
|
|
19
|
+
} catch (error) {
|
|
20
|
+
this.sub.disconnect();
|
|
21
|
+
throw error;
|
|
22
|
+
}
|
|
23
|
+
}
|
|
16
24
|
async shutdown() {
|
|
17
25
|
this.sub.disconnect();
|
|
18
26
|
}
|
|
19
27
|
async emit(event, data) {
|
|
20
28
|
await this.pub.publish(event, JSON.stringify(data));
|
|
21
29
|
}
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
30
|
+
/**
|
|
31
|
+
* @throws {Error} When the channel's SUBSCRIBE command to Redis fails,
|
|
32
|
+
* whether this call sent it or joined another in-flight on() for the same
|
|
33
|
+
* channel; the handler is not registered in that case.
|
|
34
|
+
*/ async on(event, handler) {
|
|
27
35
|
this.localEmitter.on(event, handler);
|
|
36
|
+
if (!(this.sub.status !== "wait" && this.sub.status !== "end")) this.subscriptions.add(event);
|
|
37
|
+
else if (!this.subscriptions.has(event)) {
|
|
38
|
+
let subscribing = this.subscribing.get(event);
|
|
39
|
+
if (!subscribing) {
|
|
40
|
+
subscribing = this.sub.subscribe(event).then(() => {
|
|
41
|
+
this.subscriptions.add(event);
|
|
42
|
+
}).finally(() => {
|
|
43
|
+
this.subscribing.delete(event);
|
|
44
|
+
});
|
|
45
|
+
this.subscribing.set(event, subscribing);
|
|
46
|
+
}
|
|
47
|
+
try {
|
|
48
|
+
await subscribing;
|
|
49
|
+
} catch (error) {
|
|
50
|
+
this.localEmitter.off(event, handler);
|
|
51
|
+
throw error;
|
|
52
|
+
}
|
|
53
|
+
}
|
|
28
54
|
return () => {
|
|
29
55
|
this.localEmitter.off(event, handler);
|
|
30
56
|
};
|
|
31
57
|
}
|
|
32
|
-
once(event, handler) {
|
|
58
|
+
/** @throws {Error} Same as on(), which this delegates to. */ async once(event, handler) {
|
|
33
59
|
const wrappedHandler = (data) => {
|
|
34
|
-
|
|
60
|
+
this.localEmitter.off(event, wrappedHandler);
|
|
35
61
|
handler(data);
|
|
36
62
|
};
|
|
37
|
-
|
|
38
|
-
return unsubscribe;
|
|
63
|
+
return this.on(event, wrappedHandler);
|
|
39
64
|
}
|
|
40
65
|
constructor(redis = inject(RedisService), lifecycle = inject(LifecycleManager)) {
|
|
41
66
|
this.localEmitter = mitt_default();
|
|
42
67
|
this.subscriptions = /* @__PURE__ */ new Set();
|
|
68
|
+
this.subscribing = /* @__PURE__ */ new Map();
|
|
43
69
|
this.pub = redis.client;
|
|
44
|
-
this.sub =
|
|
45
|
-
lifecycle.register(this);
|
|
70
|
+
this.sub = this.pub.duplicate();
|
|
46
71
|
this.sub.on("message", (channel, message) => {
|
|
47
72
|
if (!this.subscriptions.has(channel)) return;
|
|
48
73
|
const data = JSON.parse(message);
|
|
49
74
|
this.localEmitter.emit(channel, data);
|
|
50
75
|
});
|
|
76
|
+
lifecycle.register(this);
|
|
51
77
|
}
|
|
52
78
|
};
|
|
53
79
|
RedisEventBusAdaptor = _ts_decorate([
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","names":["Injectable","inject","LifecycleManager","RedisService","mitt","RedisEventBusAdaptor","startup","
|
|
1
|
+
{"version":3,"file":"index.js","names":["Injectable","inject","LifecycleManager","RedisService","mitt","RedisEventBusAdaptor","startup","sub","connect","subscriptions","size","subscribe","error","disconnect","shutdown","emit","event","data","pub","publish","JSON","stringify","on","handler","localEmitter","connectionInitiated","status","add","has","subscribing","get","then","finally","delete","set","off","once","wrappedHandler","redis","lifecycle","Set","Map","client","duplicate","channel","message","parse","register"],"sources":["../../src/adaptor-redis/redis-event-bus.adaptor.ts"],"sourcesContent":["import type { Lifecycle } from '@zeltjs/core';\nimport { Injectable, inject, LifecycleManager } from '@zeltjs/core';\nimport { RedisService } from '@zeltjs/redis';\nimport type Redis from 'ioredis';\nimport mitt from 'mitt';\n\nimport type { EventBusAdaptor, EventBusSchema } from '../eventbus.types';\n\n@Injectable()\nexport class RedisEventBusAdaptor implements EventBusAdaptor, Lifecycle {\n private readonly pub: Redis;\n private readonly sub: Redis;\n private readonly localEmitter = mitt<EventBusSchema>();\n private readonly subscriptions = new Set<string>();\n // Lets concurrent on() calls for the same not-yet-established channel share\n // one in-flight SUBSCRIBE, so the second caller's await reflects whether\n // that SUBSCRIBE actually succeeded instead of resolving early.\n private readonly subscribing = new Map<string, Promise<void>>();\n\n constructor(redis = inject(RedisService), lifecycle = inject(LifecycleManager)) {\n // redis.client is lazyConnect (no I/O yet); duplicate() copies that option,\n // so building the sub client and attaching the listener here is also I/O-free.\n this.pub = redis.client;\n this.sub = this.pub.duplicate();\n this.sub.on('message', (channel: string, message: string) => {\n if (!this.subscriptions.has(channel)) return;\n const data: unknown = JSON.parse(message);\n this.localEmitter.emit(channel, data);\n });\n lifecycle.register(this);\n }\n\n async startup(): Promise<void> {\n try {\n await this.sub.connect();\n if (this.subscriptions.size > 0) {\n await this.sub.subscribe(...this.subscriptions);\n }\n } catch (error) {\n // ioredis keeps rescheduling reconnects via retryStrategy after a\n // rejected connect(), and LifecycleManager never calls shutdown() for\n // a lifecycle whose own startup() threw, so this is the only place\n // left to stop the socket/reconnect timer before rethrowing.\n this.sub.disconnect();\n throw error;\n }\n }\n\n async shutdown(): Promise<void> {\n this.sub.disconnect();\n }\n\n async emit<K extends string & keyof EventBusSchema>(\n event: K,\n data: EventBusSchema[K],\n ): Promise<void> {\n await this.pub.publish(event, JSON.stringify(data));\n }\n\n /**\n * @throws {Error} When the channel's SUBSCRIBE command to Redis fails,\n * whether this call sent it or joined another in-flight on() for the same\n * channel; the handler is not registered in that case.\n */\n async on<K extends string & keyof EventBusSchema>(\n event: K,\n handler: (data: EventBusSchema[K]) => void,\n ): Promise<() => void> {\n // Registered before any await, so a message that arrives while this call\n // is still waiting on SUBSCRIBE (below) or on startup()'s bulk subscribe\n // is not missed.\n this.localEmitter.on(event, handler);\n\n // Before startup() the lazy client sits in 'wait'; sending SUBSCRIBE then\n // would auto-connect and make startup()'s connect() reject as \"already\n // connecting\". startup() bulk-subscribes everything registered until then,\n // so resolve immediately here without waiting on that bulk subscribe.\n // Once startup() has initiated the connection ioredis queues commands\n // across reconnects, so subscribing here is safe. 'end' means shutdown().\n const connectionInitiated = this.sub.status !== 'wait' && this.sub.status !== 'end';\n if (!connectionInitiated) {\n this.subscriptions.add(event);\n } else if (!this.subscriptions.has(event)) {\n // Share one in-flight SUBSCRIBE across concurrent on() calls for the\n // same channel, so a second call's await reflects the same outcome as\n // the first instead of resolving before the channel is established.\n let subscribing = this.subscribing.get(event);\n if (!subscribing) {\n subscribing = this.sub\n .subscribe(event)\n .then(() => {\n this.subscriptions.add(event);\n })\n .finally(() => {\n this.subscribing.delete(event);\n });\n this.subscribing.set(event, subscribing);\n }\n\n try {\n await subscribing;\n } catch (error) {\n // Subscribing failed, so leave no trace of this call: a caller that\n // sees on() reject must be free to retry without a leaked handler.\n // `subscriptions` never gained this channel (add() only runs on\n // success above), so there is nothing to delete there.\n this.localEmitter.off(event, handler);\n throw error;\n }\n }\n\n return () => {\n this.localEmitter.off(event, handler);\n };\n }\n\n /** @throws {Error} Same as on(), which this delegates to. */\n async once<K extends string & keyof EventBusSchema>(\n event: K,\n handler: (data: EventBusSchema[K]) => void,\n ): Promise<() => void> {\n // Unsubscribes itself directly instead of going through on()'s returned\n // function, since that function only resolves after this handler is\n // already registered.\n const wrappedHandler = (data: EventBusSchema[K]) => {\n this.localEmitter.off(event, wrappedHandler);\n handler(data);\n };\n return this.on(event, wrappedHandler);\n }\n}\n"],"mappings":";;;;;;;;;;;;;AASA,IAAaK,uBAAb,MAAaA;CAuBX,MAAMC,UAAyB;AAC7B,MAAI;AACF,SAAM,KAAKC,IAAIC,SAAO;AACtB,OAAI,KAAKC,cAAcC,OAAO,EAC5B,OAAM,KAAKH,IAAII,UAAS,GAAI,KAAKF,cAAa;WAEzCG,OAAO;AAKd,QAAKL,IAAIM,YAAU;AACnB,SAAMD;;;CAIV,MAAME,WAA0B;AAC9B,OAAKP,IAAIM,YAAU;;CAGrB,MAAME,KACJC,OACAC,MACe;AACf,QAAM,KAAKC,IAAIC,QAAQH,OAAOI,KAAKC,UAAUJ,KAAAA,CAAAA;;;;;;IAQ/C,MAAMK,GACJN,OACAO,SACqB;AAIrB,OAAKC,aAAaF,GAAGN,OAAOO,QAAAA;AAS5B,MAAI,EADwB,KAAKhB,IAAImB,WAAW,UAAU,KAAKnB,IAAImB,WAAW,OAE5E,MAAKjB,cAAckB,IAAIX,MAAAA;WACd,CAAC,KAAKP,cAAcmB,IAAIZ,MAAAA,EAAQ;GAIzC,IAAIa,cAAc,KAAKA,YAAYC,IAAId,MAAAA;AACvC,OAAI,CAACa,aAAa;AAChBA,kBAAc,KAAKtB,IAChBI,UAAUK,MAAAA,CACVe,WAAK;AACJ,UAAKtB,cAAckB,IAAIX,MAAAA;MACzB,CACCgB,cAAQ;AACP,UAAKH,YAAYI,OAAOjB,MAAAA;MAC1B;AACF,SAAKa,YAAYK,IAAIlB,OAAOa,YAAAA;;AAG9B,OAAI;AACF,UAAMA;YACCjB,OAAO;AAKd,SAAKY,aAAaW,IAAInB,OAAOO,QAAAA;AAC7B,UAAMX;;;AAIV,eAAO;AACL,QAAKY,aAAaW,IAAInB,OAAOO,QAAAA;;;+DAKjC,MAAMa,KACJpB,OACAO,SACqB;EAIrB,MAAMc,kBAAkBpB,SAAAA;AACtB,QAAKO,aAAaW,IAAInB,OAAOqB,eAAAA;AAC7Bd,WAAQN,KAAAA;;AAEV,SAAO,KAAKK,GAAGN,OAAOqB,eAAAA;;CA7GxB,YAAYC,QAAQrC,OAAOE,aAAa,EAAEoC,YAAYtC,OAAOC,iBAAiB,EAAE;OAP/DsB,eAAepB,cAAAA;OACfK,gCAAgB,IAAI+B,KAAAA;OAIpBX,8BAAc,IAAIY,KAAAA;AAKjC,OAAKvB,MAAMoB,MAAMI;AACjB,OAAKnC,MAAM,KAAKW,IAAIyB,WAAS;AAC7B,OAAKpC,IAAIe,GAAG,YAAYsB,SAAiBC,YAAAA;AACvC,OAAI,CAAC,KAAKpC,cAAcmB,IAAIgB,QAAAA,CAAU;GACtC,MAAM3B,OAAgBG,KAAK0B,MAAMD,QAAAA;AACjC,QAAKrB,aAAaT,KAAK6B,SAAS3B,KAAAA;IAClC;AACAsB,YAAUQ,SAAS,KAAI"}
|
|
@@ -2,9 +2,9 @@
|
|
|
2
2
|
interface EventBusSchema extends Record<string, unknown> {}
|
|
3
3
|
interface EventBusAdaptor {
|
|
4
4
|
emit<K extends string & keyof EventBusSchema>(event: K, data: EventBusSchema[K]): Promise<void>;
|
|
5
|
-
on<K extends string & keyof EventBusSchema>(event: K, handler: (data: EventBusSchema[K]) => void): () => void
|
|
6
|
-
once<K extends string & keyof EventBusSchema>(event: K, handler: (data: EventBusSchema[K]) => void): () => void
|
|
5
|
+
on<K extends string & keyof EventBusSchema>(event: K, handler: (data: EventBusSchema[K]) => void): Promise<() => void>;
|
|
6
|
+
once<K extends string & keyof EventBusSchema>(event: K, handler: (data: EventBusSchema[K]) => void): Promise<() => void>;
|
|
7
7
|
}
|
|
8
8
|
//#endregion
|
|
9
9
|
export { EventBusSchema as n, EventBusAdaptor as t };
|
|
10
|
-
//# sourceMappingURL=eventbus.types-
|
|
10
|
+
//# sourceMappingURL=eventbus.types-Dk9jl4Xp.d.cts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"eventbus.types-Dk9jl4Xp.d.cts","names":[],"sources":["../src/eventbus.types.ts"],"mappings":";UACiB,cAAA,SAAuB,MAAA;AAAA,UAEvB,eAAA;EACf,IAAA,0BAA8B,cAAA,EAAgB,KAAA,EAAO,CAAA,EAAG,IAAA,EAAM,cAAA,CAAe,CAAA,IAAK,OAAA;EAClF,EAAA,0BAA4B,cAAA,EAC1B,KAAA,EAAO,CAAA,EACP,OAAA,GAAU,IAAA,EAAM,cAAA,CAAe,CAAA,aAC9B,OAAA;EACH,IAAA,0BAA8B,cAAA,EAC5B,KAAA,EAAO,CAAA,EACP,OAAA,GAAU,IAAA,EAAM,cAAA,CAAe,CAAA,aAC9B,OAAA;AAAA"}
|
|
@@ -2,9 +2,9 @@
|
|
|
2
2
|
interface EventBusSchema extends Record<string, unknown> {}
|
|
3
3
|
interface EventBusAdaptor {
|
|
4
4
|
emit<K extends string & keyof EventBusSchema>(event: K, data: EventBusSchema[K]): Promise<void>;
|
|
5
|
-
on<K extends string & keyof EventBusSchema>(event: K, handler: (data: EventBusSchema[K]) => void): () => void
|
|
6
|
-
once<K extends string & keyof EventBusSchema>(event: K, handler: (data: EventBusSchema[K]) => void): () => void
|
|
5
|
+
on<K extends string & keyof EventBusSchema>(event: K, handler: (data: EventBusSchema[K]) => void): Promise<() => void>;
|
|
6
|
+
once<K extends string & keyof EventBusSchema>(event: K, handler: (data: EventBusSchema[K]) => void): Promise<() => void>;
|
|
7
7
|
}
|
|
8
8
|
//#endregion
|
|
9
9
|
export { EventBusSchema as n, EventBusAdaptor as t };
|
|
10
|
-
//# sourceMappingURL=eventbus.types-
|
|
10
|
+
//# sourceMappingURL=eventbus.types-DvL3Ux3o.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"eventbus.types-DvL3Ux3o.d.ts","names":[],"sources":["../src/eventbus.types.ts"],"mappings":";UACiB,cAAA,SAAuB,MAAA;AAAA,UAEvB,eAAA;EACf,IAAA,0BAA8B,cAAA,EAAgB,KAAA,EAAO,CAAA,EAAG,IAAA,EAAM,cAAA,CAAe,CAAA,IAAK,OAAA;EAClF,EAAA,0BAA4B,cAAA,EAC1B,KAAA,EAAO,CAAA,EACP,OAAA,GAAU,IAAA,EAAM,cAAA,CAAe,CAAA,aAC9B,OAAA;EACH,IAAA,0BAA8B,cAAA,EAC5B,KAAA,EAAO,CAAA,EACP,OAAA,GAAU,IAAA,EAAM,cAAA,CAAe,CAAA,aAC9B,OAAA;AAAA"}
|
|
@@ -1,12 +1,12 @@
|
|
|
1
|
-
import { n as EventBusSchema, t as EventBusAdaptor } from "./eventbus.types-
|
|
1
|
+
import { n as EventBusSchema, t as EventBusAdaptor } from "./eventbus.types-Dk9jl4Xp.cjs";
|
|
2
2
|
|
|
3
3
|
//#region src/adaptor-memory/memory-event-bus.adaptor.d.ts
|
|
4
4
|
declare class MemoryEventBusAdaptor implements EventBusAdaptor {
|
|
5
5
|
private readonly emitter;
|
|
6
6
|
emit<K extends string & keyof EventBusSchema>(event: K, data: EventBusSchema[K]): Promise<void>;
|
|
7
|
-
on<K extends string & keyof EventBusSchema>(event: K, handler: (data: EventBusSchema[K]) => void): () => void
|
|
8
|
-
once<K extends string & keyof EventBusSchema>(event: K, handler: (data: EventBusSchema[K]) => void): () => void
|
|
7
|
+
on<K extends string & keyof EventBusSchema>(event: K, handler: (data: EventBusSchema[K]) => void): Promise<() => void>;
|
|
8
|
+
once<K extends string & keyof EventBusSchema>(event: K, handler: (data: EventBusSchema[K]) => void): Promise<() => void>;
|
|
9
9
|
}
|
|
10
10
|
//#endregion
|
|
11
11
|
export { MemoryEventBusAdaptor as t };
|
|
12
|
-
//# sourceMappingURL=index-
|
|
12
|
+
//# sourceMappingURL=index-C2cRFzub.d.cts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index-C2cRFzub.d.cts","names":[],"sources":["../src/adaptor-memory/memory-event-bus.adaptor.ts"],"mappings":";;;cAMa,qBAAA,YAAiC,eAAA;EAAA,iBAC3B,OAAA;EAEX,IAAA,0BAA8B,cAAA,CAAA,CAClC,KAAA,EAAO,CAAA,EACP,IAAA,EAAM,cAAA,CAAe,CAAA,IACpB,OAAA;EAIG,EAAA,0BAA4B,cAAA,CAAA,CAChC,KAAA,EAAO,CAAA,EACP,OAAA,GAAU,IAAA,EAAM,cAAA,CAAe,CAAA,aAC9B,OAAA;EAKG,IAAA,0BAA8B,cAAA,CAAA,CAClC,KAAA,EAAO,CAAA,EACP,OAAA,GAAU,IAAA,EAAM,cAAA,CAAe,CAAA,aAC9B,OAAA;AAAA"}
|
|
@@ -1,12 +1,12 @@
|
|
|
1
|
-
import { n as EventBusSchema, t as EventBusAdaptor } from "./eventbus.types-
|
|
1
|
+
import { n as EventBusSchema, t as EventBusAdaptor } from "./eventbus.types-DvL3Ux3o.js";
|
|
2
2
|
|
|
3
3
|
//#region src/adaptor-memory/memory-event-bus.adaptor.d.ts
|
|
4
4
|
declare class MemoryEventBusAdaptor implements EventBusAdaptor {
|
|
5
5
|
private readonly emitter;
|
|
6
6
|
emit<K extends string & keyof EventBusSchema>(event: K, data: EventBusSchema[K]): Promise<void>;
|
|
7
|
-
on<K extends string & keyof EventBusSchema>(event: K, handler: (data: EventBusSchema[K]) => void): () => void
|
|
8
|
-
once<K extends string & keyof EventBusSchema>(event: K, handler: (data: EventBusSchema[K]) => void): () => void
|
|
7
|
+
on<K extends string & keyof EventBusSchema>(event: K, handler: (data: EventBusSchema[K]) => void): Promise<() => void>;
|
|
8
|
+
once<K extends string & keyof EventBusSchema>(event: K, handler: (data: EventBusSchema[K]) => void): Promise<() => void>;
|
|
9
9
|
}
|
|
10
10
|
//#endregion
|
|
11
11
|
export { MemoryEventBusAdaptor as t };
|
|
12
|
-
//# sourceMappingURL=index-
|
|
12
|
+
//# sourceMappingURL=index-CcpSMqqG.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index-CcpSMqqG.d.ts","names":[],"sources":["../src/adaptor-memory/memory-event-bus.adaptor.ts"],"mappings":";;;cAMa,qBAAA,YAAiC,eAAA;EAAA,iBAC3B,OAAA;EAEX,IAAA,0BAA8B,cAAA,CAAA,CAClC,KAAA,EAAO,CAAA,EACP,IAAA,EAAM,cAAA,CAAe,CAAA,IACpB,OAAA;EAIG,EAAA,0BAA4B,cAAA,CAAA,CAChC,KAAA,EAAO,CAAA,EACP,OAAA,GAAU,IAAA,EAAM,cAAA,CAAe,CAAA,aAC9B,OAAA;EAKG,IAAA,0BAA8B,cAAA,CAAA,CAClC,KAAA,EAAO,CAAA,EACP,OAAA,GAAU,IAAA,EAAM,cAAA,CAAe,CAAA,aAC9B,OAAA;AAAA"}
|
package/dist/index.cjs
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
2
|
-
const require_adaptor_memory = require("./adaptor-memory-
|
|
2
|
+
const require_adaptor_memory = require("./adaptor-memory-zYqB6KAT.cjs");
|
|
3
3
|
//#region src/eventbus.feature.ts
|
|
4
4
|
const eventbus = (opts) => ({
|
|
5
5
|
key: "eventbus",
|
package/dist/index.d.cts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { n as EventBusSchema, t as EventBusAdaptor } from "./eventbus.types-
|
|
2
|
-
import { t as MemoryEventBusAdaptor } from "./index-
|
|
1
|
+
import { n as EventBusSchema, t as EventBusAdaptor } from "./eventbus.types-Dk9jl4Xp.cjs";
|
|
2
|
+
import { t as MemoryEventBusAdaptor } from "./index-C2cRFzub.cjs";
|
|
3
3
|
import { ConfiguredFeature } from "@zeltjs/core";
|
|
4
4
|
|
|
5
5
|
//#region src/eventbus.feature.d.ts
|
|
@@ -10,8 +10,8 @@ type EventBusOptions = {
|
|
|
10
10
|
};
|
|
11
11
|
type EventBusCapabilities = {
|
|
12
12
|
readonly emit: <K extends string & keyof EventBusSchema>(event: K, data: EventBusSchema[K]) => Promise<void>;
|
|
13
|
-
readonly on: <K extends string & keyof EventBusSchema>(event: K, handler: (data: EventBusSchema[K]) => void) => () => void
|
|
14
|
-
readonly once: <K extends string & keyof EventBusSchema>(event: K, handler: (data: EventBusSchema[K]) => void) => () => void
|
|
13
|
+
readonly on: <K extends string & keyof EventBusSchema>(event: K, handler: (data: EventBusSchema[K]) => void) => Promise<() => void>;
|
|
14
|
+
readonly once: <K extends string & keyof EventBusSchema>(event: K, handler: (data: EventBusSchema[K]) => void) => Promise<() => void>;
|
|
15
15
|
};
|
|
16
16
|
declare const eventbus: (opts: EventBusOptions) => ConfiguredFeature<"eventbus", EventBusCapabilities>;
|
|
17
17
|
//#endregion
|
package/dist/index.d.cts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.cts","names":[],"sources":["../src/eventbus.feature.ts"],"mappings":";;;;;KAIK,oBAAA,WAA+B,IAAA;AAAA,KAE/B,eAAA;EAAA,SACM,OAAA,UAAiB,IAAA,cAAkB,eAAA;EAAA,SACnC,QAAA,YAAoB,oBAAA;AAAA;AAAA,KAGnB,oBAAA;EAAA,SACD,IAAA,4BAAgC,cAAA,EACvC,KAAA,EAAO,CAAA,EACP,IAAA,EAAM,cAAA,CAAe,CAAA,MAClB,OAAA;EAAA,SACI,EAAA,4BAA8B,cAAA,EACrC,KAAA,EAAO,CAAA,EACP,OAAA,GAAU,IAAA,EAAM,cAAA,CAAe,CAAA;EAAA,
|
|
1
|
+
{"version":3,"file":"index.d.cts","names":[],"sources":["../src/eventbus.feature.ts"],"mappings":";;;;;KAIK,oBAAA,WAA+B,IAAA;AAAA,KAE/B,eAAA;EAAA,SACM,OAAA,UAAiB,IAAA,cAAkB,eAAA;EAAA,SACnC,QAAA,YAAoB,oBAAA;AAAA;AAAA,KAGnB,oBAAA;EAAA,SACD,IAAA,4BAAgC,cAAA,EACvC,KAAA,EAAO,CAAA,EACP,IAAA,EAAM,cAAA,CAAe,CAAA,MAClB,OAAA;EAAA,SACI,EAAA,4BAA8B,cAAA,EACrC,KAAA,EAAO,CAAA,EACP,OAAA,GAAU,IAAA,EAAM,cAAA,CAAe,CAAA,eAC5B,OAAA;EAAA,SACI,IAAA,4BAAgC,cAAA,EACvC,KAAA,EAAO,CAAA,EACP,OAAA,GAAU,IAAA,EAAM,cAAA,CAAe,CAAA,eAC5B,OAAA;AAAA;AAAA,cAGM,QAAA,GACX,IAAA,EAAM,eAAA,KACL,iBAAA,aAA8B,oBAAA"}
|
package/dist/index.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { n as EventBusSchema, t as EventBusAdaptor } from "./eventbus.types-
|
|
2
|
-
import { t as MemoryEventBusAdaptor } from "./index-
|
|
1
|
+
import { n as EventBusSchema, t as EventBusAdaptor } from "./eventbus.types-DvL3Ux3o.js";
|
|
2
|
+
import { t as MemoryEventBusAdaptor } from "./index-CcpSMqqG.js";
|
|
3
3
|
import { ConfiguredFeature } from "@zeltjs/core";
|
|
4
4
|
|
|
5
5
|
//#region src/eventbus.feature.d.ts
|
|
@@ -10,8 +10,8 @@ type EventBusOptions = {
|
|
|
10
10
|
};
|
|
11
11
|
type EventBusCapabilities = {
|
|
12
12
|
readonly emit: <K extends string & keyof EventBusSchema>(event: K, data: EventBusSchema[K]) => Promise<void>;
|
|
13
|
-
readonly on: <K extends string & keyof EventBusSchema>(event: K, handler: (data: EventBusSchema[K]) => void) => () => void
|
|
14
|
-
readonly once: <K extends string & keyof EventBusSchema>(event: K, handler: (data: EventBusSchema[K]) => void) => () => void
|
|
13
|
+
readonly on: <K extends string & keyof EventBusSchema>(event: K, handler: (data: EventBusSchema[K]) => void) => Promise<() => void>;
|
|
14
|
+
readonly once: <K extends string & keyof EventBusSchema>(event: K, handler: (data: EventBusSchema[K]) => void) => Promise<() => void>;
|
|
15
15
|
};
|
|
16
16
|
declare const eventbus: (opts: EventBusOptions) => ConfiguredFeature<"eventbus", EventBusCapabilities>;
|
|
17
17
|
//#endregion
|
package/dist/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","names":[],"sources":["../src/eventbus.feature.ts"],"mappings":";;;;;KAIK,oBAAA,WAA+B,IAAA;AAAA,KAE/B,eAAA;EAAA,SACM,OAAA,UAAiB,IAAA,cAAkB,eAAA;EAAA,SACnC,QAAA,YAAoB,oBAAA;AAAA;AAAA,KAGnB,oBAAA;EAAA,SACD,IAAA,4BAAgC,cAAA,EACvC,KAAA,EAAO,CAAA,EACP,IAAA,EAAM,cAAA,CAAe,CAAA,MAClB,OAAA;EAAA,SACI,EAAA,4BAA8B,cAAA,EACrC,KAAA,EAAO,CAAA,EACP,OAAA,GAAU,IAAA,EAAM,cAAA,CAAe,CAAA;EAAA,
|
|
1
|
+
{"version":3,"file":"index.d.ts","names":[],"sources":["../src/eventbus.feature.ts"],"mappings":";;;;;KAIK,oBAAA,WAA+B,IAAA;AAAA,KAE/B,eAAA;EAAA,SACM,OAAA,UAAiB,IAAA,cAAkB,eAAA;EAAA,SACnC,QAAA,YAAoB,oBAAA;AAAA;AAAA,KAGnB,oBAAA;EAAA,SACD,IAAA,4BAAgC,cAAA,EACvC,KAAA,EAAO,CAAA,EACP,IAAA,EAAM,cAAA,CAAe,CAAA,MAClB,OAAA;EAAA,SACI,EAAA,4BAA8B,cAAA,EACrC,KAAA,EAAO,CAAA,EACP,OAAA,GAAU,IAAA,EAAM,cAAA,CAAe,CAAA,eAC5B,OAAA;EAAA,SACI,IAAA,4BAAgC,cAAA,EACvC,KAAA,EAAO,CAAA,EACP,OAAA,GAAU,IAAA,EAAM,cAAA,CAAe,CAAA,eAC5B,OAAA;AAAA;AAAA,cAGM,QAAA,GACX,IAAA,EAAM,eAAA,KACL,iBAAA,aAA8B,oBAAA"}
|
package/dist/index.js
CHANGED
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","names":["eventbus","opts","key","featureClasses","adaptor","handlers","blueprint","realize","resolver","handler","get","emit","event","data","on","once"],"sources":["../src/eventbus.feature.ts"],"sourcesContent":["import type { ConfiguredFeature } from '@zeltjs/core';\n\nimport type { EventBusAdaptor, EventBusSchema } from './eventbus.types';\n\ntype EventBusHandlerClass = new (...args: never[]) => object;\n\ntype EventBusOptions = {\n readonly adaptor: new (...args: never[]) => EventBusAdaptor;\n readonly handlers?: readonly EventBusHandlerClass[];\n};\n\nexport type EventBusCapabilities = {\n readonly emit: <K extends string & keyof EventBusSchema>(\n event: K,\n data: EventBusSchema[K],\n ) => Promise<void>;\n readonly on: <K extends string & keyof EventBusSchema>(\n event: K,\n handler: (data: EventBusSchema[K]) => void,\n ) => () => void
|
|
1
|
+
{"version":3,"file":"index.js","names":["eventbus","opts","key","featureClasses","adaptor","handlers","blueprint","realize","resolver","handler","get","emit","event","data","on","once"],"sources":["../src/eventbus.feature.ts"],"sourcesContent":["import type { ConfiguredFeature } from '@zeltjs/core';\n\nimport type { EventBusAdaptor, EventBusSchema } from './eventbus.types';\n\ntype EventBusHandlerClass = new (...args: never[]) => object;\n\ntype EventBusOptions = {\n readonly adaptor: new (...args: never[]) => EventBusAdaptor;\n readonly handlers?: readonly EventBusHandlerClass[];\n};\n\nexport type EventBusCapabilities = {\n readonly emit: <K extends string & keyof EventBusSchema>(\n event: K,\n data: EventBusSchema[K],\n ) => Promise<void>;\n readonly on: <K extends string & keyof EventBusSchema>(\n event: K,\n handler: (data: EventBusSchema[K]) => void,\n ) => Promise<() => void>;\n readonly once: <K extends string & keyof EventBusSchema>(\n event: K,\n handler: (data: EventBusSchema[K]) => void,\n ) => Promise<() => void>;\n};\n\nexport const eventbus = (\n opts: EventBusOptions,\n): ConfiguredFeature<'eventbus', EventBusCapabilities> => ({\n key: 'eventbus',\n featureClasses: () => [opts.adaptor, ...(opts.handlers ?? [])],\n blueprint: () => ({}),\n realize: async (resolver) => {\n for (const handler of opts.handlers ?? []) {\n await resolver.get(handler);\n }\n\n const adaptor = await resolver.get(opts.adaptor);\n return {\n emit: (event, data) => adaptor.emit(event, data),\n on: (event, handler) => adaptor.on(event, handler),\n once: (event, handler) => adaptor.once(event, handler),\n };\n },\n});\n"],"mappings":";;AA0BA,MAAaA,YACXC,UACyD;CACzDC,KAAK;CACLC,sBAAsB,CAACF,KAAKG,SAAAA,GAAaH,KAAKI,YAAY,EAAE,CAAE;CAC9DC,kBAAkB,EAAC;CACnBC,SAAS,OAAOC,aAAAA;AACd,OAAK,MAAMC,WAAWR,KAAKI,YAAY,EAAE,CACvC,OAAMG,SAASE,IAAID,QAAAA;EAGrB,MAAML,UAAU,MAAMI,SAASE,IAAIT,KAAKG,QAAO;AAC/C,SAAO;GACLO,OAAOC,OAAOC,SAAST,QAAQO,KAAKC,OAAOC,KAAAA;GAC3CC,KAAKF,OAAOH,YAAYL,QAAQU,GAAGF,OAAOH,QAAAA;GAC1CM,OAAOH,OAAOH,YAAYL,QAAQW,KAAKH,OAAOH,QAAAA;GAChD;;CAEJ"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@zeltjs/eventbus",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.12.0-canary.0",
|
|
4
4
|
"description": "Event bus abstraction for Zelt applications",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"license": "MIT",
|
|
@@ -48,8 +48,8 @@
|
|
|
48
48
|
"dist"
|
|
49
49
|
],
|
|
50
50
|
"peerDependencies": {
|
|
51
|
-
"@zeltjs/core": "0.
|
|
52
|
-
"@zeltjs/redis": "0.
|
|
51
|
+
"@zeltjs/core": "0.12.0-canary.0",
|
|
52
|
+
"@zeltjs/redis": "0.12.0-canary.0"
|
|
53
53
|
},
|
|
54
54
|
"peerDependenciesMeta": {
|
|
55
55
|
"@zeltjs/redis": {
|
|
@@ -60,8 +60,10 @@
|
|
|
60
60
|
"@types/node": "22.19.17",
|
|
61
61
|
"ioredis": "5.10.1",
|
|
62
62
|
"mitt": "3.0.1",
|
|
63
|
-
"
|
|
64
|
-
"@zeltjs/
|
|
63
|
+
"testcontainers": "10.25.0",
|
|
64
|
+
"@zeltjs/core": "0.12.0-canary.0",
|
|
65
|
+
"@zeltjs/testing": "0.12.0-canary.0",
|
|
66
|
+
"@zeltjs/redis": "0.12.0-canary.0"
|
|
65
67
|
},
|
|
66
68
|
"volta": {
|
|
67
69
|
"extends": "../../package.json"
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"adaptor-memory-BG1Xyr7_.js","names":["Injectable","mitt","MemoryEventBusAdaptor","emit","event","data","emitter","on","handler","off","once","wrappedHandler","unsubscribe"],"sources":["../src/adaptor-memory/memory-event-bus.adaptor.ts"],"sourcesContent":["import { Injectable } from '@zeltjs/core';\nimport mitt from 'mitt';\n\nimport type { EventBusAdaptor, EventBusSchema } from '../eventbus.types';\n\n@Injectable()\nexport class MemoryEventBusAdaptor implements EventBusAdaptor {\n private readonly emitter = mitt<EventBusSchema>();\n\n async emit<K extends string & keyof EventBusSchema>(\n event: K,\n data: EventBusSchema[K],\n ): Promise<void> {\n this.emitter.emit(event, data);\n }\n\n on<K extends string & keyof EventBusSchema>(\n event: K,\n handler: (data: EventBusSchema[K]) => void,\n ): () => void {\n this.emitter.on(event, handler);\n return () => this.emitter.off(event, handler);\n }\n\n once<K extends string & keyof EventBusSchema>(\n event: K,\n handler: (data: EventBusSchema[K]) => void,\n ): () => void {\n const wrappedHandler = (data: EventBusSchema[K]) => {\n unsubscribe();\n handler(data);\n };\n const unsubscribe = this.on(event, wrappedHandler);\n return unsubscribe;\n }\n}\n"],"mappings":";;;;;;;;;AAMA,IAAaE,wBAAb,MAAaA;CAGX,MAAMC,KACJC,OACAC,MACe;AACf,OAAKC,QAAQH,KAAKC,OAAOC,KAAAA;;CAG3BE,GACEH,OACAI,SACY;AACZ,OAAKF,QAAQC,GAAGH,OAAOI,QAAAA;AACvB,eAAa,KAAKF,QAAQG,IAAIL,OAAOI,QAAAA;;CAGvCE,KACEN,OACAI,SACY;EACZ,MAAMG,kBAAkBN,SAAAA;AACtBO,gBAAAA;AACAJ,WAAQH,KAAAA;;EAEV,MAAMO,cAAc,KAAKL,GAAGH,OAAOO,eAAAA;AACnC,SAAOC;;;OA1BQN,UAAUL,cAAAA"}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"eventbus.types-Cn8xuI2B.d.ts","names":[],"sources":["../src/eventbus.types.ts"],"mappings":";UACiB,cAAA,SAAuB,MAAA;AAAA,UAEvB,eAAA;EACf,IAAA,0BAA8B,cAAA,EAAgB,KAAA,EAAO,CAAA,EAAG,IAAA,EAAM,cAAA,CAAe,CAAA,IAAK,OAAA;EAClF,EAAA,0BAA4B,cAAA,EAC1B,KAAA,EAAO,CAAA,EACP,OAAA,GAAU,IAAA,EAAM,cAAA,CAAe,CAAA;EAEjC,IAAA,0BAA8B,cAAA,EAC5B,KAAA,EAAO,CAAA,EACP,OAAA,GAAU,IAAA,EAAM,cAAA,CAAe,CAAA;AAAA"}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"eventbus.types-kl1yXTK8.d.cts","names":[],"sources":["../src/eventbus.types.ts"],"mappings":";UACiB,cAAA,SAAuB,MAAA;AAAA,UAEvB,eAAA;EACf,IAAA,0BAA8B,cAAA,EAAgB,KAAA,EAAO,CAAA,EAAG,IAAA,EAAM,cAAA,CAAe,CAAA,IAAK,OAAA;EAClF,EAAA,0BAA4B,cAAA,EAC1B,KAAA,EAAO,CAAA,EACP,OAAA,GAAU,IAAA,EAAM,cAAA,CAAe,CAAA;EAEjC,IAAA,0BAA8B,cAAA,EAC5B,KAAA,EAAO,CAAA,EACP,OAAA,GAAU,IAAA,EAAM,cAAA,CAAe,CAAA;AAAA"}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"index-Buq5DkB1.d.ts","names":[],"sources":["../src/adaptor-memory/memory-event-bus.adaptor.ts"],"mappings":";;;cAMa,qBAAA,YAAiC,eAAA;EAAA,iBAC3B,OAAA;EAEX,IAAA,0BAA8B,cAAA,CAAA,CAClC,KAAA,EAAO,CAAA,EACP,IAAA,EAAM,cAAA,CAAe,CAAA,IACpB,OAAA;EAIH,EAAA,0BAA4B,cAAA,CAAA,CAC1B,KAAA,EAAO,CAAA,EACP,OAAA,GAAU,IAAA,EAAM,cAAA,CAAe,CAAA;EAMjC,IAAA,0BAA8B,cAAA,CAAA,CAC5B,KAAA,EAAO,CAAA,EACP,OAAA,GAAU,IAAA,EAAM,cAAA,CAAe,CAAA;AAAA"}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"index-qecFavvB.d.cts","names":[],"sources":["../src/adaptor-memory/memory-event-bus.adaptor.ts"],"mappings":";;;cAMa,qBAAA,YAAiC,eAAA;EAAA,iBAC3B,OAAA;EAEX,IAAA,0BAA8B,cAAA,CAAA,CAClC,KAAA,EAAO,CAAA,EACP,IAAA,EAAM,cAAA,CAAe,CAAA,IACpB,OAAA;EAIH,EAAA,0BAA4B,cAAA,CAAA,CAC1B,KAAA,EAAO,CAAA,EACP,OAAA,GAAU,IAAA,EAAM,cAAA,CAAe,CAAA;EAMjC,IAAA,0BAA8B,cAAA,CAAA,CAC5B,KAAA,EAAO,CAAA,EACP,OAAA,GAAU,IAAA,EAAM,cAAA,CAAe,CAAA;AAAA"}
|