@dxos/async 0.7.5-labs.e27f9b9 → 0.7.5-main.2567c87
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/dist/lib/browser/index.mjs +4 -35
- package/dist/lib/browser/index.mjs.map +3 -3
- package/dist/lib/browser/meta.json +1 -1
- package/dist/lib/node/index.cjs +4 -35
- package/dist/lib/node/index.cjs.map +3 -3
- package/dist/lib/node/meta.json +1 -1
- package/dist/lib/node-esm/index.mjs +4 -35
- package/dist/lib/node-esm/index.mjs.map +3 -3
- package/dist/lib/node-esm/meta.json +1 -1
- package/dist/types/src/events.d.ts +2 -13
- package/dist/types/src/events.d.ts.map +1 -1
- package/package.json +7 -7
- package/src/events.test.ts +0 -15
- package/src/events.ts +9 -49
|
@@ -1,5 +1,4 @@
|
|
|
1
1
|
import { Context } from '@dxos/context';
|
|
2
|
-
import type { MaybePromise } from '@dxos/util';
|
|
3
2
|
export type UnsubscribeCallback = () => void;
|
|
4
3
|
export type Effect = () => UnsubscribeCallback | undefined;
|
|
5
4
|
interface EventEmitterLike {
|
|
@@ -18,7 +17,6 @@ export type ListenerOptions = {
|
|
|
18
17
|
weak?: boolean;
|
|
19
18
|
once?: boolean;
|
|
20
19
|
};
|
|
21
|
-
type EventCallback<T> = (data: T) => MaybePromise<void>;
|
|
22
20
|
/**
|
|
23
21
|
* An EventEmitter variant that does not do event multiplexing and represents a single event.
|
|
24
22
|
*
|
|
@@ -65,15 +63,6 @@ export declare class Event<T = void> implements ReadOnlyEvent<T> {
|
|
|
65
63
|
* @param data param that will be passed to all listeners.
|
|
66
64
|
*/
|
|
67
65
|
emit(data: T): void;
|
|
68
|
-
/**
|
|
69
|
-
* Emit an event and wait for async listeners to complete.
|
|
70
|
-
* In most cases should only be called by the class or entity containing the event.
|
|
71
|
-
* All listeners are called in order of subscription with persistent ones first.
|
|
72
|
-
* Listeners are called sequentially.
|
|
73
|
-
*
|
|
74
|
-
* @param data param that will be passed to all listeners.
|
|
75
|
-
*/
|
|
76
|
-
emitAsync(data: T): Promise<void>;
|
|
77
66
|
/**
|
|
78
67
|
* Register an event listener.
|
|
79
68
|
* If provided callback was already registered as once-listener, it is made permanent.
|
|
@@ -82,8 +71,8 @@ export declare class Event<T = void> implements ReadOnlyEvent<T> {
|
|
|
82
71
|
* @param options.weak If true, the callback will be weakly referenced and will be garbage collected if no other references to it exist.
|
|
83
72
|
* @returns function that unsubscribes this event listener
|
|
84
73
|
*/
|
|
85
|
-
on(callback:
|
|
86
|
-
on(ctx: Context, callback:
|
|
74
|
+
on(callback: (data: T) => void): UnsubscribeCallback;
|
|
75
|
+
on(ctx: Context, callback: (data: T) => void, options?: ListenerOptions): UnsubscribeCallback;
|
|
87
76
|
/**
|
|
88
77
|
* Unsubscribe this callback from new events. Includes persistent and once-listeners.
|
|
89
78
|
* NOTE: It is recommended to use `Event.on`'s return value instead.
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"events.d.ts","sourceRoot":"","sources":["../../../src/events.ts"],"names":[],"mappings":"AAIA,OAAO,EAAE,OAAO,EAAE,MAAM,eAAe,CAAC;
|
|
1
|
+
{"version":3,"file":"events.d.ts","sourceRoot":"","sources":["../../../src/events.ts"],"names":[],"mappings":"AAIA,OAAO,EAAE,OAAO,EAAE,MAAM,eAAe,CAAC;AAExC,MAAM,MAAM,mBAAmB,GAAG,MAAM,IAAI,CAAC;AAE7C,MAAM,MAAM,MAAM,GAAG,MAAM,mBAAmB,GAAG,SAAS,CAAC;AAU3D,UAAU,gBAAgB;IACxB,EAAE,CAAC,KAAK,EAAE,MAAM,EAAE,EAAE,EAAE,CAAC,IAAI,CAAC,EAAE,GAAG,KAAK,IAAI,GAAG,IAAI,CAAC;IAClD,GAAG,CAAC,KAAK,EAAE,MAAM,EAAE,EAAE,EAAE,CAAC,IAAI,CAAC,EAAE,GAAG,KAAK,IAAI,GAAG,IAAI,CAAC;CACpD;AAED;;GAEG;AACH,qBAAa,kBAAkB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O,CAAC,SAAS,EAAE,CAAC,IAAI,EAAE,CAAC,KAAK,OAAO,GAAG,OAAO,CAAC,CAAC,CAAC,CAAC;IAErD;;;;OAIG;IACH,YAAY,CAAC,aAAa,EAAE,MAAM,GAAG,OAAO,CAAC,CAAC,CAAC,CAAC;IAEhD;;OAEG;IACH,gBAAgB,IAAI,KAAK,CAAC,IAAI,CAAC,CAAC;IAEhC;;;;;OAKG;IACH,QAAQ,CAAC,OAAO,CAAC,EAAE,MAAM,GAAG,KAAK,CAAC,IAAI,CAAC,CAAC;CACzC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@dxos/async",
|
|
3
|
-
"version": "0.7.5-
|
|
3
|
+
"version": "0.7.5-main.2567c87",
|
|
4
4
|
"description": "Async utilities.",
|
|
5
5
|
"homepage": "https://dxos.org",
|
|
6
6
|
"bugs": "https://github.com/dxos/dxos/issues",
|
|
@@ -28,12 +28,12 @@
|
|
|
28
28
|
"dependencies": {
|
|
29
29
|
"zen-observable": "^0.10.0",
|
|
30
30
|
"zen-push": "^0.3.1",
|
|
31
|
-
"@dxos/
|
|
32
|
-
"@dxos/
|
|
33
|
-
"@dxos/log": "0.7.5-
|
|
34
|
-
"@dxos/
|
|
35
|
-
"@dxos/
|
|
36
|
-
"@dxos/
|
|
31
|
+
"@dxos/debug": "0.7.5-main.2567c87",
|
|
32
|
+
"@dxos/invariant": "0.7.5-main.2567c87",
|
|
33
|
+
"@dxos/log": "0.7.5-main.2567c87",
|
|
34
|
+
"@dxos/node-std": "0.7.5-main.2567c87",
|
|
35
|
+
"@dxos/util": "0.7.5-main.2567c87",
|
|
36
|
+
"@dxos/context": "0.7.5-main.2567c87"
|
|
37
37
|
},
|
|
38
38
|
"devDependencies": {
|
|
39
39
|
"@types/zen-observable": "^0.8.3"
|
package/src/events.test.ts
CHANGED
|
@@ -89,21 +89,6 @@ describe('Event', () => {
|
|
|
89
89
|
expect(error.message).to.equal('test');
|
|
90
90
|
});
|
|
91
91
|
|
|
92
|
-
test('emitAsync', async () => {
|
|
93
|
-
const event = new Event<number>();
|
|
94
|
-
let called = 0;
|
|
95
|
-
event.on(async (num) => {
|
|
96
|
-
await sleep(10);
|
|
97
|
-
called++;
|
|
98
|
-
});
|
|
99
|
-
|
|
100
|
-
await event.emitAsync(1);
|
|
101
|
-
expect(called).to.equal(1);
|
|
102
|
-
|
|
103
|
-
// TODO(dmaretskyi): Disabled for now.
|
|
104
|
-
// expect(() => event.emit(1)).to.throw(TypeError);
|
|
105
|
-
});
|
|
106
|
-
|
|
107
92
|
// test.skip('weak', async () => {
|
|
108
93
|
// setFlagsFromString('--expose_gc');
|
|
109
94
|
// const gc = runInNewContext('gc'); // nocommit
|
package/src/events.ts
CHANGED
|
@@ -3,7 +3,6 @@
|
|
|
3
3
|
//
|
|
4
4
|
|
|
5
5
|
import { Context } from '@dxos/context';
|
|
6
|
-
import type { MaybePromise } from '@dxos/util';
|
|
7
6
|
|
|
8
7
|
export type UnsubscribeCallback = () => void;
|
|
9
8
|
|
|
@@ -43,11 +42,6 @@ export type ListenerOptions = {
|
|
|
43
42
|
once?: boolean;
|
|
44
43
|
};
|
|
45
44
|
|
|
46
|
-
type EventCallback<T> = (data: T) => MaybePromise<void>;
|
|
47
|
-
|
|
48
|
-
// TODO(dmaretskyi): Remove this once the code is cleaned up.
|
|
49
|
-
const DO_NOT_ERROR_ON_ASYNC_CALLBACK = true;
|
|
50
|
-
|
|
51
45
|
/**
|
|
52
46
|
* An EventEmitter variant that does not do event multiplexing and represents a single event.
|
|
53
47
|
*
|
|
@@ -108,25 +102,7 @@ export class Event<T = void> implements ReadOnlyEvent<T> {
|
|
|
108
102
|
*/
|
|
109
103
|
emit(data: T) {
|
|
110
104
|
for (const listener of this._listeners) {
|
|
111
|
-
listener.trigger(data);
|
|
112
|
-
|
|
113
|
-
if (listener.once) {
|
|
114
|
-
this._listeners.delete(listener);
|
|
115
|
-
}
|
|
116
|
-
}
|
|
117
|
-
}
|
|
118
|
-
|
|
119
|
-
/**
|
|
120
|
-
* Emit an event and wait for async listeners to complete.
|
|
121
|
-
* In most cases should only be called by the class or entity containing the event.
|
|
122
|
-
* All listeners are called in order of subscription with persistent ones first.
|
|
123
|
-
* Listeners are called sequentially.
|
|
124
|
-
*
|
|
125
|
-
* @param data param that will be passed to all listeners.
|
|
126
|
-
*/
|
|
127
|
-
async emitAsync(data: T) {
|
|
128
|
-
for (const listener of this._listeners) {
|
|
129
|
-
await listener.triggerAsync(data);
|
|
105
|
+
void listener.trigger(data);
|
|
130
106
|
|
|
131
107
|
if (listener.once) {
|
|
132
108
|
this._listeners.delete(listener);
|
|
@@ -142,9 +118,9 @@ export class Event<T = void> implements ReadOnlyEvent<T> {
|
|
|
142
118
|
* @param options.weak If true, the callback will be weakly referenced and will be garbage collected if no other references to it exist.
|
|
143
119
|
* @returns function that unsubscribes this event listener
|
|
144
120
|
*/
|
|
145
|
-
on(callback:
|
|
146
|
-
on(ctx: Context, callback:
|
|
147
|
-
on(_ctx: any, _callback?:
|
|
121
|
+
on(callback: (data: T) => void): UnsubscribeCallback;
|
|
122
|
+
on(ctx: Context, callback: (data: T) => void, options?: ListenerOptions): UnsubscribeCallback;
|
|
123
|
+
on(_ctx: any, _callback?: (data: T) => void, options?: ListenerOptions): UnsubscribeCallback {
|
|
148
124
|
const [ctx, callback] = _ctx instanceof Context ? [_ctx, _callback] : [new Context(), _ctx];
|
|
149
125
|
const weak = !!options?.weak;
|
|
150
126
|
const once = !!options?.once;
|
|
@@ -432,13 +408,13 @@ export interface ReadOnlyEvent<T = void> {
|
|
|
432
408
|
}
|
|
433
409
|
|
|
434
410
|
class EventListener<T> {
|
|
435
|
-
public readonly callback:
|
|
411
|
+
public readonly callback: ((data: T) => void) | WeakRef<(data: T) => void>;
|
|
436
412
|
|
|
437
413
|
private readonly _clearDispose?: () => void = undefined;
|
|
438
414
|
|
|
439
415
|
constructor(
|
|
440
416
|
event: Event<T>,
|
|
441
|
-
listener:
|
|
417
|
+
listener: (data: T) => void,
|
|
442
418
|
public readonly ctx: Context,
|
|
443
419
|
public readonly once: boolean,
|
|
444
420
|
public readonly weak: boolean,
|
|
@@ -462,27 +438,11 @@ class EventListener<T> {
|
|
|
462
438
|
}
|
|
463
439
|
}
|
|
464
440
|
|
|
465
|
-
derefCallback():
|
|
466
|
-
return this.weak ? (this.callback as WeakRef<
|
|
467
|
-
}
|
|
468
|
-
|
|
469
|
-
trigger(data: T) {
|
|
470
|
-
let result!: MaybePromise<void>;
|
|
471
|
-
try {
|
|
472
|
-
const callback = this.derefCallback();
|
|
473
|
-
result = callback?.(data);
|
|
474
|
-
} catch (err: any) {
|
|
475
|
-
this.ctx.raise(err);
|
|
476
|
-
}
|
|
477
|
-
|
|
478
|
-
if (!DO_NOT_ERROR_ON_ASYNC_CALLBACK) {
|
|
479
|
-
if (result instanceof Promise) {
|
|
480
|
-
throw new TypeError('Event has async callbacks, use emitAsync instead');
|
|
481
|
-
}
|
|
482
|
-
}
|
|
441
|
+
derefCallback(): ((data: T) => void) | undefined {
|
|
442
|
+
return this.weak ? (this.callback as WeakRef<(data: T) => void>).deref() : (this.callback as (data: T) => void);
|
|
483
443
|
}
|
|
484
444
|
|
|
485
|
-
async
|
|
445
|
+
async trigger(data: T) {
|
|
486
446
|
try {
|
|
487
447
|
const callback = this.derefCallback();
|
|
488
448
|
await callback?.(data);
|