@dxos/edge-client 0.7.5-labs.ea4b4c2 → 0.7.5-labs.f400bbc
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 +78 -187
- package/dist/lib/browser/index.mjs.map +4 -4
- package/dist/lib/browser/meta.json +1 -1
- package/dist/lib/node/index.cjs +63 -170
- package/dist/lib/node/index.cjs.map +4 -4
- package/dist/lib/node/meta.json +1 -1
- package/dist/lib/node-esm/index.mjs +78 -187
- package/dist/lib/node-esm/index.mjs.map +4 -4
- package/dist/lib/node-esm/meta.json +1 -1
- package/dist/types/src/edge-client.d.ts.map +1 -1
- package/dist/types/src/edge-http-client.d.ts +1 -0
- package/dist/types/src/edge-http-client.d.ts.map +1 -1
- package/package.json +15 -14
- package/src/edge-client.ts +1 -2
- package/src/edge-http-client.ts +4 -0
- package/dist/types/src/persistent-lifecycle.d.ts +0 -44
- package/dist/types/src/persistent-lifecycle.d.ts.map +0 -1
- package/dist/types/src/persistent-lifecycle.test.d.ts +0 -2
- package/dist/types/src/persistent-lifecycle.test.d.ts.map +0 -1
- package/src/persistent-lifecycle.test.ts +0 -71
- package/src/persistent-lifecycle.ts +0 -121
|
@@ -1,71 +0,0 @@
|
|
|
1
|
-
//
|
|
2
|
-
// Copyright 2024 DXOS.org
|
|
3
|
-
//
|
|
4
|
-
|
|
5
|
-
import { describe, expect, test } from 'vitest';
|
|
6
|
-
|
|
7
|
-
import { sleep, Trigger } from '@dxos/async';
|
|
8
|
-
import { log } from '@dxos/log';
|
|
9
|
-
import { openAndClose } from '@dxos/test-utils';
|
|
10
|
-
|
|
11
|
-
import { PersistentLifecycle } from './persistent-lifecycle';
|
|
12
|
-
|
|
13
|
-
describe('ConnectionState', () => {
|
|
14
|
-
test('first reconnect fires immediately', async () => {
|
|
15
|
-
const triggerCall = new Trigger<number>();
|
|
16
|
-
const persistentLifecycle = new PersistentLifecycle({
|
|
17
|
-
start: async () => {
|
|
18
|
-
triggerCall.wake(Date.now());
|
|
19
|
-
},
|
|
20
|
-
stop: async () => {},
|
|
21
|
-
});
|
|
22
|
-
await openAndClose(persistentLifecycle);
|
|
23
|
-
|
|
24
|
-
const triggerTimestamp = Date.now();
|
|
25
|
-
persistentLifecycle.scheduleRestart();
|
|
26
|
-
const timeToTrigger = (await triggerCall.wait({ timeout: 1000 })) - triggerTimestamp;
|
|
27
|
-
expect(timeToTrigger).to.be.lessThan(50);
|
|
28
|
-
});
|
|
29
|
-
|
|
30
|
-
test('second reconnect fires after 100ms', async () => {
|
|
31
|
-
let called = 0;
|
|
32
|
-
const triggerCall = new Trigger<number>();
|
|
33
|
-
|
|
34
|
-
const persistentLifecycle = new PersistentLifecycle({
|
|
35
|
-
start: async () => {
|
|
36
|
-
called += 1;
|
|
37
|
-
log.info('called', { called });
|
|
38
|
-
if (called < 3) {
|
|
39
|
-
throw new Error('TEST ERROR');
|
|
40
|
-
}
|
|
41
|
-
triggerCall.wake(Date.now());
|
|
42
|
-
},
|
|
43
|
-
stop: async () => {},
|
|
44
|
-
});
|
|
45
|
-
|
|
46
|
-
await openAndClose(persistentLifecycle);
|
|
47
|
-
|
|
48
|
-
const triggerTimestamp = Date.now();
|
|
49
|
-
await sleep(10);
|
|
50
|
-
const timeToTrigger = (await triggerCall.wait({ timeout: 1000 })) - triggerTimestamp;
|
|
51
|
-
expect(timeToTrigger).to.be.greaterThanOrEqual(100);
|
|
52
|
-
});
|
|
53
|
-
|
|
54
|
-
test('finish `restart` before close', async () => {
|
|
55
|
-
let restarted = false;
|
|
56
|
-
const persistentLifecycle = new PersistentLifecycle({
|
|
57
|
-
start: async () => await sleep(100),
|
|
58
|
-
stop: async () => {},
|
|
59
|
-
onRestart: async () => {
|
|
60
|
-
restarted = true;
|
|
61
|
-
},
|
|
62
|
-
});
|
|
63
|
-
|
|
64
|
-
await persistentLifecycle.open();
|
|
65
|
-
|
|
66
|
-
persistentLifecycle.scheduleRestart();
|
|
67
|
-
await sleep(10);
|
|
68
|
-
await persistentLifecycle.close();
|
|
69
|
-
expect(restarted).to.be.true;
|
|
70
|
-
});
|
|
71
|
-
});
|
|
@@ -1,121 +0,0 @@
|
|
|
1
|
-
//
|
|
2
|
-
// Copyright 2024 DXOS.org
|
|
3
|
-
//
|
|
4
|
-
|
|
5
|
-
import { DeferredTask, sleep, synchronized } from '@dxos/async';
|
|
6
|
-
import { cancelWithContext, LifecycleState, Resource } from '@dxos/context';
|
|
7
|
-
import { warnAfterTimeout } from '@dxos/debug';
|
|
8
|
-
import { log } from '@dxos/log';
|
|
9
|
-
|
|
10
|
-
const INIT_RESTART_DELAY = 100;
|
|
11
|
-
const DEFAULT_MAX_RESTART_DELAY = 5000;
|
|
12
|
-
|
|
13
|
-
export type PersistentLifecycleParams<T> = {
|
|
14
|
-
/**
|
|
15
|
-
* Create connection.
|
|
16
|
-
* If promise resolves successfully, connection is considered established.
|
|
17
|
-
*/
|
|
18
|
-
start: () => Promise<T | undefined>;
|
|
19
|
-
|
|
20
|
-
/**
|
|
21
|
-
* Reset connection to initial state.
|
|
22
|
-
*/
|
|
23
|
-
stop: (state: T) => Promise<void>;
|
|
24
|
-
|
|
25
|
-
/**
|
|
26
|
-
* Called after successful start.
|
|
27
|
-
*/
|
|
28
|
-
onRestart?: () => Promise<void>;
|
|
29
|
-
|
|
30
|
-
/**
|
|
31
|
-
* Maximum delay between restartion attempts.
|
|
32
|
-
* Default: 5000ms
|
|
33
|
-
*/
|
|
34
|
-
maxRestartDelay?: number;
|
|
35
|
-
};
|
|
36
|
-
|
|
37
|
-
/**
|
|
38
|
-
* Handles restarts (e.g. persists connection).
|
|
39
|
-
* Restarts are scheduled with exponential backoff.
|
|
40
|
-
*/
|
|
41
|
-
export class PersistentLifecycle<T> extends Resource {
|
|
42
|
-
private readonly _start: () => Promise<T | undefined>;
|
|
43
|
-
private readonly _stop: (state: T) => Promise<void>;
|
|
44
|
-
private readonly _onRestart?: () => Promise<void>;
|
|
45
|
-
private readonly _maxRestartDelay: number;
|
|
46
|
-
|
|
47
|
-
private _currentContext: T | undefined = undefined;
|
|
48
|
-
private _restartTask?: DeferredTask = undefined;
|
|
49
|
-
private _restartAfter = 0;
|
|
50
|
-
|
|
51
|
-
constructor({ start, stop, onRestart, maxRestartDelay = DEFAULT_MAX_RESTART_DELAY }: PersistentLifecycleParams<T>) {
|
|
52
|
-
super();
|
|
53
|
-
this._start = start;
|
|
54
|
-
this._stop = stop;
|
|
55
|
-
this._onRestart = onRestart;
|
|
56
|
-
this._maxRestartDelay = maxRestartDelay;
|
|
57
|
-
}
|
|
58
|
-
|
|
59
|
-
@synchronized
|
|
60
|
-
protected override async _open() {
|
|
61
|
-
this._restartTask = new DeferredTask(this._ctx, async () => {
|
|
62
|
-
try {
|
|
63
|
-
await this._restart();
|
|
64
|
-
} catch (err) {
|
|
65
|
-
log.warn('Restart failed', { err });
|
|
66
|
-
this._restartTask?.schedule();
|
|
67
|
-
}
|
|
68
|
-
});
|
|
69
|
-
this._currentContext = await this._start().catch((err) => {
|
|
70
|
-
log.warn('Start failed', { err });
|
|
71
|
-
this._restartTask?.schedule();
|
|
72
|
-
return undefined;
|
|
73
|
-
});
|
|
74
|
-
}
|
|
75
|
-
|
|
76
|
-
protected override async _close() {
|
|
77
|
-
await this._restartTask?.join();
|
|
78
|
-
await this._stopCurrentContext();
|
|
79
|
-
this._restartTask = undefined;
|
|
80
|
-
}
|
|
81
|
-
|
|
82
|
-
private async _restart() {
|
|
83
|
-
log(`restarting in ${this._restartAfter}ms`, { state: this._lifecycleState });
|
|
84
|
-
await this._stopCurrentContext();
|
|
85
|
-
if (this._lifecycleState !== LifecycleState.OPEN) {
|
|
86
|
-
return;
|
|
87
|
-
}
|
|
88
|
-
await cancelWithContext(this._ctx!, sleep(this._restartAfter));
|
|
89
|
-
this._restartAfter = Math.min(Math.max(this._restartAfter * 2, INIT_RESTART_DELAY), this._maxRestartDelay);
|
|
90
|
-
|
|
91
|
-
// May fail if the connection is not established.
|
|
92
|
-
await warnAfterTimeout(5_000, 'Connection establishment takes too long', async () => {
|
|
93
|
-
this._currentContext = await this._start();
|
|
94
|
-
});
|
|
95
|
-
|
|
96
|
-
this._restartAfter = 0;
|
|
97
|
-
await this._onRestart?.();
|
|
98
|
-
}
|
|
99
|
-
|
|
100
|
-
private async _stopCurrentContext() {
|
|
101
|
-
if (this._currentContext) {
|
|
102
|
-
try {
|
|
103
|
-
await this._stop(this._currentContext);
|
|
104
|
-
} catch (err) {
|
|
105
|
-
log.catch(err);
|
|
106
|
-
}
|
|
107
|
-
this._currentContext = undefined;
|
|
108
|
-
}
|
|
109
|
-
}
|
|
110
|
-
|
|
111
|
-
/**
|
|
112
|
-
* Scheduling restart should be done from outside.
|
|
113
|
-
*/
|
|
114
|
-
@synchronized
|
|
115
|
-
scheduleRestart() {
|
|
116
|
-
if (this._lifecycleState !== LifecycleState.OPEN) {
|
|
117
|
-
return;
|
|
118
|
-
}
|
|
119
|
-
this._restartTask!.schedule();
|
|
120
|
-
}
|
|
121
|
-
}
|