@antcde/connect-ts 0.4.27-rc.3 → 0.4.27-rc.5
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/build/comms/client.d.ts.map +1 -1
- package/build/comms/client.js +39 -41
- package/build/comms/connection.d.ts +32 -0
- package/build/comms/connection.d.ts.map +1 -0
- package/build/comms/connection.js +159 -0
- package/build/comms/handshake.d.ts +16 -0
- package/build/comms/handshake.d.ts.map +1 -0
- package/build/comms/handshake.js +73 -0
- package/build/comms/host.d.ts.map +1 -1
- package/build/comms/host.js +22 -26
- package/build/comms/hostSlot.d.ts +23 -0
- package/build/comms/hostSlot.d.ts.map +1 -0
- package/build/comms/hostSlot.js +55 -0
- package/build/comms/models.d.ts +11 -0
- package/build/comms/models.d.ts.map +1 -1
- package/build/comms/transport.d.ts +18 -2
- package/build/comms/transport.d.ts.map +1 -1
- package/build/comms/transport.js +25 -10
- package/build/index.d.ts +1 -1
- package/build/index.d.ts.map +1 -1
- package/build/service-runtime.d.ts +2 -2
- package/build/service-runtime.d.ts.map +1 -1
- package/build/service-runtime.js +2 -2
- package/build/service-types-app.d.ts +1 -1
- package/build/service-types-app.d.ts.map +1 -1
- package/build/services/AppService/factory/categories.d.ts +30 -0
- package/build/services/AppService/factory/categories.d.ts.map +1 -0
- package/build/services/AppService/factory/categories.js +11 -0
- package/build/services/AppService/factory/resourceAccess.d.ts +1 -1
- package/build/services/AppService/factory/resourceAccess.d.ts.map +1 -1
- package/build/services/AppService/factory/resourceAccess.js +5 -0
- package/build/services/AppService/factory/resourceAssignments.d.ts +66 -0
- package/build/services/AppService/factory/resourceAssignments.d.ts.map +1 -0
- package/build/services/AppService/factory/resourceAssignments.js +12 -0
- package/build/services/AppService/factory/resourceSnapshot.d.ts +13 -0
- package/build/services/AppService/factory/resourceSnapshot.d.ts.map +1 -1
- package/build/services/AppService/factory/resourceSnapshot.js +21 -0
- package/build/services/AppService/factory/resourceUploads.d.ts +37 -0
- package/build/services/AppService/factory/resourceUploads.d.ts.map +1 -0
- package/build/services/AppService/factory/resourceUploads.js +8 -0
- package/build/services/AppService/factory/resources.d.ts +15 -2
- package/build/services/AppService/factory/resources.d.ts.map +1 -1
- package/build/services/AppService/factory/resources.js +5 -0
- package/build/services/AppService/factory/taxonomy.d.ts +25 -0
- package/build/services/AppService/factory/taxonomy.d.ts.map +1 -0
- package/build/services/AppService/factory/taxonomy.js +5 -0
- package/build/services/AppService/index.d.ts +4 -5
- package/build/services/AppService/index.d.ts.map +1 -1
- package/build/services/AppService/index.js +4 -6
- package/build/services/AppService/types/appCategories.d.ts +28 -0
- package/build/services/AppService/types/appCategories.d.ts.map +1 -1
- package/build/services/AppService/types/resourceAssignments.d.ts +38 -0
- package/build/services/AppService/types/resourceAssignments.d.ts.map +1 -0
- package/build/services/AppService/types/resourceAssignments.js +10 -0
- package/build/services/AppService/types/resourceEntitlements.d.ts +10 -1
- package/build/services/AppService/types/resourceEntitlements.d.ts.map +1 -1
- package/build/services/AppService/types/resourceReleases.d.ts +13 -6
- package/build/services/AppService/types/resourceReleases.d.ts.map +1 -1
- package/build/services/AppService/types/sellableResources.d.ts +43 -1
- package/build/services/AppService/types/sellableResources.d.ts.map +1 -1
- package/build/services/AppService/types/snapshotFileSource.d.ts +110 -0
- package/build/services/AppService/types/snapshotFileSource.d.ts.map +1 -0
- package/build/services/AppService/types/snapshotFileSource.js +8 -0
- package/build/services/AppService/types/snapshotPayload.d.ts +30 -10
- package/build/services/AppService/types/snapshotPayload.d.ts.map +1 -1
- package/build/services/AppService/types/snapshotPayload.js +5 -2
- package/build/services/AppService/types/snapshotSources.d.ts +19 -1
- package/build/services/AppService/types/snapshotSources.d.ts.map +1 -1
- package/build/services/AppService/types/snapshotUploads.d.ts +109 -0
- package/build/services/AppService/types/snapshotUploads.d.ts.map +1 -0
- package/build/services/AppService/types/snapshotUploads.js +20 -0
- package/build/services/AppService/types/taxonomy.d.ts +76 -0
- package/build/services/AppService/types/taxonomy.d.ts.map +1 -0
- package/build/services/AppService/types/taxonomy.js +1 -0
- package/build/services/AppService/types.d.ts +4 -0
- package/build/services/AppService/types.d.ts.map +1 -1
- package/build/services/AppService/types.js +4 -0
- package/build/services/ProjectService.d.ts +20 -0
- package/build/services/ProjectService.d.ts.map +1 -1
- package/build/services/ProjectService.js +1 -0
- package/build/services/Webhook/TriggerService.d.ts +44 -0
- package/build/services/Webhook/TriggerService.d.ts.map +1 -1
- package/build/services/Webhook/TriggerService.js +15 -0
- package/package.json +4 -4
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"client.d.ts","sourceRoot":"","sources":["../../src/comms/client.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,kBAAkB,EAAE,MAAM,OAAO,CAAA;AAC/C,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,MAAM,CAAA;
|
|
1
|
+
{"version":3,"file":"client.d.ts","sourceRoot":"","sources":["../../src/comms/client.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,kBAAkB,EAAE,MAAM,OAAO,CAAA;AAC/C,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,MAAM,CAAA;AACtC,OAAO,KAAK,EAAE,OAAO,EAAE,OAAO,EAAE,aAAa,EAAE,OAAO,EAAE,MAAM,UAAU,CAAA;AACxE,OAAO,KAAK,EAAU,cAAc,EAAE,YAAY,EAAE,MAAM,UAAU,CAAA;AAEpE,OAAO,KAAK,EAAE,UAAU,EAAE,sBAAoB;AAC9C,OAAO,KAAK,EAAE,WAAW,EAAE,+BAA6B;AACxD,OAAO,EAML,YAAY,EACb,MAAM,MAAM,CAAA;AAMb,KAAK,iBAAiB,GAAG,CAAC,KAAK,EAAE,QAAQ,EAAE,SAAS,EAAE,QAAQ,EAAE,MAAM,CAAC,CAAA;AACvE,MAAM,MAAM,YAAY,CAAC,CAAC,GAAG,GAAG,IAAI,IAAI,CAAC,kBAAkB,CAAC,CAAC,CAAC,EAAE,iBAAiB,CAAC,MAA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AC,CAAC,CAAA;CAChG;AAED,wBAAgB,cAAc,CAAC,MAAM,EAAE,YAAY,GAAG,MAAM,CA+F3D"}
|
package/build/comms/client.js
CHANGED
|
@@ -1,8 +1,7 @@
|
|
|
1
|
-
import * as Comlink from 'comlink';
|
|
2
1
|
import { BehaviorSubject, distinctUntilChanged, map, shareReplay, Subject, Subscription, } from 'rxjs';
|
|
2
|
+
import { newHostConnection } from './connection';
|
|
3
3
|
import { notificationTypes } from './models';
|
|
4
4
|
import { newSignaler } from './signal';
|
|
5
|
-
import { receivePort, requestPort } from './transport';
|
|
6
5
|
import { clone, newScope, takeUntilClosed } from './utils';
|
|
7
6
|
export function newCommsClient(params) {
|
|
8
7
|
const subscription = newScope();
|
|
@@ -10,49 +9,48 @@ export function newCommsClient(params) {
|
|
|
10
9
|
const toolbarSubject = new BehaviorSubject({ menu: [], search: { enabled: false, input: '' }, isLoading: false });
|
|
11
10
|
const appStateSubject = new BehaviorSubject({ timestamp: 0, state: '' });
|
|
12
11
|
const incomingSignals = new Subject();
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
})
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
}));
|
|
31
|
-
await proxy.onAppState(Comlink.proxy((state) => appStateSubject.next(state)));
|
|
32
|
-
await proxy.onToolbarClick(Comlink.proxy((index) => toolbarSubject.value.menu[index]?.onClick?.()));
|
|
33
|
-
await proxy.onToolbarSearch(Comlink.proxy((value) => {
|
|
12
|
+
const { host, close } = newHostConnection({
|
|
13
|
+
parent: params.parent,
|
|
14
|
+
manifest: params.manifest,
|
|
15
|
+
initial: params.initialConnection?.port
|
|
16
|
+
? {
|
|
17
|
+
port: params.initialConnection.port,
|
|
18
|
+
context: params.initialConnection.context,
|
|
19
|
+
// Carried through so the relayed handover pins the same clientId that
|
|
20
|
+
// later handovers are checked against.
|
|
21
|
+
clientId: params.initialConnection.clientId,
|
|
22
|
+
}
|
|
23
|
+
: undefined,
|
|
24
|
+
onContext: context => contextSubject.next(context),
|
|
25
|
+
onSignal: signals => incomingSignals.next({ data: signals, origin: '', source: null }),
|
|
26
|
+
onAppState: state => appStateSubject.next(state),
|
|
27
|
+
onToolbarClick: index => toolbarSubject.value.menu[index]?.onClick?.(),
|
|
28
|
+
onToolbarSearch: (value) => {
|
|
34
29
|
const t = toolbarSubject.value;
|
|
35
30
|
toolbarSubject.next({ ...t, search: { ...t.search, input: value ?? '' } });
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
31
|
+
},
|
|
32
|
+
});
|
|
33
|
+
subscription.add(close);
|
|
34
|
+
// Subscribed once and resolved per emission: a re-adopted proxy has to receive
|
|
35
|
+
// toolbar updates too, which a `then`-captured reference would not.
|
|
36
|
+
toolbarSubject.pipe(takeUntilClosed(subscription)).subscribe(t => void host()
|
|
37
|
+
.then(p => p.setToolbar({ ...t, menu: t.menu.map(({ onClick: _onClick, ...item }) => item) }))
|
|
38
|
+
.catch(() => { }));
|
|
43
39
|
const signal = newSignaler({
|
|
44
|
-
send: async (signals) => (await
|
|
40
|
+
send: async (signals) => (await host()).sendSignal(clone(signals)),
|
|
45
41
|
receiver: incomingSignals.asObservable(),
|
|
46
42
|
});
|
|
47
43
|
const notepad = {
|
|
48
|
-
show: (tab, options) =>
|
|
49
|
-
showTask: (task, select) =>
|
|
50
|
-
hide: () =>
|
|
51
|
-
selectSbs: item =>
|
|
44
|
+
show: (tab, options) => host().then(p => p.notepad('show/tab', { tab, options })),
|
|
45
|
+
showTask: (task, select) => host().then(p => p.notepad('show/task', { task, select })),
|
|
46
|
+
hide: () => host().then(p => p.notepad('hide', {})),
|
|
47
|
+
selectSbs: item => host().then(p => p.notepad('sbs/select', { item })),
|
|
52
48
|
};
|
|
53
49
|
const notifications = notificationTypes.reduce((result, type) => Object.assign(result, {
|
|
50
|
+
// Fire-and-forget at every call site, so a missing host must not surface as
|
|
51
|
+
// an unhandled rejection on top of the error `host()` already logs.
|
|
54
52
|
[type]: (message) => {
|
|
55
|
-
const promise =
|
|
53
|
+
const promise = host().then(p => p.notify({ type, message })).catch(() => undefined);
|
|
56
54
|
return Object.assign(promise, new Subscription());
|
|
57
55
|
},
|
|
58
56
|
}), {});
|
|
@@ -62,7 +60,7 @@ export function newCommsClient(params) {
|
|
|
62
60
|
connect: params.connect ?? new Proxy({}, {
|
|
63
61
|
get: (_, service) => new Proxy({}, {
|
|
64
62
|
get: (_, name) => typeof service === 'string' && typeof name === 'string'
|
|
65
|
-
? (...args) =>
|
|
63
|
+
? (...args) => host().then(p => p.connect(service, name, clone(args)))
|
|
66
64
|
: undefined,
|
|
67
65
|
}),
|
|
68
66
|
}),
|
|
@@ -76,11 +74,11 @@ export function newCommsClient(params) {
|
|
|
76
74
|
get: () => toolbarSubject.value,
|
|
77
75
|
}),
|
|
78
76
|
appState: Object.assign(appStateSubject.pipe(map(e => e.state), distinctUntilChanged()), {
|
|
79
|
-
get: () =>
|
|
80
|
-
set: (state) =>
|
|
77
|
+
get: () => host().then(p => p.getAppState()).then(s => s.state),
|
|
78
|
+
set: (state) => host().then(p => p.setAppState({ timestamp: Date.now(), state })).then(() => { }),
|
|
81
79
|
}),
|
|
82
80
|
signal,
|
|
83
|
-
request: (req, _controller) =>
|
|
84
|
-
deprecatedRequest: (req, _controller) =>
|
|
81
|
+
request: (req, _controller) => host().then(p => p.request(req)),
|
|
82
|
+
deprecatedRequest: (req, _controller) => host().then(p => p.deprecatedRequest(req)),
|
|
85
83
|
});
|
|
86
84
|
}
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import type { HostAPI } from './hostAPI';
|
|
2
|
+
import type { Context } from './models';
|
|
3
|
+
import type { Signal } from './signal';
|
|
4
|
+
import type { PortHandover } from './transport';
|
|
5
|
+
import type { AppStateData } from './utils';
|
|
6
|
+
import type { AppManifest } from "../services/AppService";
|
|
7
|
+
import * as Comlink from 'comlink';
|
|
8
|
+
export declare const NO_HOST_CONNECTION = "ant-comms: no connection to the ANT OS host \u2014 the app cannot reach the API";
|
|
9
|
+
export interface HostConnectionParams {
|
|
10
|
+
parent: Window;
|
|
11
|
+
manifest?: AppManifest | null;
|
|
12
|
+
/** Pre-established port for nested clients (e.g. Streamlit behind a relay). */
|
|
13
|
+
initial?: PortHandover;
|
|
14
|
+
onContext: (context: Context) => void;
|
|
15
|
+
onSignal: (signals: Signal[]) => void;
|
|
16
|
+
onAppState: (state: AppStateData) => void;
|
|
17
|
+
onToolbarClick: (index: number) => void;
|
|
18
|
+
onToolbarSearch: (value: string) => void;
|
|
19
|
+
}
|
|
20
|
+
export interface HostConnection {
|
|
21
|
+
/**
|
|
22
|
+
* The live host proxy, or a rejection once it is clear there will not be one.
|
|
23
|
+
*
|
|
24
|
+
* Call this per request rather than capturing the result: a retried handshake
|
|
25
|
+
* replaces the port, and anything issued afterwards has to travel over the
|
|
26
|
+
* port that is actually open.
|
|
27
|
+
*/
|
|
28
|
+
host: () => Promise<Comlink.Remote<HostAPI>>;
|
|
29
|
+
close: () => void;
|
|
30
|
+
}
|
|
31
|
+
export declare function newHostConnection(params: HostConnectionParams): HostConnection;
|
|
32
|
+
//# sourceMappingURL=connection.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"connection.d.ts","sourceRoot":"","sources":["../../src/comms/connection.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,WAAW,CAAA;AACxC,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,UAAU,CAAA;AACvC,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,UAAU,CAAA;AACtC,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,aAAa,CAAA;AAC/C,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,SAAS,CAAA;AAC3C,OAAO,KAAK,EAAE,WAAW,EAAE,+BAA6B;AACxD,OAAO,KAAK,OAAO,MAAM,SAAS,CAAA;AA2ClC,eAAO,MAAM,kBAAkB,oFACiD,CAAA;AAehF,MAAM,WAAW,oBAAoB;IACnC,MAAM,EAAE,MAAM,CAAA;IACd,QAAQ,CAAC,EAAE,WAAW,GAAG,IAAI,CAAA;IAC7B,+EAA+E;IAC/E,OAAO,CAAC,EAAE,YAAY,CAAA;IACtB,SAAS,EAAE,CAAC,OAAO,EAAE,OAAO,KAAK,IAAI,CAAA;IACrC,QAAQ,EAAE,CAAC,OAAO,EAAE,MAAM,EAAE,KAAK,IAAI,CAAA;IACrC,UAAU,EAAE,CAAC,KAAK,EAAE,YAAY,KAAK,IAAI,CAAA;IACzC,cAAc,EAAE,CAAC,KAAK,EAAE,MAAM,KAAK,IAAI,CAAA;IACvC,eAAe,EAAE,CAAC,KAAK,EAAE,MAAM,KAAK,IAAI,CAAA;CACzC;AAED,MAAM,WAAW,cAAc;IAC7B;;;;;;OAMG;IACH,IAAI,EAAE,MAAM,OAAO,CAAC,OAAO,CAAC,MAAM,CAAC,OAAO,CAAC,CAAC,CAAA;IAC5C,KAAK,EAAE,MAAM,IAAI,CAAA;CAClB;AAED,wBAAgB,iBAAiB,CAAC,MAAM,EAAE,oBAAoB,GAAG,cAAc,CAwH9E"}
|
|
@@ -0,0 +1,159 @@
|
|
|
1
|
+
import * as Comlink from 'comlink';
|
|
2
|
+
import { newHostSlot } from './hostSlot';
|
|
3
|
+
import { onPort, requestPort } from './transport';
|
|
4
|
+
/**
|
|
5
|
+
* Owns the port handover between an app and its ANT OS host, and keeps it alive.
|
|
6
|
+
*
|
|
7
|
+
* Both halves of the handover are single unbuffered postMessages: the client
|
|
8
|
+
* announces READY, the host answers with INIT and a transferred port. Either can
|
|
9
|
+
* land while the far side has no listener yet — the host only arms its READY
|
|
10
|
+
* listener once it knows which window to accept it from, and the client's is not
|
|
11
|
+
* installed until its bundle runs. One attempt each meant a miss was permanent:
|
|
12
|
+
* the app mounted, rendered, and then never fetched anything, because every
|
|
13
|
+
* `connect()` call awaited a proxy that would never arrive.
|
|
14
|
+
*
|
|
15
|
+
* So both sides now keep trying, which is what the legacy post-robot path has
|
|
16
|
+
* always done with its init interval (see comms/compat.ts) and why it never
|
|
17
|
+
* showed this failure. The host re-exposes on every READY it sees; this side
|
|
18
|
+
* re-announces until a port lands, and adopts each port the host offers.
|
|
19
|
+
*/
|
|
20
|
+
/** Re-announce this often until the host hands a port over. */
|
|
21
|
+
const HANDSHAKE_RETRY_MS = 500;
|
|
22
|
+
/**
|
|
23
|
+
* Hold off re-announcing for this long after a port arrives.
|
|
24
|
+
*
|
|
25
|
+
* Negotiating one costs several round trips, and the host closes the previous
|
|
26
|
+
* port every time it answers an announcement — so announcing again mid-negotiation
|
|
27
|
+
* would pull the port out from under the negotiation that is already succeeding,
|
|
28
|
+
* on repeat. A bounded pause rather than a flag: if negotiation genuinely stalls
|
|
29
|
+
* (a port whose host has gone away never settles), retrying has to resume rather
|
|
30
|
+
* than wait on a promise that will not.
|
|
31
|
+
*/
|
|
32
|
+
const NEGOTIATION_GRACE_MS = 2000;
|
|
33
|
+
/**
|
|
34
|
+
* Stop waiting for a host after this long and reject calls instead of leaving
|
|
35
|
+
* them pending forever. A permanently unresolved promise is the worst failure
|
|
36
|
+
* mode available here: no request, no error, no timeout — nothing to see.
|
|
37
|
+
*/
|
|
38
|
+
const HANDSHAKE_TIMEOUT_MS = 20000;
|
|
39
|
+
export const NO_HOST_CONNECTION = 'ant-comms: no connection to the ANT OS host — the app cannot reach the API';
|
|
40
|
+
/**
|
|
41
|
+
* Whether there is a host to announce to at all.
|
|
42
|
+
*
|
|
43
|
+
* A standalone page is its own parent and has none, and should not spend the
|
|
44
|
+
* tab's lifetime posting messages to itself. Compared against `window` rather
|
|
45
|
+
* than `globalThis` because only the realm's window object is type-compatible
|
|
46
|
+
* with the parent handle; where there is no DOM at all (tests) the caller's
|
|
47
|
+
* parent is a stand-in and worth announcing to.
|
|
48
|
+
*/
|
|
49
|
+
function isEmbedded(parent) {
|
|
50
|
+
return typeof window === 'undefined' || parent !== window;
|
|
51
|
+
}
|
|
52
|
+
export function newHostConnection(params) {
|
|
53
|
+
let releaseCurrent = null;
|
|
54
|
+
/**
|
|
55
|
+
* Bumped per handover. One that finishes negotiating after a newer one started
|
|
56
|
+
* is holding a port the host has already closed, so adopting it would produce
|
|
57
|
+
* a client that looks connected and answers nothing.
|
|
58
|
+
*/
|
|
59
|
+
let generation = 0;
|
|
60
|
+
let closed = false;
|
|
61
|
+
/**
|
|
62
|
+
* The host's own id, learned from the first handover. Later handovers must
|
|
63
|
+
* carry the same one: a forged INIT that reached us despite the source check
|
|
64
|
+
* must not be able to swap the adopted proxy for one it controls.
|
|
65
|
+
*/
|
|
66
|
+
let acceptedClientId = null;
|
|
67
|
+
const slot = newHostSlot(HANDSHAKE_TIMEOUT_MS, NO_HOST_CONNECTION);
|
|
68
|
+
const negotiate = async (handover) => {
|
|
69
|
+
if (closed) {
|
|
70
|
+
handover.port.close();
|
|
71
|
+
return;
|
|
72
|
+
}
|
|
73
|
+
const mine = ++generation;
|
|
74
|
+
params.onContext(handover.context);
|
|
75
|
+
const candidate = Comlink.wrap(handover.port);
|
|
76
|
+
const release = () => {
|
|
77
|
+
candidate[Comlink.releaseProxy]();
|
|
78
|
+
handover.port.close();
|
|
79
|
+
};
|
|
80
|
+
// Registered together rather than in sequence: each is its own round trip,
|
|
81
|
+
// and five of them stretch the window during which a retry could fire and
|
|
82
|
+
// pull this port out from under us.
|
|
83
|
+
await Promise.all([
|
|
84
|
+
candidate.onContext(Comlink.proxy(params.onContext)),
|
|
85
|
+
candidate.onSignal(Comlink.proxy(params.onSignal)),
|
|
86
|
+
candidate.onAppState(Comlink.proxy(params.onAppState)),
|
|
87
|
+
candidate.onToolbarClick(Comlink.proxy(params.onToolbarClick)),
|
|
88
|
+
candidate.onToolbarSearch(Comlink.proxy(params.onToolbarSearch)),
|
|
89
|
+
]);
|
|
90
|
+
// Last, so readiness still means "every callback is wired".
|
|
91
|
+
await candidate.ready(params.manifest ?? null);
|
|
92
|
+
if (mine !== generation || closed) {
|
|
93
|
+
release();
|
|
94
|
+
return;
|
|
95
|
+
}
|
|
96
|
+
acceptedClientId ?? (acceptedClientId = handover.clientId);
|
|
97
|
+
releaseCurrent?.();
|
|
98
|
+
releaseCurrent = release;
|
|
99
|
+
slot.set(candidate);
|
|
100
|
+
};
|
|
101
|
+
// Negotiation over a port the host has since closed simply never settles, so
|
|
102
|
+
// the generation check decides the winner rather than the order these resolve.
|
|
103
|
+
let quietUntil = 0;
|
|
104
|
+
const stopListening = onPort(isEmbedded(params.parent) ? params.parent : null, (handover) => {
|
|
105
|
+
// A handover naming a different host than the one we already accepted is not
|
|
106
|
+
// ours to adopt, whatever its source claims to be.
|
|
107
|
+
if (acceptedClientId !== null && handover.clientId !== acceptedClientId) {
|
|
108
|
+
handover.port.close();
|
|
109
|
+
return;
|
|
110
|
+
}
|
|
111
|
+
quietUntil = Date.now() + NEGOTIATION_GRACE_MS;
|
|
112
|
+
void negotiate(handover).catch(() => { });
|
|
113
|
+
});
|
|
114
|
+
let announceTimer = null;
|
|
115
|
+
let giveUpTimer = null;
|
|
116
|
+
const stopAnnouncing = () => {
|
|
117
|
+
if (announceTimer !== null) {
|
|
118
|
+
clearInterval(announceTimer);
|
|
119
|
+
announceTimer = null;
|
|
120
|
+
}
|
|
121
|
+
if (giveUpTimer !== null) {
|
|
122
|
+
clearTimeout(giveUpTimer);
|
|
123
|
+
giveUpTimer = null;
|
|
124
|
+
}
|
|
125
|
+
};
|
|
126
|
+
if (isEmbedded(params.parent)) {
|
|
127
|
+
requestPort(params.parent);
|
|
128
|
+
announceTimer = setInterval(() => {
|
|
129
|
+
if (slot.peek())
|
|
130
|
+
return stopAnnouncing();
|
|
131
|
+
if (Date.now() < quietUntil)
|
|
132
|
+
return;
|
|
133
|
+
requestPort(params.parent);
|
|
134
|
+
}, HANDSHAKE_RETRY_MS);
|
|
135
|
+
// Give up in step with `host()`, so a genuinely host-less page stops posting
|
|
136
|
+
// rather than retrying for the lifetime of the tab. A host that shows up
|
|
137
|
+
// later still re-exposes on its own, and `onPort` is still listening.
|
|
138
|
+
giveUpTimer = setTimeout(stopAnnouncing, HANDSHAKE_TIMEOUT_MS);
|
|
139
|
+
}
|
|
140
|
+
if (params.initial)
|
|
141
|
+
void negotiate(params.initial).catch(() => { });
|
|
142
|
+
return {
|
|
143
|
+
host: slot.get,
|
|
144
|
+
close: () => {
|
|
145
|
+
if (closed)
|
|
146
|
+
return;
|
|
147
|
+
closed = true;
|
|
148
|
+
// Advance past every in-flight negotiation so none can adopt after this,
|
|
149
|
+
// and fail anything already waiting rather than leaving it to time out
|
|
150
|
+
// against a connection that is never coming back.
|
|
151
|
+
generation++;
|
|
152
|
+
stopAnnouncing();
|
|
153
|
+
stopListening();
|
|
154
|
+
releaseCurrent?.();
|
|
155
|
+
releaseCurrent = null;
|
|
156
|
+
slot.reset();
|
|
157
|
+
},
|
|
158
|
+
};
|
|
159
|
+
}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import type { BehaviorSubject, Observable, Subscription } from 'rxjs';
|
|
2
|
+
import type { HostAPI } from './hostAPI';
|
|
3
|
+
import type { Context } from './models';
|
|
4
|
+
export interface HandshakeParams {
|
|
5
|
+
clientId: string;
|
|
6
|
+
scope: Subscription;
|
|
7
|
+
/** Emits the pane's iframe window, and again whenever it is replaced. */
|
|
8
|
+
windowChanges: Observable<Window | undefined>;
|
|
9
|
+
contextChanges: BehaviorSubject<Context>;
|
|
10
|
+
connectionInitialized: Observable<void>;
|
|
11
|
+
hostAPI: HostAPI;
|
|
12
|
+
/** Drops callbacks bound to a port that is about to be replaced. */
|
|
13
|
+
resetCallbacks: () => void;
|
|
14
|
+
}
|
|
15
|
+
export declare function installHandshake(params: HandshakeParams): void;
|
|
16
|
+
//# sourceMappingURL=handshake.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"handshake.d.ts","sourceRoot":"","sources":["../../src/comms/handshake.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,eAAe,EAAE,UAAU,EAAE,YAAY,EAAE,MAAM,MAAM,CAAA;AACrE,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,WAAW,CAAA;AACxC,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,UAAU,CAAA;AA+BvC,MAAM,WAAW,eAAe;IAC9B,QAAQ,EAAE,MAAM,CAAA;IAChB,KAAK,EAAE,YAAY,CAAA;IACnB,yEAAyE;IACzE,aAAa,EAAE,UAAU,CAAC,MAAM,GAAG,SAAS,CAAC,CAAA;IAC7C,cAAc,EAAE,eAAe,CAAC,OAAO,CAAC,CAAA;IACxC,qBAAqB,EAAE,UAAU,CAAC,IAAI,CAAC,CAAA;IACvC,OAAO,EAAE,OAAO,CAAA;IAChB,oEAAoE;IACpE,cAAc,EAAE,MAAM,IAAI,CAAA;CAC3B;AAED,wBAAgB,gBAAgB,CAAC,MAAM,EAAE,eAAe,GAAG,IAAI,CAoD9D"}
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
import * as Comlink from 'comlink';
|
|
2
|
+
import { filter, take } from 'rxjs';
|
|
3
|
+
import { onPortRequest, transferPort } from './transport';
|
|
4
|
+
import { takeUntilClosed } from './utils';
|
|
5
|
+
/**
|
|
6
|
+
* The host half of the port handover, and keeping it alive.
|
|
7
|
+
*
|
|
8
|
+
* The handover is one unbuffered postMessage in each direction: the client
|
|
9
|
+
* announces READY, the host answers with INIT and a transferred port. Either can
|
|
10
|
+
* land while the far side has no listener yet — the client's bundle may not have
|
|
11
|
+
* run when the host first exposes, and the host's READY listener is only armed
|
|
12
|
+
* once it knows which window to accept it from.
|
|
13
|
+
*
|
|
14
|
+
* Answering only the first announcement turned a single miss into a permanently
|
|
15
|
+
* dead channel: every `connect()` call from the app awaited a proxy that never
|
|
16
|
+
* arrived, with no request, no error and no timeout to show for it. So the client
|
|
17
|
+
* re-announces until a port lands (comms/connection.ts) and the host re-exposes
|
|
18
|
+
* for every announcement it sees. Convergence needs both halves. This is what the
|
|
19
|
+
* legacy post-robot path has always done with its init interval (comms/compat.ts),
|
|
20
|
+
* and why it never showed this failure.
|
|
21
|
+
*/
|
|
22
|
+
/**
|
|
23
|
+
* How long a bound client may go without completing the handshake before the host
|
|
24
|
+
* says so. Matches the client's own give-up window so both ends report the same
|
|
25
|
+
* failure at the same time.
|
|
26
|
+
*/
|
|
27
|
+
const HANDSHAKE_WATCHDOG_MS = 20000;
|
|
28
|
+
export function installHandshake(params) {
|
|
29
|
+
const { clientId, scope, hostAPI, resetCallbacks } = params;
|
|
30
|
+
let currentPort = null;
|
|
31
|
+
let disposeReadyListener = null;
|
|
32
|
+
let watchdog = null;
|
|
33
|
+
const expose = (target) => {
|
|
34
|
+
currentPort?.close();
|
|
35
|
+
currentPort = transferPort(target, { clientId, context: params.contextChanges.value });
|
|
36
|
+
Comlink.expose(hostAPI, currentPort);
|
|
37
|
+
};
|
|
38
|
+
// Declared ahead of the subscription below: windowChanges is a ReplaySubject,
|
|
39
|
+
// so subscribing can emit synchronously, and anything the callback reaches for
|
|
40
|
+
// must already be initialised.
|
|
41
|
+
const clearWatchdog = () => {
|
|
42
|
+
if (watchdog !== null) {
|
|
43
|
+
clearTimeout(watchdog);
|
|
44
|
+
// Nulled, not just cleared, so a later window re-arms it — otherwise a
|
|
45
|
+
// replaced iframe that never connects is never reported.
|
|
46
|
+
watchdog = null;
|
|
47
|
+
}
|
|
48
|
+
};
|
|
49
|
+
params.windowChanges.pipe(filter(Boolean), takeUntilClosed(scope)).subscribe((iframeWindow) => {
|
|
50
|
+
// Re-armed per window: the previous listener only accepts READY from the
|
|
51
|
+
// window it was created for, which is no longer the one in the pane.
|
|
52
|
+
disposeReadyListener?.();
|
|
53
|
+
disposeReadyListener = onPortRequest(iframeWindow, (source) => {
|
|
54
|
+
resetCallbacks();
|
|
55
|
+
expose(source);
|
|
56
|
+
});
|
|
57
|
+
expose(iframeWindow);
|
|
58
|
+
// Report a channel that never came up from this side too. The embedder clears
|
|
59
|
+
// its loading overlay on a grace period, so an app whose handshake failed
|
|
60
|
+
// looks loaded and merely idle, and the app's own error lands in the iframe's
|
|
61
|
+
// console rather than the one anyone opens first. Armed on the first window
|
|
62
|
+
// rather than at construction, so a pane that never opens an app stays quiet.
|
|
63
|
+
watchdog ?? (watchdog = setTimeout(() => {
|
|
64
|
+
console.warn(`ant-comms: client ${clientId} never completed the port handshake — the app cannot reach the API`);
|
|
65
|
+
}, HANDSHAKE_WATCHDOG_MS));
|
|
66
|
+
});
|
|
67
|
+
params.connectionInitialized.pipe(take(1), takeUntilClosed(scope)).subscribe(clearWatchdog);
|
|
68
|
+
scope.add(() => {
|
|
69
|
+
disposeReadyListener?.();
|
|
70
|
+
clearWatchdog();
|
|
71
|
+
currentPort?.close();
|
|
72
|
+
});
|
|
73
|
+
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"host.d.ts","sourceRoot":"","sources":["../../src/comms/host.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,UAAU,EAAE,OAAO,EAAE,gBAAgB,EAAE,MAAM,MAAM,CAAA;AACjE,OAAO,KAAK,EAAE,eAAe,EAAiB,MAAM,WAAW,CAAA;AAC/D,OAAO,KAAK,EAAE,OAAO,EAAE,OAAO,EAAE,YAAY,EAAE,OAAO,EAAE,MAAM,UAAU,CAAA;AACvE,OAAO,KAAK,EAAE,MAAM,EAAE,YAAY,EAAE,MAAM,EAAE,cAAc,EAAE,YAAY,EAAE,MAAM,UAAU,CAAA;AAE1F,OAAO,KAAK,EAAE,UAAU,EAAE,sBAAoB;AAC9C,OAAO,KAAK,EAAE,WAAW,EAAE,+BAA6B;
|
|
1
|
+
{"version":3,"file":"host.d.ts","sourceRoot":"","sources":["../../src/comms/host.ts"],"names":[],"mappings":"AAAA,OAAO,KAA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,QAAQ,EAAE,MAAM,CAAA;IAChB,KAAK,EAAE,YAAY,CAAA;IACnB,qBAAqB,EAAE,OAAO,CAAC,IAAI,CAAC,CAAA;IACpC,eAAe,EAAE,aAAa,CAAC,WAAW,GAAG,IAAI,CAAC,CAAA;CACnD;AAED,MAAM,MAAM,WAAW,GAAG,CAAC,GAAG,EAAE,GAAG,EAAE,IAAI,EAAE,eAAe,EAAE,IAAI,EAAE,MAAM,OAAO,CAAC,GAAG,CAAC,KAAK,OAAO,CAAC,GAAG,CAAC,CAAA;AAErG,MAAM,WAAW,IAAK,SAAQ,gBAAgB;IAC5C,OAAO,EAAE,UAAU,CAAC,OAAO,CAAC,GAAG;QAAE,GAAG,EAAE,MAAM,OAAO,CAAC;QAAC,GAAG,EAAE;YAAE,MAAM,EAAE,CAAC,KAAK,EAAE,MAAM,KAAK,OAAO,CAAC,OAAO,CAAC,CAAA;SAAE,CAAA;KAAE,CAAA;IAC3G,uBAAuB,EAAE,UAAU,CAAC,OAAO,CAAC,CAAA;IAC5C,QAAQ,EAAE,UAAU,CAAC,WAAW,GAAG,IAAI,CAAC,CAAA;IACxC,MAAM,EAAE,cAAc,GAAG,YAAY,CAAA;IACrC,QAAQ,EAAE;QACR,OAAO,EAAE,UAAU,CAAC,MAAM,CAAC,CAAA;QAC3B,GAAG,EAAE,MAAM,MAAM,CAAA;QACjB,GAAG,EAAE,CAAC,KAAK,EAAE,MAAM,KAAK,IAAI,CAAA;KAC7B,CAAA;IACD,KAAK,EAAE,YAAY,CAAA;CACpB;AAED,wBAAgB,YAAY,CAAC,MAAM,EAAE,UAAU,GAAG,IAAI,CAmGrD"}
|
package/build/comms/host.js
CHANGED
|
@@ -1,11 +1,21 @@
|
|
|
1
|
-
import
|
|
2
|
-
import { BehaviorSubject, combineLatest, distinctUntilChanged, filter, firstValueFrom, map, ReplaySubject, Subject as RxSubject, share, startWith, Subscription, take, } from 'rxjs';
|
|
1
|
+
import { BehaviorSubject, combineLatest, distinctUntilChanged, firstValueFrom, map, ReplaySubject, Subject as RxSubject, share, startWith, Subscription, take, } from 'rxjs';
|
|
3
2
|
import { installCompat } from './compat';
|
|
3
|
+
import { installHandshake } from './handshake';
|
|
4
4
|
import { createHostAPI } from './hostAPI';
|
|
5
5
|
import { recordServiceCalls } from './recordServiceCalls';
|
|
6
6
|
import { changes, newSignaler } from './signal';
|
|
7
|
-
import { onPortRequest, transferPort } from './transport';
|
|
8
7
|
import { clone, newId, newScope, takeUntilClosed } from './utils';
|
|
8
|
+
/**
|
|
9
|
+
* The "nothing wired" callback set, for both the initial state and the reset a
|
|
10
|
+
* re-exposed port performs. One definition so a newly added callback cannot be
|
|
11
|
+
* cleared in one place and left dangling in the other.
|
|
12
|
+
*
|
|
13
|
+
* A factory, not a shared constant: the result becomes (or is assigned over) the
|
|
14
|
+
* live `callbacks` object, and a shared literal would alias every host to it.
|
|
15
|
+
*/
|
|
16
|
+
function noCallbacks() {
|
|
17
|
+
return { context: null, signal: null, appState: null, toolbarClick: null, toolbarSearch: null };
|
|
18
|
+
}
|
|
9
19
|
export function newCommsHost(params) {
|
|
10
20
|
const scope = new Subscription();
|
|
11
21
|
const clientId = newId();
|
|
@@ -17,7 +27,7 @@ export function newCommsHost(params) {
|
|
|
17
27
|
const box = { scope, params: effectiveParams, clientId, connectionInitialized, manifestSubject };
|
|
18
28
|
const appState = new BehaviorSubject({ timestamp: 0, state: params.appState ?? '' });
|
|
19
29
|
const toolbar = new BehaviorSubject({ menu: [], search: { enabled: false, input: '' }, isLoading: false });
|
|
20
|
-
const callbacks =
|
|
30
|
+
const callbacks = noCallbacks();
|
|
21
31
|
const incomingSignals = new RxSubject();
|
|
22
32
|
const defaultConnectCall = ({ params }, { service, name, args }) =>
|
|
23
33
|
// @ts-expect-error dynamic service dispatch by name
|
|
@@ -37,27 +47,14 @@ export function newCommsHost(params) {
|
|
|
37
47
|
onClientSignal: signals => incomingSignals.next({ data: signals, origin: '', source: null }),
|
|
38
48
|
callbacks,
|
|
39
49
|
});
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
const expose = (target) => {
|
|
49
|
-
currentPort?.close();
|
|
50
|
-
currentPort = transferPort(target, { clientId, context: params.contextChanges.value });
|
|
51
|
-
Comlink.expose(hostAPI, currentPort);
|
|
52
|
-
};
|
|
53
|
-
clientWindowChanges.pipe(take(1), takeUntilClosed(scope)).subscribe(expose);
|
|
54
|
-
clientWindowChanges.pipe(filter(Boolean), take(1), takeUntilClosed(scope)).subscribe((iframeWindow) => {
|
|
55
|
-
scope.add(onPortRequest(iframeWindow, (source) => {
|
|
56
|
-
console.info('ready message received (client-initiated, HMR recovery)');
|
|
57
|
-
resetCallbacks();
|
|
58
|
-
clientWindowChanges.next(source);
|
|
59
|
-
expose(source);
|
|
60
|
-
}));
|
|
50
|
+
installHandshake({
|
|
51
|
+
clientId,
|
|
52
|
+
scope,
|
|
53
|
+
windowChanges: clientWindowChanges.asObservable(),
|
|
54
|
+
contextChanges: params.contextChanges,
|
|
55
|
+
connectionInitialized: connectionInitialized.asObservable(),
|
|
56
|
+
hostAPI,
|
|
57
|
+
resetCallbacks: () => Object.assign(callbacks, noCallbacks()),
|
|
61
58
|
});
|
|
62
59
|
installCompat({
|
|
63
60
|
clientId,
|
|
@@ -73,7 +70,6 @@ export function newCommsHost(params) {
|
|
|
73
70
|
combineLatest([params.contextChanges, connectionInitialized]).pipe(takeUntilClosed(scope)).subscribe(([ctx]) => callbacks.context?.(clone(ctx)));
|
|
74
71
|
appState.pipe(takeUntilClosed(scope)).subscribe(state => callbacks.appState?.(state));
|
|
75
72
|
scope.add(connectionInitialized);
|
|
76
|
-
scope.add(() => currentPort?.close());
|
|
77
73
|
const signal = newSignaler({
|
|
78
74
|
send: async (signals) => {
|
|
79
75
|
await firstValueFrom(connectionInitialized.pipe(take(1)));
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import type * as Comlink from 'comlink';
|
|
2
|
+
import type { HostAPI } from './hostAPI';
|
|
3
|
+
/**
|
|
4
|
+
* A slot holding the host proxy, which callers can await before one exists.
|
|
5
|
+
*
|
|
6
|
+
* Split out of newHostConnection because it is the piece that decides what a call
|
|
7
|
+
* does when there is no connection yet, and that answer is the whole point: it
|
|
8
|
+
* used to be "wait forever". Every `connect()` from an app whose handshake was
|
|
9
|
+
* dropped sat on an unresolved promise — no request, no error, no timeout. Now a
|
|
10
|
+
* caller waits only as long as a handshake could plausibly still land, then fails.
|
|
11
|
+
*/
|
|
12
|
+
export interface HostSlot {
|
|
13
|
+
/** The proxy, once there is one; rejects if none arrives in time. */
|
|
14
|
+
get: () => Promise<Comlink.Remote<HostAPI>>;
|
|
15
|
+
/** Adopt a proxy and hand it to everyone waiting. */
|
|
16
|
+
set: (proxy: Comlink.Remote<HostAPI>) => void;
|
|
17
|
+
/** Whatever is adopted right now, without waiting. */
|
|
18
|
+
peek: () => Comlink.Remote<HostAPI> | null;
|
|
19
|
+
/** Fail everyone waiting and drop the adopted proxy (teardown). */
|
|
20
|
+
reset: () => void;
|
|
21
|
+
}
|
|
22
|
+
export declare function newHostSlot(timeoutMs: number, message: string): HostSlot;
|
|
23
|
+
//# sourceMappingURL=hostSlot.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"hostSlot.d.ts","sourceRoot":"","sources":["../../src/comms/hostSlot.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,KAAK,OAAO,MAAM,SAAS,CAAA;AACvC,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,WAAW,CAAA;AAExC;;;;;;;;GAQG;AAEH,MAAM,WAAW,QAAQ;IACvB,qEAAqE;IACrE,GAAG,EAAE,MAAM,OAAO,CAAC,OAAO,CAAC,MAAM,CAAC,OAAO,CAAC,CAAC,CAAA;IAC3C,qDAAqD;IACrD,GAAG,EAAE,CAAC,KAAK,EAAE,OAAO,CAAC,MAAM,CAAC,OAAO,CAAC,KAAK,IAAI,CAAA;IAC7C,sDAAsD;IACtD,IAAI,EAAE,MAAM,OAAO,CAAC,MAAM,CAAC,OAAO,CAAC,GAAG,IAAI,CAAA;IAC1C,mEAAmE;IACnE,KAAK,EAAE,MAAM,IAAI,CAAA;CAClB;AAOD,wBAAgB,WAAW,CAAC,SAAS,EAAE,MAAM,EAAE,OAAO,EAAE,MAAM,GAAG,QAAQ,CA4DxE"}
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
export function newHostSlot(timeoutMs, message) {
|
|
2
|
+
let proxy = null;
|
|
3
|
+
let closed = false;
|
|
4
|
+
let warned = false;
|
|
5
|
+
const waiting = new Set();
|
|
6
|
+
const fail = (reject) => {
|
|
7
|
+
// Once per slot: every pending call rejects, and one line in the console is
|
|
8
|
+
// the point — a wall of them is not.
|
|
9
|
+
if (!warned) {
|
|
10
|
+
warned = true;
|
|
11
|
+
console.error(message);
|
|
12
|
+
}
|
|
13
|
+
reject(new Error(message));
|
|
14
|
+
};
|
|
15
|
+
return {
|
|
16
|
+
peek: () => proxy,
|
|
17
|
+
get: () => {
|
|
18
|
+
if (proxy)
|
|
19
|
+
return Promise.resolve(proxy);
|
|
20
|
+
if (closed)
|
|
21
|
+
return Promise.reject(new Error(message));
|
|
22
|
+
return new Promise((resolve, reject) => {
|
|
23
|
+
let timer;
|
|
24
|
+
const waiter = {
|
|
25
|
+
settle: (ready) => {
|
|
26
|
+
clearTimeout(timer);
|
|
27
|
+
resolve(ready);
|
|
28
|
+
},
|
|
29
|
+
reject: () => {
|
|
30
|
+
clearTimeout(timer);
|
|
31
|
+
fail(reject);
|
|
32
|
+
},
|
|
33
|
+
};
|
|
34
|
+
timer = setTimeout(() => {
|
|
35
|
+
waiting.delete(waiter);
|
|
36
|
+
fail(reject);
|
|
37
|
+
}, timeoutMs);
|
|
38
|
+
waiting.add(waiter);
|
|
39
|
+
});
|
|
40
|
+
},
|
|
41
|
+
set: (next) => {
|
|
42
|
+
proxy = next;
|
|
43
|
+
const pending = [...waiting];
|
|
44
|
+
waiting.clear();
|
|
45
|
+
pending.forEach(waiter => waiter.settle(next));
|
|
46
|
+
},
|
|
47
|
+
reset: () => {
|
|
48
|
+
closed = true;
|
|
49
|
+
proxy = null;
|
|
50
|
+
const pending = [...waiting];
|
|
51
|
+
waiting.clear();
|
|
52
|
+
pending.forEach(waiter => waiter.reject());
|
|
53
|
+
},
|
|
54
|
+
};
|
|
55
|
+
}
|
package/build/comms/models.d.ts
CHANGED
|
@@ -14,6 +14,17 @@ export interface Context {
|
|
|
14
14
|
selectedTask?: Task | null;
|
|
15
15
|
notepadTask?: Task | null;
|
|
16
16
|
project?: ProjectShort | null;
|
|
17
|
+
/**
|
|
18
|
+
* True when the selected project cannot be written to — today that means it is
|
|
19
|
+
* archived. Apps should disable their write affordances rather than let the user
|
|
20
|
+
* discover it from a 403: the API refuses every non-read call on an archived
|
|
21
|
+
* project (`code: 'PROJECT_ARCHIVED'`).
|
|
22
|
+
*
|
|
23
|
+
* A separate flag rather than `project.is_archive` because `ProjectShort` does
|
|
24
|
+
* not carry that field, and because "read-only" is the thing an app actually
|
|
25
|
+
* needs to know — the reason may widen later.
|
|
26
|
+
*/
|
|
27
|
+
projectReadOnly?: boolean | null;
|
|
17
28
|
sbs?: {
|
|
18
29
|
code: string;
|
|
19
30
|
} | null;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"models.d.ts","sourceRoot":"","sources":["../../src/comms/models.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,MAAM,CAAA;AACxC,OAAO,KAAK,EAAE,gBAAgB,EAAE,iCAA+B;AAC/D,OAAO,KAAK,EAAE,OAAO,EAAE,mCAAiC;AACxD,OAAO,KAAK,EAAE,YAAY,EAAE,mCAAiC;AAC7D,OAAO,KAAK,EAAE,IAAI,EAAE,gCAA8B;AAClD,OAAO,KAAK,EAAE,IAAI,EAAE,gCAA8B;AAElD,MAAM,WAAW,OAAO;IAC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