@rise-x/apps-sdk 0.19.0 → 0.20.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/API.md +141 -0
- package/README.md +176 -9
- package/build/accessors.d.ts +2 -1
- package/build/accessors.js +10 -0
- package/build/connectors/types.d.ts +8 -0
- package/build/connectors/types.js +36 -4
- package/build/connectors/work.js +30 -1
- package/build/index.d.ts +2 -1
- package/build/index.js +4 -1
- package/build/live/index.d.ts +43 -0
- package/build/live/index.js +183 -0
- package/build/liveChannel.d.ts +32 -0
- package/build/liveChannel.js +60 -0
- package/build/mockShell.d.ts +2 -0
- package/build/mockShell.js +1 -0
- package/build/query/index.d.ts +1 -0
- package/build/query/index.js +4 -2
- package/build/query/live.d.ts +14 -0
- package/build/query/live.js +115 -0
- package/build/shell/index.d.ts +2 -1
- package/build/shell/installShellBridge.d.ts +15 -1
- package/build/shell/installShellBridge.js +7 -1
- package/build/shell/liveHub.d.ts +25 -0
- package/build/shell/liveHub.js +201 -0
- package/build/shellBridge.d.ts +116 -1
- package/build/shellBridge.js +5 -1
- package/build/ui/demo.html +181 -179
- package/build/ui/standalone.js +60 -60
- package/build/ui/styles.css +1 -1
- package/build/ui/theme.css +9 -5
- package/live/package.json +8 -0
- package/package.json +7 -1
- package/template/AGENTS.md +6 -0
|
@@ -0,0 +1,201 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.LIVE_HUB_EVENTS = void 0;
|
|
4
|
+
exports.mapLiveEvent = mapLiveEvent;
|
|
5
|
+
exports.createLiveHub = createLiveHub;
|
|
6
|
+
// The host side of the live channel. Every host goes through this one module, so an app cannot
|
|
7
|
+
// tell which host it runs in: one mapping from the hub's events to `LiveEvent`s, one rule for the
|
|
8
|
+
// status values, one way to send a lock. A host only reports its connection and that
|
|
9
|
+
// connection's state (`_setLiveConnection` on the mutable shell).
|
|
10
|
+
const liveChannel_1 = require("../liveChannel");
|
|
11
|
+
/** The hub events apps see, by their wire names. Everything else on the connection is the host's. */
|
|
12
|
+
exports.LIVE_HUB_EVENTS = [
|
|
13
|
+
"DataPatch",
|
|
14
|
+
"DataObjectPatch",
|
|
15
|
+
"WorkUpdated",
|
|
16
|
+
"WorkDeleted",
|
|
17
|
+
"EntityUpdated",
|
|
18
|
+
"EntityDeleted",
|
|
19
|
+
"ComponentLockUpdated",
|
|
20
|
+
];
|
|
21
|
+
// The wire carries the operation's enum name. A Map, so a name like "constructor" cannot match.
|
|
22
|
+
const OPERATIONS = new Map([
|
|
23
|
+
["Set", "set"],
|
|
24
|
+
["Push", "push"],
|
|
25
|
+
["Pull", "pull"],
|
|
26
|
+
["Unset", "unset"],
|
|
27
|
+
["AddToSet", "addToSet"],
|
|
28
|
+
["Merge", "merge"],
|
|
29
|
+
]);
|
|
30
|
+
// A write that names no flow sends the empty id, which matches no flow an app holds.
|
|
31
|
+
const EMPTY_ID = "00000000-0000-0000-0000-000000000000";
|
|
32
|
+
const isWire = (value) => typeof value === "object" && value !== null && !Array.isArray(value);
|
|
33
|
+
/** A non-empty string, or undefined. */
|
|
34
|
+
const text = (value) => typeof value === "string" && value !== "" ? value : undefined;
|
|
35
|
+
const idOf = (value) => {
|
|
36
|
+
const id = text(value);
|
|
37
|
+
return id === EMPTY_ID ? undefined : id;
|
|
38
|
+
};
|
|
39
|
+
// An absent field has to be absent on the event, not present and undefined.
|
|
40
|
+
const compact = (event) => Object.fromEntries(Object.entries(event).filter(([, value]) => value !== undefined));
|
|
41
|
+
function workData(p, withValue) {
|
|
42
|
+
if (p.resourceType !== "Work")
|
|
43
|
+
return null;
|
|
44
|
+
const workId = text(p.resourceId);
|
|
45
|
+
if (!workId || typeof p.path !== "string")
|
|
46
|
+
return null;
|
|
47
|
+
const operation = typeof p.operation === "string" ? OPERATIONS.get(p.operation) : undefined;
|
|
48
|
+
// An operation outside the SDK's vocabulary (an increment, a replace) still changed the work,
|
|
49
|
+
// but what travels is not the new value: report the path only.
|
|
50
|
+
const hasValue = withValue && operation !== undefined;
|
|
51
|
+
return compact({
|
|
52
|
+
type: "workData",
|
|
53
|
+
workId,
|
|
54
|
+
flowOriginId: idOf(p.originId),
|
|
55
|
+
// A write with no path wrote the whole document.
|
|
56
|
+
path: p.path.trim() === "" ? "$" : p.path,
|
|
57
|
+
operation,
|
|
58
|
+
hasValue,
|
|
59
|
+
value: hasValue && operation !== "unset" ? p.value : undefined,
|
|
60
|
+
userId: text(p.userId),
|
|
61
|
+
updateId: text(p.updateId),
|
|
62
|
+
ticks: typeof p.ticks === "number" ? p.ticks : undefined,
|
|
63
|
+
});
|
|
64
|
+
}
|
|
65
|
+
function lock(p) {
|
|
66
|
+
if (p.resourceType !== "Work")
|
|
67
|
+
return null;
|
|
68
|
+
const workId = text(p.resourceId);
|
|
69
|
+
const userId = text(p.userId);
|
|
70
|
+
// An unlock may carry an empty component id, and dropping it would strand the lock.
|
|
71
|
+
if (!workId || !userId || typeof p.componentId !== "string")
|
|
72
|
+
return null;
|
|
73
|
+
return compact({
|
|
74
|
+
type: "lock",
|
|
75
|
+
workId,
|
|
76
|
+
userId,
|
|
77
|
+
displayName: text(p.displayName),
|
|
78
|
+
componentId: p.componentId,
|
|
79
|
+
dataPath: text(p.dataPath),
|
|
80
|
+
at: text(p.utcTime),
|
|
81
|
+
locked: p.isLocked === true || p.isLocked === "true",
|
|
82
|
+
});
|
|
83
|
+
}
|
|
84
|
+
/**
|
|
85
|
+
* Turns one hub event into the event an app sees. Null for an event that is not for apps (a
|
|
86
|
+
* patch to a resource that is not a work item) or a payload that is not what the platform sends:
|
|
87
|
+
* a push from the server must never throw into the connection.
|
|
88
|
+
*/
|
|
89
|
+
function mapLiveEvent(name, payload) {
|
|
90
|
+
switch (name) {
|
|
91
|
+
case "DataPatch":
|
|
92
|
+
return isWire(payload) ? workData(payload, true) : null;
|
|
93
|
+
case "DataObjectPatch":
|
|
94
|
+
return isWire(payload) ? workData(payload, false) : null;
|
|
95
|
+
case "WorkUpdated": {
|
|
96
|
+
if (!isWire(payload))
|
|
97
|
+
return null;
|
|
98
|
+
const workId = text(payload.id);
|
|
99
|
+
if (!workId)
|
|
100
|
+
return null;
|
|
101
|
+
return compact({
|
|
102
|
+
type: "workUpdated",
|
|
103
|
+
workId,
|
|
104
|
+
displayName: text(payload.displayName),
|
|
105
|
+
});
|
|
106
|
+
}
|
|
107
|
+
case "WorkDeleted": {
|
|
108
|
+
const workId = text(payload);
|
|
109
|
+
return workId ? { type: "workDeleted", workId } : null;
|
|
110
|
+
}
|
|
111
|
+
case "EntityUpdated": {
|
|
112
|
+
if (!isWire(payload))
|
|
113
|
+
return null;
|
|
114
|
+
const assetId = text(payload.id);
|
|
115
|
+
if (!assetId)
|
|
116
|
+
return null;
|
|
117
|
+
return compact({
|
|
118
|
+
type: "assetUpdated",
|
|
119
|
+
assetId,
|
|
120
|
+
displayName: text(payload.displayName),
|
|
121
|
+
});
|
|
122
|
+
}
|
|
123
|
+
case "EntityDeleted": {
|
|
124
|
+
const assetId = text(payload);
|
|
125
|
+
return assetId ? { type: "assetDeleted", assetId } : null;
|
|
126
|
+
}
|
|
127
|
+
case "ComponentLockUpdated":
|
|
128
|
+
return isWire(payload) ? lock(payload) : null;
|
|
129
|
+
default:
|
|
130
|
+
return null;
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
// How many recent write ids the hub remembers, to drop a second delivery of the same write.
|
|
134
|
+
const RECENT_UPDATES = 50;
|
|
135
|
+
/** The live channel a host's bridge offers, fed from whichever hub connection the host holds. */
|
|
136
|
+
function createLiveHub() {
|
|
137
|
+
let connection = null;
|
|
138
|
+
let hasConnected = false;
|
|
139
|
+
const recent = [];
|
|
140
|
+
const channel = (0, liveChannel_1.createLiveChannel)({
|
|
141
|
+
// Over the hub in every host, never HTTP: a lock is advisory, so without a connection the
|
|
142
|
+
// call rejects and the app carries on. `invoke` rather than `send`: the hub answers once it has
|
|
143
|
+
// handled the lock, so a failure reaches the caller instead of vanishing.
|
|
144
|
+
setComponentLock: async ({ workId, componentId, dataPath, locked }) => {
|
|
145
|
+
if (!connection || channel.api.getStatus() !== "connected") {
|
|
146
|
+
throw new Error("[@rise-x/apps-sdk] cannot send a component lock: the live connection is not connected");
|
|
147
|
+
}
|
|
148
|
+
await connection.invoke(locked ? "LockWorkComponent" : "UnlockWorkComponent", { workId, componentId, dataPath: dataPath ?? "" });
|
|
149
|
+
},
|
|
150
|
+
});
|
|
151
|
+
// A user named on a work item who also holds access through a group gets one write twice, as a
|
|
152
|
+
// DataPatch and a DataObjectPatch with the same updateId. The first one wins.
|
|
153
|
+
const isRepeat = (event) => {
|
|
154
|
+
if (event.type !== "workData" || !event.updateId)
|
|
155
|
+
return false;
|
|
156
|
+
if (recent.includes(event.updateId))
|
|
157
|
+
return true;
|
|
158
|
+
recent.push(event.updateId);
|
|
159
|
+
if (recent.length > RECENT_UPDATES)
|
|
160
|
+
recent.shift();
|
|
161
|
+
return false;
|
|
162
|
+
};
|
|
163
|
+
// One handler per hub event, moved from connection to connection.
|
|
164
|
+
const handlers = exports.LIVE_HUB_EVENTS.map((name) => [
|
|
165
|
+
name,
|
|
166
|
+
(...args) => {
|
|
167
|
+
const event = mapLiveEvent(name, args[0]);
|
|
168
|
+
if (event && !isRepeat(event))
|
|
169
|
+
channel.emit(event);
|
|
170
|
+
},
|
|
171
|
+
]);
|
|
172
|
+
// `connecting` until the first connection, `reconnecting` while retrying one that was up,
|
|
173
|
+
// `disconnected` after a close (until a connection is back, whatever the host retries meanwhile).
|
|
174
|
+
const normalise = (state) => {
|
|
175
|
+
if (state === "connected")
|
|
176
|
+
return "connected";
|
|
177
|
+
if (!hasConnected)
|
|
178
|
+
return "connecting";
|
|
179
|
+
if (state !== "connecting")
|
|
180
|
+
return state;
|
|
181
|
+
const current = channel.api.getStatus();
|
|
182
|
+
return current === "connected" ? "reconnecting" : current;
|
|
183
|
+
};
|
|
184
|
+
return {
|
|
185
|
+
api: channel.api,
|
|
186
|
+
update(next, state) {
|
|
187
|
+
const previous = connection;
|
|
188
|
+
if (next !== previous) {
|
|
189
|
+
for (const [name, handler] of handlers)
|
|
190
|
+
previous?.off(name, handler);
|
|
191
|
+
for (const [name, handler] of handlers)
|
|
192
|
+
next?.on(name, handler);
|
|
193
|
+
connection = next;
|
|
194
|
+
}
|
|
195
|
+
const reported = next ? state : "disconnected";
|
|
196
|
+
channel.setStatus(normalise(reported));
|
|
197
|
+
if (reported === "connected")
|
|
198
|
+
hasConnected = true;
|
|
199
|
+
},
|
|
200
|
+
};
|
|
201
|
+
}
|
package/build/shellBridge.d.ts
CHANGED
|
@@ -243,6 +243,115 @@ export interface OfflineApi {
|
|
|
243
243
|
*/
|
|
244
244
|
subscribeFlowDownload(flowOriginId: string, listener: () => void): ShellEventUnsubscribe;
|
|
245
245
|
}
|
|
246
|
+
/**
|
|
247
|
+
* The state of the host's live connection, the same in every host: `connecting` until the first
|
|
248
|
+
* connection is up, `reconnecting` while the host retries one that dropped, `disconnected` after
|
|
249
|
+
* it closed, and `connected` while events arrive. Coming back to `connected` after an earlier
|
|
250
|
+
* connection also sends one `resync` event.
|
|
251
|
+
*/
|
|
252
|
+
export type LiveStatus = "connecting" | "connected" | "reconnecting" | "disconnected";
|
|
253
|
+
/**
|
|
254
|
+
* A user's component lock on a work item: the platform's record that they have an editor open on
|
|
255
|
+
* part of it. The platform keeps one lock per user per work, so a user's second lock on a work
|
|
256
|
+
* replaces the first, whichever editor (an app's or the host's own) took it.
|
|
257
|
+
*/
|
|
258
|
+
export interface WorkComponentLock {
|
|
259
|
+
workId: string;
|
|
260
|
+
userId: string;
|
|
261
|
+
displayName?: string;
|
|
262
|
+
/** The id the locking editor identified itself with. */
|
|
263
|
+
componentId: string;
|
|
264
|
+
dataPath?: string;
|
|
265
|
+
/** When the lock was taken or last refreshed, as an ISO 8601 string. */
|
|
266
|
+
at?: string;
|
|
267
|
+
}
|
|
268
|
+
/**
|
|
269
|
+
* A platform change, in the SDK's own names: the host translates what its connection hears, so
|
|
270
|
+
* an app never depends on the platform's wire names.
|
|
271
|
+
*
|
|
272
|
+
* Events are best-effort. The platform does not replay what was sent while the connection was
|
|
273
|
+
* down, so a `resync` follows every reconnect: re-read what you hold. Events arrive for every
|
|
274
|
+
* work item and asset the user can see, in every ecosystem they belong to, so filter by the ids
|
|
275
|
+
* and flows you hold. Treat an event as a reason to re-read, never as the data.
|
|
276
|
+
*/
|
|
277
|
+
export type LiveEvent =
|
|
278
|
+
/**
|
|
279
|
+
* One write to a work item's data document. `hasValue` is false when the platform reported
|
|
280
|
+
* only that the path changed: always for an object or list value, for a user who has access
|
|
281
|
+
* through a group, and for an operation outside {@link WorkPatchOperationName}. Read the path
|
|
282
|
+
* to learn the new value then.
|
|
283
|
+
*/
|
|
284
|
+
{
|
|
285
|
+
type: "workData";
|
|
286
|
+
workId: string;
|
|
287
|
+
/** The work's flow origin id, when the write named one. */
|
|
288
|
+
flowOriginId?: string;
|
|
289
|
+
/** The `$`-rooted JSON path written, e.g. `$.section.field`. */
|
|
290
|
+
path: string;
|
|
291
|
+
/** Absent for an operation outside this vocabulary. */
|
|
292
|
+
operation?: WorkPatchOperationName;
|
|
293
|
+
hasValue: boolean;
|
|
294
|
+
/** What was written, when `hasValue` is true. Absent for an `unset`. */
|
|
295
|
+
value?: unknown;
|
|
296
|
+
/** The user who wrote. Your own writes come back too. */
|
|
297
|
+
userId?: string;
|
|
298
|
+
updateId?: string;
|
|
299
|
+
ticks?: number;
|
|
300
|
+
}
|
|
301
|
+
/**
|
|
302
|
+
* The work item changed in a way the platform did not itemise: created, submitted, moved to
|
|
303
|
+
* another step, written in bulk. Only its identity travels, so read the work to see the result.
|
|
304
|
+
*/
|
|
305
|
+
| {
|
|
306
|
+
type: "workUpdated";
|
|
307
|
+
workId: string;
|
|
308
|
+
displayName?: string;
|
|
309
|
+
} | {
|
|
310
|
+
type: "workDeleted";
|
|
311
|
+
workId: string;
|
|
312
|
+
}
|
|
313
|
+
/** An asset was created or changed. Read it to see the result. */
|
|
314
|
+
| {
|
|
315
|
+
type: "assetUpdated";
|
|
316
|
+
assetId: string;
|
|
317
|
+
displayName?: string;
|
|
318
|
+
} | {
|
|
319
|
+
type: "assetDeleted";
|
|
320
|
+
assetId: string;
|
|
321
|
+
}
|
|
322
|
+
/** A user took (`locked: true`) or released a component lock on a work item. */
|
|
323
|
+
| ({
|
|
324
|
+
type: "lock";
|
|
325
|
+
locked: boolean;
|
|
326
|
+
} & WorkComponentLock)
|
|
327
|
+
/** The connection came back, so changes may have been missed: re-read what you hold, once. */
|
|
328
|
+
| {
|
|
329
|
+
type: "resync";
|
|
330
|
+
};
|
|
331
|
+
export interface SetComponentLockArgs {
|
|
332
|
+
workId: string;
|
|
333
|
+
/** A GUID that identifies the editor. Use the same one for the same editor in every session. */
|
|
334
|
+
componentId: string;
|
|
335
|
+
dataPath?: string;
|
|
336
|
+
/** `false` releases the user's lock on the work. */
|
|
337
|
+
locked: boolean;
|
|
338
|
+
}
|
|
339
|
+
/**
|
|
340
|
+
* Live platform events and presence, over the host's existing connection. An app never opens a
|
|
341
|
+
* connection of its own.
|
|
342
|
+
*/
|
|
343
|
+
export interface LiveApi {
|
|
344
|
+
getStatus(): LiveStatus;
|
|
345
|
+
/** Fires when the status changes, with the new one. */
|
|
346
|
+
subscribeStatus(listener: (status: LiveStatus) => void): ShellEventUnsubscribe;
|
|
347
|
+
/** Receive every event until the returned function is called. */
|
|
348
|
+
subscribe(listener: (event: LiveEvent) => void): ShellEventUnsubscribe;
|
|
349
|
+
/**
|
|
350
|
+
* Take or release the user's component lock on a work item, so others see who is editing.
|
|
351
|
+
* Advisory: it never gates a write. Rejects while the connection is not `connected`.
|
|
352
|
+
*/
|
|
353
|
+
setComponentLock(args: SetComponentLockArgs): Promise<void>;
|
|
354
|
+
}
|
|
246
355
|
export interface DianaShell {
|
|
247
356
|
version: number;
|
|
248
357
|
getUser(): ShellUser | null;
|
|
@@ -258,6 +367,12 @@ export interface DianaShell {
|
|
|
258
367
|
* that as "no local copy exists", so an app degrades to network-only rather than throwing.
|
|
259
368
|
*/
|
|
260
369
|
getCache(): CacheApi | null;
|
|
370
|
+
/**
|
|
371
|
+
* Live platform events and presence, or null on a host that predates bridge v5 or offers no live
|
|
372
|
+
* events. A connection that is only down still answers, with status `disconnected`. Read it
|
|
373
|
+
* through `getShellLive()` or the `@rise-x/apps-sdk/live` hooks, which handle both.
|
|
374
|
+
*/
|
|
375
|
+
getLive(): LiveApi | null;
|
|
261
376
|
navigate: ShellNavigate;
|
|
262
377
|
}
|
|
263
378
|
declare global {
|
|
@@ -265,4 +380,4 @@ declare global {
|
|
|
265
380
|
__DIANA_SHELL__?: DianaShell;
|
|
266
381
|
}
|
|
267
382
|
}
|
|
268
|
-
export declare const SHELL_BRIDGE_VERSION =
|
|
383
|
+
export declare const SHELL_BRIDGE_VERSION = 5;
|
package/build/shellBridge.js
CHANGED
|
@@ -8,4 +8,8 @@ exports.SHELL_BRIDGE_VERSION = void 0;
|
|
|
8
8
|
// to call rather than comparing versions: `typeof offline.queueWorkDataUpdate === 'function'`.
|
|
9
9
|
// (v3 added getAi(); v2 added the V4ApiKey arg to getApiV4.)
|
|
10
10
|
// requireShell() compares the host's shell.version against this constant and warns once on mismatch.
|
|
11
|
-
|
|
11
|
+
// Bumped to 5: adds getLive(), live platform events and component locks (LiveApi) over the host's
|
|
12
|
+
// own connection, so apps stop polling. Additive: a v4-built app keeps working on a v5 host, and a
|
|
13
|
+
// v5 app on an older host finds no getLive, so getShellLive() answers null and the app keeps its
|
|
14
|
+
// own reads.
|
|
15
|
+
exports.SHELL_BRIDGE_VERSION = 5;
|