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

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