@dxos/async 0.6.12-main.f9d0246 → 0.6.12-staging.0b4bb48
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 +3 -6
- package/dist/lib/browser/index.mjs.map +2 -2
- package/dist/lib/browser/meta.json +1 -1
- package/dist/lib/node/index.cjs +3 -6
- package/dist/lib/node/index.cjs.map +2 -2
- package/dist/lib/node/meta.json +1 -1
- package/dist/types/src/event-emitter.d.ts +2 -2
- package/dist/types/src/event-emitter.d.ts.map +1 -1
- package/dist/types/src/events.d.ts +2 -14
- package/dist/types/src/events.d.ts.map +1 -1
- package/package.json +8 -9
- package/src/event-emitter.test.ts +3 -1
- package/src/events.test.ts +23 -22
- package/src/events.ts +3 -16
- package/src/mutex.test.ts +3 -2
- package/src/observable-value.test.ts +3 -1
- package/src/observable.test.ts +8 -6
- package/src/task-scheduling.test.ts +6 -2
- package/src/test-stream.test.ts +1 -1
- package/src/testing.test.ts +2 -1
- package/src/timeout.test.ts +5 -2
- package/src/trigger.test.ts +7 -2
- package/src/until.test.ts +10 -7
- package/src/update-scheduler.test.ts +2 -1
- package/dist/lib/node-esm/index.mjs +0 -1337
- package/dist/lib/node-esm/index.mjs.map +0 -7
- package/dist/lib/node-esm/meta.json +0 -1
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@dxos/async",
|
|
3
|
-
"version": "0.6.12-
|
|
3
|
+
"version": "0.6.12-staging.0b4bb48",
|
|
4
4
|
"description": "Async utilities.",
|
|
5
5
|
"homepage": "https://dxos.org",
|
|
6
6
|
"bugs": "https://github.com/dxos/dxos/issues",
|
|
@@ -10,8 +10,7 @@
|
|
|
10
10
|
".": {
|
|
11
11
|
"browser": "./dist/lib/browser/index.mjs",
|
|
12
12
|
"node": {
|
|
13
|
-
"
|
|
14
|
-
"default": "./dist/lib/node-esm/index.mjs"
|
|
13
|
+
"default": "./dist/lib/node/index.cjs"
|
|
15
14
|
},
|
|
16
15
|
"types": "./dist/types/src/index.d.ts"
|
|
17
16
|
}
|
|
@@ -27,12 +26,12 @@
|
|
|
27
26
|
"dependencies": {
|
|
28
27
|
"zen-observable": "^0.10.0",
|
|
29
28
|
"zen-push": "^0.3.1",
|
|
30
|
-
"@dxos/debug": "0.6.12-
|
|
31
|
-
"@dxos/
|
|
32
|
-
"@dxos/
|
|
33
|
-
"@dxos/
|
|
34
|
-
"@dxos/
|
|
35
|
-
"@dxos/context": "0.6.12-
|
|
29
|
+
"@dxos/debug": "0.6.12-staging.0b4bb48",
|
|
30
|
+
"@dxos/invariant": "0.6.12-staging.0b4bb48",
|
|
31
|
+
"@dxos/node-std": "0.6.12-staging.0b4bb48",
|
|
32
|
+
"@dxos/util": "0.6.12-staging.0b4bb48",
|
|
33
|
+
"@dxos/log": "0.6.12-staging.0b4bb48",
|
|
34
|
+
"@dxos/context": "0.6.12-staging.0b4bb48"
|
|
36
35
|
},
|
|
37
36
|
"devDependencies": {
|
|
38
37
|
"@types/zen-observable": "^0.8.3"
|
|
@@ -2,8 +2,10 @@
|
|
|
2
2
|
// Copyright 2020 DXOS.org
|
|
3
3
|
//
|
|
4
4
|
|
|
5
|
+
import { expect } from 'chai';
|
|
5
6
|
import { EventEmitter } from 'node:events';
|
|
6
|
-
|
|
7
|
+
|
|
8
|
+
import { describe, test } from '@dxos/test';
|
|
7
9
|
|
|
8
10
|
import { onEvent, waitForEvent } from './event-emitter';
|
|
9
11
|
import { latch } from './latch';
|
package/src/events.test.ts
CHANGED
|
@@ -2,11 +2,12 @@
|
|
|
2
2
|
// Copyright 2020 DXOS.org
|
|
3
3
|
//
|
|
4
4
|
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
import {
|
|
5
|
+
import { expect } from 'chai';
|
|
6
|
+
import { setFlagsFromString } from 'node:v8';
|
|
7
|
+
import { runInNewContext } from 'node:vm';
|
|
8
8
|
|
|
9
9
|
import { Context } from '@dxos/context';
|
|
10
|
+
import { describe, test } from '@dxos/test';
|
|
10
11
|
|
|
11
12
|
import { Event } from './events';
|
|
12
13
|
import { sleep } from './timeout';
|
|
@@ -89,28 +90,28 @@ describe('Event', () => {
|
|
|
89
90
|
expect(error.message).to.equal('test');
|
|
90
91
|
});
|
|
91
92
|
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
93
|
+
test.skip('weak', async () => {
|
|
94
|
+
setFlagsFromString('--expose_gc');
|
|
95
|
+
const gc = runInNewContext('gc'); // nocommit
|
|
95
96
|
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
97
|
+
let called = 0;
|
|
98
|
+
let callback: (() => void) | null = () => {
|
|
99
|
+
called++;
|
|
100
|
+
};
|
|
100
101
|
|
|
101
|
-
|
|
102
|
-
|
|
102
|
+
const event = new Event();
|
|
103
|
+
event.on(new Context(), callback, { weak: true });
|
|
103
104
|
|
|
104
|
-
|
|
105
|
-
|
|
105
|
+
event.emit();
|
|
106
|
+
expect(called).to.equal(1);
|
|
106
107
|
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
108
|
+
callback = null;
|
|
109
|
+
while (event.listenerCount() > 0) {
|
|
110
|
+
gc();
|
|
111
|
+
await sleep(5);
|
|
112
|
+
}
|
|
112
113
|
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
114
|
+
event.emit();
|
|
115
|
+
expect(called).to.equal(1);
|
|
116
|
+
});
|
|
116
117
|
});
|
package/src/events.ts
CHANGED
|
@@ -65,7 +65,7 @@ export type ListenerOptions = {
|
|
|
65
65
|
* ## Advantages over traditional EventEmitter:
|
|
66
66
|
* 1. User describes explicitly what events a class has as they are defined as class fields.
|
|
67
67
|
* 2. User can explicitly define event visibility (public, protected, private).
|
|
68
|
-
* 3.
|
|
68
|
+
* 3. Havings events as class fields allows the compiler to check for correct event usage.
|
|
69
69
|
* 4. User can type the data that event will be emitting.
|
|
70
70
|
* 5. Uses composition instead of inheritance.
|
|
71
71
|
* 6. Removes the cases where event names intersect when used in cases with inheritance.
|
|
@@ -73,9 +73,6 @@ export type ListenerOptions = {
|
|
|
73
73
|
*/
|
|
74
74
|
// TODO(burdon): Rename EventSink? (Event is a built-in type).
|
|
75
75
|
export class Event<T = void> implements ReadOnlyEvent<T> {
|
|
76
|
-
/**
|
|
77
|
-
* Wrap objects that have on/off style event emitters.
|
|
78
|
-
*/
|
|
79
76
|
static wrap<T>(emitter: EventEmitterLike, eventName: string): Event<T> {
|
|
80
77
|
const event = new Event<T>();
|
|
81
78
|
|
|
@@ -262,7 +259,7 @@ export class Event<T = void> implements ReadOnlyEvent<T> {
|
|
|
262
259
|
* If event is emitted for the first time or event wasn't fired for `timeout` milliseconds,
|
|
263
260
|
* the event is emitted after `timeout / 8` ms.
|
|
264
261
|
*/
|
|
265
|
-
// TODO(burdon):
|
|
262
|
+
// TODO(burdon): Provide function.
|
|
266
263
|
debounce(timeout = 0) {
|
|
267
264
|
let firing: NodeJS.Timeout | undefined;
|
|
268
265
|
let lastFired: number | undefined;
|
|
@@ -346,15 +343,13 @@ export class Event<T = void> implements ReadOnlyEvent<T> {
|
|
|
346
343
|
*/
|
|
347
344
|
export interface ReadOnlyEvent<T = void> {
|
|
348
345
|
/**
|
|
349
|
-
*
|
|
346
|
+
* Registers an event listener.
|
|
350
347
|
* If provided callback was already registered as once-listener, it is made permanent.
|
|
351
348
|
*
|
|
352
349
|
* @param callback
|
|
353
|
-
* @param options.weak If true, the callback will be weakly referenced and will be garbage collected if no other references to it exist.
|
|
354
350
|
* @returns function that unsubscribes this event listener
|
|
355
351
|
*/
|
|
356
352
|
on(callback: (data: T) => void): UnsubscribeCallback;
|
|
357
|
-
on(ctx: Context, callback: (data: T) => void, options?: ListenerOptions): UnsubscribeCallback;
|
|
358
353
|
|
|
359
354
|
/**
|
|
360
355
|
* Unsubscribes this callback from new events. Includes persistent and once-listeners.
|
|
@@ -397,14 +392,6 @@ export interface ReadOnlyEvent<T = void> {
|
|
|
397
392
|
* Turn any variant of `Event<T>` into an `Event<void>` discarding the callback parameter.
|
|
398
393
|
*/
|
|
399
394
|
discardParameter(): Event<void>;
|
|
400
|
-
|
|
401
|
-
/**
|
|
402
|
-
* Triggers an event with at least `timeout` milliseconds between each event.
|
|
403
|
-
* If the event is triggered more often, the event is delayed until the timeout is reached.
|
|
404
|
-
* If event is emitted for the first time or event wasn't fired for `timeout` milliseconds,
|
|
405
|
-
* the event is emitted after `timeout / 8` ms.
|
|
406
|
-
*/
|
|
407
|
-
debounce(timeout?: number): Event<void>;
|
|
408
395
|
}
|
|
409
396
|
|
|
410
397
|
class EventListener<T> {
|
package/src/mutex.test.ts
CHANGED
|
@@ -2,9 +2,10 @@
|
|
|
2
2
|
// Copyright 2020 DXOS.org
|
|
3
3
|
//
|
|
4
4
|
|
|
5
|
-
import {
|
|
5
|
+
import { expect } from 'chai';
|
|
6
6
|
|
|
7
7
|
import { expectToThrow } from '@dxos/debug';
|
|
8
|
+
import { describe, test } from '@dxos/test';
|
|
8
9
|
|
|
9
10
|
import { Mutex, synchronized } from './mutex';
|
|
10
11
|
import { sleep } from './timeout';
|
|
@@ -138,7 +139,7 @@ describe('Mutex', () => {
|
|
|
138
139
|
|
|
139
140
|
expect(error!.stack!.includes('throwsError')).to.be.true;
|
|
140
141
|
expect(error!.stack!.includes('callmutex')).to.be.true;
|
|
141
|
-
})
|
|
142
|
+
}).skipEnvironments('webkit');
|
|
142
143
|
|
|
143
144
|
test('works with explicit resource management syntax', async () => {
|
|
144
145
|
const mutex = new Mutex();
|
|
@@ -2,7 +2,9 @@
|
|
|
2
2
|
// Copyright 2022 DXOS.org
|
|
3
3
|
//
|
|
4
4
|
|
|
5
|
-
import {
|
|
5
|
+
import { expect } from 'chai';
|
|
6
|
+
|
|
7
|
+
import { describe, test } from '@dxos/test';
|
|
6
8
|
|
|
7
9
|
import { type AsyncEvents, TimeoutError } from './errors';
|
|
8
10
|
import { latch } from './latch';
|
package/src/observable.test.ts
CHANGED
|
@@ -2,7 +2,9 @@
|
|
|
2
2
|
// Copyright 2023 DXOS.org
|
|
3
3
|
//
|
|
4
4
|
|
|
5
|
-
import {
|
|
5
|
+
import { expect } from 'chai';
|
|
6
|
+
|
|
7
|
+
import { afterTest, describe, test } from '@dxos/test';
|
|
6
8
|
|
|
7
9
|
import { Event } from './events';
|
|
8
10
|
import { MulticastObservable, Observable, PushStream } from './observable';
|
|
@@ -21,7 +23,7 @@ describe('multicast observable', () => {
|
|
|
21
23
|
expect(value).to.equal(1);
|
|
22
24
|
trigger.wake();
|
|
23
25
|
});
|
|
24
|
-
|
|
26
|
+
afterTest(() => subscription.unsubscribe());
|
|
25
27
|
await trigger.wait();
|
|
26
28
|
});
|
|
27
29
|
|
|
@@ -44,8 +46,8 @@ describe('multicast observable', () => {
|
|
|
44
46
|
}
|
|
45
47
|
});
|
|
46
48
|
|
|
47
|
-
|
|
48
|
-
|
|
49
|
+
afterTest(() => subscription1.unsubscribe());
|
|
50
|
+
afterTest(() => subscription2.unsubscribe());
|
|
49
51
|
|
|
50
52
|
const next = { new: 'value' };
|
|
51
53
|
event.emit(next);
|
|
@@ -98,7 +100,7 @@ describe('multicast observable', () => {
|
|
|
98
100
|
const subscription = mapped.subscribe((value) => {
|
|
99
101
|
result.wake(value);
|
|
100
102
|
});
|
|
101
|
-
|
|
103
|
+
afterTest(() => subscription.unsubscribe());
|
|
102
104
|
expect(await result.wait()).to.deep.equal(6);
|
|
103
105
|
});
|
|
104
106
|
|
|
@@ -119,7 +121,7 @@ describe('multicast observable', () => {
|
|
|
119
121
|
const subscription = reduced.subscribe((value) => {
|
|
120
122
|
result.wake(value);
|
|
121
123
|
});
|
|
122
|
-
|
|
124
|
+
afterTest(() => subscription.unsubscribe());
|
|
123
125
|
expect(await result.wait()).to.deep.equal(6);
|
|
124
126
|
});
|
|
125
127
|
|
|
@@ -2,9 +2,11 @@
|
|
|
2
2
|
// Copyright 2022 DXOS.org
|
|
3
3
|
//
|
|
4
4
|
|
|
5
|
-
import {
|
|
5
|
+
import { expect } from 'chai';
|
|
6
|
+
import waitForExpect from 'wait-for-expect';
|
|
6
7
|
|
|
7
8
|
import { Context } from '@dxos/context';
|
|
9
|
+
import { describe, test } from '@dxos/test';
|
|
8
10
|
|
|
9
11
|
import { DeferredTask, scheduleTask } from './task-scheduling';
|
|
10
12
|
import { sleep } from './timeout';
|
|
@@ -23,7 +25,9 @@ describe('task-scheduling', () => {
|
|
|
23
25
|
throw new Error('test');
|
|
24
26
|
});
|
|
25
27
|
|
|
26
|
-
await
|
|
28
|
+
await waitForExpect(() => {
|
|
29
|
+
expect(error.message).to.eq('test');
|
|
30
|
+
});
|
|
27
31
|
});
|
|
28
32
|
|
|
29
33
|
test('cancelation', async () => {
|
package/src/test-stream.test.ts
CHANGED
package/src/testing.test.ts
CHANGED
|
@@ -2,9 +2,10 @@
|
|
|
2
2
|
// Copyright 2020 DXOS.org
|
|
3
3
|
//
|
|
4
4
|
|
|
5
|
-
import {
|
|
5
|
+
import { expect } from 'chai';
|
|
6
6
|
|
|
7
7
|
import { expectToThrow } from '@dxos/debug';
|
|
8
|
+
import { describe, test } from '@dxos/test';
|
|
8
9
|
|
|
9
10
|
import { waitForCondition } from './testing';
|
|
10
11
|
|
package/src/timeout.test.ts
CHANGED
|
@@ -2,7 +2,10 @@
|
|
|
2
2
|
// Copyright 2020 DXOS.org
|
|
3
3
|
//
|
|
4
4
|
|
|
5
|
-
import {
|
|
5
|
+
import { expect } from 'chai';
|
|
6
|
+
|
|
7
|
+
import { expectToThrow } from '@dxos/debug';
|
|
8
|
+
import { describe, test } from '@dxos/test';
|
|
6
9
|
|
|
7
10
|
import { asyncTimeout, sleep } from './timeout';
|
|
8
11
|
|
|
@@ -15,7 +18,7 @@ describe('timeout', () => {
|
|
|
15
18
|
|
|
16
19
|
test('fails', async () => {
|
|
17
20
|
const promise = sleep(200).then(() => 'test');
|
|
18
|
-
await
|
|
21
|
+
await expectToThrow(() => asyncTimeout(promise, 100, new Error('timeout')));
|
|
19
22
|
});
|
|
20
23
|
|
|
21
24
|
test('sleep', async () => {
|
package/src/trigger.test.ts
CHANGED
|
@@ -2,11 +2,16 @@
|
|
|
2
2
|
// Copyright 2020 DXOS.org
|
|
3
3
|
//
|
|
4
4
|
|
|
5
|
-
import {
|
|
5
|
+
import chai, { expect } from 'chai';
|
|
6
|
+
import chaiAsPromised from 'chai-as-promised';
|
|
7
|
+
|
|
8
|
+
import { describe, test } from '@dxos/test';
|
|
6
9
|
|
|
7
10
|
import { sleep } from './timeout';
|
|
8
11
|
import { Trigger, TriggerState, trigger } from './trigger';
|
|
9
12
|
|
|
13
|
+
chai.use(chaiAsPromised);
|
|
14
|
+
|
|
10
15
|
describe('trigger', () => {
|
|
11
16
|
test('trigger', async () => {
|
|
12
17
|
const [value, setValue] = trigger<any>();
|
|
@@ -47,7 +52,7 @@ describe('trigger', () => {
|
|
|
47
52
|
const error = new Error('Test error');
|
|
48
53
|
const promise = trigger.wait();
|
|
49
54
|
trigger.throw(error);
|
|
50
|
-
await expect(promise).
|
|
55
|
+
await expect(promise).to.be.rejectedWith(error);
|
|
51
56
|
expect(trigger.state).to.equal(TriggerState.REJECTED);
|
|
52
57
|
trigger.reset();
|
|
53
58
|
expect(trigger.state).to.equal(TriggerState.WAITING);
|
package/src/until.test.ts
CHANGED
|
@@ -2,7 +2,10 @@
|
|
|
2
2
|
// Copyright 2020 DXOS.org
|
|
3
3
|
//
|
|
4
4
|
|
|
5
|
-
import {
|
|
5
|
+
import { expect } from 'chai';
|
|
6
|
+
|
|
7
|
+
import { expectToThrow } from '@dxos/debug';
|
|
8
|
+
import { describe, test } from '@dxos/test';
|
|
6
9
|
|
|
7
10
|
import { sleep } from './timeout';
|
|
8
11
|
import { until } from './until';
|
|
@@ -19,29 +22,29 @@ describe('until', () => {
|
|
|
19
22
|
});
|
|
20
23
|
|
|
21
24
|
test('error', async () => {
|
|
22
|
-
await
|
|
25
|
+
await expectToThrow(async () => {
|
|
23
26
|
await until(async (resolve, reject) => {
|
|
24
27
|
await sleep(100);
|
|
25
28
|
reject(new Error());
|
|
26
29
|
});
|
|
27
|
-
})
|
|
30
|
+
});
|
|
28
31
|
});
|
|
29
32
|
|
|
30
33
|
test('catch', async () => {
|
|
31
|
-
await
|
|
34
|
+
await expectToThrow(async () => {
|
|
32
35
|
await until(async () => {
|
|
33
36
|
await sleep(100);
|
|
34
37
|
throw new Error();
|
|
35
38
|
});
|
|
36
|
-
})
|
|
39
|
+
});
|
|
37
40
|
});
|
|
38
41
|
|
|
39
42
|
test('timeout', async () => {
|
|
40
|
-
await
|
|
43
|
+
await expectToThrow(async () => {
|
|
41
44
|
await until(async (resolve) => {
|
|
42
45
|
await sleep(500);
|
|
43
46
|
resolve();
|
|
44
47
|
}, 100); // Timeout before complete.
|
|
45
|
-
})
|
|
48
|
+
});
|
|
46
49
|
});
|
|
47
50
|
});
|
|
@@ -2,9 +2,10 @@
|
|
|
2
2
|
// Copyright 2023 DXOS.org
|
|
3
3
|
//
|
|
4
4
|
|
|
5
|
-
import {
|
|
5
|
+
import { expect } from 'chai';
|
|
6
6
|
|
|
7
7
|
import { Context } from '@dxos/context';
|
|
8
|
+
import { describe, test } from '@dxos/test';
|
|
8
9
|
|
|
9
10
|
import { sleep } from './timeout';
|
|
10
11
|
import { UpdateScheduler } from './update-scheduler';
|