@vielzeug/herald 1.0.6 → 2.0.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 +12 -40
- package/dist/bus.cjs +1 -1
- package/dist/bus.cjs.map +1 -1
- package/dist/bus.d.ts +3 -12
- package/dist/bus.d.ts.map +1 -1
- package/dist/bus.js +122 -89
- package/dist/bus.js.map +1 -1
- package/dist/devtools.cjs +1 -1
- package/dist/devtools.cjs.map +1 -1
- package/dist/devtools.d.ts +1 -22
- package/dist/devtools.d.ts.map +1 -1
- package/dist/devtools.js +2 -13
- package/dist/devtools.js.map +1 -1
- package/dist/herald.cjs +1 -1
- package/dist/herald.cjs.map +1 -1
- package/dist/herald.iife.js +1 -1
- package/dist/herald.iife.js.map +1 -1
- package/dist/herald.js +1 -1
- package/dist/herald.js.map +1 -1
- package/dist/index.cjs +1 -1
- package/dist/index.d.ts +1 -2
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +2 -3
- package/dist/pipe.cjs +1 -1
- package/dist/pipe.cjs.map +1 -1
- package/dist/pipe.d.ts +2 -2
- package/dist/pipe.d.ts.map +1 -1
- package/dist/pipe.js +8 -5
- package/dist/pipe.js.map +1 -1
- package/dist/testing/testing.cjs +1 -1
- package/dist/testing/testing.cjs.map +1 -1
- package/dist/testing/testing.d.ts +1 -3
- package/dist/testing/testing.d.ts.map +1 -1
- package/dist/testing/testing.js +24 -30
- package/dist/testing/testing.js.map +1 -1
- package/dist/types.d.ts +11 -46
- package/dist/types.d.ts.map +1 -1
- package/package.json +1 -1
- package/dist/_delegate.cjs +0 -2
- package/dist/_delegate.cjs.map +0 -1
- package/dist/_delegate.d.ts +0 -2
- package/dist/_delegate.d.ts.map +0 -1
- package/dist/_delegate.js +0 -30
- package/dist/_delegate.js.map +0 -1
- package/dist/behavior-bus.cjs +0 -2
- package/dist/behavior-bus.cjs.map +0 -1
- package/dist/behavior-bus.d.ts +0 -19
- package/dist/behavior-bus.d.ts.map +0 -1
- package/dist/behavior-bus.js +0 -57
- package/dist/behavior-bus.js.map +0 -1
package/dist/testing/testing.js
CHANGED
|
@@ -1,44 +1,38 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { createBus as e } from "../bus.js";
|
|
2
2
|
import { isUnsafeObjectKey as t } from "../_prototype.js";
|
|
3
|
-
import { createBus as n } from "../bus.js";
|
|
4
3
|
//#region src/testing/testing.ts
|
|
5
|
-
function
|
|
6
|
-
let
|
|
7
|
-
...
|
|
4
|
+
function n(n) {
|
|
5
|
+
let r = /* @__PURE__ */ new Map(), i = e({
|
|
6
|
+
...n,
|
|
8
7
|
_onDispatch: (e, t) => {
|
|
9
|
-
let n =
|
|
10
|
-
n ? n.push(t) :
|
|
8
|
+
let n = r.get(e);
|
|
9
|
+
n ? n.push(t) : r.set(e, [t]);
|
|
11
10
|
}
|
|
12
|
-
});
|
|
13
|
-
function
|
|
11
|
+
}), a = i.dispose;
|
|
12
|
+
function o() {
|
|
14
13
|
let e = {};
|
|
15
|
-
for (let [n,
|
|
14
|
+
for (let [n, i] of r) t(n) || (e[n] = [...i]);
|
|
16
15
|
return e;
|
|
17
16
|
}
|
|
18
|
-
function
|
|
19
|
-
return [...
|
|
20
|
-
}
|
|
21
|
-
function l(e) {
|
|
22
|
-
return i.get(e)?.length ?? 0;
|
|
17
|
+
function s(e) {
|
|
18
|
+
return [...r.get(e) ?? []];
|
|
23
19
|
}
|
|
24
|
-
function
|
|
25
|
-
|
|
20
|
+
function c(e) {
|
|
21
|
+
return r.get(e)?.length ?? 0;
|
|
26
22
|
}
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
let r = f(e, t, n), i = e, o = a.get(i);
|
|
30
|
-
return o || (o = /* @__PURE__ */ new Set(), a.set(i, o)), o.add(r), r;
|
|
31
|
-
};
|
|
32
|
-
function p(e) {
|
|
33
|
-
let t = e, n = a.get(t);
|
|
34
|
-
if (n) {
|
|
35
|
-
for (let e of n) e();
|
|
36
|
-
n.clear();
|
|
37
|
-
}
|
|
23
|
+
function l() {
|
|
24
|
+
a(), r.clear();
|
|
38
25
|
}
|
|
39
|
-
return
|
|
26
|
+
return Object.assign(i, {
|
|
27
|
+
allEmitted: o,
|
|
28
|
+
dispose: l,
|
|
29
|
+
emitted: s,
|
|
30
|
+
emittedCount: c,
|
|
31
|
+
reset: () => r.clear(),
|
|
32
|
+
[Symbol.dispose]: l
|
|
33
|
+
});
|
|
40
34
|
}
|
|
41
35
|
//#endregion
|
|
42
|
-
export {
|
|
36
|
+
export { n as createTestBus };
|
|
43
37
|
|
|
44
38
|
//# sourceMappingURL=testing.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"testing.js","names":[],"sources":["../../src/testing/testing.ts"],"sourcesContent":["import type { Bus, BusOptions, EventKey, EventMap
|
|
1
|
+
{"version":3,"file":"testing.js","names":[],"sources":["../../src/testing/testing.ts"],"sourcesContent":["import type { Bus, BusOptions, EventKey, EventMap } from '..';\nimport type { InternalBusOptions } from '../bus';\n\nimport { isUnsafeObjectKey } from '../_prototype';\nimport { createBus } from '../bus';\n\n/** A test bus is a regular bus with typed emission recording on top. */\nexport type TestBus<T extends EventMap> = Bus<T> & {\n /** Returns a snapshot of all payloads dispatched across every recorded event, keyed by event name. */\n allEmitted(): { [K in EventKey<T>]?: T[K][] };\n /** Snapshot of all payloads that were successfully dispatched for the given event key, in order. */\n emitted<K extends EventKey<T>>(event: K): T[K][];\n /** Number of times the given event was successfully dispatched. Shorthand for `emitted(event).length`. */\n emittedCount<K extends EventKey<T>>(event: K): number;\n /** Clear emitted records without disposing the bus. */\n reset(): void;\n};\n\nexport function createTestBus<T extends EventMap = Record<string, unknown>>(options?: BusOptions<T>): TestBus<T> {\n const records = new Map<string, unknown[]>();\n const bus = createBus<T>({\n ...options,\n _onDispatch: (event: EventKey<T>, payload: unknown) => {\n const list = records.get(event);\n\n if (list) list.push(payload);\n else records.set(event, [payload]);\n },\n } as InternalBusOptions<T>);\n const disposeBus = bus.dispose;\n\n function allEmitted(): { [K in EventKey<T>]?: T[K][] } {\n const result: { [K in EventKey<T>]?: T[K][] } = {};\n\n for (const [key, list] of records) {\n if (isUnsafeObjectKey(key)) continue;\n\n (result as Record<string, unknown[]>)[key] = [...list];\n }\n\n return result;\n }\n\n function emitted<K extends EventKey<T>>(event: K): T[K][] {\n return [...(records.get(event) ?? [])] as T[K][];\n }\n\n function emittedCount<K extends EventKey<T>>(event: K): number {\n return records.get(event)?.length ?? 0;\n }\n\n function dispose(): void {\n disposeBus();\n records.clear();\n }\n\n return Object.assign(bus, {\n allEmitted,\n dispose,\n emitted,\n emittedCount,\n reset: () => records.clear(),\n [Symbol.dispose]: dispose,\n }) as TestBus<T>;\n}\n"],"mappings":";;;AAkBA,SAAgB,EAA4D,GAAqC;CAC/G,IAAM,oBAAU,IAAI,IAAuB,GACrC,IAAM,EAAa;EACvB,GAAG;EACH,cAAc,GAAoB,MAAqB;GACrD,IAAM,IAAO,EAAQ,IAAI,CAAK;GAE9B,AAAI,IAAM,EAAK,KAAK,CAAO,IACtB,EAAQ,IAAI,GAAO,CAAC,CAAO,CAAC;EACnC;CACF,CAA0B,GACpB,IAAa,EAAI;CAEvB,SAAS,IAA8C;EACrD,IAAM,IAA0C,CAAC;EAEjD,KAAK,IAAM,CAAC,GAAK,MAAS,GACpB,EAAkB,CAAG,MAEzB,EAAsC,KAAO,CAAC,GAAG,CAAI;EAGvD,OAAO;CACT;CAEA,SAAS,EAA+B,GAAkB;EACxD,OAAO,CAAC,GAAI,EAAQ,IAAI,CAAK,KAAK,CAAC,CAAE;CACvC;CAEA,SAAS,EAAoC,GAAkB;EAC7D,OAAO,EAAQ,IAAI,CAAK,CAAC,EAAE,UAAU;CACvC;CAEA,SAAS,IAAgB;EAEvB,AADA,EAAW,GACX,EAAQ,MAAM;CAChB;CAEA,OAAO,OAAO,OAAO,GAAK;EACxB;EACA;EACA;EACA;EACA,aAAa,EAAQ,MAAM;GAC1B,OAAO,UAAU;CACpB,CAAC;AACH"}
|
package/dist/types.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
export type EventMap =
|
|
2
|
-
export type EventKey<T extends EventMap> = keyof T
|
|
1
|
+
export type EventMap = object;
|
|
2
|
+
export type EventKey<T extends EventMap> = Extract<keyof T, string>;
|
|
3
3
|
export type Listener<T> = (payload: T) => void;
|
|
4
4
|
export type Unsubscribe = () => void;
|
|
5
5
|
/**
|
|
@@ -111,10 +111,15 @@ export type PipeableKey<S extends EventMap, T extends EventMap> = {
|
|
|
111
111
|
* - A `PipeableKey` string — forward the event under the same name.
|
|
112
112
|
* - A `{ from, to }` object — forward the event under a different name on the target bus.
|
|
113
113
|
*/
|
|
114
|
-
export type
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
114
|
+
export type RenamedPipeEntry<S extends EventMap, T extends EventMap> = {
|
|
115
|
+
[From in EventKey<S>]: {
|
|
116
|
+
[To in EventKey<T>]: S[From] extends T[To] ? {
|
|
117
|
+
from: From;
|
|
118
|
+
to: To;
|
|
119
|
+
} : never;
|
|
120
|
+
}[EventKey<T>];
|
|
121
|
+
}[EventKey<S>];
|
|
122
|
+
export type PipeEntry<S extends EventMap, T extends EventMap> = PipeableKey<S, T> | RenamedPipeEntry<S, T>;
|
|
118
123
|
/**
|
|
119
124
|
* An `AsyncGenerator` extended with `AsyncDisposable`. Returned by `bus.events()`.
|
|
120
125
|
*
|
|
@@ -234,44 +239,4 @@ export type Bus<T extends EventMap> = {
|
|
|
234
239
|
*/
|
|
235
240
|
wildcardCount(): number;
|
|
236
241
|
};
|
|
237
|
-
/** Map of event names to their initial values for `createBehaviorBus`. */
|
|
238
|
-
export type BehaviorInitial<T extends EventMap> = {
|
|
239
|
-
[K in EventKey<T>]?: T[K];
|
|
240
|
-
};
|
|
241
|
-
/** Options for `createBehaviorBus` — extends standard `BusOptions`. */
|
|
242
|
-
export type BehaviorBusOptions<T extends EventMap = EventMap> = BusOptions<T>;
|
|
243
|
-
/**
|
|
244
|
-
* A bus that remembers the last emitted value for each event.
|
|
245
|
-
* New subscribers immediately receive the current value via `on()` and `once()`.
|
|
246
|
-
*/
|
|
247
|
-
export type BehaviorBus<T extends EventMap> = Bus<T> & {
|
|
248
|
-
/**
|
|
249
|
-
* Returns the most recently emitted value for the given event, or `undefined` if no value has
|
|
250
|
-
* been emitted yet (and no initial value was provided).
|
|
251
|
-
*/
|
|
252
|
-
current<K extends EventKey<T>>(event: K): T[K] | undefined;
|
|
253
|
-
/**
|
|
254
|
-
* Clear the current value for a specific event, or for all events when called without arguments.
|
|
255
|
-
* After reset, new subscribers will not receive a replayed value until the next emit.
|
|
256
|
-
* Does not affect active subscriptions or the disposed state of the bus.
|
|
257
|
-
*
|
|
258
|
-
* @example
|
|
259
|
-
* bus.reset('count'); // clear only 'count'
|
|
260
|
-
* bus.reset(); // clear all buffers
|
|
261
|
-
*/
|
|
262
|
-
reset(event?: EventKey<T>): void;
|
|
263
|
-
/**
|
|
264
|
-
* Returns a plain object snapshot of the most recently emitted value for every currently
|
|
265
|
-
* buffered event. Events with no value in the buffer are omitted from the result.
|
|
266
|
-
*
|
|
267
|
-
* Useful for serialization, hydration, and debugging multiple channels at once.
|
|
268
|
-
*
|
|
269
|
-
* @example
|
|
270
|
-
* const bus = createBehaviorBus<{ theme: string; zoom: number }>({ theme: 'light', zoom: 1 });
|
|
271
|
-
* bus.snapshot(); // { theme: 'light', zoom: 1 }
|
|
272
|
-
* bus.emit('theme', 'dark');
|
|
273
|
-
* bus.snapshot(); // { theme: 'dark', zoom: 1 }
|
|
274
|
-
*/
|
|
275
|
-
snapshot(): Partial<T>;
|
|
276
|
-
};
|
|
277
242
|
//# sourceMappingURL=types.d.ts.map
|
package/dist/types.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"AAAA,MAAM,MAAM,QAAQ,GAAG,MAAM,CAAC
|
|
1
|
+
{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E,MAAM,CAAC,EAAE,WAAW,CAAA;KAAE,GAAG,OAAO,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC;IACtF;;;;OAIG;IACH,OAAO,CAAC,KAAK,CAAC,CAAC,SAAS,SAAS,CAAC,QAAQ,CAAC,CAAC,CAAC,EAAE,QAAQ,CAAC,CAAC,CAAC,EAAE,GAAG,QAAQ,CAAC,CAAC,CAAC,EAAE,CAAC,EAC3E,MAAM,EAAE,CAAC,EACT,IAAI,CAAC,EAAE;QAAE,MAAM,CAAC,EAAE,WAAW,CAAA;KAAE,GAC9B,OAAO,CAAC,aAAa,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,CAAC;IAChC;;;OAGG;IACH,aAAa,IAAI,MAAM,CAAC;CACzB,CAAC"}
|
package/package.json
CHANGED
package/dist/_delegate.cjs
DELETED
|
@@ -1,2 +0,0 @@
|
|
|
1
|
-
function e(e){let t={dispose:()=>e.dispose(),emit:e.emit,eventNames:e.eventNames,events:e.events,listenerCount:e.listenerCount,on:e.on,onAny:e.onAny,once:e.once,[Symbol.dispose]:()=>e.dispose(),wait:e.wait,waitAny:e.waitAny,wildcardCount:e.wildcardCount};return Object.defineProperty(t,"disposalSignal",{configurable:!0,enumerable:!0,get:()=>e.disposalSignal}),Object.defineProperty(t,"disposed",{configurable:!0,enumerable:!0,get:()=>e.disposed}),t}exports.makeBusDelegate=e;
|
|
2
|
-
//# sourceMappingURL=_delegate.cjs.map
|
package/dist/_delegate.cjs.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"_delegate.cjs","names":[],"sources":["../src/_delegate.ts"],"sourcesContent":["import type { Bus, EventMap } from './types';\n\n/**\n * Creates a mutable delegation object that forwards every Bus<T> method and getter to `bus`.\n * Callers can overwrite individual properties on the returned object to override specific\n * behaviour (e.g. `delegate.emit = customEmit`) without repeating the full delegation boilerplate\n * for the remaining ~14 methods.\n *\n * Getters (`disposed`, `disposalSignal`) are defined via `Object.defineProperty` so they remain\n * live — reading them always queries the underlying bus rather than snapshotting the value at\n * creation time.\n *\n * @internal Not re-exported from the public index.\n */\nexport function makeBusDelegate<T extends EventMap>(bus: Bus<T>): Bus<T> {\n type BusMethods<T extends EventMap> = Omit<Bus<T>, 'disposed' | 'disposalSignal'>;\n\n const delegate = {\n dispose: () => bus.dispose(),\n emit: bus.emit,\n eventNames: bus.eventNames,\n events: bus.events,\n listenerCount: bus.listenerCount,\n on: bus.on,\n onAny: bus.onAny,\n once: bus.once,\n [Symbol.dispose]: () => bus.dispose(),\n wait: bus.wait,\n waitAny: bus.waitAny,\n wildcardCount: bus.wildcardCount,\n } satisfies BusMethods<T> as Bus<T>; // satisfies catches any missing method; cast is safe — getters added below.\n\n Object.defineProperty(delegate, 'disposalSignal', {\n configurable: true,\n enumerable: true,\n get: () => bus.disposalSignal,\n });\n\n Object.defineProperty(delegate, 'disposed', {\n configurable: true,\n enumerable: true,\n get: () => bus.disposed,\n });\n\n return delegate;\n}\n"],"mappings":"AAcA,SAAgB,EAAoC,EAAqB,CAGvE,IAAM,EAAW,CACf,YAAe,EAAI,QAAQ,EAC3B,KAAM,EAAI,KACV,WAAY,EAAI,WAChB,OAAQ,EAAI,OACZ,cAAe,EAAI,cACnB,GAAI,EAAI,GACR,MAAO,EAAI,MACX,KAAM,EAAI,MACT,OAAO,aAAgB,EAAI,QAAQ,EACpC,KAAM,EAAI,KACV,QAAS,EAAI,QACb,cAAe,EAAI,aACrB,EAcA,OAZA,OAAO,eAAe,EAAU,iBAAkB,CAChD,aAAc,GACd,WAAY,GACZ,QAAW,EAAI,cACjB,CAAC,EAED,OAAO,eAAe,EAAU,WAAY,CAC1C,aAAc,GACd,WAAY,GACZ,QAAW,EAAI,QACjB,CAAC,EAEM,CACT"}
|
package/dist/_delegate.d.ts
DELETED
package/dist/_delegate.d.ts.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"_delegate.d.ts","sourceRoot":"","sources":["../src/_delegate.ts"],"names":[],"mappings":""}
|
package/dist/_delegate.js
DELETED
|
@@ -1,30 +0,0 @@
|
|
|
1
|
-
//#region src/_delegate.ts
|
|
2
|
-
function e(e) {
|
|
3
|
-
let t = {
|
|
4
|
-
dispose: () => e.dispose(),
|
|
5
|
-
emit: e.emit,
|
|
6
|
-
eventNames: e.eventNames,
|
|
7
|
-
events: e.events,
|
|
8
|
-
listenerCount: e.listenerCount,
|
|
9
|
-
on: e.on,
|
|
10
|
-
onAny: e.onAny,
|
|
11
|
-
once: e.once,
|
|
12
|
-
[Symbol.dispose]: () => e.dispose(),
|
|
13
|
-
wait: e.wait,
|
|
14
|
-
waitAny: e.waitAny,
|
|
15
|
-
wildcardCount: e.wildcardCount
|
|
16
|
-
};
|
|
17
|
-
return Object.defineProperty(t, "disposalSignal", {
|
|
18
|
-
configurable: !0,
|
|
19
|
-
enumerable: !0,
|
|
20
|
-
get: () => e.disposalSignal
|
|
21
|
-
}), Object.defineProperty(t, "disposed", {
|
|
22
|
-
configurable: !0,
|
|
23
|
-
enumerable: !0,
|
|
24
|
-
get: () => e.disposed
|
|
25
|
-
}), t;
|
|
26
|
-
}
|
|
27
|
-
//#endregion
|
|
28
|
-
export { e as makeBusDelegate };
|
|
29
|
-
|
|
30
|
-
//# sourceMappingURL=_delegate.js.map
|
package/dist/_delegate.js.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"_delegate.js","names":[],"sources":["../src/_delegate.ts"],"sourcesContent":["import type { Bus, EventMap } from './types';\n\n/**\n * Creates a mutable delegation object that forwards every Bus<T> method and getter to `bus`.\n * Callers can overwrite individual properties on the returned object to override specific\n * behaviour (e.g. `delegate.emit = customEmit`) without repeating the full delegation boilerplate\n * for the remaining ~14 methods.\n *\n * Getters (`disposed`, `disposalSignal`) are defined via `Object.defineProperty` so they remain\n * live — reading them always queries the underlying bus rather than snapshotting the value at\n * creation time.\n *\n * @internal Not re-exported from the public index.\n */\nexport function makeBusDelegate<T extends EventMap>(bus: Bus<T>): Bus<T> {\n type BusMethods<T extends EventMap> = Omit<Bus<T>, 'disposed' | 'disposalSignal'>;\n\n const delegate = {\n dispose: () => bus.dispose(),\n emit: bus.emit,\n eventNames: bus.eventNames,\n events: bus.events,\n listenerCount: bus.listenerCount,\n on: bus.on,\n onAny: bus.onAny,\n once: bus.once,\n [Symbol.dispose]: () => bus.dispose(),\n wait: bus.wait,\n waitAny: bus.waitAny,\n wildcardCount: bus.wildcardCount,\n } satisfies BusMethods<T> as Bus<T>; // satisfies catches any missing method; cast is safe — getters added below.\n\n Object.defineProperty(delegate, 'disposalSignal', {\n configurable: true,\n enumerable: true,\n get: () => bus.disposalSignal,\n });\n\n Object.defineProperty(delegate, 'disposed', {\n configurable: true,\n enumerable: true,\n get: () => bus.disposed,\n });\n\n return delegate;\n}\n"],"mappings":";AAcA,SAAgB,EAAoC,GAAqB;CAGvE,IAAM,IAAW;EACf,eAAe,EAAI,QAAQ;EAC3B,MAAM,EAAI;EACV,YAAY,EAAI;EAChB,QAAQ,EAAI;EACZ,eAAe,EAAI;EACnB,IAAI,EAAI;EACR,OAAO,EAAI;EACX,MAAM,EAAI;GACT,OAAO,gBAAgB,EAAI,QAAQ;EACpC,MAAM,EAAI;EACV,SAAS,EAAI;EACb,eAAe,EAAI;CACrB;CAcA,OAZA,OAAO,eAAe,GAAU,kBAAkB;EAChD,cAAc;EACd,YAAY;EACZ,WAAW,EAAI;CACjB,CAAC,GAED,OAAO,eAAe,GAAU,YAAY;EAC1C,cAAc;EACd,YAAY;EACZ,WAAW,EAAI;CACjB,CAAC,GAEM;AACT"}
|
package/dist/behavior-bus.cjs
DELETED
|
@@ -1,2 +0,0 @@
|
|
|
1
|
-
const e=require("./_delegate.cjs"),t=require("./_prototype.cjs"),n=require("./_safe.cjs"),r=require("./bus.cjs");function i(i,a){let o=new Map(Object.entries(i??{}).filter(([,e])=>e!==void 0)),s=r.createBus({...a,_onDispatch:(e,t)=>{o.set(e,t)}});function c(e,t,r){let i=a?.onError?e=>a.onError({err:e,event:t,payload:r,timestamp:Date.now()}):void 0,o=n.callSafely(()=>e(r),i);if(o.threw)throw o.err}function l(e,t,n){if(s.disposed||n?.signal?.aborted)return r.noop;if(o.has(e)){let i=o.get(e);if(n?.once)return c(t,e,i),r.noop;c(t,e,i)}return s.on(e,t,n)}function u(e,t,n){return l(e,t,{once:!0,signal:n?.signal})}function d(e){return o.has(e)?o.get(e):void 0}function f(e){e===void 0?o.clear():o.delete(e)}function p(){let e={};for(let[n,r]of o)t.isUnsafeObjectKey(n)||(e[n]=r);return e}function m(){s.dispose(),o.clear()}let h=e.makeBusDelegate(s);return h.current=d,h.dispose=m,h.on=l,h.once=u,h.reset=f,h.snapshot=p,h[Symbol.dispose]=m,h}exports.createBehaviorBus=i;
|
|
2
|
-
//# sourceMappingURL=behavior-bus.cjs.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"behavior-bus.cjs","names":[],"sources":["../src/behavior-bus.ts"],"sourcesContent":["import type { InternalBusOptions } from './bus';\nimport type {\n BehaviorBus,\n BehaviorBusOptions,\n BehaviorInitial,\n EmissionErrorContext,\n EventKey,\n EventMap,\n Listener,\n SubscribeOptions,\n Unsubscribe,\n} from './types';\n\nimport { makeBusDelegate } from './_delegate';\nimport { isUnsafeObjectKey } from './_prototype';\nimport { callSafely } from './_safe';\nimport { createBus, noop } from './bus';\n\n/**\n * Creates a bus that replays the last known value for each event to new subscribers.\n *\n * When a listener subscribes via `on()` or `once()`, it immediately receives the current\n * buffered value for that event (if any have been emitted or were provided as initial values).\n * `events()`, `wait()`, and `waitAny()` behave like a regular bus — no replay.\n *\n * @param initial - Optional map of event names to their starting values.\n * @param options - Standard bus options.\n *\n * @example\n * const bus = createBehaviorBus<{ count: number }>({ count: 0 });\n * bus.on('count', (n) => console.log(n)); // logs 0 immediately\n * bus.emit('count', 1);\n * bus.on('count', (n) => console.log(n)); // logs 1 immediately (latest value)\n */\nexport function createBehaviorBus<T extends EventMap>(\n initial?: BehaviorInitial<T>,\n options?: BehaviorBusOptions<T>,\n): BehaviorBus<T> {\n // Flat buffer: one value per event key.\n // Seeded from initial values; updated via _onDispatch when dispatch actually runs.\n const buffers = new Map<string, unknown>(Object.entries(initial ?? {}).filter(([, v]) => v !== undefined));\n\n // _onDispatch fires after middleware passes (dispatch guaranteed to run).\n // This replaces the sentinel-middleware + mutable-flag pattern.\n const bus = createBus<T>({\n ...options,\n _onDispatch: (event: EventKey<T>, payload: unknown) => {\n buffers.set(event as string, payload);\n },\n } as InternalBusOptions<T>);\n\n // Wrap the listener call during synchronous replay so a throw routes through onError when set.\n // Only one listener is replayed per call here (unlike bus.ts's dispatch() broadcast loop), so\n // there's no \"keep going\" batch to preserve — an unforwarded throw rethrows immediately.\n function callSafeReplay(listener: Listener<unknown>, event: EventKey<T>, value: unknown): void {\n const onError = options?.onError\n ? (err: unknown) =>\n options.onError!({ err, event, payload: value, timestamp: Date.now() } as EmissionErrorContext<T>)\n : undefined;\n\n const result = callSafely(() => listener(value), onError);\n\n if (result.threw) throw result.err;\n }\n\n function on<K extends EventKey<T>>(event: K, listener: Listener<T[K]>, opts?: SubscribeOptions): Unsubscribe {\n if (bus.disposed || opts?.signal?.aborted) return noop;\n\n if (buffers.has(event as string)) {\n const value = buffers.get(event as string) as T[K];\n\n if (opts?.once) {\n callSafeReplay(listener as Listener<unknown>, event, value);\n\n return noop;\n }\n\n callSafeReplay(listener as Listener<unknown>, event, value);\n }\n\n return bus.on(event, listener, opts);\n }\n\n function once<K extends EventKey<T>>(\n event: K,\n listener: Listener<T[K]>,\n opts?: { signal?: AbortSignal },\n ): Unsubscribe {\n return on(event, listener, { once: true, signal: opts?.signal });\n }\n\n function current<K extends EventKey<T>>(event: K): T[K] | undefined {\n return buffers.has(event as string) ? (buffers.get(event as string) as T[K]) : undefined;\n }\n\n function reset(event?: EventKey<T>): void {\n if (event !== undefined) buffers.delete(event as string);\n else buffers.clear();\n }\n\n function snapshot(): Partial<T> {\n const result: Partial<T> = {};\n\n for (const [key, value] of buffers) {\n // Guard against a `__proto__`/`constructor`/`prototype` event name hijacking result's\n // own prototype via the bracket-assignment accessor — see _prototype.ts.\n if (isUnsafeObjectKey(key)) continue;\n\n (result as Record<string, unknown>)[key] = value;\n }\n\n return result;\n }\n\n function dispose(): void {\n bus.dispose();\n buffers.clear();\n }\n\n const delegate = makeBusDelegate<T>(bus) as BehaviorBus<T>;\n\n delegate.current = current;\n delegate.dispose = dispose;\n delegate.on = on;\n delegate.once = once;\n delegate.reset = reset;\n delegate.snapshot = snapshot;\n delegate[Symbol.dispose] = dispose;\n\n return delegate;\n}\n"],"mappings":"iHAkCA,SAAgB,EACd,EACA,EACgB,CAGhB,IAAM,EAAU,IAAI,IAAqB,OAAO,QAAQ,GAAW,CAAC,CAAC,CAAC,CAAC,QAAQ,EAAG,KAAO,IAAM,IAAA,EAAS,CAAC,EAInG,EAAM,EAAA,UAAa,CACvB,GAAG,EACH,aAAc,EAAoB,IAAqB,CACrD,EAAQ,IAAI,EAAiB,CAAO,CACtC,CACF,CAA0B,EAK1B,SAAS,EAAe,EAA6B,EAAoB,EAAsB,CAC7F,IAAM,EAAU,GAAS,QACpB,GACC,EAAQ,QAAS,CAAE,MAAK,QAAO,QAAS,EAAO,UAAW,KAAK,IAAI,CAAE,CAA4B,EACnG,IAAA,GAEE,EAAS,EAAA,eAAiB,EAAS,CAAK,EAAG,CAAO,EAExD,GAAI,EAAO,MAAO,MAAM,EAAO,GACjC,CAEA,SAAS,EAA0B,EAAU,EAA0B,EAAsC,CAC3G,GAAI,EAAI,UAAY,GAAM,QAAQ,QAAS,OAAO,EAAA,KAElD,GAAI,EAAQ,IAAI,CAAe,EAAG,CAChC,IAAM,EAAQ,EAAQ,IAAI,CAAe,EAEzC,GAAI,GAAM,KAGR,OAFA,EAAe,EAA+B,EAAO,CAAK,EAEnD,EAAA,KAGT,EAAe,EAA+B,EAAO,CAAK,CAC5D,CAEA,OAAO,EAAI,GAAG,EAAO,EAAU,CAAI,CACrC,CAEA,SAAS,EACP,EACA,EACA,EACa,CACb,OAAO,EAAG,EAAO,EAAU,CAAE,KAAM,GAAM,OAAQ,GAAM,MAAO,CAAC,CACjE,CAEA,SAAS,EAA+B,EAA4B,CAClE,OAAO,EAAQ,IAAI,CAAe,EAAK,EAAQ,IAAI,CAAe,EAAa,IAAA,EACjF,CAEA,SAAS,EAAM,EAA2B,CACpC,IAAU,IAAA,GACT,EAAQ,MAAM,EADM,EAAQ,OAAO,CAAe,CAEzD,CAEA,SAAS,GAAuB,CAC9B,IAAM,EAAqB,CAAC,EAE5B,IAAK,GAAM,CAAC,EAAK,KAAU,EAGrB,EAAA,kBAAkB,CAAG,IAEzB,EAAoC,GAAO,GAG7C,OAAO,CACT,CAEA,SAAS,GAAgB,CACvB,EAAI,QAAQ,EACZ,EAAQ,MAAM,CAChB,CAEA,IAAM,EAAW,EAAA,gBAAmB,CAAG,EAUvC,MARA,GAAS,QAAU,EACnB,EAAS,QAAU,EACnB,EAAS,GAAK,EACd,EAAS,KAAO,EAChB,EAAS,MAAQ,EACjB,EAAS,SAAW,EACpB,EAAS,OAAO,SAAW,EAEpB,CACT"}
|
package/dist/behavior-bus.d.ts
DELETED
|
@@ -1,19 +0,0 @@
|
|
|
1
|
-
import type { BehaviorBus, BehaviorBusOptions, BehaviorInitial, EventMap } from './types';
|
|
2
|
-
/**
|
|
3
|
-
* Creates a bus that replays the last known value for each event to new subscribers.
|
|
4
|
-
*
|
|
5
|
-
* When a listener subscribes via `on()` or `once()`, it immediately receives the current
|
|
6
|
-
* buffered value for that event (if any have been emitted or were provided as initial values).
|
|
7
|
-
* `events()`, `wait()`, and `waitAny()` behave like a regular bus — no replay.
|
|
8
|
-
*
|
|
9
|
-
* @param initial - Optional map of event names to their starting values.
|
|
10
|
-
* @param options - Standard bus options.
|
|
11
|
-
*
|
|
12
|
-
* @example
|
|
13
|
-
* const bus = createBehaviorBus<{ count: number }>({ count: 0 });
|
|
14
|
-
* bus.on('count', (n) => console.log(n)); // logs 0 immediately
|
|
15
|
-
* bus.emit('count', 1);
|
|
16
|
-
* bus.on('count', (n) => console.log(n)); // logs 1 immediately (latest value)
|
|
17
|
-
*/
|
|
18
|
-
export declare function createBehaviorBus<T extends EventMap>(initial?: BehaviorInitial<T>, options?: BehaviorBusOptions<T>): BehaviorBus<T>;
|
|
19
|
-
//# sourceMappingURL=behavior-bus.d.ts.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"behavior-bus.d.ts","sourceRoot":"","sources":["../src/behavior-bus.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EACV,WAAW,EACX,kBAAkB,EAClB,eAAe,EAGf,QAAQ,EAIT,MAAM,SAAS,CAAC;AAOjB;;;;;;;;;;;;;;;GAeG;AACH,wBAAgB,iBAAiB,CAAC,CAAC,SAAS,QAAQ,EAClD,OAAO,CAAC,EAAE,eAAe,CAAC,CAAC,CAAC,EAC5B,OAAO,CAAC,EAAE,kBAAkB,CAAC,CAAC,CAAC,GAC9B,WAAW,CAAC,CAAC,CAAC,CA6FhB"}
|
package/dist/behavior-bus.js
DELETED
|
@@ -1,57 +0,0 @@
|
|
|
1
|
-
import { makeBusDelegate as e } from "./_delegate.js";
|
|
2
|
-
import { isUnsafeObjectKey as t } from "./_prototype.js";
|
|
3
|
-
import { callSafely as n } from "./_safe.js";
|
|
4
|
-
import { createBus as r, noop as i } from "./bus.js";
|
|
5
|
-
//#region src/behavior-bus.ts
|
|
6
|
-
function a(a, o) {
|
|
7
|
-
let s = new Map(Object.entries(a ?? {}).filter(([, e]) => e !== void 0)), c = r({
|
|
8
|
-
...o,
|
|
9
|
-
_onDispatch: (e, t) => {
|
|
10
|
-
s.set(e, t);
|
|
11
|
-
}
|
|
12
|
-
});
|
|
13
|
-
function l(e, t, r) {
|
|
14
|
-
let i = o?.onError ? (e) => o.onError({
|
|
15
|
-
err: e,
|
|
16
|
-
event: t,
|
|
17
|
-
payload: r,
|
|
18
|
-
timestamp: Date.now()
|
|
19
|
-
}) : void 0, a = n(() => e(r), i);
|
|
20
|
-
if (a.threw) throw a.err;
|
|
21
|
-
}
|
|
22
|
-
function u(e, t, n) {
|
|
23
|
-
if (c.disposed || n?.signal?.aborted) return i;
|
|
24
|
-
if (s.has(e)) {
|
|
25
|
-
let r = s.get(e);
|
|
26
|
-
if (n?.once) return l(t, e, r), i;
|
|
27
|
-
l(t, e, r);
|
|
28
|
-
}
|
|
29
|
-
return c.on(e, t, n);
|
|
30
|
-
}
|
|
31
|
-
function d(e, t, n) {
|
|
32
|
-
return u(e, t, {
|
|
33
|
-
once: !0,
|
|
34
|
-
signal: n?.signal
|
|
35
|
-
});
|
|
36
|
-
}
|
|
37
|
-
function f(e) {
|
|
38
|
-
return s.has(e) ? s.get(e) : void 0;
|
|
39
|
-
}
|
|
40
|
-
function p(e) {
|
|
41
|
-
e === void 0 ? s.clear() : s.delete(e);
|
|
42
|
-
}
|
|
43
|
-
function m() {
|
|
44
|
-
let e = {};
|
|
45
|
-
for (let [n, r] of s) t(n) || (e[n] = r);
|
|
46
|
-
return e;
|
|
47
|
-
}
|
|
48
|
-
function h() {
|
|
49
|
-
c.dispose(), s.clear();
|
|
50
|
-
}
|
|
51
|
-
let g = e(c);
|
|
52
|
-
return g.current = f, g.dispose = h, g.on = u, g.once = d, g.reset = p, g.snapshot = m, g[Symbol.dispose] = h, g;
|
|
53
|
-
}
|
|
54
|
-
//#endregion
|
|
55
|
-
export { a as createBehaviorBus };
|
|
56
|
-
|
|
57
|
-
//# sourceMappingURL=behavior-bus.js.map
|
package/dist/behavior-bus.js.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"behavior-bus.js","names":[],"sources":["../src/behavior-bus.ts"],"sourcesContent":["import type { InternalBusOptions } from './bus';\nimport type {\n BehaviorBus,\n BehaviorBusOptions,\n BehaviorInitial,\n EmissionErrorContext,\n EventKey,\n EventMap,\n Listener,\n SubscribeOptions,\n Unsubscribe,\n} from './types';\n\nimport { makeBusDelegate } from './_delegate';\nimport { isUnsafeObjectKey } from './_prototype';\nimport { callSafely } from './_safe';\nimport { createBus, noop } from './bus';\n\n/**\n * Creates a bus that replays the last known value for each event to new subscribers.\n *\n * When a listener subscribes via `on()` or `once()`, it immediately receives the current\n * buffered value for that event (if any have been emitted or were provided as initial values).\n * `events()`, `wait()`, and `waitAny()` behave like a regular bus — no replay.\n *\n * @param initial - Optional map of event names to their starting values.\n * @param options - Standard bus options.\n *\n * @example\n * const bus = createBehaviorBus<{ count: number }>({ count: 0 });\n * bus.on('count', (n) => console.log(n)); // logs 0 immediately\n * bus.emit('count', 1);\n * bus.on('count', (n) => console.log(n)); // logs 1 immediately (latest value)\n */\nexport function createBehaviorBus<T extends EventMap>(\n initial?: BehaviorInitial<T>,\n options?: BehaviorBusOptions<T>,\n): BehaviorBus<T> {\n // Flat buffer: one value per event key.\n // Seeded from initial values; updated via _onDispatch when dispatch actually runs.\n const buffers = new Map<string, unknown>(Object.entries(initial ?? {}).filter(([, v]) => v !== undefined));\n\n // _onDispatch fires after middleware passes (dispatch guaranteed to run).\n // This replaces the sentinel-middleware + mutable-flag pattern.\n const bus = createBus<T>({\n ...options,\n _onDispatch: (event: EventKey<T>, payload: unknown) => {\n buffers.set(event as string, payload);\n },\n } as InternalBusOptions<T>);\n\n // Wrap the listener call during synchronous replay so a throw routes through onError when set.\n // Only one listener is replayed per call here (unlike bus.ts's dispatch() broadcast loop), so\n // there's no \"keep going\" batch to preserve — an unforwarded throw rethrows immediately.\n function callSafeReplay(listener: Listener<unknown>, event: EventKey<T>, value: unknown): void {\n const onError = options?.onError\n ? (err: unknown) =>\n options.onError!({ err, event, payload: value, timestamp: Date.now() } as EmissionErrorContext<T>)\n : undefined;\n\n const result = callSafely(() => listener(value), onError);\n\n if (result.threw) throw result.err;\n }\n\n function on<K extends EventKey<T>>(event: K, listener: Listener<T[K]>, opts?: SubscribeOptions): Unsubscribe {\n if (bus.disposed || opts?.signal?.aborted) return noop;\n\n if (buffers.has(event as string)) {\n const value = buffers.get(event as string) as T[K];\n\n if (opts?.once) {\n callSafeReplay(listener as Listener<unknown>, event, value);\n\n return noop;\n }\n\n callSafeReplay(listener as Listener<unknown>, event, value);\n }\n\n return bus.on(event, listener, opts);\n }\n\n function once<K extends EventKey<T>>(\n event: K,\n listener: Listener<T[K]>,\n opts?: { signal?: AbortSignal },\n ): Unsubscribe {\n return on(event, listener, { once: true, signal: opts?.signal });\n }\n\n function current<K extends EventKey<T>>(event: K): T[K] | undefined {\n return buffers.has(event as string) ? (buffers.get(event as string) as T[K]) : undefined;\n }\n\n function reset(event?: EventKey<T>): void {\n if (event !== undefined) buffers.delete(event as string);\n else buffers.clear();\n }\n\n function snapshot(): Partial<T> {\n const result: Partial<T> = {};\n\n for (const [key, value] of buffers) {\n // Guard against a `__proto__`/`constructor`/`prototype` event name hijacking result's\n // own prototype via the bracket-assignment accessor — see _prototype.ts.\n if (isUnsafeObjectKey(key)) continue;\n\n (result as Record<string, unknown>)[key] = value;\n }\n\n return result;\n }\n\n function dispose(): void {\n bus.dispose();\n buffers.clear();\n }\n\n const delegate = makeBusDelegate<T>(bus) as BehaviorBus<T>;\n\n delegate.current = current;\n delegate.dispose = dispose;\n delegate.on = on;\n delegate.once = once;\n delegate.reset = reset;\n delegate.snapshot = snapshot;\n delegate[Symbol.dispose] = dispose;\n\n return delegate;\n}\n"],"mappings":";;;;;AAkCA,SAAgB,EACd,GACA,GACgB;CAGhB,IAAM,IAAU,IAAI,IAAqB,OAAO,QAAQ,KAAW,CAAC,CAAC,CAAC,CAAC,QAAQ,GAAG,OAAO,MAAM,KAAA,CAAS,CAAC,GAInG,IAAM,EAAa;EACvB,GAAG;EACH,cAAc,GAAoB,MAAqB;GACrD,EAAQ,IAAI,GAAiB,CAAO;EACtC;CACF,CAA0B;CAK1B,SAAS,EAAe,GAA6B,GAAoB,GAAsB;EAC7F,IAAM,IAAU,GAAS,WACpB,MACC,EAAQ,QAAS;GAAE;GAAK;GAAO,SA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