@arqlet/engine-client 0.4.0 → 0.5.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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@arqlet/engine-client",
3
- "version": "0.4.0",
3
+ "version": "0.5.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",
@@ -71,7 +71,7 @@
71
71
  "access": "public"
72
72
  },
73
73
  "dependencies": {
74
- "@arqlet/contracts": "^0.4.0"
74
+ "@arqlet/contracts": "^0.5.0"
75
75
  },
76
76
  "scripts": {
77
77
  "test": "node --test"
package/session.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- import type {Graph, Hidden, Overview, OverviewRequest, Scene, Snapshot} from '@arqlet/contracts';
1
+ import type {Graph, GraphNeighbourhood, GraphPatch, GraphTrace, Hidden, NeighbourhoodAnswer, Overview, OverviewRequest, Scene, Snapshot, TraceAnswer} from '@arqlet/contracts';
2
2
  import type {EngineRequest, EngineResponse, Run} from './wasm-client.js';
3
3
 
4
4
  /** An engine answer as the session reports it. `stale`: an import or reset overtook the change, nothing was applied. */
@@ -11,6 +11,9 @@ export interface SessionResult {
11
11
  unchanged?: boolean;
12
12
  }
13
13
 
14
+ /** The answer of a read-only request (`trace`, `neighbourhood`): its fields beside `ok: true`, or the error. */
15
+ export type ReadAnswer<T> = ({ok: true} & T) | {ok: false; error: string};
16
+
14
17
  /** Provisional: one graph, its current snapshot, and changes applied in order with revision checks. */
15
18
  export interface Session {
16
19
  /**
@@ -41,11 +44,26 @@ export interface Session {
41
44
  * and routes of the current view that carry them; `distance` the hops of every visible node from the path.
42
45
  * `edgeKinds`: the paths follow only edges of these kinds (default: every edge).
43
46
  */
44
- trace(from: string, to: string, init?: {mode?: 'shortest' | 'all'; edgeKinds?: string[]}): Promise<EngineResponse>;
47
+ trace(from: string, to: string, init?: {mode?: 'shortest' | 'all'; edgeKinds?: string[]}): Promise<ReadAnswer<TraceAnswer>>;
48
+ /**
49
+ * Provisional: the nodes within `hops` edges (default 1) of a node or of each of a list, read only, answered as
50
+ * `trace` (`nodes`, `edges`, `visible`, `distance`) plus the `hops` of every node; `nodes` can go to `reveal`.
51
+ * `direction`: `out` (what it leads to), `in` (what leads to it) or `both` (default); `edgeKinds`: only these kinds.
52
+ */
53
+ neighbourhood(
54
+ nodeId: string | string[],
55
+ init?: {hops?: number; direction?: 'out' | 'in' | 'both'; edgeKinds?: string[]},
56
+ ): Promise<ReadAnswer<NeighbourhoodAnswer>>;
57
+ /**
58
+ * Provisional: changes the graph without a new import (removals first; a removed node takes what it holds and its
59
+ * edges along); the view keeps its state and is laid out as a toggle lays it out (abstracting views) or again from
60
+ * where it was.
61
+ */
62
+ patch(patch: GraphPatch): Promise<SessionResult>;
45
63
  /** Provisional: opens the groups that hold `nodeIds`; with `partial` (default true) only as far as needed. */
46
64
  reveal(nodeIds: string[], init?: {partial?: boolean}): Promise<SessionResult>;
47
- /** Provisional: hides nodes by kind or id (with what they hold) and edges by kind, keeping the place of the rest;
48
- * `hide` replaces what was hidden before, `{}` shows everything. */
65
+ /** Provisional: hides nodes by kind or id (with what they hold) and edges by kind, keeping the place of the rest, or
66
+ * (`compact: true`) laying the view out again without them; `hide` replaces what was hidden before, `{}` shows everything. */
49
67
  filter(hide?: Hidden): Promise<SessionResult>;
50
68
  /** The scene of the current snapshot, computed once per snapshot. Throws the engine's error. */
51
69
  scene(): Promise<Scene>;
@@ -118,6 +136,13 @@ export interface OverviewSession {
118
136
  open(graph: Graph, request?: OverviewSessionRequest): Promise<OverviewAnswer>;
119
137
  /** Lays out the graph again; `start` without positions continues from the last overview (same cluster options). */
120
138
  layOut(request?: OverviewSessionRequest): Promise<OverviewAnswer>;
139
+ /** Provisional: the paths between two nodes of the overview's graph, read only (`GraphTrace`). */
140
+ trace(from: string, to: string, init?: {mode?: 'shortest' | 'all'; edgeKinds?: string[]}): Promise<ReadAnswer<GraphTrace>>;
141
+ /** Provisional: the nodes within `hops` of a node (or of each of a list) of the overview's graph, read only. */
142
+ neighbourhood(
143
+ nodeId: string | string[],
144
+ init?: {hops?: number; direction?: 'out' | 'in' | 'both'; edgeKinds?: string[]},
145
+ ): Promise<ReadAnswer<GraphNeighbourhood>>;
121
146
  /** The last overview, or null. */
122
147
  readonly overview: OverviewAnswer['overview'] | null;
123
148
  /** Whether the graph stays in the engine. */
package/session.js CHANGED
@@ -175,12 +175,31 @@ export function createSession(run, {onChange, resident: wanted = 'auto'} = {}) {
175
175
  return call({op: 'trace', snapshot, from, to, mode, ...(edgeKinds ? {edgeKinds} : {})});
176
176
  },
177
177
 
178
+ /**
179
+ * Provisional: the nodes within `hops` edges (default 1) of `nodeId` (a node id or a list), read only:
180
+ * `{ok, nodes, edges, hops, visible: {nodes, routes}, distance}` as for `trace`; `nodes` can go to `reveal`.
181
+ * `direction`: `out`, `in` or `both` (default); `edgeKinds`: follow only edges of these kinds.
182
+ */
183
+ async neighbourhood(nodeId, {hops, direction, edgeKinds} = {}) {
184
+ if (!snapshot) return {ok: false, error: 'Nothing imported yet.'};
185
+ return call({op: 'neighbourhood', snapshot, ...(Array.isArray(nodeId) ? {nodeIds: nodeId} : {nodeId}),
186
+ ...(hops !== undefined ? {hops} : {}), ...(direction ? {direction} : {}), ...(edgeKinds ? {edgeKinds} : {})});
187
+ },
188
+
189
+ /**
190
+ * Provisional: changes the graph without a new import: `{nodes, edges, removeNodes, removeEdges}` (removals first; a
191
+ * removed node takes what it holds and its edges along). The view keeps its state; with a level of detail,
192
+ * communities or groups it is laid out as a toggle lays it out, otherwise again from where it was.
193
+ */
194
+ patch(patch) { return change({op: 'patch', patch}); },
195
+
178
196
  /** Provisional: opens the groups that hold `nodeIds`; with `partial` (default) only as far as needed. */
179
197
  reveal(nodeIds, {partial = true} = {}) { return change({op: 'reveal', nodeIds, partial}); },
180
198
 
181
199
  /**
182
- * Provisional: hides nodes by kind or id (each with what it holds) and edges by kind, keeping the place of the rest.
183
- * `hide` replaces what was hidden before; `{}` shows everything again.
200
+ * Provisional: hides nodes by kind or id (each with what it holds) and edges by kind, keeping the place of the rest;
201
+ * with `compact: true` the view is laid out again without them, so the rest closes up. `hide` replaces what was hidden
202
+ * before; `{}` shows everything again.
184
203
  */
185
204
  filter(hide = {}) { return change({op: 'filter', hide}); },
186
205
 
@@ -218,8 +237,8 @@ export function createSession(run, {onChange, resident: wanted = 'auto'} = {}) {
218
237
  }
219
238
 
220
239
  // The options the overview's clusters depend on; a continuation needs the same ones (as the engine's session checks).
221
- // Defaults as the engine's (`communities` 120, `satellites` 0); `groups` compared as given.
222
- const clustersOf = options => JSON.stringify([options?.communities ?? 120, options?.satellites ?? 0, options?.groups ?? null]);
240
+ // Defaults as the engine's (`communities` 120, `satellites` 0); `groups` and `hubs` compared as given.
241
+ const clustersOf = options => JSON.stringify([options?.communities ?? 120, options?.satellites ?? 0, options?.groups ?? null, options?.hubs ?? null]);
223
242
 
224
243
  /**
225
244
  * Provisional (ADR 0019 for the overview): an overview of one graph, laid out again as the host asks, with the graph kept
@@ -236,7 +255,17 @@ export function createOverviewSession(run, {resident: wanted = 'auto'} = {}) {
236
255
  let graph = null, started = false, last = null, clusters = null, generation = 0, queue = Promise.resolve();
237
256
 
238
257
  const send = async request => {
239
- if (resident) return (await opened).run(request);
258
+ if (resident) {
259
+ // On the graph the engine has, only what moves comes back (`positionsOnly`) while the clusters stay; it is put
260
+ // together with the last overview here, so every answer is a whole overview.
261
+ const asked = !request.graph && last ? {...request, positionsOnly: true} : request;
262
+ const answer = await (await opened).run(asked);
263
+ if (answer.ok && answer.overview?.positionsOnly) {
264
+ const {positionsOnly: _moved, ...moved} = answer.overview;
265
+ return {...answer, overview: {...last, ...moved}};
266
+ }
267
+ return answer;
268
+ }
240
269
  const sent = {...request, graph: request.graph ?? graph};
241
270
  const start = request.start;
242
271
  if (start && start.x === undefined) {
@@ -246,6 +275,17 @@ export function createOverviewSession(run, {resident: wanted = 'auto'} = {}) {
246
275
  }
247
276
  return run(sent);
248
277
  };
278
+ // A read of the overview's graph, after the changes before it: the session's graph, or the one kept here.
279
+ const read = request => {
280
+ const mine = generation;
281
+ const result = queue.then(async () => {
282
+ if (!started) return {ok: false, error: 'No graph yet: open one first.'};
283
+ const answer = resident ? await (await opened).run(request) : await run({...request, graph});
284
+ return mine === generation ? answer : {ok: false, stale: true, error: 'A new graph overtook this read.'};
285
+ });
286
+ queue = result.catch(() => {});
287
+ return result;
288
+ };
249
289
  const enqueue = (request, fresh) => {
250
290
  if (fresh) generation++;
251
291
  const mine = generation;
@@ -276,6 +316,18 @@ export function createOverviewSession(run, {resident: wanted = 'auto'} = {}) {
276
316
  const {graph: _ignored, ...rest} = request;
277
317
  return enqueue({...rest, op: 'overview'}, false);
278
318
  },
319
+ /**
320
+ * Provisional: the paths between two nodes of the overview's graph (`overviewTrace`: `{ok, found, reversed, length,
321
+ * nodes, edges}`), read only; `mode` and `edgeKinds` as for `trace`. The viewer's `pathOf` maps them to its discs.
322
+ */
323
+ trace(from, to, {mode, edgeKinds} = {}) {
324
+ return read({op: 'overviewTrace', from, to, ...(mode ? {mode} : {}), ...(edgeKinds ? {edgeKinds} : {})});
325
+ },
326
+ /** Provisional: the nodes within `hops` of a node of the overview's graph (`overviewNeighbourhood`), read only. */
327
+ neighbourhood(nodeId, {hops, direction, edgeKinds} = {}) {
328
+ return read({op: 'overviewNeighbourhood', ...(Array.isArray(nodeId) ? {nodeIds: nodeId} : {nodeId}),
329
+ ...(hops !== undefined ? {hops} : {}), ...(direction ? {direction} : {}), ...(edgeKinds ? {edgeKinds} : {})});
330
+ },
279
331
  /** The last overview (`{ids, parent, x, y, r, …}`), or null. */
280
332
  get overview() { return last; },
281
333
  /** Whether the graph stays in the engine. */