@beignet/provider-event-bus-memory 0.0.49 → 0.0.50
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 +9 -0
- package/README.md +33 -15
- package/dist/index.d.ts +6 -3
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +18 -8
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
- package/src/index.ts +22 -13
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,14 @@
|
|
|
1
1
|
# @beignet/provider-event-bus-memory
|
|
2
2
|
|
|
3
|
+
## 0.0.50
|
|
4
|
+
|
|
5
|
+
### Patch Changes
|
|
6
|
+
|
|
7
|
+
- 6fe026a: Expose event-subscription readiness and asynchronous cleanup, await listener
|
|
8
|
+
registries during provider startup with bounded rollback, diagnose unsafe app
|
|
9
|
+
wiring, and make Redis subscription lifecycle failures observable with bounded
|
|
10
|
+
provider shutdown and recoverable channel state.
|
|
11
|
+
|
|
3
12
|
## 0.0.49
|
|
4
13
|
|
|
5
14
|
### Patch Changes
|
package/README.md
CHANGED
|
@@ -43,11 +43,13 @@ const UserRegistered = defineEvent("user.registered", {
|
|
|
43
43
|
const eventBus = createMemoryEventBus();
|
|
44
44
|
|
|
45
45
|
// Subscribe to events
|
|
46
|
-
const
|
|
46
|
+
const subscription = eventBus.subscribe(UserRegistered, (payload) => {
|
|
47
47
|
console.log(`User registered: ${payload.email}`);
|
|
48
48
|
// Send welcome email, update analytics, etc.
|
|
49
49
|
});
|
|
50
50
|
|
|
51
|
+
await subscription.ready;
|
|
52
|
+
|
|
51
53
|
// Publish events
|
|
52
54
|
await eventBus.publish(UserRegistered, {
|
|
53
55
|
userId: "123",
|
|
@@ -55,7 +57,7 @@ await eventBus.publish(UserRegistered, {
|
|
|
55
57
|
});
|
|
56
58
|
|
|
57
59
|
// Unsubscribe when done
|
|
58
|
-
unsubscribe();
|
|
60
|
+
await subscription.unsubscribe();
|
|
59
61
|
```
|
|
60
62
|
|
|
61
63
|
## Framework setup
|
|
@@ -116,14 +118,18 @@ const OrderPlaced = defineEvent("order.placed", {
|
|
|
116
118
|
});
|
|
117
119
|
|
|
118
120
|
// Subscribe to events in your application setup
|
|
119
|
-
ctx.ports.eventBus.subscribe(
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
}
|
|
121
|
+
const orderPlacedSubscription = ctx.ports.eventBus.subscribe(
|
|
122
|
+
OrderPlaced,
|
|
123
|
+
async (payload) => {
|
|
124
|
+
// Send order confirmation email
|
|
125
|
+
await ctx.ports.mailer.send({
|
|
126
|
+
to: customer.email,
|
|
127
|
+
subject: "Order Confirmation",
|
|
128
|
+
text: `Your order ${payload.orderId} has been placed!`,
|
|
129
|
+
});
|
|
130
|
+
},
|
|
131
|
+
);
|
|
132
|
+
await orderPlacedSubscription.ready;
|
|
127
133
|
|
|
128
134
|
const placeOrder = useCase
|
|
129
135
|
.command("orders.place")
|
|
@@ -142,6 +148,11 @@ const placeOrder = useCase
|
|
|
142
148
|
return order;
|
|
143
149
|
});
|
|
144
150
|
});
|
|
151
|
+
|
|
152
|
+
// Call this from the application's shutdown hook.
|
|
153
|
+
export async function stopOrderListeners() {
|
|
154
|
+
await orderPlacedSubscription.unsubscribe();
|
|
155
|
+
}
|
|
145
156
|
```
|
|
146
157
|
|
|
147
158
|
## EventBus port API
|
|
@@ -168,18 +179,22 @@ The optional third `{ trace }` argument is transport metadata used by Beignet
|
|
|
168
179
|
listeners. When `instrumentation` exposes a tracing port, the bus captures the
|
|
169
180
|
active context automatically. Existing two-argument calls are unchanged.
|
|
170
181
|
|
|
171
|
-
### `subscribe<E>(event, handler):
|
|
182
|
+
### `subscribe<E>(event, handler): EventSubscription`
|
|
172
183
|
|
|
173
|
-
Subscribe to a domain event.
|
|
184
|
+
Subscribe to a domain event. The memory adapter is active synchronously, so
|
|
185
|
+
`ready` is already resolved. `unsubscribe()` is idempotent and removes local
|
|
186
|
+
delivery before its promise resolves.
|
|
174
187
|
|
|
175
188
|
```typescript
|
|
176
|
-
const
|
|
189
|
+
const subscription = eventBus.subscribe(UserRegistered, (payload, options) => {
|
|
177
190
|
console.log(`New user: ${payload.email}`);
|
|
178
191
|
console.log(options?.trace?.traceparent);
|
|
179
192
|
});
|
|
180
193
|
|
|
194
|
+
await subscription.ready;
|
|
195
|
+
|
|
181
196
|
// Later, when you want to stop listening:
|
|
182
|
-
unsubscribe();
|
|
197
|
+
await subscription.unsubscribe();
|
|
183
198
|
```
|
|
184
199
|
|
|
185
200
|
## TypeScript support
|
|
@@ -211,7 +226,8 @@ describe("User Registration", () => {
|
|
|
211
226
|
const eventBus = createMemoryEventBus();
|
|
212
227
|
const handler = mock(() => {});
|
|
213
228
|
|
|
214
|
-
eventBus.subscribe(UserRegistered, handler);
|
|
229
|
+
const subscription = eventBus.subscribe(UserRegistered, handler);
|
|
230
|
+
await subscription.ready;
|
|
215
231
|
|
|
216
232
|
// Perform registration
|
|
217
233
|
await registerUser(ctx, { email: "test@example.com" });
|
|
@@ -220,6 +236,8 @@ describe("User Registration", () => {
|
|
|
220
236
|
userId: expect.any(String),
|
|
221
237
|
email: "test@example.com",
|
|
222
238
|
});
|
|
239
|
+
|
|
240
|
+
await subscription.unsubscribe();
|
|
223
241
|
});
|
|
224
242
|
});
|
|
225
243
|
```
|
package/dist/index.d.ts
CHANGED
|
@@ -14,15 +14,16 @@
|
|
|
14
14
|
* const eventBus = createMemoryEventBus();
|
|
15
15
|
*
|
|
16
16
|
* // Subscribe to an event
|
|
17
|
-
* const
|
|
17
|
+
* const subscription = eventBus.subscribe(UserRegistered, (payload) => {
|
|
18
18
|
* console.log(`User registered: ${payload.email}`);
|
|
19
19
|
* });
|
|
20
|
+
* await subscription.ready;
|
|
20
21
|
*
|
|
21
22
|
* // Publish an event
|
|
22
23
|
* await eventBus.publish(UserRegistered, { userId: "123", email: "test@example.com" });
|
|
23
24
|
*
|
|
24
25
|
* // Unsubscribe when done
|
|
25
|
-
* unsubscribe();
|
|
26
|
+
* await subscription.unsubscribe();
|
|
26
27
|
* ```
|
|
27
28
|
*/
|
|
28
29
|
import type { EventBusPort } from "@beignet/core/ports";
|
|
@@ -91,11 +92,13 @@ export interface MemoryEventBusProviderPorts {
|
|
|
91
92
|
* ```ts
|
|
92
93
|
* const eventBus = createMemoryEventBus();
|
|
93
94
|
*
|
|
94
|
-
* eventBus.subscribe(UserRegistered, (payload) => {
|
|
95
|
+
* const subscription = eventBus.subscribe(UserRegistered, (payload) => {
|
|
95
96
|
* console.log(`User registered: ${payload.email}`);
|
|
96
97
|
* });
|
|
98
|
+
* await subscription.ready;
|
|
97
99
|
*
|
|
98
100
|
* await eventBus.publish(UserRegistered, { userId: "123", email: "test@example.com" });
|
|
101
|
+
* await subscription.unsubscribe();
|
|
99
102
|
* ```
|
|
100
103
|
*
|
|
101
104
|
* @example
|
package/dist/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;GA2BG;AAGH,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,qBAAqB,CAAC;AACxD,OAAO,EAGL,KAAK,6BAA6B,EACnC,MAAM,yBAAyB,CAAC;AAGjC;;GAEG;AACH,MAAM,MAAM,sBAAsB,GAAG,gBAAgB,GAAG,iBAAiB,CAAC;AAE1E;;GAEG;AACH,MAAM,WAAW,qBAAqB;IACpC;;;OAGG;IACH,eAAe,CAAC,EAAE,6BAA6B,CAAC;IAEhD;;;;;;OAMG;IACH,QAAQ,CAAC,EAAE,sBAAsB,CAAC;IAElC;;;;;;;;;;;OAWG;IACH,cAAc,CAAC,EAAE,CACf,KAAK,EAAE,OAAO,EACd,SAAS,EAAE,MAAM,EACjB,OAAO,EAAE,OAAO,KACb,IAAI,CAAC;CACX;AAED;;GAEG;AACH,MAAM,WAAW,6BACf,SAAQ,IAAI,CAAC,qBAAqB,EAAE,iBAAiB,CAAC;IACtD;;OAEG;IACH,IAAI,CAAC,EAAE,MAAM,CAAC;CACf;AAED;;GAEG;AACH,MAAM,WAAW,2BAA2B;IAC1C;;OAEG;IACH,QAAQ,EAAE,YAAY,CAAC;CACxB;AAED;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA6BG;AACH,wBAAgB,oBAAoB,CAClC,OAAO,GAAE,qBAA0B,GAClC,YAAY,CA0Gd;AAED;;GAEG;AACH,wBAAgB,4BAA4B,CAC1C,OAAO,GAAE,6BAAkC;;kBAiB5C"}
|
package/dist/index.js
CHANGED
|
@@ -14,15 +14,16 @@
|
|
|
14
14
|
* const eventBus = createMemoryEventBus();
|
|
15
15
|
*
|
|
16
16
|
* // Subscribe to an event
|
|
17
|
-
* const
|
|
17
|
+
* const subscription = eventBus.subscribe(UserRegistered, (payload) => {
|
|
18
18
|
* console.log(`User registered: ${payload.email}`);
|
|
19
19
|
* });
|
|
20
|
+
* await subscription.ready;
|
|
20
21
|
*
|
|
21
22
|
* // Publish an event
|
|
22
23
|
* await eventBus.publish(UserRegistered, { userId: "123", email: "test@example.com" });
|
|
23
24
|
*
|
|
24
25
|
* // Unsubscribe when done
|
|
25
|
-
* unsubscribe();
|
|
26
|
+
* await subscription.unsubscribe();
|
|
26
27
|
* ```
|
|
27
28
|
*/
|
|
28
29
|
import { createProvider, createProviderInstrumentation, } from "@beignet/core/providers";
|
|
@@ -38,11 +39,13 @@ import { captureTraceCarrier, parseTraceCarrier } from "@beignet/core/tracing";
|
|
|
38
39
|
* ```ts
|
|
39
40
|
* const eventBus = createMemoryEventBus();
|
|
40
41
|
*
|
|
41
|
-
* eventBus.subscribe(UserRegistered, (payload) => {
|
|
42
|
+
* const subscription = eventBus.subscribe(UserRegistered, (payload) => {
|
|
42
43
|
* console.log(`User registered: ${payload.email}`);
|
|
43
44
|
* });
|
|
45
|
+
* await subscription.ready;
|
|
44
46
|
*
|
|
45
47
|
* await eventBus.publish(UserRegistered, { userId: "123", email: "test@example.com" });
|
|
48
|
+
* await subscription.unsubscribe();
|
|
46
49
|
* ```
|
|
47
50
|
*
|
|
48
51
|
* @example
|
|
@@ -118,11 +121,18 @@ export function createMemoryEventBus(options = {}) {
|
|
|
118
121
|
handlers.set(event.name, subs);
|
|
119
122
|
}
|
|
120
123
|
subs.add(handler);
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
124
|
+
let unsubscribed = false;
|
|
125
|
+
return {
|
|
126
|
+
ready: Promise.resolve(),
|
|
127
|
+
async unsubscribe() {
|
|
128
|
+
if (unsubscribed)
|
|
129
|
+
return;
|
|
130
|
+
unsubscribed = true;
|
|
131
|
+
subs.delete(handler);
|
|
132
|
+
if (subs.size === 0) {
|
|
133
|
+
handlers.delete(event.name);
|
|
134
|
+
}
|
|
135
|
+
},
|
|
126
136
|
};
|
|
127
137
|
},
|
|
128
138
|
};
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;GA2BG;AAIH,OAAO,EACL,cAAc,EACd,6BAA6B,GAE9B,MAAM,yBAAyB,CAAC;AACjC,OAAO,EAAE,mBAAmB,EAAE,iBAAiB,EAAE,MAAM,uBAAuB,CAAC;AAkE/E;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA6BG;AACH,MAAM,UAAU,oBAAoB,CAClC,UAAiC,EAAE;IAEnC,MAAM,cAAc,GAAG,OAAO,EAAE,cAAc,CAAC;IAC/C,MAAM,QAAQ,GAAG,OAAO,CAAC,QAA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pB,IAAI,CAAC,MAAM,CACT,OAGyB,CAC1B,CAAC;oBACF,IAAI,IAAI,CAAC,IAAI,KAAK,CAAC,EAAE,CAAC;wBACpB,QAAQ,CAAC,MAAM,CAAC,KAAK,CAAC,IAAI,CAAC,CAAC;oBAC9B,CAAC;gBACH,CAAC;aACF,CAAC;QACJ,CAAC;KACF,CAAC;AACJ,CAAC;AAED;;GAEG;AACH,MAAM,UAAU,4BAA4B,CAC1C,UAAyC,EAAE;IAE3C,MAAM,EAAE,IAAI,GAAG,kBAAkB,EAAE,GAAG,eAAe,EAAE,GAAG,OAAO,CAAC;IAElE,OAAO,cAAc,CAAC;QACpB,IAAI;QACJ,KAAK,CAAC,EAAE,KAAK,EAAE;YACb,OAAO;gBACL,KAAK,EAAE;oBACL,QAAQ,EAAE,oBAAoB,CAAC;wBAC7B,GAAG,eAAe;wBAClB,eAAe,EAAE,KAAK;qBACvB,CAAC;iBACmC;aACxC,CAAC;QACJ,CAAC;KACF,CAAC,CAAC;AACL,CAAC"}
|
package/package.json
CHANGED
package/src/index.ts
CHANGED
|
@@ -14,15 +14,16 @@
|
|
|
14
14
|
* const eventBus = createMemoryEventBus();
|
|
15
15
|
*
|
|
16
16
|
* // Subscribe to an event
|
|
17
|
-
* const
|
|
17
|
+
* const subscription = eventBus.subscribe(UserRegistered, (payload) => {
|
|
18
18
|
* console.log(`User registered: ${payload.email}`);
|
|
19
19
|
* });
|
|
20
|
+
* await subscription.ready;
|
|
20
21
|
*
|
|
21
22
|
* // Publish an event
|
|
22
23
|
* await eventBus.publish(UserRegistered, { userId: "123", email: "test@example.com" });
|
|
23
24
|
*
|
|
24
25
|
* // Unsubscribe when done
|
|
25
|
-
* unsubscribe();
|
|
26
|
+
* await subscription.unsubscribe();
|
|
26
27
|
* ```
|
|
27
28
|
*/
|
|
28
29
|
|
|
@@ -110,11 +111,13 @@ export interface MemoryEventBusProviderPorts {
|
|
|
110
111
|
* ```ts
|
|
111
112
|
* const eventBus = createMemoryEventBus();
|
|
112
113
|
*
|
|
113
|
-
* eventBus.subscribe(UserRegistered, (payload) => {
|
|
114
|
+
* const subscription = eventBus.subscribe(UserRegistered, (payload) => {
|
|
114
115
|
* console.log(`User registered: ${payload.email}`);
|
|
115
116
|
* });
|
|
117
|
+
* await subscription.ready;
|
|
116
118
|
*
|
|
117
119
|
* await eventBus.publish(UserRegistered, { userId: "123", email: "test@example.com" });
|
|
120
|
+
* await subscription.unsubscribe();
|
|
118
121
|
* ```
|
|
119
122
|
*
|
|
120
123
|
* @example
|
|
@@ -216,16 +219,22 @@ export function createMemoryEventBus(
|
|
|
216
219
|
) => void | Promise<void>,
|
|
217
220
|
);
|
|
218
221
|
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
222
|
+
let unsubscribed = false;
|
|
223
|
+
return {
|
|
224
|
+
ready: Promise.resolve(),
|
|
225
|
+
async unsubscribe() {
|
|
226
|
+
if (unsubscribed) return;
|
|
227
|
+
unsubscribed = true;
|
|
228
|
+
subs.delete(
|
|
229
|
+
handler as unknown as (
|
|
230
|
+
payload: unknown,
|
|
231
|
+
options?: EventPublishOptions,
|
|
232
|
+
) => void | Promise<void>,
|
|
233
|
+
);
|
|
234
|
+
if (subs.size === 0) {
|
|
235
|
+
handlers.delete(event.name);
|
|
236
|
+
}
|
|
237
|
+
},
|
|
229
238
|
};
|
|
230
239
|
},
|
|
231
240
|
};
|