@kuindji/reactive 1.0.24 → 1.2.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 +160 -14
- package/dist/action.d.ts +31 -10
- package/dist/action.js +156 -23
- package/dist/actionBus.d.ts +13 -4
- package/dist/actionBus.js +201 -5
- package/dist/actionMap.d.ts +26 -19
- package/dist/actionMap.js +10 -4
- package/dist/event.d.ts +37 -3
- package/dist/event.js +345 -78
- package/dist/eventBus.d.ts +7 -3
- package/dist/eventBus.js +194 -34
- package/dist/index.d.ts +7 -7
- package/dist/index.js +7 -7
- package/dist/lib/actionMapInternal.d.ts +8 -0
- package/dist/lib/actionMapInternal.js +8 -0
- package/dist/lib/isPromiseLike.d.ts +1 -0
- package/dist/lib/isPromiseLike.js +5 -0
- package/dist/lib/normalizeEventOptions.d.ts +13 -0
- package/dist/lib/normalizeEventOptions.js +21 -0
- package/dist/lib/types.d.ts +1 -1
- package/dist/react/ErrorBoundary.d.ts +1 -1
- package/dist/react/listenerOptionsEqual.d.ts +27 -0
- package/dist/react/listenerOptionsEqual.js +121 -0
- package/dist/react/useAction.d.ts +3 -3
- package/dist/react/useAction.js +10 -7
- package/dist/react/useActionBus.d.ts +4 -4
- package/dist/react/useActionBus.js +32 -2
- package/dist/react/useActionBusStatus.d.ts +13 -0
- package/dist/react/useActionBusStatus.js +26 -0
- package/dist/react/useActionMap.d.ts +4 -4
- package/dist/react/useActionMap.js +40 -7
- package/dist/react/useAsyncAction.d.ts +20 -0
- package/dist/react/useAsyncAction.js +53 -0
- package/dist/react/useEvent.d.ts +2 -2
- package/dist/react/useEvent.js +18 -2
- package/dist/react/useEventBus.d.ts +2 -2
- package/dist/react/useEventBus.js +14 -10
- package/dist/react/useListenToAction.d.ts +1 -1
- package/dist/react/useListenToAction.js +17 -38
- package/dist/react/useListenToActionBus.d.ts +3 -3
- package/dist/react/useListenToActionBus.js +15 -9
- package/dist/react/useListenToEvent.d.ts +2 -2
- package/dist/react/useListenToEvent.js +8 -6
- package/dist/react/useListenToEventBus.d.ts +3 -3
- package/dist/react/useListenToEventBus.js +9 -7
- package/dist/react/useListenToStoreChanges.d.ts +3 -3
- package/dist/react/useListenToStoreChanges.js +9 -7
- package/dist/react/useReconciledListener.d.ts +33 -0
- package/dist/react/useReconciledListener.js +44 -0
- package/dist/react/useStore.d.ts +2 -2
- package/dist/react/useStore.js +71 -19
- package/dist/react/useStoreSelector.d.ts +35 -0
- package/dist/react/useStoreSelector.js +144 -0
- package/dist/react/useStoreState.d.ts +2 -2
- package/dist/react/useStoreState.js +26 -21
- package/dist/react.d.ts +16 -13
- package/dist/react.js +16 -13
- package/dist/store.d.ts +12 -8
- package/dist/store.js +473 -39
- package/package.json +13 -3
package/dist/eventBus.d.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import { createEvent } from "./event";
|
|
2
|
-
import type { EventDefinitionHelper, EventOptions, ListenerOptions } from "./event";
|
|
3
|
-
import { ApiType, BaseHandler, ErrorListenerSignature, KeyOf, MapKey, ProxyType, TriggerReturnType } from "./lib/types";
|
|
1
|
+
import { createEvent } from "./event.js";
|
|
2
|
+
import type { EventDefinitionHelper, EventOptions, ListenerOptions } from "./event.js";
|
|
3
|
+
import { ApiType, BaseHandler, ErrorListenerSignature, KeyOf, MapKey, ProxyType, TriggerReturnType } from "./lib/types.js";
|
|
4
4
|
type InterceptorFunction = (name: MapKey, args: any[], tags: string[] | null, returnType: TriggerReturnType | null) => boolean;
|
|
5
5
|
type RelaySource = {
|
|
6
6
|
on: (name: any, fn: (...args: any[]) => any, options?: ListenerOptions) => void;
|
|
@@ -59,6 +59,7 @@ export declare function createEventBus<EventsMap extends BaseEventMap = DefaultE
|
|
|
59
59
|
readonly remove: <K extends KeyOf<GetEventsMap<EventsMap>>, H extends GetEventsMap<EventsMap>[K]["signature"]>(name: K, handler: H, context?: object | null, tag?: string | null) => void;
|
|
60
60
|
/** @alias removeListener */
|
|
61
61
|
readonly unsubscribe: <K extends KeyOf<GetEventsMap<EventsMap>>, H extends GetEventsMap<EventsMap>[K]["signature"]>(name: K, handler: H, context?: object | null, tag?: string | null) => void;
|
|
62
|
+
readonly updateListenerOptions: <K extends KeyOf<GetEventsMap<EventsMap>>, H extends GetEventsMap<EventsMap>[K]["signature"]>(name: K, handler: H, context?: object | null, nextOptions?: ListenerOptions) => boolean;
|
|
62
63
|
readonly trigger: <K extends KeyOf<GetEventsMap<EventsMap>>, A extends GetEventsMap<EventsMap>[K]["arguments"]>(name: K, ...args: A) => void;
|
|
63
64
|
/** @alias trigger */
|
|
64
65
|
readonly emit: <K extends KeyOf<GetEventsMap<EventsMap>>, A extends GetEventsMap<EventsMap>[K]["arguments"]>(name: K, ...args: A) => void;
|
|
@@ -66,6 +67,7 @@ export declare function createEventBus<EventsMap extends BaseEventMap = DefaultE
|
|
|
66
67
|
readonly dispatch: <K extends KeyOf<GetEventsMap<EventsMap>>, A extends GetEventsMap<EventsMap>[K]["arguments"]>(name: K, ...args: A) => void;
|
|
67
68
|
readonly get: <K extends KeyOf<GetEventsMap<EventsMap>>>(name: K) => GetEventTypesMap<EventsMap>[K];
|
|
68
69
|
readonly add: (name: MapKey, options?: EventOptions<BaseHandler>) => void;
|
|
70
|
+
readonly setOptions: (options?: EventBusOptions<BaseEventMap>) => void;
|
|
69
71
|
readonly first: <K extends KeyOf<GetEventsMap<EventsMap>>, A extends GetEventsMap<EventsMap>[K]["arguments"]>(name: K, ...args: A) => ReturnType<GetEventTypesMap<EventsMap>[K]["first"]>;
|
|
70
72
|
readonly resolveFirst: <K extends KeyOf<GetEventsMap<EventsMap>>, A extends GetEventsMap<EventsMap>[K]["arguments"]>(name: K, ...args: A) => ReturnType<GetEventTypesMap<EventsMap>[K]["resolveFirst"]>;
|
|
71
73
|
readonly all: <K extends KeyOf<GetEventsMap<EventsMap>>, A extends GetEventsMap<EventsMap>[K]["arguments"]>(name: K, ...args: A) => ReturnType<GetEventTypesMap<EventsMap>[K]["all"]>;
|
|
@@ -89,6 +91,8 @@ export declare function createEventBus<EventsMap extends BaseEventMap = DefaultE
|
|
|
89
91
|
readonly stopIntercepting: () => void;
|
|
90
92
|
readonly isIntercepting: () => boolean;
|
|
91
93
|
readonly reset: () => void;
|
|
94
|
+
readonly destroy: () => void;
|
|
95
|
+
readonly isDestroyed: () => boolean;
|
|
92
96
|
readonly suspendAll: (withQueue?: boolean) => void;
|
|
93
97
|
readonly resumeAll: () => void;
|
|
94
98
|
readonly relay: ({ eventSource, remoteEventName, localEventName, proxyType, localEventNamePrefix, }: {
|
package/dist/eventBus.js
CHANGED
|
@@ -1,5 +1,7 @@
|
|
|
1
|
-
import { createEvent } from "./event";
|
|
2
|
-
import
|
|
1
|
+
import { createEvent } from "./event.js";
|
|
2
|
+
import isPromiseLike from "./lib/isPromiseLike.js";
|
|
3
|
+
import { normalizeEventOptions } from "./lib/normalizeEventOptions.js";
|
|
4
|
+
import { ProxyType, TriggerReturnType, } from "./lib/types.js";
|
|
3
5
|
function proxyReturnTypeToTriggerReturnType(proxyType) {
|
|
4
6
|
switch (proxyType) {
|
|
5
7
|
case ProxyType.TRIGGER:
|
|
@@ -71,10 +73,15 @@ function proxyReturnTypeToTriggerReturnType(proxyType) {
|
|
|
71
73
|
}
|
|
72
74
|
export function createEventBus(eventBusOptions) {
|
|
73
75
|
const events = new Map();
|
|
76
|
+
let currentEventBusOptions = eventBusOptions;
|
|
74
77
|
let currentTagsFilter = null;
|
|
75
78
|
let interceptor = null;
|
|
76
79
|
const proxyListeners = [];
|
|
77
80
|
const eventSources = [];
|
|
81
|
+
let destroyed = false;
|
|
82
|
+
// Registry of active relays so destroy() can unwind the external listeners
|
|
83
|
+
// they attach (reset()/destroy() otherwise leave them dangling).
|
|
84
|
+
const relays = [];
|
|
78
85
|
const asterisk = createEvent();
|
|
79
86
|
const errorEvent = createEvent();
|
|
80
87
|
const _getProxyListener = ({ remoteEventName, localEventName, returnType, resolve, localEventNamePrefix, }) => {
|
|
@@ -128,6 +135,9 @@ export function createEventBus(eventBusOptions) {
|
|
|
128
135
|
return listener;
|
|
129
136
|
};
|
|
130
137
|
const add = (name, options) => {
|
|
138
|
+
if (destroyed) {
|
|
139
|
+
throw new Error("EventBus is destroyed");
|
|
140
|
+
}
|
|
131
141
|
if (!events.has(name)) {
|
|
132
142
|
events.set(name, createEvent(options));
|
|
133
143
|
}
|
|
@@ -135,10 +145,31 @@ export function createEventBus(eventBusOptions) {
|
|
|
135
145
|
const _getOrAddEvent = (name) => {
|
|
136
146
|
var _a;
|
|
137
147
|
if (!events.has(name)) {
|
|
138
|
-
events.set(name, createEvent((_a =
|
|
148
|
+
events.set(name, createEvent((_a = currentEventBusOptions === null || currentEventBusOptions === void 0 ? void 0 : currentEventBusOptions.eventOptions) === null || _a === void 0 ? void 0 : _a[name]));
|
|
139
149
|
}
|
|
140
150
|
return events.get(name);
|
|
141
151
|
};
|
|
152
|
+
// Parameter is intentionally map-independent (like `add`) so that the
|
|
153
|
+
// method type is identical across instantiations and a typed bus stays
|
|
154
|
+
// assignable to BaseEventBus (which the React listener hooks rely on).
|
|
155
|
+
const setOptions = (options) => {
|
|
156
|
+
currentEventBusOptions = options;
|
|
157
|
+
const eventOptions = options === null || options === void 0 ? void 0 : options.eventOptions;
|
|
158
|
+
if (!eventOptions) {
|
|
159
|
+
return;
|
|
160
|
+
}
|
|
161
|
+
// Apply present entries to already-created events. Removed entries are
|
|
162
|
+
// intentionally left as-is (the bus can change an event's options but
|
|
163
|
+
// does not un-set them).
|
|
164
|
+
Object.keys(eventOptions).forEach((name) => {
|
|
165
|
+
const e = events.get(name);
|
|
166
|
+
if (e) {
|
|
167
|
+
// Normalize so fields removed from a present entry reset to
|
|
168
|
+
// their defaults (event.setOptions merges, it does not reset).
|
|
169
|
+
e.setOptions(normalizeEventOptions(eventOptions[name]));
|
|
170
|
+
}
|
|
171
|
+
});
|
|
172
|
+
};
|
|
142
173
|
const intercept = (fn) => {
|
|
143
174
|
interceptor = fn;
|
|
144
175
|
};
|
|
@@ -149,9 +180,15 @@ export function createEventBus(eventBusOptions) {
|
|
|
149
180
|
return interceptor !== null;
|
|
150
181
|
};
|
|
151
182
|
const get = (name) => {
|
|
183
|
+
if (destroyed) {
|
|
184
|
+
throw new Error("EventBus is destroyed");
|
|
185
|
+
}
|
|
152
186
|
return _getOrAddEvent(name);
|
|
153
187
|
};
|
|
154
188
|
const on = (name, handler, options) => {
|
|
189
|
+
if (destroyed) {
|
|
190
|
+
throw new Error("EventBus is destroyed");
|
|
191
|
+
}
|
|
155
192
|
const e = _getOrAddEvent(name);
|
|
156
193
|
eventSources.forEach((evs) => {
|
|
157
194
|
if (evs.eventSource.accepts === false
|
|
@@ -168,18 +205,19 @@ export function createEventBus(eventBusOptions) {
|
|
|
168
205
|
resolve,
|
|
169
206
|
localEventNamePrefix: null,
|
|
170
207
|
});
|
|
171
|
-
evs.eventSource.on(name, listener.listener, evs.eventSource
|
|
208
|
+
evs.eventSource.on(name, listener.listener, evs.eventSource);
|
|
172
209
|
evs.subscribed.push(name);
|
|
173
210
|
}
|
|
174
211
|
});
|
|
175
212
|
return e.addListener(handler, options);
|
|
176
213
|
};
|
|
177
214
|
const once = (name, handler, options) => {
|
|
178
|
-
|
|
179
|
-
options.limit = 1;
|
|
180
|
-
return on(name, handler, options);
|
|
215
|
+
return on(name, handler, Object.assign(Object.assign({}, (options || {})), { limit: 1 }));
|
|
181
216
|
};
|
|
182
217
|
const promise = (name, options) => {
|
|
218
|
+
if (destroyed) {
|
|
219
|
+
throw new Error("EventBus is destroyed");
|
|
220
|
+
}
|
|
183
221
|
const e = _getOrAddEvent(name);
|
|
184
222
|
return e.promise(options);
|
|
185
223
|
};
|
|
@@ -192,24 +230,32 @@ export function createEventBus(eventBusOptions) {
|
|
|
192
230
|
const isEmpty = !e.hasListener();
|
|
193
231
|
eventSources.forEach((evs) => {
|
|
194
232
|
const inx = evs.subscribed.indexOf(name);
|
|
195
|
-
if (inx !== -1) {
|
|
233
|
+
if (inx !== -1 && isEmpty) {
|
|
196
234
|
evs.subscribed.splice(inx, 1);
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
evs.eventSource.un(name, listener.listener, evs.eventSource, tag);
|
|
207
|
-
}
|
|
235
|
+
const { returnType, resolve } = proxyReturnTypeToTriggerReturnType(evs.eventSource.proxyType || ProxyType.TRIGGER);
|
|
236
|
+
const listener = _getProxyListener({
|
|
237
|
+
localEventName: null,
|
|
238
|
+
remoteEventName: name,
|
|
239
|
+
returnType,
|
|
240
|
+
resolve,
|
|
241
|
+
localEventNamePrefix: null,
|
|
242
|
+
});
|
|
243
|
+
evs.eventSource.un(name, listener.listener, evs.eventSource, tag);
|
|
208
244
|
}
|
|
209
245
|
});
|
|
210
246
|
}
|
|
211
247
|
};
|
|
248
|
+
const updateListenerOptions = (name, handler, context, nextOptions) => {
|
|
249
|
+
const e = events.get(name);
|
|
250
|
+
if (!e) {
|
|
251
|
+
return false;
|
|
252
|
+
}
|
|
253
|
+
return e.updateListenerOptions(handler, context !== null && context !== void 0 ? context : null, nextOptions);
|
|
254
|
+
};
|
|
212
255
|
const _trigger = (name, args, returnType, resolve) => {
|
|
256
|
+
if (destroyed) {
|
|
257
|
+
throw new Error("EventBus is destroyed");
|
|
258
|
+
}
|
|
213
259
|
if (name === "*") {
|
|
214
260
|
return;
|
|
215
261
|
}
|
|
@@ -220,6 +266,20 @@ export function createEventBus(eventBusOptions) {
|
|
|
220
266
|
}
|
|
221
267
|
}
|
|
222
268
|
const e = _getOrAddEvent(name);
|
|
269
|
+
const handleError = (error) => {
|
|
270
|
+
errorEvent.trigger({
|
|
271
|
+
name,
|
|
272
|
+
error: error instanceof Error
|
|
273
|
+
? error
|
|
274
|
+
: new Error(String(error)),
|
|
275
|
+
args,
|
|
276
|
+
type: "event",
|
|
277
|
+
});
|
|
278
|
+
if (errorEvent.hasListener()) {
|
|
279
|
+
return undefined;
|
|
280
|
+
}
|
|
281
|
+
throw error;
|
|
282
|
+
};
|
|
223
283
|
const runner = () => {
|
|
224
284
|
let result;
|
|
225
285
|
switch (returnType) {
|
|
@@ -276,21 +336,13 @@ export function createEventBus(eventBusOptions) {
|
|
|
276
336
|
result = runner();
|
|
277
337
|
}
|
|
278
338
|
asterisk.trigger(name, args, currentTagsFilter);
|
|
339
|
+
if (isPromiseLike(result)) {
|
|
340
|
+
return Promise.resolve(result).catch(handleError);
|
|
341
|
+
}
|
|
279
342
|
return result;
|
|
280
343
|
}
|
|
281
344
|
catch (error) {
|
|
282
|
-
|
|
283
|
-
name,
|
|
284
|
-
error: error instanceof Error
|
|
285
|
-
? error
|
|
286
|
-
: new Error(String(error)),
|
|
287
|
-
args,
|
|
288
|
-
type: "event",
|
|
289
|
-
});
|
|
290
|
-
if (errorEvent.hasListener()) {
|
|
291
|
-
return undefined;
|
|
292
|
-
}
|
|
293
|
-
throw error;
|
|
345
|
+
return handleError(error);
|
|
294
346
|
}
|
|
295
347
|
};
|
|
296
348
|
const trigger = (name, ...args) => {
|
|
@@ -348,27 +400,80 @@ export function createEventBus(eventBusOptions) {
|
|
|
348
400
|
return _trigger(name, args, TriggerReturnType.RAW, false);
|
|
349
401
|
};
|
|
350
402
|
const withTags = (tags, callback) => {
|
|
403
|
+
const prevTagsFilter = currentTagsFilter;
|
|
351
404
|
currentTagsFilter = tags;
|
|
352
405
|
try {
|
|
353
406
|
return callback();
|
|
354
407
|
}
|
|
355
408
|
finally {
|
|
356
|
-
currentTagsFilter =
|
|
409
|
+
currentTagsFilter = prevTagsFilter;
|
|
357
410
|
}
|
|
358
411
|
};
|
|
359
412
|
const reset = () => {
|
|
413
|
+
// Detach relays BEFORE clearing proxyListeners: unrelay() resolves the
|
|
414
|
+
// external listener via _getProxyListener, which depends on the original
|
|
415
|
+
// entry still being present. Clearing proxyListeners first would lose the
|
|
416
|
+
// callback identity, leaving the external subscription dangling so a
|
|
417
|
+
// later destroy() could never remove it.
|
|
418
|
+
relays.slice().forEach((r) => {
|
|
419
|
+
unrelay({
|
|
420
|
+
eventSource: r.eventSource,
|
|
421
|
+
remoteEventName: r.remoteEventName,
|
|
422
|
+
localEventName: r.localEventName,
|
|
423
|
+
proxyType: r.proxyType,
|
|
424
|
+
localEventNamePrefix: r.localEventNamePrefix,
|
|
425
|
+
});
|
|
426
|
+
});
|
|
360
427
|
if (eventSources.length > 0) {
|
|
361
|
-
eventSources.forEach((evs) => {
|
|
428
|
+
eventSources.slice().forEach((evs) => {
|
|
362
429
|
removeEventSource(evs.eventSource);
|
|
363
430
|
});
|
|
364
431
|
}
|
|
432
|
+
// Reset each owned event before dropping it: clearing the map alone
|
|
433
|
+
// leaves listener AbortSignal handlers attached to their signals, which
|
|
434
|
+
// retains the orphaned events (and their listeners) until the signal
|
|
435
|
+
// aborts. reset() detaches those handlers and clears the listeners.
|
|
436
|
+
events.forEach((event) => {
|
|
437
|
+
event.reset();
|
|
438
|
+
});
|
|
365
439
|
events.clear();
|
|
366
440
|
interceptor = null;
|
|
367
441
|
currentTagsFilter = null;
|
|
368
442
|
asterisk.reset();
|
|
369
443
|
proxyListeners.length = 0;
|
|
370
444
|
eventSources.length = 0;
|
|
445
|
+
relays.length = 0;
|
|
446
|
+
};
|
|
447
|
+
// One-call teardown: unwind external attachments (relays + event sources)
|
|
448
|
+
// that reset() leaves dangling, destroy every owned event, and mark the bus
|
|
449
|
+
// dead. Post-destroy trigger/addListener throw rather than silently no-op.
|
|
450
|
+
const destroy = () => {
|
|
451
|
+
relays.slice().forEach((r) => {
|
|
452
|
+
unrelay({
|
|
453
|
+
eventSource: r.eventSource,
|
|
454
|
+
remoteEventName: r.remoteEventName,
|
|
455
|
+
localEventName: r.localEventName,
|
|
456
|
+
proxyType: r.proxyType,
|
|
457
|
+
localEventNamePrefix: r.localEventNamePrefix,
|
|
458
|
+
});
|
|
459
|
+
});
|
|
460
|
+
eventSources.slice().forEach((evs) => {
|
|
461
|
+
removeEventSource(evs.eventSource);
|
|
462
|
+
});
|
|
463
|
+
events.forEach((event) => {
|
|
464
|
+
event.destroy();
|
|
465
|
+
});
|
|
466
|
+
events.clear();
|
|
467
|
+
asterisk.destroy();
|
|
468
|
+
errorEvent.destroy();
|
|
469
|
+
proxyListeners.length = 0;
|
|
470
|
+
eventSources.length = 0;
|
|
471
|
+
relays.length = 0;
|
|
472
|
+
interceptor = null;
|
|
473
|
+
currentTagsFilter = null;
|
|
474
|
+
destroyed = true;
|
|
371
475
|
};
|
|
476
|
+
const isDestroyed = () => destroyed;
|
|
372
477
|
const suspendAll = (withQueue = false) => {
|
|
373
478
|
events.forEach((event) => {
|
|
374
479
|
event.suspend(withQueue);
|
|
@@ -380,6 +485,12 @@ export function createEventBus(eventBusOptions) {
|
|
|
380
485
|
});
|
|
381
486
|
};
|
|
382
487
|
const relay = ({ eventSource, remoteEventName, localEventName, proxyType, localEventNamePrefix, }) => {
|
|
488
|
+
// Like the other registration methods, refuse on a dead bus: attaching
|
|
489
|
+
// the proxy listener here would leave a dangling subscription that
|
|
490
|
+
// throws "EventBus is destroyed" the next time the source fires.
|
|
491
|
+
if (destroyed) {
|
|
492
|
+
throw new Error("EventBus is destroyed");
|
|
493
|
+
}
|
|
383
494
|
const { returnType, resolve } = proxyReturnTypeToTriggerReturnType(proxyType || ProxyType.TRIGGER);
|
|
384
495
|
const listener = _getProxyListener({
|
|
385
496
|
localEventName: localEventName || null,
|
|
@@ -394,6 +505,18 @@ export function createEventBus(eventBusOptions) {
|
|
|
394
505
|
else {
|
|
395
506
|
eventSource.on(remoteEventName, listener.listener);
|
|
396
507
|
}
|
|
508
|
+
relays.push({
|
|
509
|
+
eventSource,
|
|
510
|
+
remoteEventName,
|
|
511
|
+
localEventName: localEventName || null,
|
|
512
|
+
// Store the resolved proxyType (undefined resolves to TRIGGER) so the
|
|
513
|
+
// registry key matches how the proxy listener is actually resolved.
|
|
514
|
+
// Otherwise unrelay({ proxyType: TRIGGER }) for a relay({}) (undefined)
|
|
515
|
+
// detaches the listener but leaves a stale registry entry, which a
|
|
516
|
+
// later reset()/destroy() then unrelays a second time.
|
|
517
|
+
proxyType: proxyType || ProxyType.TRIGGER,
|
|
518
|
+
localEventNamePrefix: localEventNamePrefix || null,
|
|
519
|
+
});
|
|
397
520
|
};
|
|
398
521
|
const unrelay = ({ eventSource, remoteEventName, localEventName, proxyType, localEventNamePrefix, }) => {
|
|
399
522
|
const { returnType, resolve } = proxyReturnTypeToTriggerReturnType(proxyType || ProxyType.TRIGGER);
|
|
@@ -412,14 +535,47 @@ export function createEventBus(eventBusOptions) {
|
|
|
412
535
|
eventSource.un(remoteEventName, listener.listener);
|
|
413
536
|
}
|
|
414
537
|
}
|
|
538
|
+
const inx = relays.findIndex((r) => r.eventSource === eventSource
|
|
539
|
+
&& r.remoteEventName === remoteEventName
|
|
540
|
+
&& r.localEventName === (localEventName || null)
|
|
541
|
+
// Compare on the resolved proxyType so an undefined relay matches an
|
|
542
|
+
// explicit TRIGGER unrelay (and vice versa), mirroring how the proxy
|
|
543
|
+
// listener itself resolves equivalent types.
|
|
544
|
+
&& r.proxyType === (proxyType || ProxyType.TRIGGER)
|
|
545
|
+
&& r.localEventNamePrefix === (localEventNamePrefix || null));
|
|
546
|
+
if (inx !== -1) {
|
|
547
|
+
relays.splice(inx, 1);
|
|
548
|
+
}
|
|
415
549
|
};
|
|
416
550
|
const addEventSource = (eventSource) => {
|
|
551
|
+
if (destroyed) {
|
|
552
|
+
throw new Error("EventBus is destroyed");
|
|
553
|
+
}
|
|
417
554
|
if (eventSources.find((evs) => evs.eventSource.name === eventSource.name)) {
|
|
418
555
|
return;
|
|
419
556
|
}
|
|
420
|
-
|
|
557
|
+
const eventSourceData = {
|
|
421
558
|
eventSource,
|
|
422
559
|
subscribed: [],
|
|
560
|
+
};
|
|
561
|
+
eventSources.push(eventSourceData);
|
|
562
|
+
events.forEach((event, name) => {
|
|
563
|
+
if (!event.hasListener()
|
|
564
|
+
|| eventSource.accepts === false
|
|
565
|
+
|| (typeof eventSource.accepts === "function"
|
|
566
|
+
&& !eventSource.accepts(name))) {
|
|
567
|
+
return;
|
|
568
|
+
}
|
|
569
|
+
const { returnType, resolve } = proxyReturnTypeToTriggerReturnType(eventSource.proxyType || ProxyType.TRIGGER);
|
|
570
|
+
const listener = _getProxyListener({
|
|
571
|
+
localEventName: null,
|
|
572
|
+
remoteEventName: name,
|
|
573
|
+
returnType,
|
|
574
|
+
resolve,
|
|
575
|
+
localEventNamePrefix: null,
|
|
576
|
+
});
|
|
577
|
+
eventSource.on(name, listener.listener, eventSource);
|
|
578
|
+
eventSourceData.subscribed.push(name);
|
|
423
579
|
});
|
|
424
580
|
};
|
|
425
581
|
const removeEventSource = (eventSource) => {
|
|
@@ -462,6 +618,7 @@ export function createEventBus(eventBusOptions) {
|
|
|
462
618
|
remove: un,
|
|
463
619
|
/** @alias removeListener */
|
|
464
620
|
unsubscribe: un,
|
|
621
|
+
updateListenerOptions,
|
|
465
622
|
trigger,
|
|
466
623
|
/** @alias trigger */
|
|
467
624
|
emit: trigger,
|
|
@@ -469,6 +626,7 @@ export function createEventBus(eventBusOptions) {
|
|
|
469
626
|
dispatch: trigger,
|
|
470
627
|
get,
|
|
471
628
|
add,
|
|
629
|
+
setOptions,
|
|
472
630
|
first,
|
|
473
631
|
resolveFirst,
|
|
474
632
|
all,
|
|
@@ -492,6 +650,8 @@ export function createEventBus(eventBusOptions) {
|
|
|
492
650
|
stopIntercepting,
|
|
493
651
|
isIntercepting,
|
|
494
652
|
reset,
|
|
653
|
+
destroy,
|
|
654
|
+
isDestroyed,
|
|
495
655
|
suspendAll,
|
|
496
656
|
resumeAll,
|
|
497
657
|
relay,
|
package/dist/index.d.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
export * from "./action";
|
|
2
|
-
export * from "./actionBus";
|
|
3
|
-
export * from "./actionMap";
|
|
4
|
-
export * from "./event";
|
|
5
|
-
export * from "./eventBus";
|
|
6
|
-
export * from "./lib/types";
|
|
7
|
-
export * from "./store";
|
|
1
|
+
export * from "./action.js";
|
|
2
|
+
export * from "./actionBus.js";
|
|
3
|
+
export * from "./actionMap.js";
|
|
4
|
+
export * from "./event.js";
|
|
5
|
+
export * from "./eventBus.js";
|
|
6
|
+
export * from "./lib/types.js";
|
|
7
|
+
export * from "./store.js";
|
package/dist/index.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
export * from "./action";
|
|
2
|
-
export * from "./actionBus";
|
|
3
|
-
export * from "./actionMap";
|
|
4
|
-
export * from "./event";
|
|
5
|
-
export * from "./eventBus";
|
|
6
|
-
export * from "./lib/types";
|
|
7
|
-
export * from "./store";
|
|
1
|
+
export * from "./action.js";
|
|
2
|
+
export * from "./actionBus.js";
|
|
3
|
+
export * from "./actionMap.js";
|
|
4
|
+
export * from "./event.js";
|
|
5
|
+
export * from "./eventBus.js";
|
|
6
|
+
export * from "./lib/types.js";
|
|
7
|
+
export * from "./store.js";
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Internal-only symbol used to expose an in-place setter for an action map's
|
|
3
|
+
* forwarded error listeners. Lives under src/lib (which is not re-exported by
|
|
4
|
+
* the package root in index.ts) so it does not become part of the public API
|
|
5
|
+
* surface. Imported by createActionMap (to attach the setter) and by
|
|
6
|
+
* useActionMap (to call it during reconciliation).
|
|
7
|
+
*/
|
|
8
|
+
export declare const ActionMapSetErrorListeners: unique symbol;
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Internal-only symbol used to expose an in-place setter for an action map's
|
|
3
|
+
* forwarded error listeners. Lives under src/lib (which is not re-exported by
|
|
4
|
+
* the package root in index.ts) so it does not become part of the public API
|
|
5
|
+
* surface. Imported by createActionMap (to attach the setter) and by
|
|
6
|
+
* useActionMap (to call it during reconciliation).
|
|
7
|
+
*/
|
|
8
|
+
export const ActionMapSetErrorListeners = Symbol("actionMapSetErrorListeners");
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export default function isPromiseLike<T = unknown>(value: unknown): value is PromiseLike<T>;
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import type { EventOptions } from "../event.js";
|
|
2
|
+
import type { BaseHandler } from "./types.js";
|
|
3
|
+
/**
|
|
4
|
+
* Produces a complete {@link EventOptions} object with every field set to the
|
|
5
|
+
* provided value or its createEvent default. Used by the reconciliation paths
|
|
6
|
+
* (useEvent and eventBus.setOptions) so that applying options via the
|
|
7
|
+
* merge-based event.setOptions also resets fields that were removed across
|
|
8
|
+
* renders, instead of leaving stale values from a previous render.
|
|
9
|
+
*
|
|
10
|
+
* Internal-only: this module lives under src/lib (not re-exported by the
|
|
11
|
+
* package root) and is not part of the public API surface.
|
|
12
|
+
*/
|
|
13
|
+
export declare function normalizeEventOptions(options?: EventOptions<BaseHandler> | null): EventOptions<BaseHandler>;
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Produces a complete {@link EventOptions} object with every field set to the
|
|
3
|
+
* provided value or its createEvent default. Used by the reconciliation paths
|
|
4
|
+
* (useEvent and eventBus.setOptions) so that applying options via the
|
|
5
|
+
* merge-based event.setOptions also resets fields that were removed across
|
|
6
|
+
* renders, instead of leaving stale values from a previous render.
|
|
7
|
+
*
|
|
8
|
+
* Internal-only: this module lives under src/lib (not re-exported by the
|
|
9
|
+
* package root) and is not part of the public API surface.
|
|
10
|
+
*/
|
|
11
|
+
export function normalizeEventOptions(options) {
|
|
12
|
+
var _a, _b, _c, _d, _e, _f;
|
|
13
|
+
return {
|
|
14
|
+
async: (_a = options === null || options === void 0 ? void 0 : options.async) !== null && _a !== void 0 ? _a : null,
|
|
15
|
+
limit: (_b = options === null || options === void 0 ? void 0 : options.limit) !== null && _b !== void 0 ? _b : null,
|
|
16
|
+
autoTrigger: (_c = options === null || options === void 0 ? void 0 : options.autoTrigger) !== null && _c !== void 0 ? _c : null,
|
|
17
|
+
filter: (_d = options === null || options === void 0 ? void 0 : options.filter) !== null && _d !== void 0 ? _d : null,
|
|
18
|
+
filterContext: (_e = options === null || options === void 0 ? void 0 : options.filterContext) !== null && _e !== void 0 ? _e : null,
|
|
19
|
+
maxListeners: (_f = options === null || options === void 0 ? void 0 : options.maxListeners) !== null && _f !== void 0 ? _f : 0,
|
|
20
|
+
};
|
|
21
|
+
}
|
package/dist/lib/types.d.ts
CHANGED
|
@@ -44,6 +44,6 @@ export type ErrorResponse<Arguments extends any[] = any[]> = {
|
|
|
44
44
|
error: Error;
|
|
45
45
|
args: Arguments;
|
|
46
46
|
name?: MapKey;
|
|
47
|
-
type: "action" | "event" | "store-change" | "store-pipe" | "store-control";
|
|
47
|
+
type: "action" | "action-status" | "event" | "store-change" | "store-pipe" | "store-control";
|
|
48
48
|
};
|
|
49
49
|
export type ErrorListenerSignature<Arguments extends any[] = any[]> = (errorResponse: ErrorResponse<Arguments>) => void;
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import type { EventOptions, ListenerOptions } from "../event.js";
|
|
2
|
+
import type { BaseEventMap, EventBusOptions } from "../eventBus.js";
|
|
3
|
+
import type { BaseHandler } from "../lib/types.js";
|
|
4
|
+
/**
|
|
5
|
+
* Order-insensitive set comparison for listener tags.
|
|
6
|
+
* `undefined`, `[]` and missing all compare equal. Order and duplicates do
|
|
7
|
+
* not matter because the core only ever uses tags via membership and
|
|
8
|
+
* intersection checks.
|
|
9
|
+
*/
|
|
10
|
+
export declare function areTagsEqual(a?: string[], b?: string[]): boolean;
|
|
11
|
+
/**
|
|
12
|
+
* Domain-specific comparator for {@link ListenerOptions}. Avoids generic deep
|
|
13
|
+
* equality: primitives compare after default semantics, `context`/`extraData`
|
|
14
|
+
* compare by reference, and `tags` use order-insensitive set comparison.
|
|
15
|
+
*/
|
|
16
|
+
export declare function areListenerOptionsEqual(a?: ListenerOptions | null, b?: ListenerOptions | null): boolean;
|
|
17
|
+
/**
|
|
18
|
+
* Domain-specific comparator for {@link EventOptions}. Primitives compare after
|
|
19
|
+
* default semantics; `filter`/`filterContext` compare by reference.
|
|
20
|
+
*/
|
|
21
|
+
export declare function areEventOptionsEqual(a?: EventOptions<BaseHandler> | null, b?: EventOptions<BaseHandler> | null): boolean;
|
|
22
|
+
/**
|
|
23
|
+
* Compares the per-event `eventOptions` maps of two {@link EventBusOptions}.
|
|
24
|
+
* Equal when every event name present in either map has semantically equal
|
|
25
|
+
* {@link EventOptions} (missing entries compare as default options).
|
|
26
|
+
*/
|
|
27
|
+
export declare function areEventBusOptionsEqual(a?: EventBusOptions<BaseEventMap> | null, b?: EventBusOptions<BaseEventMap> | null): boolean;
|