@arqlet/engine-client 0.8.0 → 0.9.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/arqlet.wasm CHANGED
Binary file
package/elk.d.ts CHANGED
@@ -49,11 +49,37 @@ export interface ElkLayoutOptions {
49
49
  /** Runs the refinement after the layout. Default `false`. It removes conflicts (routes through nodes, shared lines);
50
50
  * a drawing without them comes back the same, a little later. */
51
51
  refine?: boolean;
52
+ /** Reports where each call's time went in `timings` (#152). Default `false`. */
53
+ timings?: boolean;
54
+ }
55
+
56
+ /** Where a call's time went, in ms (#152). The phases below `layout` are parts of it; 0 where the engine has no clock. */
57
+ export interface ElkTimings {
58
+ total: number;
59
+ layout: number;
60
+ placement: number;
61
+ organicStress: number;
62
+ separation: number;
63
+ repair: number;
64
+ portResolution: number;
65
+ routing: number;
66
+ /** Single pass, parts of `routing`. */
67
+ plan: number;
68
+ channelGrowth: number;
69
+ construction: number;
70
+ fallbackRouting: number;
71
+ finalization: number;
72
+ buses: number;
73
+ /** Outside `layout`: the ELK translation in and out, and a refinement on request. */
74
+ elkTranslation: number;
75
+ refinement: number;
52
76
  }
53
77
 
54
78
  export interface ElkLayout {
55
79
  /** ELK options without an Arqlet counterpart, reported by the last `layout` call. */
56
80
  diagnostics: string[];
81
+ /** With `timings: true`, where the last `layout` call's time went; otherwise `null`. */
82
+ timings: ElkTimings | null;
57
83
  /** Resolves with a copy of `graph`, laid out: positions, sizes, ports, labels and edge `sections`. */
58
84
  layout(graph: ElkNode): Promise<ElkNode>;
59
85
  }
package/elk.js CHANGED
@@ -2,14 +2,17 @@
2
2
  // `createEngine`, a worker or a backend): `layout(graph)` resolves with the graph in ELK's JSON form laid out (`x`, `y`,
3
3
  // sizes, ports, labels and edge `sections`), as `elk.layout(graph)` does. Not a 1:1 ELK: the options without a counterpart are
4
4
  // listed in `diagnostics` after each call (see crates/elk). `options` are Arqlet options that win over the translated
5
- // ones; `refine` runs the refinement after the layout.
6
- export function createElkLayout(run,{options,refine=false}={}) {
5
+ // ones; `refine` runs the refinement after the layout; with `timings` each call reports where its time went in `timings`
6
+ // (ms by phase; all 0 when the engine has no clock).
7
+ export function createElkLayout(run,{options,refine=false,timings=false}={}) {
7
8
  return {
8
9
  diagnostics:[],
10
+ timings:null,
9
11
  async layout(graph) {
10
- const response=await run({op:'elkLayout',graph,...(options?{options}:{}),refine});
12
+ const response=await run({op:'elkLayout',graph,...(options?{options}:{}),refine,...(timings?{timings:true}:{})});
11
13
  if(!response.ok) throw new Error(response.error);
12
14
  this.diagnostics=response.diagnostics;
15
+ this.timings=response.timings??null;
13
16
  return response.graph;
14
17
  },
15
18
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@arqlet/engine-client",
3
- "version": "0.8.0",
3
+ "version": "0.9.0",
4
4
  "description": "The Arqlet graph-layout engine as WebAssembly, with a loader, a worker protocol, an ELK-style layout call and preset sequencing. Provisional.",
5
5
  "keywords": [
6
6
  "graph",
@@ -84,7 +84,7 @@
84
84
  "access": "public"
85
85
  },
86
86
  "dependencies": {
87
- "@arqlet/contracts": "^0.8.0"
87
+ "@arqlet/contracts": "^0.9.0"
88
88
  },
89
89
  "scripts": {
90
90
  "test": "node --test"
package/session.d.ts CHANGED
@@ -147,6 +147,13 @@ export interface Session {
147
147
  * view) at once, the changes still under way resolving as stale.
148
148
  */
149
149
  load(snapshot: Snapshot, options?: {reset?: boolean}): Promise<SessionResult>;
150
+ /** Provisional (#148): keeps the current snapshot under a mark (the same mark while it is unchanged); at most `limit` (default 32). */
151
+ keep(options?: {limit?: number}): Promise<{ok: boolean; mark?: number; error?: string}>;
152
+ /**
153
+ * Provisional (#148): the snapshot kept under `mark` becomes current again with its geometry (next layout version);
154
+ * `evicted` when it is no longer kept, `changed` when the graph changed since.
155
+ */
156
+ recall(mark: number): Promise<SessionResult & {evicted?: boolean; changed?: boolean}>;
150
157
  reset(): void;
151
158
  /** Whether container `id` is collapsed in the current snapshot. */
152
159
  isCollapsed(id: string): boolean;
package/session.js CHANGED
@@ -120,6 +120,9 @@ export function createSession(engine, {onChange, resident: wanted = 'auto'} = {}
120
120
  let run = engine;
121
121
  let snapshot = null;
122
122
  let generation = 0;
123
+ // Snapshots kept by mark when the session is not resident (#148); a resident session keeps them in the engine.
124
+ let kept = [];
125
+ let marks = 0;
123
126
  let queue = Promise.resolve();
124
127
  const scenes = new WeakMap();
125
128
  if (wanted === true && !run.openSession) throw Error('This engine has no resident sessions');
@@ -187,6 +190,7 @@ export function createSession(engine, {onChange, resident: wanted = 'auto'} = {}
187
190
  // A new view does not wait for the changes still under way: they resolve as stale.
188
191
  generation++;
189
192
  queue = Promise.resolve();
193
+ kept = [];
190
194
  return enqueue(() => call({op: 'import', graph, limit, ...(options ? {options} : {})}));
191
195
  },
192
196
 
@@ -196,17 +200,55 @@ export function createSession(engine, {onChange, resident: wanted = 'auto'} = {}
196
200
  */
197
201
  load(snapshot, {reset = false} = {}) {
198
202
  // With `reset` (another view) it does not wait for the changes still under way: they resolve as stale.
203
+ // The same graph keeps the snapshots kept for `recall` (#148); another one (`reset`) drops them.
199
204
  if (reset) {
200
205
  generation++;
201
206
  queue = Promise.resolve();
207
+ kept = [];
202
208
  }
203
- return enqueue(() => (resident ? call({op: 'load', snapshot}) : {ok: true, snapshot}));
209
+ return enqueue(() => (resident ? call({op: 'load', snapshot, keep: !reset}) : {ok: true, snapshot}));
210
+ },
211
+
212
+ /**
213
+ * Provisional (#148): keeps the current snapshot under a mark, `{ok, mark}` (the same mark while it is unchanged), at
214
+ * most `limit` (default 32) of them, the oldest dropped first. With a resident session the engine keeps them (only the
215
+ * mark crosses); otherwise this session does. A new import drops them.
216
+ */
217
+ keep({limit = 32} = {}) {
218
+ return enqueue(async () => {
219
+ if (!snapshot) return {ok: false, error: 'Nothing imported yet.'};
220
+ if (resident) return call({op: 'keep', limit});
221
+ const last = kept.at(-1);
222
+ if (last && last.snapshot === snapshot) return {ok: true, mark: last.mark};
223
+ kept.push({mark: ++marks, snapshot});
224
+ if (kept.length > limit) kept.splice(0, kept.length - limit);
225
+ return {ok: true, mark: marks};
226
+ });
227
+ },
228
+
229
+ /**
230
+ * Provisional (#148): makes the snapshot kept under `mark` current again (its geometry as it was) under the next layout
231
+ * version. `{ok: false, evicted: true}` when it is no longer kept, `{ok: false, changed: true}` when the graph changed
232
+ * since (another revision); then lay out again from the decisions (`restore`).
233
+ */
234
+ recall(mark) {
235
+ return enqueue(async () => {
236
+ if (!snapshot) return {ok: false, error: 'Nothing imported yet.'};
237
+ if (resident) return call({op: 'recall', mark});
238
+ const entry = kept.find(k => k.mark === mark);
239
+ if (!entry) return {ok: false, evicted: true, error: `The snapshot of mark ${mark} is no longer kept`};
240
+ if (entry.snapshot.revision !== snapshot.revision) {
241
+ return {ok: false, changed: true, error: `The graph changed since mark ${mark}`};
242
+ }
243
+ return {ok: true, snapshot: {...entry.snapshot, layoutVersion: snapshot.layoutVersion + 1}};
244
+ });
204
245
  },
205
246
 
206
247
  /** Drops the snapshot; changes still under way resolve as stale. */
207
248
  reset() {
208
249
  generation++;
209
250
  snapshot = null;
251
+ kept = [];
210
252
  },
211
253
 
212
254
  /** Whether container `id` is collapsed in the current snapshot. */
@@ -82,7 +82,11 @@ export interface ViewState {
82
82
  }
83
83
 
84
84
  /** A step's answer: the engine's, or `cancelled` when `cancel()` dropped it before it started. */
85
- export type ViewResult = SessionResult & {cancelled?: boolean};
85
+ export type ViewResult = SessionResult & {
86
+ cancelled?: boolean;
87
+ /** `back` / `forward` (#148): false when the kept snapshot came back with its geometry, true when the view was laid out again. */
88
+ relaidOut?: boolean;
89
+ };
86
90
 
87
91
  /**
88
92
  * Provisional (request 31): a headless view controller over a layout session. `subscribe` and `getState` fit React's
@@ -95,7 +99,7 @@ export interface ViewController {
95
99
  open(id: string): Promise<ViewResult>;
96
100
  close(id: string): Promise<ViewResult>;
97
101
  toggle(id: string): Promise<ViewResult>;
98
- /** The next `count` (default `page`) children of a `+N more` box; `container` or `rest:<container>`. */
102
+ /** The next `count` (default `page`) children of a `+N more` box, or the first page of a collapsed container; `container` or `rest:<container>`. */
99
103
  showRest(container: string, count?: number): Promise<ViewResult>;
100
104
  reveal(nodeIds: string[], init?: {partial?: boolean}): Promise<ViewResult>;
101
105
  filter(hide?: Hidden): Promise<ViewResult>;
@@ -155,6 +159,6 @@ export interface ViewController {
155
159
  cancel(): void;
156
160
  }
157
161
 
158
- export function createViewController(session: Session, options?: {page?: number; historyLimit?: number; scenes?: boolean; detailLimit?: number; refine?: RefineRun; publicationInterval?: number}): ViewController;
162
+ export function createViewController(session: Session, options?: {page?: number; historyLimit?: number; scenes?: boolean; detailLimit?: number; refine?: RefineRun; publicationInterval?: number; keepGeometry?: boolean}): ViewController;
159
163
 
160
164
  export function sceneDiff(before: Scene | null, after: Scene | null): SceneDiff;
@@ -154,9 +154,14 @@ export function decodeBookmark(text) {
154
154
  * `(request, {onPublication, signal}) => Promise<answer>` that runs a refinement request on a snapshot the controller
155
155
  * sends and calls `onPublication(publication)` for every intermediate result of a progressive one. It runs beside the
156
156
  * interactive steps, never in their queue; `publicationInterval` (ms) throttles how often intermediate results are shown.
157
+ *
158
+ * `keepGeometry` (default true, provisional, #148): every step keeps the snapshots before and after it (`session.keep`),
159
+ * so that `back` and `forward` restore the geometry as it was (`session.recall`) instead of laying the view out again;
160
+ * only when a snapshot is no longer kept or the graph changed since is the view laid out again from the decisions
161
+ * (the step's result then says `relaidOut: true`). With `false` only the decisions are kept, as before.
157
162
  */
158
163
  export function createViewController(session, {page = 12, historyLimit = 50, scenes = true, detailLimit, refine: refineRun,
159
- publicationInterval = 120} = {}) {
164
+ publicationInterval = 120, keepGeometry = true} = {}) {
160
165
  let state = Object.freeze({
161
166
  snapshot: null, scene: null, diff: null, busy: false, error: null,
162
167
  highlight: null, selection: null, details: null, hover: null, search: null, focus: null, peek: null, refining: null, refined: null,
@@ -205,6 +210,12 @@ export function createViewController(session, {page = 12, historyLimit = 50, sce
205
210
  return answer.ok ? answer.digest ?? null : null;
206
211
  };
207
212
 
213
+ // The current snapshot kept by the session for `back` (#148): its mark, or null without it.
214
+ const mark = async () => {
215
+ if (!keepGeometry || typeof session.keep !== 'function') return null;
216
+ const answer = await session.keep({limit: Math.min(historyLimit + 1, 1000)});
217
+ return answer?.ok ? answer.mark : null;
218
+ };
208
219
  const intent = async () => {
209
220
  const answer = await session.apply({op: 'intent'});
210
221
  if (!answer.ok) throw Error(answer.error ?? 'No intent');
@@ -293,6 +304,7 @@ export function createViewController(session, {page = 12, historyLimit = 50, sce
293
304
  if (!session.snapshot) return {ok: false, error: 'Nothing imported yet.'};
294
305
  stopRefinement('cancelled');
295
306
  const before = remember ? await intent() : null;
307
+ const beforeMark = remember ? await mark() : null;
296
308
  const result = await run();
297
309
  if (!current()) return {ok: false, cancelled: true};
298
310
  if (!result.ok) {
@@ -300,6 +312,7 @@ export function createViewController(session, {page = 12, historyLimit = 50, sce
300
312
  return result;
301
313
  }
302
314
  const after = remember ? await intent() : null;
315
+ const afterMark = remember ? await mark() : null;
303
316
  if (!current()) return {ok: false, cancelled: true};
304
317
  // A look into a container belongs to the view before the change.
305
318
  if (state.peek) state = Object.freeze({...state, peek: null});
@@ -307,10 +320,11 @@ export function createViewController(session, {page = 12, historyLimit = 50, sce
307
320
  // A follow-up of the last change (such as a container keeping the size a drop gave it): one entry for both.
308
321
  const last = back.at(-1);
309
322
  last.after = after;
323
+ last.afterMark = afterMark;
310
324
  if (same(last.before, last.after)) back.pop();
311
325
  forward = [];
312
326
  } else if (remember && !same(before, after)) {
313
- back.push({label, before, after});
327
+ back.push({label, before, after, beforeMark, afterMark});
314
328
  if (back.length > historyLimit) back.shift();
315
329
  forward = [];
316
330
  }
@@ -318,13 +332,26 @@ export function createViewController(session, {page = 12, historyLimit = 50, sce
318
332
  return result;
319
333
  });
320
334
 
321
- // Back and forward: the engine restores the remembered decisions; a refused restore keeps the entry.
335
+ // Back and forward: the kept snapshot of that side comes back with its geometry (#148); when it is no longer kept or the
336
+ // graph changed since, the engine restores the remembered decisions and lays the view out again. A refused step keeps
337
+ // the entry.
322
338
  const step = direction => enqueue(async current => {
323
339
  stopRefinement('cancelled');
324
340
  const from = direction < 0 ? back : forward;
325
341
  const entry = from.at(-1);
326
342
  if (!entry) return {ok: false, error: direction < 0 ? 'Nothing to undo.' : 'Nothing to redo.'};
327
- const result = await session.apply({op: 'restore', intent: direction < 0 ? entry.before : entry.after});
343
+ const kept = direction < 0 ? entry.beforeMark : entry.afterMark;
344
+ let result = kept == null ? null : await session.recall(kept);
345
+ if (result && !result.ok && !result.evicted && !result.changed) {
346
+ publish({error: result.error ?? 'The engine refused the step.'});
347
+ return result;
348
+ }
349
+ if (result?.ok) {
350
+ result = {...result, relaidOut: false};
351
+ } else {
352
+ result = await session.apply({op: 'restore', intent: direction < 0 ? entry.before : entry.after});
353
+ if (result.ok) result = {...result, relaidOut: true};
354
+ }
328
355
  if (!current()) return {ok: false, cancelled: true};
329
356
  if (!result.ok) {
330
357
  publish({error: result.error ?? 'The engine refused the restore.'});
@@ -376,7 +403,8 @@ export function createViewController(session, {page = 12, historyLimit = 50, sce
376
403
 
377
404
  /**
378
405
  * Shows the next `count` (default `page`) children of `container`'s `+N more` box; on the last page the container is
379
- * whole again. `container` may also be the rest's id (`rest:<container>`).
406
+ * whole again. A collapsed container opens in part with its first page. `container` may also be the rest's id
407
+ * (`rest:<container>`).
380
408
  */
381
409
  showRest(container, count = page) {
382
410
  const id = container.startsWith('rest:') ? container.slice(5) : container;
@@ -649,6 +677,7 @@ export function createViewController(session, {page = 12, historyLimit = 50, sce
649
677
  }
650
678
  stopRefinement('cancelled');
651
679
  const before = await intent();
680
+ const beforeMark = await mark();
652
681
  const result = await session.apply({op: 'restore', intent: bookmark.intent});
653
682
  if (!current()) return {ok: false, cancelled: true};
654
683
  if (!result.ok) {
@@ -657,7 +686,7 @@ export function createViewController(session, {page = 12, historyLimit = 50, sce
657
686
  }
658
687
  const after = await intent();
659
688
  if (!same(before, after)) {
660
- back.push({label: 'Open bookmark', before, after});
689
+ back.push({label: 'Open bookmark', before, after, beforeMark, afterMark: await mark()});
661
690
  if (back.length > historyLimit) back.shift();
662
691
  forward = [];
663
692
  }
@@ -700,6 +729,7 @@ export function createViewController(session, {page = 12, historyLimit = 50, sce
700
729
  // With `remember` (a label) the change from the session's snapshot to this one is remembered for `back`.
701
730
  stopRefinement('cancelled');
702
731
  const before = remember && session.snapshot ? await intent() : null;
732
+ const beforeMark = before ? await mark() : null;
703
733
  const result = await session.load(snapshot, {reset: forget});
704
734
  if (!current()) return {ok: false, cancelled: true};
705
735
  if (!result.ok) {
@@ -714,7 +744,7 @@ export function createViewController(session, {page = 12, historyLimit = 50, sce
714
744
  } else if (before) {
715
745
  const after = await intent();
716
746
  if (!same(before, after)) {
717
- back.push({label: remember, before, after});
747
+ back.push({label: remember, before, after, beforeMark, afterMark: await mark()});
718
748
  if (back.length > historyLimit) back.shift();
719
749
  forward = [];
720
750
  }