@dxos/app-graph 0.8.4-main.5ad4a44 → 0.8.4-main.66e292d
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/dist/lib/browser/index.mjs +201 -223
- package/dist/lib/browser/index.mjs.map +3 -3
- package/dist/lib/browser/meta.json +1 -1
- package/dist/lib/node-esm/index.mjs +201 -223
- package/dist/lib/node-esm/index.mjs.map +3 -3
- package/dist/lib/node-esm/meta.json +1 -1
- package/dist/types/src/graph-builder.d.ts +15 -15
- package/dist/types/src/graph-builder.d.ts.map +1 -1
- package/dist/types/src/graph.d.ts +18 -18
- package/dist/types/src/graph.d.ts.map +1 -1
- package/dist/types/src/node.d.ts +1 -1
- package/dist/types/src/node.d.ts.map +1 -1
- package/dist/types/src/stories/EchoGraph.stories.d.ts.map +1 -1
- package/dist/types/src/testing.d.ts +3 -4
- package/dist/types/src/testing.d.ts.map +1 -1
- package/dist/types/tsconfig.tsbuildinfo +1 -1
- package/package.json +22 -22
- package/src/graph-builder.test.ts +26 -26
- package/src/graph-builder.ts +44 -42
- package/src/graph.test.ts +2 -2
- package/src/graph.ts +111 -71
- package/src/node.ts +1 -1
- package/src/signals-integration.test.ts +32 -27
- package/src/stories/EchoGraph.stories.tsx +32 -33
- package/src/stories/Tree.tsx +1 -1
- package/src/testing.ts +6 -5
- package/dist/types/src/experimental/graph-projections.test.d.ts +0 -25
- package/dist/types/src/experimental/graph-projections.test.d.ts.map +0 -1
- package/src/experimental/graph-projections.test.ts +0 -56
package/src/graph.ts
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
// Copyright 2023 DXOS.org
|
|
3
3
|
//
|
|
4
4
|
|
|
5
|
-
import {
|
|
5
|
+
import { Atom, Registry } from '@effect-atom/atom-react';
|
|
6
6
|
import * as Function from 'effect/Function';
|
|
7
7
|
import * as Option from 'effect/Option';
|
|
8
8
|
import * as Record from 'effect/Record';
|
|
@@ -16,7 +16,9 @@ import { type MakeOptional, isNonNullable } from '@dxos/util';
|
|
|
16
16
|
import { type Action, type ActionGroup, type Node, type NodeArg, type Relation } from './node';
|
|
17
17
|
|
|
18
18
|
const graphSymbol = Symbol('graph');
|
|
19
|
-
type DeepWriteable<T> = {
|
|
19
|
+
type DeepWriteable<T> = {
|
|
20
|
+
-readonly [K in keyof T]: T[K] extends object ? DeepWriteable<T[K]> : T[K];
|
|
21
|
+
};
|
|
20
22
|
type NodeInternal = DeepWriteable<Node> & { [graphSymbol]: Graph };
|
|
21
23
|
|
|
22
24
|
/**
|
|
@@ -79,32 +81,32 @@ export interface ReadableGraph {
|
|
|
79
81
|
*/
|
|
80
82
|
toJSON(id?: string): object;
|
|
81
83
|
|
|
82
|
-
json(id?: string):
|
|
84
|
+
json(id?: string): Atom.Atom<any>;
|
|
83
85
|
|
|
84
86
|
/**
|
|
85
|
-
* Get the
|
|
87
|
+
* Get the atom key for the node with the given id.
|
|
86
88
|
*/
|
|
87
|
-
node(id: string):
|
|
89
|
+
node(id: string): Atom.Atom<Option.Option<Node>>;
|
|
88
90
|
|
|
89
91
|
/**
|
|
90
|
-
* Get the
|
|
92
|
+
* Get the atom key for the node with the given id.
|
|
91
93
|
*/
|
|
92
|
-
nodeOrThrow(id: string):
|
|
94
|
+
nodeOrThrow(id: string): Atom.Atom<Node>;
|
|
93
95
|
|
|
94
96
|
/**
|
|
95
|
-
* Get the
|
|
97
|
+
* Get the atom key for the connections of the node with the given id.
|
|
96
98
|
*/
|
|
97
|
-
connections(id: string, relation?: Relation):
|
|
99
|
+
connections(id: string, relation?: Relation): Atom.Atom<Node[]>;
|
|
98
100
|
|
|
99
101
|
/**
|
|
100
|
-
* Get the
|
|
102
|
+
* Get the atom key for the actions of the node with the given id.
|
|
101
103
|
*/
|
|
102
|
-
actions(id: string):
|
|
104
|
+
actions(id: string): Atom.Atom<(Action | ActionGroup)[]>;
|
|
103
105
|
|
|
104
106
|
/**
|
|
105
|
-
* Get the
|
|
107
|
+
* Get the atom key for the edges of the node with the given id.
|
|
106
108
|
*/
|
|
107
|
-
edges(id: string):
|
|
109
|
+
edges(id: string): Atom.Atom<Edges>;
|
|
108
110
|
|
|
109
111
|
/**
|
|
110
112
|
* Alias for `getNodeOrThrow(ROOT_ID)`.
|
|
@@ -228,7 +230,10 @@ export interface WritableGraph extends ExpandableGraph {
|
|
|
228
230
|
* The Graph represents the user interface information architecture of the application constructed via plugins.
|
|
229
231
|
*/
|
|
230
232
|
export class Graph implements WritableGraph {
|
|
231
|
-
readonly onNodeChanged = new Event<{
|
|
233
|
+
readonly onNodeChanged = new Event<{
|
|
234
|
+
id: string;
|
|
235
|
+
node: Option.Option<Node>;
|
|
236
|
+
}>();
|
|
232
237
|
|
|
233
238
|
private readonly _onExpand?: (id: string, relation: Relation) => void;
|
|
234
239
|
private readonly _onInitialize?: (id: string) => Promise<void>;
|
|
@@ -239,55 +244,63 @@ export class Graph implements WritableGraph {
|
|
|
239
244
|
private readonly _initialized = Record.empty<string, boolean>();
|
|
240
245
|
private readonly _initialEdges = Record.empty<string, Edges>();
|
|
241
246
|
private readonly _initialNodes = Record.fromEntries([
|
|
242
|
-
[
|
|
247
|
+
[
|
|
248
|
+
ROOT_ID,
|
|
249
|
+
this._constructNode({
|
|
250
|
+
id: ROOT_ID,
|
|
251
|
+
type: ROOT_TYPE,
|
|
252
|
+
data: null,
|
|
253
|
+
properties: {},
|
|
254
|
+
}),
|
|
255
|
+
],
|
|
243
256
|
]);
|
|
244
257
|
|
|
245
258
|
/** @internal */
|
|
246
|
-
readonly _node =
|
|
259
|
+
readonly _node = Atom.family<string, Atom.Writable<Option.Option<Node>>>((id) => {
|
|
247
260
|
const initial = Option.flatten(Record.get(this._initialNodes, id));
|
|
248
|
-
return
|
|
261
|
+
return Atom.make<Option.Option<Node>>(initial).pipe(Atom.keepAlive, Atom.withLabel(`graph:node:${id}`));
|
|
249
262
|
});
|
|
250
263
|
|
|
251
|
-
private readonly _nodeOrThrow =
|
|
252
|
-
return
|
|
264
|
+
private readonly _nodeOrThrow = Atom.family<string, Atom.Atom<Node>>((id) => {
|
|
265
|
+
return Atom.make((get) => {
|
|
253
266
|
const node = get(this._node(id));
|
|
254
267
|
invariant(Option.isSome(node), `Node not available: ${id}`);
|
|
255
268
|
return node.value;
|
|
256
269
|
});
|
|
257
270
|
});
|
|
258
271
|
|
|
259
|
-
private readonly _edges =
|
|
272
|
+
private readonly _edges = Atom.family<string, Atom.Writable<Edges>>((id) => {
|
|
260
273
|
const initial = Record.get(this._initialEdges, id).pipe(Option.getOrElse(() => ({ inbound: [], outbound: [] })));
|
|
261
|
-
return
|
|
274
|
+
return Atom.make<Edges>(initial).pipe(Atom.keepAlive, Atom.withLabel(`graph:edges:${id}`));
|
|
262
275
|
});
|
|
263
276
|
|
|
264
|
-
// NOTE: Currently the argument to the family needs to be referentially stable for the
|
|
265
|
-
// TODO(wittjosiah):
|
|
266
|
-
private readonly _connections =
|
|
267
|
-
return
|
|
277
|
+
// NOTE: Currently the argument to the family needs to be referentially stable for the atom to be referentially stable.
|
|
278
|
+
// TODO(wittjosiah): Atom feature request, support for something akin to `ComplexMap` to allow for complex arguments.
|
|
279
|
+
private readonly _connections = Atom.family<string, Atom.Atom<Node[]>>((key) => {
|
|
280
|
+
return Atom.make((get) => {
|
|
268
281
|
const [id, relation] = key.split('$');
|
|
269
282
|
const edges = get(this._edges(id));
|
|
270
283
|
return edges[relation as Relation]
|
|
271
284
|
.map((id) => get(this._node(id)))
|
|
272
285
|
.filter(Option.isSome)
|
|
273
286
|
.map((o) => o.value);
|
|
274
|
-
}).pipe(
|
|
287
|
+
}).pipe(Atom.withLabel(`graph:connections:${key}`));
|
|
275
288
|
});
|
|
276
289
|
|
|
277
|
-
private readonly _actions =
|
|
278
|
-
return
|
|
290
|
+
private readonly _actions = Atom.family<string, Atom.Atom<(Action | ActionGroup)[]>>((id) => {
|
|
291
|
+
return Atom.make((get) => {
|
|
279
292
|
return get(this._connections(`${id}$outbound`)).filter(
|
|
280
293
|
(node) => node.type === ACTION_TYPE || node.type === ACTION_GROUP_TYPE,
|
|
281
294
|
);
|
|
282
|
-
}).pipe(
|
|
295
|
+
}).pipe(Atom.withLabel(`graph:actions:${id}`));
|
|
283
296
|
});
|
|
284
297
|
|
|
285
|
-
private readonly _json =
|
|
286
|
-
return
|
|
298
|
+
private readonly _json = Atom.family<string, Atom.Atom<any>>((id) => {
|
|
299
|
+
return Atom.make((get) => {
|
|
287
300
|
const toJSON = (node: Node, seen: string[] = []): any => {
|
|
288
301
|
const nodes = get(this.connections(node.id));
|
|
289
302
|
const obj: Record<string, any> = {
|
|
290
|
-
id: node.id
|
|
303
|
+
id: node.id,
|
|
291
304
|
type: node.type,
|
|
292
305
|
};
|
|
293
306
|
if (node.properties.label) {
|
|
@@ -307,7 +320,7 @@ export class Graph implements WritableGraph {
|
|
|
307
320
|
|
|
308
321
|
const root = get(this.nodeOrThrow(id));
|
|
309
322
|
return toJSON(root);
|
|
310
|
-
}).pipe(
|
|
323
|
+
}).pipe(Atom.withLabel(`graph:json:${id}`));
|
|
311
324
|
});
|
|
312
325
|
|
|
313
326
|
constructor({ registry, nodes, edges, onInitialize, onExpand, onRemoveNode }: GraphParams = {}) {
|
|
@@ -337,15 +350,15 @@ export class Graph implements WritableGraph {
|
|
|
337
350
|
return this._json(id);
|
|
338
351
|
}
|
|
339
352
|
|
|
340
|
-
node(id: string):
|
|
353
|
+
node(id: string): Atom.Atom<Option.Option<Node>> {
|
|
341
354
|
return this._node(id);
|
|
342
355
|
}
|
|
343
356
|
|
|
344
|
-
nodeOrThrow(id: string):
|
|
357
|
+
nodeOrThrow(id: string): Atom.Atom<Node> {
|
|
345
358
|
return this._nodeOrThrow(id);
|
|
346
359
|
}
|
|
347
360
|
|
|
348
|
-
connections(id: string, relation: Relation = 'outbound'):
|
|
361
|
+
connections(id: string, relation: Relation = 'outbound'): Atom.Atom<Node[]> {
|
|
349
362
|
return this._connections(`${id}$${relation}`);
|
|
350
363
|
}
|
|
351
364
|
|
|
@@ -353,7 +366,7 @@ export class Graph implements WritableGraph {
|
|
|
353
366
|
return this._actions(id);
|
|
354
367
|
}
|
|
355
368
|
|
|
356
|
-
edges(id: string):
|
|
369
|
+
edges(id: string): Atom.Atom<Edges> {
|
|
357
370
|
return this._edges(id);
|
|
358
371
|
}
|
|
359
372
|
|
|
@@ -401,38 +414,48 @@ export class Graph implements WritableGraph {
|
|
|
401
414
|
}
|
|
402
415
|
|
|
403
416
|
addNodes(nodes: NodeArg<any, Record<string, any>>[]): void {
|
|
404
|
-
|
|
417
|
+
Atom.batch(() => {
|
|
405
418
|
nodes.map((node) => this.addNode(node));
|
|
406
419
|
});
|
|
407
420
|
}
|
|
408
421
|
|
|
409
422
|
addNode({ nodes, edges, ...nodeArg }: NodeArg<any, Record<string, any>>): void {
|
|
410
423
|
const { id, type, data = null, properties = {} } = nodeArg;
|
|
411
|
-
const
|
|
412
|
-
const node = this._registry.get(
|
|
424
|
+
const nodeAtom = this._node(id);
|
|
425
|
+
const node = this._registry.get(nodeAtom);
|
|
413
426
|
Option.match(node, {
|
|
414
427
|
onSome: (node) => {
|
|
415
428
|
const typeChanged = node.type !== type;
|
|
416
429
|
const dataChanged = node.data !== data;
|
|
417
430
|
const propertiesChanged = Object.keys(properties).some((key) => node.properties[key] !== properties[key]);
|
|
418
|
-
log('existing node', {
|
|
431
|
+
log('existing node', {
|
|
432
|
+
id,
|
|
433
|
+
typeChanged,
|
|
434
|
+
dataChanged,
|
|
435
|
+
propertiesChanged,
|
|
436
|
+
});
|
|
419
437
|
if (typeChanged || dataChanged || propertiesChanged) {
|
|
420
438
|
log('updating node', { id, type, data, properties });
|
|
421
|
-
const newNode = Option.some({
|
|
422
|
-
|
|
439
|
+
const newNode = Option.some({
|
|
440
|
+
...node,
|
|
441
|
+
type,
|
|
442
|
+
data,
|
|
443
|
+
properties: { ...node.properties, ...properties },
|
|
444
|
+
});
|
|
445
|
+
this._registry.set(nodeAtom, newNode);
|
|
423
446
|
this.onNodeChanged.emit({ id, node: newNode });
|
|
424
447
|
}
|
|
425
448
|
},
|
|
426
449
|
onNone: () => {
|
|
427
450
|
log('new node', { id, type, data, properties });
|
|
428
451
|
const newNode = this._constructNode({ id, type, data, properties });
|
|
429
|
-
this._registry.set(
|
|
452
|
+
this._registry.set(nodeAtom, newNode);
|
|
430
453
|
this.onNodeChanged.emit({ id, node: newNode });
|
|
431
454
|
},
|
|
432
455
|
});
|
|
433
456
|
|
|
434
457
|
if (nodes) {
|
|
435
|
-
//
|
|
458
|
+
// Atom.batch(() => {
|
|
436
459
|
this.addNodes(nodes);
|
|
437
460
|
const _edges = nodes.map((node) => ({ source: id, target: node.id }));
|
|
438
461
|
this.addEdges(_edges);
|
|
@@ -445,15 +468,15 @@ export class Graph implements WritableGraph {
|
|
|
445
468
|
}
|
|
446
469
|
|
|
447
470
|
removeNodes(ids: string[], edges = false): void {
|
|
448
|
-
|
|
471
|
+
Atom.batch(() => {
|
|
449
472
|
ids.map((id) => this.removeNode(id, edges));
|
|
450
473
|
});
|
|
451
474
|
}
|
|
452
475
|
|
|
453
476
|
removeNode(id: string, edges = false): void {
|
|
454
|
-
const
|
|
455
|
-
// TODO(wittjosiah): Is there a way to mark these
|
|
456
|
-
this._registry.set(
|
|
477
|
+
const nodeAtom = this._node(id);
|
|
478
|
+
// TODO(wittjosiah): Is there a way to mark these atom values for garbage collection?
|
|
479
|
+
this._registry.set(nodeAtom, Option.none());
|
|
457
480
|
this.onNodeChanged.emit({ id, node: Option.none() });
|
|
458
481
|
// TODO(wittjosiah): Reset expanded and initialized flags?
|
|
459
482
|
|
|
@@ -470,55 +493,67 @@ export class Graph implements WritableGraph {
|
|
|
470
493
|
}
|
|
471
494
|
|
|
472
495
|
addEdges(edges: Edge[]): void {
|
|
473
|
-
|
|
496
|
+
Atom.batch(() => {
|
|
474
497
|
edges.map((edge) => this.addEdge(edge));
|
|
475
498
|
});
|
|
476
499
|
}
|
|
477
500
|
|
|
478
501
|
addEdge(edgeArg: Edge): void {
|
|
479
|
-
const
|
|
480
|
-
const source = this._registry.get(
|
|
502
|
+
const sourceAtom = this._edges(edgeArg.source);
|
|
503
|
+
const source = this._registry.get(sourceAtom);
|
|
481
504
|
if (!source.outbound.includes(edgeArg.target)) {
|
|
482
|
-
log('add outbound edge', {
|
|
483
|
-
|
|
505
|
+
log('add outbound edge', {
|
|
506
|
+
source: edgeArg.source,
|
|
507
|
+
target: edgeArg.target,
|
|
508
|
+
});
|
|
509
|
+
this._registry.set(sourceAtom, {
|
|
510
|
+
inbound: source.inbound,
|
|
511
|
+
outbound: [...source.outbound, edgeArg.target],
|
|
512
|
+
});
|
|
484
513
|
}
|
|
485
514
|
|
|
486
|
-
const
|
|
487
|
-
const target = this._registry.get(
|
|
515
|
+
const targetAtom = this._edges(edgeArg.target);
|
|
516
|
+
const target = this._registry.get(targetAtom);
|
|
488
517
|
if (!target.inbound.includes(edgeArg.source)) {
|
|
489
|
-
log('add inbound edge', {
|
|
490
|
-
|
|
518
|
+
log('add inbound edge', {
|
|
519
|
+
source: edgeArg.source,
|
|
520
|
+
target: edgeArg.target,
|
|
521
|
+
});
|
|
522
|
+
this._registry.set(targetAtom, {
|
|
523
|
+
inbound: [...target.inbound, edgeArg.source],
|
|
524
|
+
outbound: target.outbound,
|
|
525
|
+
});
|
|
491
526
|
}
|
|
492
527
|
}
|
|
493
528
|
|
|
494
529
|
removeEdges(edges: Edge[], removeOrphans = false): void {
|
|
495
|
-
|
|
530
|
+
Atom.batch(() => {
|
|
496
531
|
edges.map((edge) => this.removeEdge(edge, removeOrphans));
|
|
497
532
|
});
|
|
498
533
|
}
|
|
499
534
|
|
|
500
535
|
removeEdge(edgeArg: Edge, removeOrphans = false): void {
|
|
501
|
-
const
|
|
502
|
-
const source = this._registry.get(
|
|
536
|
+
const sourceAtom = this._edges(edgeArg.source);
|
|
537
|
+
const source = this._registry.get(sourceAtom);
|
|
503
538
|
if (source.outbound.includes(edgeArg.target)) {
|
|
504
|
-
this._registry.set(
|
|
539
|
+
this._registry.set(sourceAtom, {
|
|
505
540
|
inbound: source.inbound,
|
|
506
541
|
outbound: source.outbound.filter((id) => id !== edgeArg.target),
|
|
507
542
|
});
|
|
508
543
|
}
|
|
509
544
|
|
|
510
|
-
const
|
|
511
|
-
const target = this._registry.get(
|
|
545
|
+
const targetAtom = this._edges(edgeArg.target);
|
|
546
|
+
const target = this._registry.get(targetAtom);
|
|
512
547
|
if (target.inbound.includes(edgeArg.source)) {
|
|
513
|
-
this._registry.set(
|
|
548
|
+
this._registry.set(targetAtom, {
|
|
514
549
|
inbound: target.inbound.filter((id) => id !== edgeArg.source),
|
|
515
550
|
outbound: target.outbound,
|
|
516
551
|
});
|
|
517
552
|
}
|
|
518
553
|
|
|
519
554
|
if (removeOrphans) {
|
|
520
|
-
const source = this._registry.get(
|
|
521
|
-
const target = this._registry.get(
|
|
555
|
+
const source = this._registry.get(sourceAtom);
|
|
556
|
+
const target = this._registry.get(targetAtom);
|
|
522
557
|
if (source.outbound.length === 0 && source.inbound.length === 0 && edgeArg.source !== ROOT_ID) {
|
|
523
558
|
this.removeNodes([edgeArg.source]);
|
|
524
559
|
}
|
|
@@ -529,12 +564,12 @@ export class Graph implements WritableGraph {
|
|
|
529
564
|
}
|
|
530
565
|
|
|
531
566
|
sortEdges(id: string, relation: Relation, order: string[]): void {
|
|
532
|
-
const
|
|
533
|
-
const edges = this._registry.get(
|
|
567
|
+
const edgesAtom = this._edges(id);
|
|
568
|
+
const edges = this._registry.get(edgesAtom);
|
|
534
569
|
const unsorted = edges[relation].filter((id) => !order.includes(id)) ?? [];
|
|
535
570
|
const sorted = order.filter((id) => edges[relation].includes(id)) ?? [];
|
|
536
571
|
edges[relation].splice(0, edges[relation].length, ...[...sorted, ...unsorted]);
|
|
537
|
-
this._registry.set(
|
|
572
|
+
this._registry.set(edgesAtom, edges);
|
|
538
573
|
}
|
|
539
574
|
|
|
540
575
|
traverse({ visitor, source = ROOT_ID, relation = 'outbound' }: GraphTraversalOptions, path: string[] = []): void {
|
|
@@ -599,6 +634,11 @@ export class Graph implements WritableGraph {
|
|
|
599
634
|
|
|
600
635
|
/** @internal */
|
|
601
636
|
_constructNode(node: NodeArg<any>): Option.Option<Node> {
|
|
602
|
-
return Option.some({
|
|
637
|
+
return Option.some({
|
|
638
|
+
[graphSymbol]: this,
|
|
639
|
+
data: null,
|
|
640
|
+
properties: {},
|
|
641
|
+
...node,
|
|
642
|
+
});
|
|
603
643
|
}
|
|
604
644
|
}
|
package/src/node.ts
CHANGED
|
@@ -77,7 +77,7 @@ export type InvokeParams = {
|
|
|
77
77
|
caller?: string;
|
|
78
78
|
};
|
|
79
79
|
|
|
80
|
-
export type ActionData = (params?: InvokeParams) => MaybePromise<
|
|
80
|
+
export type ActionData = (params?: InvokeParams) => MaybePromise<any>;
|
|
81
81
|
|
|
82
82
|
export type Action<TProperties extends Record<string, any> = Record<string, any>> = Readonly<
|
|
83
83
|
Omit<Node<ActionData, TProperties>, 'properties'> & {
|
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
// Copyright 2025 DXOS.org
|
|
3
3
|
//
|
|
4
4
|
|
|
5
|
-
import {
|
|
5
|
+
import { Atom, Registry } from '@effect-atom/atom-react';
|
|
6
6
|
import { signal } from '@preact/signals-core';
|
|
7
7
|
import { afterEach, beforeEach, describe, expect, onTestFinished, test } from 'vitest';
|
|
8
8
|
|
|
@@ -14,25 +14,25 @@ import { EchoTestBuilder } from '@dxos/echo-db/testing';
|
|
|
14
14
|
import { registerSignalsRuntime } from '@dxos/echo-signals';
|
|
15
15
|
|
|
16
16
|
import { ROOT_ID } from './graph';
|
|
17
|
-
import { GraphBuilder,
|
|
18
|
-
import {
|
|
17
|
+
import { GraphBuilder, atomFromSignal, createExtension } from './graph-builder';
|
|
18
|
+
import { atomFromQuery } from './testing';
|
|
19
19
|
|
|
20
20
|
registerSignalsRuntime();
|
|
21
21
|
|
|
22
22
|
const EXAMPLE_TYPE = 'dxos.org/type/example';
|
|
23
23
|
|
|
24
24
|
describe('signals integration', () => {
|
|
25
|
-
test('creating
|
|
25
|
+
test('creating atom from signal', () => {
|
|
26
26
|
const registry = Registry.make();
|
|
27
27
|
const state = signal<number>(0);
|
|
28
|
-
const value =
|
|
29
|
-
const inline =
|
|
30
|
-
// NOTE: This will create a new
|
|
31
|
-
// This test is verifying that this behaves the same as using a stable
|
|
28
|
+
const value = atomFromSignal(() => state.value);
|
|
29
|
+
const inline = Atom.make((get) => {
|
|
30
|
+
// NOTE: This will create a new atom instance each time.
|
|
31
|
+
// This test is verifying that this behaves the same as using a stable atom instance.
|
|
32
32
|
// The parent will remain subscribed to one instance until the new one is created.
|
|
33
33
|
// The old one will then be garbage collected because it is no longer referenced.
|
|
34
|
-
const
|
|
35
|
-
return get(
|
|
34
|
+
const atom = atomFromSignal(() => get(value));
|
|
35
|
+
return get(atom);
|
|
36
36
|
});
|
|
37
37
|
|
|
38
38
|
let count = 0;
|
|
@@ -72,15 +72,15 @@ describe('signals integration', () => {
|
|
|
72
72
|
await dbBuilder.close();
|
|
73
73
|
});
|
|
74
74
|
|
|
75
|
-
test('
|
|
75
|
+
test('atom references are loaded lazily and receive signal notifications', async () => {
|
|
76
76
|
const registry = Registry.make();
|
|
77
77
|
await using peer = await dbBuilder.createPeer();
|
|
78
78
|
|
|
79
79
|
let outerId: string;
|
|
80
80
|
{
|
|
81
81
|
await using db = await peer.createDatabase();
|
|
82
|
-
const inner = db.add({ name: 'inner' });
|
|
83
|
-
const outer = db.add({ inner: Ref.make(inner) });
|
|
82
|
+
const inner = db.add(Obj.make(Type.Expando, { name: 'inner' }));
|
|
83
|
+
const outer = db.add(Obj.make(Type.Expando, { inner: Ref.make(inner) }));
|
|
84
84
|
outerId = outer.id;
|
|
85
85
|
await db.flush();
|
|
86
86
|
}
|
|
@@ -89,23 +89,24 @@ describe('signals integration', () => {
|
|
|
89
89
|
{
|
|
90
90
|
await using db = await peer.openLastDatabase();
|
|
91
91
|
const outer = (await db.query(Filter.ids(outerId)).first()) as any;
|
|
92
|
-
const
|
|
93
|
-
|
|
92
|
+
const innerAtom = atomFromSignal(() => outer.inner.target);
|
|
94
93
|
const loaded = new Trigger();
|
|
94
|
+
|
|
95
95
|
let count = 0;
|
|
96
|
-
const cancel = registry.subscribe(
|
|
96
|
+
const cancel = registry.subscribe(innerAtom, (inner) => {
|
|
97
97
|
count++;
|
|
98
98
|
if (inner) {
|
|
99
99
|
loaded.wake();
|
|
100
100
|
}
|
|
101
101
|
});
|
|
102
|
+
|
|
102
103
|
onTestFinished(() => cancel());
|
|
103
104
|
|
|
104
|
-
expect(registry.get(
|
|
105
|
+
expect(registry.get(innerAtom)).to.eq(undefined);
|
|
105
106
|
expect(count).to.eq(1);
|
|
106
107
|
|
|
107
108
|
await loaded.wait();
|
|
108
|
-
expect(registry.get(
|
|
109
|
+
expect(registry.get(innerAtom)).to.include({ name: 'inner' });
|
|
109
110
|
expect(count).to.eq(2);
|
|
110
111
|
}
|
|
111
112
|
});
|
|
@@ -117,8 +118,8 @@ describe('signals integration', () => {
|
|
|
117
118
|
let outerId, innerId: string;
|
|
118
119
|
{
|
|
119
120
|
await using db = await peer.createDatabase();
|
|
120
|
-
const inner = db.add({ name: 'inner' });
|
|
121
|
-
const outer = db.add({ inner: Ref.make(inner) });
|
|
121
|
+
const inner = db.add(Obj.make(Type.Expando, { name: 'inner' }));
|
|
122
|
+
const outer = db.add(Obj.make(Type.Expando, { inner: Ref.make(inner) }));
|
|
122
123
|
innerId = inner.id;
|
|
123
124
|
outerId = outer.id;
|
|
124
125
|
await db.flush();
|
|
@@ -129,8 +130,8 @@ describe('signals integration', () => {
|
|
|
129
130
|
{
|
|
130
131
|
await using db = await peer.openLastDatabase();
|
|
131
132
|
const outer = (await db.query(Filter.ids(outerId)).first()) as any;
|
|
132
|
-
const
|
|
133
|
-
const inner = registry.get(
|
|
133
|
+
const innerAtom = atomFromSignal(() => outer.inner.target);
|
|
134
|
+
const inner = registry.get(innerAtom);
|
|
134
135
|
expect(inner).to.eq(undefined);
|
|
135
136
|
|
|
136
137
|
const builder = new GraphBuilder({ registry });
|
|
@@ -138,8 +139,8 @@ describe('signals integration', () => {
|
|
|
138
139
|
createExtension({
|
|
139
140
|
id: 'outbound-connector',
|
|
140
141
|
connector: () =>
|
|
141
|
-
|
|
142
|
-
const inner = get(
|
|
142
|
+
Atom.make((get) => {
|
|
143
|
+
const inner = get(innerAtom) as any;
|
|
143
144
|
return inner ? [{ id: inner.id, type: EXAMPLE_TYPE, data: inner.name }] : [];
|
|
144
145
|
}),
|
|
145
146
|
}),
|
|
@@ -184,9 +185,13 @@ describe('signals integration', () => {
|
|
|
184
185
|
connector: () => {
|
|
185
186
|
const query = db.query(Filter.type(Type.Expando));
|
|
186
187
|
|
|
187
|
-
return
|
|
188
|
-
const objects = get(
|
|
189
|
-
return objects.map((object) => ({
|
|
188
|
+
return Atom.make((get) => {
|
|
189
|
+
const objects = get(atomFromQuery(query));
|
|
190
|
+
return objects.map((object) => ({
|
|
191
|
+
id: object.id,
|
|
192
|
+
type: EXAMPLE_TYPE,
|
|
193
|
+
data: object.name,
|
|
194
|
+
}));
|
|
190
195
|
});
|
|
191
196
|
},
|
|
192
197
|
}),
|