@classcad/client 0.0.0-stage → 3.0.0-beta.20261119100608

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.
Files changed (108) hide show
  1. package/README.md +20 -2
  2. package/bin/buerli-classcad.cjs +23 -0
  3. package/build/cjs/BuerliCadFacade.js +718 -0
  4. package/build/cjs/cclass/utils/assembly.js +687 -0
  5. package/build/cjs/cclass/utils/base.js +136 -0
  6. package/build/cjs/cclass/utils/index.js +99 -0
  7. package/build/cjs/cclass/utils/part.js +153 -0
  8. package/build/cjs/classcadEngine.js +33 -0
  9. package/build/cjs/createApi.js +71 -0
  10. package/build/cjs/createUnwrappedApi.js +43 -0
  11. package/build/cjs/facade.js +103 -0
  12. package/build/cjs/globals.js +157 -0
  13. package/build/cjs/index.js +102 -0
  14. package/build/cjs/io/AwvNodeClient.js +290 -0
  15. package/build/cjs/io/SocketIOClient.js +339 -0
  16. package/build/cjs/io/Transaction.js +134 -0
  17. package/build/cjs/io/WASMClient.js +433 -0
  18. package/build/cjs/io/WSClient.js +646 -0
  19. package/build/cjs/io/engineKey.js +202 -0
  20. package/build/cjs/re-export.js +35 -0
  21. package/build/cjs/session/host.js +479 -0
  22. package/build/cjs/session/identity.js +38 -0
  23. package/build/cjs/session/selection.js +283 -0
  24. package/build/cjs/session/share.js +229 -0
  25. package/build/cjs/session/types.js +23 -0
  26. package/build/cjs/test/SocketIOClient.spec.js +178 -0
  27. package/build/cjs/types/ComClient.js +10 -0
  28. package/build/cjs/utils/createRecursiveBufferGeometry.js +55 -0
  29. package/build/cjs/utils/createRecursiveScene.js +140 -0
  30. package/build/cjs/utils/getDifferentColoredMeshesFromEntity.js +24 -0
  31. package/build/cjs/utils/index.js +20 -0
  32. package/build/cjs/utils/polyline.js +65 -0
  33. package/build/cjs/utils/waitForSelect.js +29 -0
  34. package/build/esm/BuerliCadFacade.js +714 -0
  35. package/build/esm/cclass/utils/assembly.js +665 -0
  36. package/build/esm/cclass/utils/base.js +127 -0
  37. package/build/esm/cclass/utils/index.js +62 -0
  38. package/build/esm/cclass/utils/part.js +144 -0
  39. package/build/esm/classcadEngine.js +30 -0
  40. package/build/esm/createApi.js +66 -0
  41. package/build/esm/createUnwrappedApi.js +39 -0
  42. package/build/esm/facade.js +98 -0
  43. package/build/esm/globals.js +150 -0
  44. package/build/esm/index.js +80 -0
  45. package/build/esm/io/AwvNodeClient.js +286 -0
  46. package/build/esm/io/SocketIOClient.js +332 -0
  47. package/build/esm/io/Transaction.js +130 -0
  48. package/build/esm/io/WASMClient.js +429 -0
  49. package/build/esm/io/WSClient.js +642 -0
  50. package/build/esm/io/engineKey.js +196 -0
  51. package/build/esm/re-export.js +3 -0
  52. package/build/esm/session/host.js +474 -0
  53. package/build/esm/session/identity.js +34 -0
  54. package/build/esm/session/selection.js +277 -0
  55. package/build/esm/session/share.js +221 -0
  56. package/build/esm/session/types.js +20 -0
  57. package/build/esm/test/SocketIOClient.spec.js +178 -0
  58. package/build/esm/types/ComClient.js +7 -0
  59. package/build/esm/utils/createRecursiveBufferGeometry.js +51 -0
  60. package/build/esm/utils/createRecursiveScene.js +136 -0
  61. package/build/esm/utils/getDifferentColoredMeshesFromEntity.js +20 -0
  62. package/build/esm/utils/index.js +4 -0
  63. package/build/esm/utils/polyline.js +60 -0
  64. package/build/esm/utils/waitForSelect.js +23 -0
  65. package/build/types/BuerliCadFacade.d.ts +410 -0
  66. package/build/types/cclass/utils/assembly.d.ts +180 -0
  67. package/build/types/cclass/utils/base.d.ts +55 -0
  68. package/build/types/cclass/utils/index.d.ts +46 -0
  69. package/build/types/cclass/utils/part.d.ts +46 -0
  70. package/build/types/classcadEngine.d.ts +40 -0
  71. package/build/types/createApi.d.ts +65 -0
  72. package/build/types/createUnwrappedApi.d.ts +36 -0
  73. package/build/types/facade.d.ts +9 -0
  74. package/build/types/globals.d.ts +79 -0
  75. package/build/types/index.d.ts +82 -0
  76. package/build/types/io/AwvNodeClient.d.ts +182 -0
  77. package/build/types/io/SocketIOClient.d.ts +84 -0
  78. package/build/types/io/Transaction.d.ts +56 -0
  79. package/build/types/io/WASMClient.d.ts +239 -0
  80. package/build/types/io/WSClient.d.ts +242 -0
  81. package/build/types/io/engineKey.d.ts +137 -0
  82. package/build/types/re-export.d.ts +6 -0
  83. package/build/types/session/host.d.ts +108 -0
  84. package/build/types/session/identity.d.ts +46 -0
  85. package/build/types/session/selection.d.ts +164 -0
  86. package/build/types/session/share.d.ts +131 -0
  87. package/build/types/session/types.d.ts +113 -0
  88. package/build/types/test/SocketIOClient.spec.d.ts +0 -0
  89. package/build/types/types/ComClient.d.ts +307 -0
  90. package/build/types/utils/createRecursiveBufferGeometry.d.ts +8 -0
  91. package/build/types/utils/createRecursiveScene.d.ts +24 -0
  92. package/build/types/utils/getDifferentColoredMeshesFromEntity.d.ts +14 -0
  93. package/build/types/utils/index.d.ts +4 -0
  94. package/build/types/utils/polyline.d.ts +43 -0
  95. package/build/types/utils/waitForSelect.d.ts +4 -0
  96. package/doc/api/advanced/connection.md +488 -0
  97. package/doc/api/clients.md +2045 -0
  98. package/doc/api/engine-keys.md +230 -0
  99. package/doc/api/index.md +58 -0
  100. package/doc/api/modeling.md +682 -0
  101. package/doc/api/other.md +33 -0
  102. package/doc/api/session-sharing.md +706 -0
  103. package/package.json +60 -4
  104. package/test/classcadEngine.test.cjs +44 -0
  105. package/test/connect.test.cjs +81 -0
  106. package/test/engineKey.test.cjs +80 -0
  107. package/test/onClient.test.cjs +82 -0
  108. package/typedoc.json +8 -0
@@ -0,0 +1,429 @@
1
+ /// <reference types="emscripten" />
2
+ import { getDrawing } from '@classcad/sdkjs';
3
+ import { decode as b64ab_decode } from 'base64-arraybuffer';
4
+ import { wrap } from 'comlink';
5
+ import { inflate } from 'pako';
6
+ import { classcadEngine } from '../classcadEngine';
7
+ import { MessageLevel } from '../re-export';
8
+ import { v4 as uuidv4 } from 'uuid';
9
+ import { AwvNodeClient } from './AwvNodeClient';
10
+ import { SessionHost } from '../session/host';
11
+ import { CLIENT_CHANNEL } from '../session/identity';
12
+ import { resolveEngineKey } from './engineKey';
13
+ /**
14
+ * Where a page that hosts its own engine is joined: the session address of a ClassCAD MCP on the same machine.
15
+ *
16
+ * The default of the {@link WASMClient} option `sessionUrl`. 127.0.0.1 on
17
+ * purpose: `localhost` may be IPv6 in the browser and IPv4 in the MCP.
18
+ *
19
+ * @category Session Sharing
20
+ */
21
+ export const DEFAULT_SESSION_URL = 'ws://127.0.0.1:9098/session';
22
+ class classCadWorkerApi {
23
+ async init(url, config) {
24
+ return;
25
+ }
26
+ async execute(command) {
27
+ return { messages: [], binaryMessages: [] };
28
+ }
29
+ }
30
+ /**
31
+ * Runs the ClassCAD engine in the browser, as WebAssembly in a web worker, and hosts its session for guests.
32
+ *
33
+ * `connect` gets the engine's key first: from `token` (fetched from ClassCAD, cached in `localStorage` and renewed),
34
+ * `getKey` or `classcadKey` (see {@link resolveEngineKey}). Then it loads the engine from `url`, by default from
35
+ * ClassCAD's CDN in the version this package is made for, and starts it in a worker. A public access token
36
+ * (`ccpk_…`, from classcad.ch/account) may sit in a web page; it yields keys for the account's registered domains
37
+ * and for localhost.
38
+ *
39
+ * Nobody can connect to a page, so the page hosts its session itself. With the first invite
40
+ * ({@link WASMClient.createInvite}) it offers its invites at `sessionUrl`, a ClassCAD MCP on the same machine or the
41
+ * ClassCAD share relay, where guests join with `?invite=<token>` as they would join a server. Undo and redo are not
42
+ * supported by this client.
43
+ *
44
+ * @example
45
+ * ```ts
46
+ * import { init, WASMClient } from '@classcad/client'
47
+ *
48
+ * // A public access token from classcad.ch/account; it works on the account's domains and on localhost
49
+ * init(drawingId => new WASMClient(drawingId, { token: 'ccpk_…' }))
50
+ * ```
51
+ * Its events (`connected`, `conn_error`, …) and the members all clients share are on {@link AwvNodeClient}.
52
+ *
53
+ * @example
54
+ * ```ts
55
+ * import { init, WASMClient } from '@classcad/client'
56
+ * import type { EngineKey } from '@classcad/client'
57
+ *
58
+ * // The app's own sign-in: its server hands signed-in users a key (see requestEngineKey)
59
+ * init(
60
+ * drawingId =>
61
+ * new WASMClient(drawingId, {
62
+ * getKey: async () => {
63
+ * const res = await fetch('/api/engine-key', { headers: { authorization: `Bearer ${await idToken()}` } })
64
+ * if (!res.ok) throw new Error(`No engine key: ${res.status}`)
65
+ * return (await res.json()) as EngineKey
66
+ * },
67
+ * }),
68
+ * )
69
+ * ```
70
+ * @category Clients
71
+ */
72
+ export class WASMClient extends AwvNodeClient {
73
+ /** The guests in this page's session. */
74
+ get peers() {
75
+ var _a, _b;
76
+ return (_b = (_a = this.host) === null || _a === void 0 ? void 0 : _a.peers) !== null && _b !== void 0 ? _b : [];
77
+ }
78
+ /**
79
+ * @param drawingId - The drawing the client serves (the factory given to {@link init} is called with it).
80
+ * @param config - The engine's key (`token`, `getKey` or `classcadKey`), where the engine is loaded from, and the
81
+ * session's settings.
82
+ */
83
+ constructor(drawingId, config) {
84
+ var _a, _b, _c, _d;
85
+ super();
86
+ this.config = {};
87
+ this.keySource = {};
88
+ this.url = '';
89
+ // ── Session sharing ──
90
+ // The engine lives in this page's worker; nobody can connect to it from
91
+ // outside. The page hosts the session itself (session/host.ts): it speaks
92
+ // the session protocol a ClassCAD server speaks, and a broker introduces the
93
+ // guests, who join with `?invite=<token>` like they join a server: a
94
+ // ClassCAD MCP on the same machine, or the ClassCAD share relay (`sessionUrl`,
95
+ // with `sessionAuth`), where they join from anywhere. Invites, peers and
96
+ // presence therefore work as on a WSClient. The host is created with the
97
+ // first invite. An invite the broker will not take is dropped, and the client
98
+ // emits 'invite_refused' (invite, OfferRefusal).
99
+ /** Invites are honoured: guests join at `sessionUrl` with `?invite=<token>`. */
100
+ this.shareKind = 'invite';
101
+ this.sessionUrl = DEFAULT_SESSION_URL;
102
+ this.setDrawingId(drawingId);
103
+ this.sessionUrl = (_a = config.sessionUrl) !== null && _a !== void 0 ? _a : DEFAULT_SESSION_URL;
104
+ this.sessionAuth = config.sessionAuth;
105
+ this.identity = config.identity;
106
+ this.config = {
107
+ cclasses: { type: 'classcadkey', key: (_b = config.classcadKey) !== null && _b !== void 0 ? _b : '' },
108
+ logToConsole: (_c = config.logToConsole) !== null && _c !== void 0 ? _c : false,
109
+ };
110
+ this.keySource = {
111
+ classcadKey: config.classcadKey,
112
+ token: config.token,
113
+ getKey: config.getKey,
114
+ keyUrl: config.keyUrl,
115
+ appType: 'wasm',
116
+ client: `@classcad/client, engine ${classcadEngine.version}`,
117
+ };
118
+ this.url = (_d = config === null || config === void 0 ? void 0 : config.url) !== null && _d !== void 0 ? _d : classcadEngine.wasmUrl;
119
+ // Modify URL to ensure it's absolute (i.e. starts with http)
120
+ this.url = this.url.startsWith('http') ? this.url : window.location.origin.replace(/\/$/, '') + this.url;
121
+ }
122
+ /**
123
+ * Gets the engine's key, loads the engine's worker from `url` and starts the engine. Rejects with an
124
+ * {@link EngineKeyError} when there is no key, and when the engine cannot be loaded.
125
+ */
126
+ connect() {
127
+ return new Promise(async (resolve, reject) => {
128
+ try {
129
+ // The key first: without one the engine does not start
130
+ const key = await resolveEngineKey(this.keySource);
131
+ this.config = Object.assign(Object.assign({}, this.config), { cclasses: { type: 'classcadkey', key } });
132
+ const workerUrl = this.url.replace(/\/$/, '') + '/' + 'ClassCADWasmWorker.js';
133
+ const res = await fetch(workerUrl);
134
+ const text = await res.text();
135
+ const blob = new Blob([text], { type: 'application/javascript' });
136
+ const url = URL.createObjectURL(blob);
137
+ this.worker = new Worker(url, { type: 'module' });
138
+ this.service = await new (wrap(this.worker).classCadWorkerApi)();
139
+ await this.service.init(this.url, Object.assign(Object.assign({}, this.config), { siteLocation: JSON.parse(JSON.stringify(window.location)) }));
140
+ this.emit('connected', this, true);
141
+ this.syncState();
142
+ resolve(this);
143
+ }
144
+ catch (error) {
145
+ reject(error);
146
+ }
147
+ });
148
+ }
149
+ /** Ends the session (guests are closed, invites taken back) and terminates the engine's worker. */
150
+ disconnect() {
151
+ var _a;
152
+ // The session ends with its engine: every guest is closed, every invite taken back.
153
+ if (this.host) {
154
+ const hadPeers = this.host.peers.length > 0;
155
+ this.host.close();
156
+ this.host = undefined;
157
+ if (hadPeers)
158
+ this.emit('peers_changed', this, this.peers);
159
+ }
160
+ this.clearIdentities();
161
+ // Ternminate the worker, if it exists
162
+ (_a = this.worker) === null || _a === void 0 ? void 0 : _a.terminate();
163
+ return new Promise(async (resolve, reject) => {
164
+ try {
165
+ resolve(this);
166
+ }
167
+ catch (error) {
168
+ reject(error);
169
+ }
170
+ });
171
+ }
172
+ /**
173
+ * Runs one raw engine command: applies what the engine emitted for it
174
+ * (structure, patches, graphic) to the drawing's store and returns it as it
175
+ * came. Every command takes this way, the page's own and a guest's — the
176
+ * page's view follows both.
177
+ */
178
+ async run(command) {
179
+ var _a;
180
+ if (!this.service)
181
+ throw new Error('WASMClient is not connected');
182
+ const data = await this.service.execute(command);
183
+ if (!data)
184
+ return { messages: [], binaryMessages: [] };
185
+ for (const message of data.messages) {
186
+ if (message.command === 'Result' && message.from === 'GetTree') {
187
+ getDrawing(this._drawingId).api.structure.set(message.result);
188
+ }
189
+ else if (message.command === 'Patch') {
190
+ getDrawing(this._drawingId).api.structure.applyPatches(message.ops);
191
+ }
192
+ else if (message.command === 'UndoStack') {
193
+ // The undo history changed: after an undoable command or an undo / redo, the page's or a guest's.
194
+ this.undoStack = message;
195
+ this.applyUndoStack(this.undoStack);
196
+ }
197
+ }
198
+ if (data.binaryMessages) {
199
+ getDrawing(this._drawingId).api.graphic.addPackages(data.binaryMessages);
200
+ }
201
+ return { messages: data.messages, binaryMessages: (_a = data.binaryMessages) !== null && _a !== void 0 ? _a : [] };
202
+ }
203
+ /** Like run(), for the page itself: its guests get the reply too, and stream data comes back inflated. */
204
+ async executeRaw(command) {
205
+ var _a;
206
+ const reply = await this.run(command);
207
+ (_a = this.host) === null || _a === void 0 ? void 0 : _a.deliver(command, reply);
208
+ const messages = reply.messages.map(m => m.streamData
209
+ ? Object.assign(Object.assign({}, m), { streamData: Object.fromEntries(Object.entries(m.streamData).map(([key, value]) => [
210
+ key,
211
+ new TextDecoder().decode(inflate(new Uint8Array(b64ab_decode(value)), { raw: true })),
212
+ ])) }) : m);
213
+ return { messages, binaryMessages: reply.binaryMessages };
214
+ }
215
+ /**
216
+ * Runs `command` on the engine and resolves to its reply. What the command changed (structure, graphic) goes into
217
+ * the drawing, and to the session's guests. Rejects with the reply when it carries an error.
218
+ *
219
+ * @param name - Not used by this client.
220
+ * @param command - The request.
221
+ */
222
+ requestByName(name, command) {
223
+ return new Promise(async (resolve, reject) => {
224
+ var _a, _b;
225
+ try {
226
+ const responseData = {
227
+ results: [],
228
+ errors: [],
229
+ messages: [],
230
+ };
231
+ if (command) {
232
+ if (!this.service)
233
+ return;
234
+ const data = await this.executeRaw(command);
235
+ for (const message of data.messages) {
236
+ if (message.command === 'Result') {
237
+ if (message.from === 'Execute' ||
238
+ message.from === 'GetEmissionConfig' ||
239
+ message.from === 'SetEmissionConfig' ||
240
+ message.from === 'Undo' ||
241
+ message.from === 'Redo') {
242
+ // GetEmissionConfig/SetEmissionConfig (per-connection emission config) reply
243
+ // with the effective flags in `result`, resolved like an
244
+ // Execute result. An old WASM build answers `result: ''`;
245
+ // AwvNodeClient turns that into EMISSION_CONFIG_UNSUPPORTED.
246
+ responseData.results.push(message);
247
+ }
248
+ }
249
+ else if (message.command === 'ErrorMessage') {
250
+ if (message.attributes) {
251
+ responseData.errors.push(message.attributes);
252
+ }
253
+ }
254
+ }
255
+ }
256
+ // Transform the "old" ClassCAD response to the new ClassCAD response format introduced by api.v1.
257
+ // There is always only one result expected per command!
258
+ const result = (_a = responseData.results[0]) === null || _a === void 0 ? void 0 : _a.result;
259
+ let jobResult = result;
260
+ if (typeof (result === null || result === void 0 ? void 0 : result.result) !== 'undefined') {
261
+ jobResult = result === null || result === void 0 ? void 0 : result.result;
262
+ }
263
+ const streamData = (result === null || result === void 0 ? void 0 : result.streamData) || ((_b = responseData.results[0]) === null || _b === void 0 ? void 0 : _b.streamData);
264
+ const newMessages = (result === null || result === void 0 ? void 0 : result.messages) || responseData.messages;
265
+ const oldErrors = (result === null || result === void 0 ? void 0 : result.errors) || responseData.errors;
266
+ const hadNewMessages = newMessages.length > 0;
267
+ const hadOldErrors = oldErrors.length > 0;
268
+ if (!hadNewMessages && hadOldErrors) {
269
+ for (const oldError of oldErrors) {
270
+ let level = MessageLevel.info;
271
+ // errorState --> 0:eCCNoError, 1:eCCErrorWarning, 2:eCCErrorGoOn, 3:eCCErrorStop
272
+ switch (oldError.errorState) {
273
+ case 1:
274
+ level = MessageLevel.warning;
275
+ break;
276
+ case 2:
277
+ case 3:
278
+ case 4:
279
+ level = MessageLevel.error;
280
+ break;
281
+ default:
282
+ break;
283
+ }
284
+ newMessages.push({ code: oldError.errorCode, message: oldError.errorMessage, level, api: '' }); // TODO: remove api from
285
+ }
286
+ }
287
+ let maxLevel = undefined;
288
+ for (const message of newMessages) {
289
+ if (!maxLevel || message.level > maxLevel) {
290
+ maxLevel = message.level;
291
+ }
292
+ }
293
+ const sr = { result: jobResult, maxLevel, messages: newMessages, streamData };
294
+ const hadError = typeof sr.maxLevel !== 'undefined' && sr.maxLevel >= MessageLevel.error;
295
+ if (hadError) {
296
+ reject(sr);
297
+ }
298
+ else {
299
+ resolve(sr);
300
+ }
301
+ }
302
+ catch (error) {
303
+ reject(error);
304
+ }
305
+ });
306
+ }
307
+ request(command) {
308
+ return this.requestByName('send', command);
309
+ }
310
+ /** The page's session host, made with the first invite (see session/host.ts). */
311
+ hosting() {
312
+ if (this.host)
313
+ return this.host;
314
+ const host = new SessionHost({
315
+ url: this.sessionUrl,
316
+ auth: this.sessionAuth,
317
+ // An invite the broker will not take is good for nothing: it leaves the list, and the app is told why.
318
+ onRefused: (invite, refusal) => {
319
+ this.untrackInvite(invite.invite);
320
+ this.emit('invite_refused', this, invite, refusal);
321
+ },
322
+ // A guest's command, on this page's engine.
323
+ execute: command => this.run(command),
324
+ containers: () => { var _a, _b; return Object.values((_b = (_a = getDrawing(this._drawingId)) === null || _a === void 0 ? void 0 : _a.graphic.containers) !== null && _b !== void 0 ? _b : {}); },
325
+ undoStack: () => this.undoStack,
326
+ onPeerJoined: peer => {
327
+ this.emit('peer_joined', this, peer);
328
+ this.emit('peers_changed', this, this.peers);
329
+ },
330
+ onPeerLeft: peer => {
331
+ this.emit('peer_left', this, peer);
332
+ this.emit('peers_changed', this, this.peers);
333
+ },
334
+ onPresence: msg => {
335
+ this.noteIdentity(msg);
336
+ this.emit('presence', this, msg);
337
+ },
338
+ });
339
+ this.host = host;
340
+ if (this.identity)
341
+ host.sendPresence(CLIENT_CHANNEL, this.identity);
342
+ return host;
343
+ }
344
+ /**
345
+ * Mints an invite for this page's session. Guests join with it at the
346
+ * session address (`sessionUrl`: a local ClassCAD MCP, or the share relay),
347
+ * which introduces them to this page; the invite stays joinable until it is
348
+ * revoked or the client disconnects.
349
+ *
350
+ * When the share relay will not take the invite (no sign-in, a plan that does not share), it is dropped and the
351
+ * client emits `invite_refused`.
352
+ *
353
+ * @param role - What guests with the invite may do: `'edit'` (default) or `'view'`.
354
+ * @param name - A name to show for the invite.
355
+ * @example
356
+ * ```ts
357
+ * import { BuerliCadFacade, buildShareUrl, init, WASMClient } from '@classcad/client'
358
+ * import type { OfferRefusal, SessionInvite } from '@classcad/client'
359
+ *
360
+ * // The engine in the page; guests join from anywhere through the share relay
361
+ * let client!: WASMClient
362
+ * init(
363
+ * drawingId =>
364
+ * (client = new WASMClient(drawingId, {
365
+ * token: 'ccpk_…',
366
+ * sessionUrl: 'wss://<relay>/session',
367
+ * // The relay takes invites from signed-in ClassCAD accounts
368
+ * sessionAuth: refresh => classcadIdToken(refresh),
369
+ * })),
370
+ * )
371
+ * await new BuerliCadFacade().connect()
372
+ *
373
+ * client.on('invite_refused', (_client, invite: SessionInvite, refusal: OfferRefusal) =>
374
+ * showError(refusal.code === 4403 ? 'Your plan does not share sessions' : `${invite.name}: ${refusal.reason}`),
375
+ * )
376
+ * const invite = await client.createInvite('edit', 'team')
377
+ * const link = buildShareUrl(invite)
378
+ * ```
379
+ */
380
+ createInvite(role = 'edit', name = '') {
381
+ const invite = { invite: uuidv4(), role, name, kind: 'invite' };
382
+ this.hosting().offer(invite);
383
+ this.trackInvite(invite);
384
+ return Promise.resolve(invite);
385
+ }
386
+ /**
387
+ * Takes an invite back; guests that joined with it are cut off.
388
+ *
389
+ * @param invite - The invite token.
390
+ * @returns The token, whether this client had made it, and how many guests were cut off.
391
+ */
392
+ revokeInvite(invite) {
393
+ var _a, _b;
394
+ const known = this.invites.some(i => i.invite === invite);
395
+ const kicked = (_b = (_a = this.host) === null || _a === void 0 ? void 0 : _a.revoke(invite)) !== null && _b !== void 0 ? _b : 0;
396
+ this.untrackInvite(invite);
397
+ return Promise.resolve({ invite, revoked: known, kicked });
398
+ }
399
+ /**
400
+ * Presence to the guests of this page's session (see WSClient.sendPresence). Nothing happens while nobody was invited.
401
+ *
402
+ * @param channel - The channel, e.g. `'view'` for a camera viewpoint.
403
+ * @param data - The payload, sent as it is; the last one per channel is kept for guests who join later.
404
+ */
405
+ sendPresence(channel, data) {
406
+ var _a;
407
+ (_a = this.host) === null || _a === void 0 ? void 0 : _a.sendPresence(channel, data);
408
+ }
409
+ /**
410
+ * Goes back to the state before the current one of the engine's undo history, or to
411
+ * `options.state`. The engine records a state after every command sent with
412
+ * `options.undoable` (api-js marks the modelling calls) and reports the history in an
413
+ * UndoStack message, which updates `cad.states`. The result is the history afterwards:
414
+ * `{ stack, current }`. An engine from before undo answers without one; nothing happens then.
415
+ */
416
+ undo(options) {
417
+ return this.requestByName('undo', Object.assign({ command: 'Undo' }, ((options === null || options === void 0 ? void 0 : options.state) ? { state: options.state } : {})));
418
+ }
419
+ /** Goes forward to the state after the current one, or to `options.state` (see undo). */
420
+ redo(options) {
421
+ return this.requestByName('redo', Object.assign({ command: 'Redo' }, ((options === null || options === void 0 ? void 0 : options.state) ? { state: options.state } : {})));
422
+ }
423
+ syncState() {
424
+ var _a, _b, _c, _d;
425
+ const dr = getDrawing(this._drawingId);
426
+ (_b = (_a = dr === null || dr === void 0 ? void 0 : dr.api) === null || _a === void 0 ? void 0 : _a.cad) === null || _b === void 0 ? void 0 : _b.setActive(true); // TODO: what to do?
427
+ (_d = (_c = dr === null || dr === void 0 ? void 0 : dr.api) === null || _c === void 0 ? void 0 : _c.cad) === null || _d === void 0 ? void 0 : _d.setConnected(Boolean(this.service));
428
+ }
429
+ }