@asaidimu/utils-events 1.0.3 → 1.1.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 +24 -24
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -69,7 +69,7 @@ pnpm add @asaidimu/utils-events
|
|
|
69
69
|
## Quick Start
|
|
70
70
|
|
|
71
71
|
```typescript
|
|
72
|
-
import { createEventBus } from
|
|
72
|
+
import { createEventBus } from "@asaidimu/utils-events";
|
|
73
73
|
|
|
74
74
|
// 1. Define your event map
|
|
75
75
|
interface AppEvents {
|
|
@@ -82,12 +82,12 @@ interface AppEvents {
|
|
|
82
82
|
const bus = createEventBus<AppEvents>();
|
|
83
83
|
|
|
84
84
|
// 3. Subscribe
|
|
85
|
-
const unsubscribe = bus.subscribe(
|
|
85
|
+
const unsubscribe = bus.subscribe("userLogin", (payload) => {
|
|
86
86
|
console.log(`Welcome ${payload.name}!`);
|
|
87
87
|
});
|
|
88
88
|
|
|
89
89
|
// 4. Emit an event
|
|
90
|
-
bus.emit({ name:
|
|
90
|
+
bus.emit({ name: "userLogin", payload: { userId: "123", name: "Alice" } });
|
|
91
91
|
// Logs: "Welcome Alice!"
|
|
92
92
|
|
|
93
93
|
// 5. Unsubscribe when done
|
|
@@ -102,20 +102,20 @@ unsubscribe();
|
|
|
102
102
|
|
|
103
103
|
```typescript
|
|
104
104
|
function createEventBus<TEventMap extends Record<string, any>>(
|
|
105
|
-
options?: EventBusOptions
|
|
106
|
-
): EventBus<TEventMap
|
|
105
|
+
options?: EventBusOptions,
|
|
106
|
+
): EventBus<TEventMap>;
|
|
107
107
|
```
|
|
108
108
|
|
|
109
109
|
Creates a new event bus instance.
|
|
110
110
|
|
|
111
111
|
#### Options
|
|
112
112
|
|
|
113
|
-
| Option
|
|
114
|
-
|
|
|
115
|
-
| `batch.size`
|
|
116
|
-
| `batch.delay`
|
|
117
|
-
| `errorHandler`
|
|
118
|
-
| `broadcast.channel`
|
|
113
|
+
| Option | Type | Default | Description |
|
|
114
|
+
| ------------------- | ----------------------------- | --------------------- | ------------------------------------------------------------------------- |
|
|
115
|
+
| `batch.size` | `number` | `undefined` | Enables **deferred mode**; flush when queue reaches this size. |
|
|
116
|
+
| `batch.delay` | `number` | `1000` (if batching) | Quiet period (ms) before flushing a batch. |
|
|
117
|
+
| `errorHandler` | `(error: EventError) => void` | `console.error` | Custom error handler for subscriber callbacks. |
|
|
118
|
+
| `broadcast.channel` | `string` | `"event-bus-channel"` | Enables cross-instance broadcast using the given `BroadcastChannel` name. |
|
|
119
119
|
|
|
120
120
|
> If `batch.size` is provided, the bus runs in **deferred mode** (events are queued and flushed asynchronously). Otherwise it runs in **synchronous mode** (events are dispatched immediately).
|
|
121
121
|
|
|
@@ -133,7 +133,7 @@ subscribe<TEventName extends keyof TEventMap>(
|
|
|
133
133
|
Registers a permanent listener. Returns an **unsubscribe function**.
|
|
134
134
|
|
|
135
135
|
```typescript
|
|
136
|
-
const off = bus.subscribe(
|
|
136
|
+
const off = bus.subscribe("dataUpdate", ({ records }) => {
|
|
137
137
|
updateUI(records);
|
|
138
138
|
});
|
|
139
139
|
|
|
@@ -155,8 +155,8 @@ once<TEventName extends keyof TEventMap>(
|
|
|
155
155
|
Registers a one-time listener that automatically unsubscribes after the first emission. Returns a **cancel function** (to unsubscribe before it fires).
|
|
156
156
|
|
|
157
157
|
```typescript
|
|
158
|
-
bus.once(
|
|
159
|
-
console.log(
|
|
158
|
+
bus.once("userLogin", (payload) => {
|
|
159
|
+
console.log("First login only");
|
|
160
160
|
});
|
|
161
161
|
|
|
162
162
|
// The callback will run at most once.
|
|
@@ -178,7 +178,7 @@ emit<TEventName extends keyof TEventMap>(
|
|
|
178
178
|
Dispatches an event. In **synchronous mode** all subscribers run immediately. In **deferred mode** the event is queued and flushed according to `batch.size` and `batch.delay`. Cross-instance messages are sent **immediately** even in deferred mode to avoid latency.
|
|
179
179
|
|
|
180
180
|
```typescript
|
|
181
|
-
bus.emit({ name:
|
|
181
|
+
bus.emit({ name: "dataUpdate", payload: { records: 42 } });
|
|
182
182
|
```
|
|
183
183
|
|
|
184
184
|
---
|
|
@@ -226,9 +226,9 @@ When many events are fired in rapid succession (e.g., keystrokes, scroll handler
|
|
|
226
226
|
```typescript
|
|
227
227
|
const batchedBus = createEventBus<MyEvents>({
|
|
228
228
|
batch: {
|
|
229
|
-
size: 20,
|
|
230
|
-
delay: 100
|
|
231
|
-
}
|
|
229
|
+
size: 20, // flush after 20 queued events
|
|
230
|
+
delay: 100, // or after 100ms of inactivity
|
|
231
|
+
},
|
|
232
232
|
});
|
|
233
233
|
```
|
|
234
234
|
|
|
@@ -242,15 +242,15 @@ Enable the `broadcast` option to automatically send every emitted event to other
|
|
|
242
242
|
|
|
243
243
|
```typescript
|
|
244
244
|
const bus = createEventBus<MyEvents>({
|
|
245
|
-
broadcast: { channel:
|
|
245
|
+
broadcast: { channel: "my-app-events" },
|
|
246
246
|
});
|
|
247
247
|
|
|
248
248
|
// In tab A
|
|
249
|
-
bus.emit({ name:
|
|
249
|
+
bus.emit({ name: "userLogin", payload: { userId: "1" } });
|
|
250
250
|
|
|
251
251
|
// In tab B (same origin)
|
|
252
|
-
bus.subscribe(
|
|
253
|
-
console.log(
|
|
252
|
+
bus.subscribe("userLogin", (payload) => {
|
|
253
|
+
console.log("Another tab logged in:", payload.userId);
|
|
254
254
|
});
|
|
255
255
|
```
|
|
256
256
|
|
|
@@ -265,9 +265,9 @@ const bus = createEventBus<MyEvents>({
|
|
|
265
265
|
errorHandler: (err) => {
|
|
266
266
|
myErrorTracker.capture(err, {
|
|
267
267
|
eventName: err.eventName,
|
|
268
|
-
payload: err.payload
|
|
268
|
+
payload: err.payload,
|
|
269
269
|
});
|
|
270
|
-
}
|
|
270
|
+
},
|
|
271
271
|
});
|
|
272
272
|
```
|
|
273
273
|
|