@dxos/app-graph 0.8.4-main.7ace549 → 0.8.4-main.937b3ca

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 (40) hide show
  1. package/dist/lib/browser/index.mjs +1012 -551
  2. package/dist/lib/browser/index.mjs.map +4 -4
  3. package/dist/lib/browser/meta.json +1 -1
  4. package/dist/lib/node-esm/index.mjs +1011 -551
  5. package/dist/lib/node-esm/index.mjs.map +4 -4
  6. package/dist/lib/node-esm/meta.json +1 -1
  7. package/dist/types/src/atoms.d.ts +8 -0
  8. package/dist/types/src/atoms.d.ts.map +1 -0
  9. package/dist/types/src/graph-builder.d.ts +107 -65
  10. package/dist/types/src/graph-builder.d.ts.map +1 -1
  11. package/dist/types/src/graph.d.ts +174 -204
  12. package/dist/types/src/graph.d.ts.map +1 -1
  13. package/dist/types/src/index.d.ts +6 -3
  14. package/dist/types/src/index.d.ts.map +1 -1
  15. package/dist/types/src/node-matcher.d.ts +218 -0
  16. package/dist/types/src/node-matcher.d.ts.map +1 -0
  17. package/dist/types/src/node-matcher.test.d.ts +2 -0
  18. package/dist/types/src/node-matcher.test.d.ts.map +1 -0
  19. package/dist/types/src/node.d.ts +32 -3
  20. package/dist/types/src/node.d.ts.map +1 -1
  21. package/dist/types/src/stories/EchoGraph.stories.d.ts.map +1 -1
  22. package/dist/types/tsconfig.tsbuildinfo +1 -1
  23. package/package.json +35 -33
  24. package/src/atoms.ts +25 -0
  25. package/src/graph-builder.test.ts +494 -78
  26. package/src/graph-builder.ts +543 -250
  27. package/src/graph.test.ts +297 -104
  28. package/src/graph.ts +934 -404
  29. package/src/index.ts +9 -3
  30. package/src/node-matcher.test.ts +301 -0
  31. package/src/node-matcher.ts +284 -0
  32. package/src/node.ts +39 -6
  33. package/src/stories/EchoGraph.stories.tsx +92 -82
  34. package/src/stories/Tree.tsx +1 -1
  35. package/dist/types/src/signals-integration.test.d.ts +0 -2
  36. package/dist/types/src/signals-integration.test.d.ts.map +0 -1
  37. package/dist/types/src/testing.d.ts +0 -4
  38. package/dist/types/src/testing.d.ts.map +0 -1
  39. package/src/signals-integration.test.ts +0 -223
  40. package/src/testing.ts +0 -21
@@ -6,24 +6,25 @@ import { Atom, type Registry, RegistryContext, useAtomValue } from '@effect-atom
6
6
  import { type Meta, type StoryObj } from '@storybook/react-vite';
7
7
  import * as Function from 'effect/Function';
8
8
  import * as Option from 'effect/Option';
9
- import type * as Schema from 'effect/Schema';
10
- import React, { type PropsWithChildren, useCallback, useContext, useEffect, useMemo, useState } from 'react';
9
+ import React, { type PropsWithChildren, useCallback, useContext, useEffect, useMemo, useRef, useState } from 'react';
11
10
 
12
- import { Filter, type Live, Query, type Space, SpaceState, isSpace, live } from '@dxos/client/echo';
13
- import { type Database, Obj, Type } from '@dxos/echo';
11
+ import { Filter, type Space, SpaceState, isSpace } from '@dxos/client/echo';
12
+ import { Obj, Query } from '@dxos/echo';
13
+ import { TestSchema } from '@dxos/echo/testing';
14
+ import { AtomObj, AtomQuery } from '@dxos/echo-atom';
14
15
  import { faker } from '@dxos/random';
15
16
  import { type Client, useClient } from '@dxos/react-client';
16
17
  import { withClientProvider } from '@dxos/react-client/testing';
17
18
  import { Icon, IconButton, Input, Select } from '@dxos/react-ui';
18
19
  import { withTheme } from '@dxos/react-ui/testing';
19
20
  import { Path, Tree } from '@dxos/react-ui-list';
20
- import { getSize, mx } from '@dxos/react-ui-theme';
21
+ import { getSize, mx } from '@dxos/ui-theme';
21
22
  import { byPosition, isNonNullable, safeParseInt } from '@dxos/util';
22
23
 
23
- import { type ExpandableGraph, ROOT_ID } from '../graph';
24
- import { GraphBuilder, atomFromObservable, atomFromSignal, createExtension } from '../graph-builder';
25
- import { type Node } from '../node';
26
- import { atomFromQuery } from '../testing';
24
+ import * as CreateAtom from '../atoms';
25
+ import * as Graph from '../graph';
26
+ import * as GraphBuilder from '../graph-builder';
27
+ import * as Node from '../node';
27
28
 
28
29
  import { JsonTree } from './Tree';
29
30
 
@@ -47,47 +48,44 @@ const actionWeights = {
47
48
  [Action.RENAME_OBJECT]: 4,
48
49
  };
49
50
 
50
- const createGraph = (client: Client, registry: Registry.Registry): ExpandableGraph => {
51
- const spaceBuilderExtension = createExtension({
51
+ const createGraph = (client: Client, registry: Registry.Registry): Graph.ExpandableGraph => {
52
+ const spaceBuilderExtension = GraphBuilder.createExtensionRaw({
52
53
  id: 'space',
53
54
  connector: (node) =>
54
55
  Atom.make((get) =>
55
56
  Function.pipe(
56
57
  get(node),
57
- Option.flatMap((node) => (node.id === ROOT_ID ? Option.some(node) : Option.none())),
58
+ Option.flatMap((node) => (node.id === Node.RootId ? Option.some(node) : Option.none())),
58
59
  Option.map(() => {
59
- const spaces = get(atomFromObservable(client.spaces)) ?? [];
60
+ const spaces = get(CreateAtom.fromObservable(client.spaces)) ?? [];
60
61
  return spaces
61
- .filter((space) => get(atomFromObservable(space.state)) === SpaceState.SPACE_READY)
62
- .map((space) => ({
63
- id: space.id,
64
- type: 'dxos.org/type/Space',
65
- properties: {
66
- label: get(atomFromSignal(() => space.properties.name)),
67
- },
68
- data: space,
69
- }));
62
+ .filter((space: any) => get(CreateAtom.fromObservable(space.state)) === SpaceState.SPACE_READY)
63
+ .map((space) => {
64
+ const propertiesSnapshot = get(AtomObj.make(space.properties));
65
+ return {
66
+ id: space.id,
67
+ type: 'dxos.org/type/Space',
68
+ properties: {
69
+ label: propertiesSnapshot.name,
70
+ },
71
+ data: space,
72
+ };
73
+ });
70
74
  }),
71
75
  Option.getOrElse(() => []),
72
76
  ),
73
77
  ),
74
78
  });
75
79
 
76
- const objectBuilderExtension = createExtension({
80
+ const objectBuilderExtension = GraphBuilder.createExtensionRaw({
77
81
  id: 'object',
78
82
  connector: (node) => {
79
- // TODO(wittjosiah): Find a simpler way to define this type.
80
- let result: Database.QueryResult<Schema.Schema.Type<typeof Type.Expando>> | undefined;
81
83
  return Atom.make((get) =>
82
84
  Function.pipe(
83
85
  get(node),
84
86
  Option.flatMap((node) => (isSpace(node.data) ? Option.some(node.data) : Option.none())),
85
87
  Option.map((space) => {
86
- if (!result) {
87
- result = space.db.query(Query.type(Type.Expando, { type: 'test' }));
88
- }
89
-
90
- const objects = get(atomFromQuery(result));
88
+ const objects = get(AtomQuery.make(space.db, Query.type(TestSchema.Expando, { type: 'test' })));
91
89
  return objects.map((object) => ({
92
90
  id: object.id,
93
91
  type: 'dxos.org/type/test',
@@ -101,13 +99,14 @@ const createGraph = (client: Client, registry: Registry.Registry): ExpandableGra
101
99
  },
102
100
  });
103
101
 
104
- const graph = new GraphBuilder({ registry })
105
- .addExtension(spaceBuilderExtension)
106
- .addExtension(objectBuilderExtension).graph;
102
+ const builder = GraphBuilder.make({ registry });
103
+ GraphBuilder.addExtension(builder, spaceBuilderExtension);
104
+ GraphBuilder.addExtension(builder, objectBuilderExtension);
105
+ const graph = builder.graph;
107
106
  graph.onNodeChanged.on(({ id }) => {
108
- graph.expand(id);
107
+ Graph.expand(graph, id);
109
108
  });
110
- graph.expand(ROOT_ID);
109
+ Graph.expand(graph, Node.RootId);
111
110
  (window as any).graph = graph;
112
111
  return graph;
113
112
  };
@@ -128,9 +127,9 @@ const getRandomSpace = (client: Client): Space | undefined => {
128
127
  const getSpaceWithObjects = async (client: Client): Promise<Space | undefined> => {
129
128
  const readySpaces = client.spaces.get().filter((space) => space.state.get() === SpaceState.SPACE_READY);
130
129
  const spaceQueries = await Promise.all(
131
- readySpaces.map((space) => space.db.query(Filter.type(Type.Expando, { type: 'test' })).run()),
130
+ readySpaces.map((space) => space.db.query(Filter.type(TestSchema.Expando, { type: 'test' })).run()),
132
131
  );
133
- const spaces = readySpaces.filter((space, index) => spaceQueries[index].objects.length > 0);
132
+ const spaces = readySpaces.filter((space, index) => spaceQueries[index].length > 0);
134
133
  return spaces[Math.floor(Math.random() * spaces.length)];
135
134
  };
136
135
 
@@ -147,14 +146,16 @@ const runAction = async (client: Client, action: Action) => {
147
146
  case Action.RENAME_SPACE: {
148
147
  const space = getRandomSpace(client);
149
148
  if (space) {
150
- space.properties.name = faker.commerce.productName();
149
+ Obj.change(space.properties, (p) => {
150
+ p.name = faker.commerce.productName();
151
+ });
151
152
  }
152
153
  break;
153
154
  }
154
155
 
155
156
  case Action.ADD_OBJECT:
156
157
  getRandomSpace(client)?.db.add(
157
- Obj.make(Type.Expando, {
158
+ Obj.make(TestSchema.Expando, {
158
159
  type: 'test',
159
160
  name: faker.commerce.productName(),
160
161
  }),
@@ -164,7 +165,7 @@ const runAction = async (client: Client, action: Action) => {
164
165
  case Action.REMOVE_OBJECT: {
165
166
  const space = await getSpaceWithObjects(client);
166
167
  if (space) {
167
- const { objects } = await space.db.query(Filter.type(Type.Expando, { type: 'test' })).run();
168
+ const objects = await space.db.query(Filter.type(TestSchema.Expando, { type: 'test' })).run();
168
169
  space.db.remove(objects[Math.floor(Math.random() * objects.length)]);
169
170
  }
170
171
  break;
@@ -173,8 +174,11 @@ const runAction = async (client: Client, action: Action) => {
173
174
  case Action.RENAME_OBJECT: {
174
175
  const space = await getSpaceWithObjects(client);
175
176
  if (space) {
176
- const { objects } = await space.db.query(Filter.type(Type.Expando, { type: 'test' })).run();
177
- objects[Math.floor(Math.random() * objects.length)].name = faker.commerce.productName();
177
+ const objects = await space.db.query(Filter.type(TestSchema.Expando, { type: 'test' })).run();
178
+ const object = objects[Math.floor(Math.random() * objects.length)];
179
+ Obj.change(object, (o) => {
180
+ o.name = faker.commerce.productName();
181
+ });
178
182
  }
179
183
  break;
180
184
  }
@@ -284,26 +288,37 @@ export const TreeView: Story = {
284
288
  const client = useClient();
285
289
  const registry = useContext(RegistryContext);
286
290
  const graph = useMemo(() => createGraph(client, registry), [client, registry]);
287
- const state = useMemo(() => new Map<string, Live<{ open: boolean; current: boolean }>>(), []);
291
+ const stateRef = useRef(new Map<string, Atom.Writable<{ open: boolean; current: boolean }>>());
292
+
293
+ const getOrCreateState = useMemo(
294
+ () => (path: string) => {
295
+ let atom = stateRef.current.get(path);
296
+ if (!atom) {
297
+ atom = Atom.make({ open: true, current: false }).pipe(Atom.keepAlive);
298
+ stateRef.current.set(path, atom);
299
+ }
300
+ return atom;
301
+ },
302
+ [],
303
+ );
288
304
 
289
305
  const useItems = useCallback(
290
- (node?: Node, options?: { disposition?: string; sort?: boolean }) => {
291
- const connections = useAtomValue(graph.connections(node?.id ?? ROOT_ID));
306
+ (node?: Node.Node, options?: { disposition?: string; sort?: boolean }) => {
307
+ const connections = useAtomValue(graph.connections(node?.id ?? Node.RootId));
292
308
  return options?.sort ? connections.toSorted((a, b) => byPosition(a.properties, b.properties)) : connections;
293
309
  },
294
310
  [graph],
295
311
  );
296
312
 
297
313
  const getProps = useCallback(
298
- (node: Node, path: string[]) => {
299
- const children = graph
300
- .getConnections(node.id, 'outbound')
314
+ (node: Node.Node, path: string[]) => {
315
+ const children = Graph.getConnections(graph, node.id, 'outbound')
301
316
  .map((n) => {
302
317
  // Break cycles.
303
318
  const nextPath = [...path, node.id];
304
- return nextPath.includes(n.id) ? undefined : (n as Node);
319
+ return nextPath.includes(n.id) ? undefined : (n as Node.Node);
305
320
  })
306
- .filter(isNonNullable) as Node[];
321
+ .filter(isNonNullable) as Node.Node[];
307
322
  const parentOf =
308
323
  children.length > 0 ? children.map(({ id }) => id) : node.properties.role === 'branch' ? [] : undefined;
309
324
  return {
@@ -316,59 +331,54 @@ export const TreeView: Story = {
316
331
  [graph],
317
332
  );
318
333
 
319
- const isOpen = useCallback(
320
- (_path: string[]) => {
321
- const path = Path.create(..._path);
322
- const object = state.get(path) ?? live({ open: true, current: false });
323
- if (!state.has(path)) {
324
- state.set(path, object);
325
- }
334
+ // Hook that subscribes to item state via Atom.
335
+ const useItemState = (_path: string[]) => {
336
+ const path = useMemo(() => Path.create(..._path), [_path.join('~')]);
337
+ const atom = getOrCreateState(path);
338
+ return useAtomValue(atom);
339
+ };
326
340
 
327
- return object.open;
328
- },
329
- [state],
330
- );
341
+ const useIsOpen = (_path: string[]) => {
342
+ return useItemState(_path).open;
343
+ };
331
344
 
332
- const isCurrent = useCallback(
333
- (_path: string[]) => {
334
- const path = Path.create(..._path);
335
- const object = state.get(path) ?? live({ open: false, current: false });
336
- if (!state.has(path)) {
337
- state.set(path, object);
338
- }
339
-
340
- return object.current;
341
- },
342
- [state],
343
- );
345
+ const useIsCurrent = (_path: string[]) => {
346
+ return useItemState(_path).current;
347
+ };
344
348
 
345
349
  const onOpenChange = useCallback(
346
350
  ({ path: _path, open }: { path: string[]; open: boolean }) => {
347
351
  const path = Path.create(..._path);
348
- const object = state.get(path);
349
- object!.open = open;
352
+ const atom = stateRef.current.get(path);
353
+ if (atom) {
354
+ const prev = registry.get(atom);
355
+ registry.set(atom, { ...prev, open });
356
+ }
350
357
  },
351
- [state],
358
+ [registry],
352
359
  );
353
360
 
354
361
  const onSelect = useCallback(
355
362
  ({ path: _path, current }: { path: string[]; current: boolean }) => {
356
363
  const path = Path.create(..._path);
357
- const object = state.get(path);
358
- object!.current = current;
364
+ const atom = stateRef.current.get(path);
365
+ if (atom) {
366
+ const prev = registry.get(atom);
367
+ registry.set(atom, { ...prev, current });
368
+ }
359
369
  },
360
- [state],
370
+ [registry],
361
371
  );
362
372
 
363
373
  return (
364
374
  <>
365
375
  <Controls />
366
376
  <Tree
367
- id={ROOT_ID}
377
+ id={Node.RootId}
368
378
  useItems={useItems}
369
379
  getProps={getProps}
370
- isOpen={isOpen}
371
- isCurrent={isCurrent}
380
+ useIsOpen={useIsOpen}
381
+ useIsCurrent={useIsCurrent}
372
382
  onOpenChange={onOpenChange}
373
383
  onSelect={onSelect}
374
384
  />
@@ -4,7 +4,7 @@
4
4
 
5
5
  import React, { type FC, type HTMLAttributes, useState } from 'react';
6
6
 
7
- import { mx } from '@dxos/react-ui-theme';
7
+ import { mx } from '@dxos/ui-theme';
8
8
 
9
9
  // TODO(burdon): Copied form devtools.
10
10
 
@@ -1,2 +0,0 @@
1
- export {};
2
- //# sourceMappingURL=signals-integration.test.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"signals-integration.test.d.ts","sourceRoot":"","sources":["../../../src/signals-integration.test.ts"],"names":[],"mappings":""}
@@ -1,4 +0,0 @@
1
- import { Atom } from '@effect-atom/atom-react';
2
- import { type Database, type Entity } from '@dxos/echo';
3
- export declare const atomFromQuery: <T extends Entity.Unknown = Entity.Unknown>(query: Database.QueryResult<T>) => Atom.Atom<T[]>;
4
- //# sourceMappingURL=testing.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"testing.d.ts","sourceRoot":"","sources":["../../../src/testing.ts"],"names":[],"mappings":"AAIA,OAAO,EAAE,IAAI,EAAE,MAAM,yBAAyB,CAAC;AAE/C,OAAO,EAAE,KAAK,QAAQ,EAAE,KAAK,MAAM,EAAE,MAAM,YAAY,CAAC;AAExD,eAAO,MAAM,aAAa,GAAI,CAAC,SAAS,MAAM,CAAC,OAAO,GAAG,MAAM,CAAC,OAAO,EACrE,OAAO,QAAQ,CAAC,WAAW,CAAC,CAAC,CAAC,KAC7B,IAAI,CAAC,IAAI,CAAC,CAAC,EAAE,CAUf,CAAC"}
@@ -1,223 +0,0 @@
1
- //
2
- // Copyright 2025 DXOS.org
3
- //
4
-
5
- import { Atom, Registry } from '@effect-atom/atom-react';
6
- import { signal } from '@preact/signals-core';
7
- import { afterEach, beforeEach, describe, expect, onTestFinished, test } from 'vitest';
8
-
9
- import { Trigger } from '@dxos/async';
10
- import { Obj, Type } from '@dxos/echo';
11
- import { Ref } from '@dxos/echo/internal';
12
- import { Filter } from '@dxos/echo-db';
13
- import { EchoTestBuilder } from '@dxos/echo-db/testing';
14
- import { registerSignalsRuntime } from '@dxos/echo-signals';
15
-
16
- import { ROOT_ID } from './graph';
17
- import { GraphBuilder, atomFromSignal, createExtension } from './graph-builder';
18
- import { atomFromQuery } from './testing';
19
-
20
- registerSignalsRuntime();
21
-
22
- const EXAMPLE_TYPE = 'dxos.org/type/example';
23
-
24
- describe('signals integration', () => {
25
- test('creating atom from signal', () => {
26
- const registry = Registry.make();
27
- const state = signal<number>(0);
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
- // The parent will remain subscribed to one instance until the new one is created.
33
- // The old one will then be garbage collected because it is no longer referenced.
34
- const atom = atomFromSignal(() => get(value));
35
- return get(atom);
36
- });
37
-
38
- let count = 0;
39
- const cancel = registry.subscribe(value, (value) => {
40
- count = value;
41
- });
42
- onTestFinished(() => cancel());
43
-
44
- let inlineCount = 0;
45
- const inlineCancel = registry.subscribe(inline, (value) => {
46
- inlineCount = value;
47
- });
48
- onTestFinished(() => inlineCancel());
49
-
50
- registry.get(value);
51
- registry.get(inline);
52
- expect(count).to.eq(0);
53
- expect(inlineCount).to.eq(0);
54
-
55
- state.value = 1;
56
- expect(count).to.eq(1);
57
- expect(inlineCount).to.eq(1);
58
-
59
- state.value = 2;
60
- expect(count).to.eq(2);
61
- expect(inlineCount).to.eq(2);
62
- });
63
-
64
- describe('echo', () => {
65
- let dbBuilder: EchoTestBuilder;
66
-
67
- beforeEach(async () => {
68
- dbBuilder = await new EchoTestBuilder().open();
69
- });
70
-
71
- afterEach(async () => {
72
- await dbBuilder.close();
73
- });
74
-
75
- test('atom references are loaded lazily and receive signal notifications', async () => {
76
- const registry = Registry.make();
77
- await using peer = await dbBuilder.createPeer();
78
-
79
- let outerId: string;
80
- {
81
- await using db = await peer.createDatabase();
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
- outerId = outer.id;
85
- await db.flush();
86
- }
87
-
88
- await peer.reload();
89
- {
90
- await using db = await peer.openLastDatabase();
91
- const outer = (await db.query(Filter.ids(outerId)).first()) as any;
92
- const innerAtom = atomFromSignal(() => outer.inner.target);
93
- const loaded = new Trigger();
94
-
95
- let count = 0;
96
- const cancel = registry.subscribe(innerAtom, (inner) => {
97
- count++;
98
- if (inner) {
99
- loaded.wake();
100
- }
101
- });
102
-
103
- onTestFinished(() => cancel());
104
-
105
- expect(registry.get(innerAtom)).to.eq(undefined);
106
- expect(count).to.eq(1);
107
-
108
- await loaded.wait();
109
- expect(registry.get(innerAtom)).to.include({ name: 'inner' });
110
- expect(count).to.eq(2);
111
- }
112
- });
113
-
114
- test('references graph builder', async () => {
115
- const registry = Registry.make();
116
- await using peer = await dbBuilder.createPeer();
117
-
118
- let outerId, innerId: string;
119
- {
120
- await using db = await peer.createDatabase();
121
- const inner = db.add(Obj.make(Type.Expando, { name: 'inner' }));
122
- const outer = db.add(Obj.make(Type.Expando, { inner: Ref.make(inner) }));
123
- innerId = inner.id;
124
- outerId = outer.id;
125
- await db.flush();
126
- }
127
-
128
- await peer.reload();
129
-
130
- {
131
- await using db = await peer.openLastDatabase();
132
- const outer = (await db.query(Filter.ids(outerId)).first()) as any;
133
- const innerAtom = atomFromSignal(() => outer.inner.target);
134
- const inner = registry.get(innerAtom);
135
- expect(inner).to.eq(undefined);
136
-
137
- const builder = new GraphBuilder({ registry });
138
- builder.addExtension(
139
- createExtension({
140
- id: 'outbound-connector',
141
- connector: () =>
142
- Atom.make((get) => {
143
- const inner = get(innerAtom) as any;
144
- return inner ? [{ id: inner.id, type: EXAMPLE_TYPE, data: inner.name }] : [];
145
- }),
146
- }),
147
- );
148
-
149
- const graph = builder.graph;
150
-
151
- const loaded = new Trigger();
152
- let count = 0;
153
- const cancel = registry.subscribe(graph.connections(ROOT_ID), (nodes) => {
154
- count++;
155
- if (nodes.length > 0) {
156
- loaded.wake();
157
- }
158
- });
159
- onTestFinished(() => cancel());
160
- registry.get(graph.connections(ROOT_ID));
161
- expect(count).to.eq(1);
162
-
163
- graph.expand(ROOT_ID);
164
- await loaded.wait();
165
- expect(count).to.eq(2);
166
-
167
- const nodes = registry.get(graph.connections(ROOT_ID));
168
- expect(nodes).has.length(1);
169
- expect(nodes[0].id).to.eq(innerId);
170
- expect(nodes[0].data).to.eq('inner');
171
- }
172
- });
173
-
174
- test('query graph builder', async () => {
175
- const registry = Registry.make();
176
- await using peer = await dbBuilder.createPeer();
177
- await using db = await peer.createDatabase();
178
- db.add(Obj.make(Type.Expando, { name: 'a' }));
179
- db.add(Obj.make(Type.Expando, { name: 'b' }));
180
-
181
- const builder = new GraphBuilder({ registry });
182
- builder.addExtension(
183
- createExtension({
184
- id: 'expando',
185
- connector: () => {
186
- const query = db.query(Filter.type(Type.Expando));
187
-
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
- }));
195
- });
196
- },
197
- }),
198
- );
199
-
200
- const graph = builder.graph;
201
- let count = 0;
202
- const cancel = registry.subscribe(graph.connections(ROOT_ID), (nodes) => {
203
- count = nodes.length;
204
- });
205
- onTestFinished(() => cancel());
206
-
207
- registry.get(graph.connections(ROOT_ID));
208
- expect(count).to.eq(0);
209
-
210
- graph.expand(ROOT_ID);
211
- expect(count).to.eq(2);
212
-
213
- const object = db.add(Obj.make(Type.Expando, { name: 'c' }));
214
- await db.flush();
215
- expect(count).to.eq(3);
216
-
217
- // NOTE: This graph builder is not reactive to the object update.
218
- object.name = 'updated';
219
- await db.flush();
220
- expect(count).to.eq(3);
221
- });
222
- });
223
- });
package/src/testing.ts DELETED
@@ -1,21 +0,0 @@
1
- //
2
- // Copyright 2025 DXOS.org
3
- //
4
-
5
- import { Atom } from '@effect-atom/atom-react';
6
-
7
- import { type Database, type Entity } from '@dxos/echo';
8
-
9
- export const atomFromQuery = <T extends Entity.Unknown = Entity.Unknown>(
10
- query: Database.QueryResult<T>,
11
- ): Atom.Atom<T[]> => {
12
- return Atom.make((get) => {
13
- const unsubscribe = query.subscribe((result) => {
14
- get.setSelf(result.objects);
15
- });
16
-
17
- get.addFinalizer(() => unsubscribe());
18
-
19
- return query.objects;
20
- });
21
- };