@dxos/app-graph 0.8.4-main.d05673bc65 → 0.8.4-main.dfabb4ec29

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 (51) hide show
  1. package/dist/lib/neutral/chunk-J5LGTIGS.mjs +10 -0
  2. package/dist/lib/neutral/chunk-J5LGTIGS.mjs.map +7 -0
  3. package/dist/lib/{browser/index.mjs → neutral/chunk-WJJ5KEOH.mjs} +246 -348
  4. package/dist/lib/neutral/chunk-WJJ5KEOH.mjs.map +7 -0
  5. package/dist/lib/neutral/index.mjs +40 -0
  6. package/dist/lib/neutral/index.mjs.map +7 -0
  7. package/dist/lib/neutral/meta.json +1 -0
  8. package/dist/lib/neutral/scheduler.mjs +15 -0
  9. package/dist/lib/neutral/scheduler.mjs.map +7 -0
  10. package/dist/lib/neutral/testing/index.mjs +40 -0
  11. package/dist/lib/neutral/testing/index.mjs.map +7 -0
  12. package/dist/types/src/atoms.d.ts.map +1 -1
  13. package/dist/types/src/graph-builder.d.ts +1 -1
  14. package/dist/types/src/graph-builder.d.ts.map +1 -1
  15. package/dist/types/src/graph.d.ts.map +1 -1
  16. package/dist/types/src/index.d.ts +1 -0
  17. package/dist/types/src/index.d.ts.map +1 -1
  18. package/dist/types/src/node-matcher.d.ts +42 -16
  19. package/dist/types/src/node-matcher.d.ts.map +1 -1
  20. package/dist/types/src/node.d.ts +9 -3
  21. package/dist/types/src/node.d.ts.map +1 -1
  22. package/dist/types/src/scheduler.browser.d.ts +2 -0
  23. package/dist/types/src/scheduler.browser.d.ts.map +1 -0
  24. package/dist/types/src/scheduler.d.ts +8 -0
  25. package/dist/types/src/scheduler.d.ts.map +1 -0
  26. package/dist/types/src/stories/EchoGraph.stories.d.ts.map +1 -1
  27. package/dist/types/src/testing/index.d.ts +2 -0
  28. package/dist/types/src/testing/index.d.ts.map +1 -0
  29. package/dist/types/src/testing/setup-graph-builder.d.ts +31 -0
  30. package/dist/types/src/testing/setup-graph-builder.d.ts.map +1 -0
  31. package/dist/types/src/util.d.ts +20 -14
  32. package/dist/types/src/util.d.ts.map +1 -1
  33. package/dist/types/tsconfig.tsbuildinfo +1 -1
  34. package/package.json +37 -26
  35. package/src/graph-builder.test.ts +276 -25
  36. package/src/graph-builder.ts +43 -15
  37. package/src/graph.ts +15 -17
  38. package/src/index.ts +1 -0
  39. package/src/node-matcher.ts +57 -25
  40. package/src/node.ts +29 -3
  41. package/src/scheduler.browser.ts +5 -0
  42. package/src/scheduler.ts +17 -0
  43. package/src/stories/EchoGraph.stories.tsx +8 -10
  44. package/src/testing/index.ts +5 -0
  45. package/src/testing/setup-graph-builder.ts +41 -0
  46. package/src/util.ts +48 -18
  47. package/dist/lib/browser/index.mjs.map +0 -7
  48. package/dist/lib/browser/meta.json +0 -1
  49. package/dist/lib/node-esm/index.mjs +0 -1580
  50. package/dist/lib/node-esm/index.mjs.map +0 -7
  51. package/dist/lib/node-esm/meta.json +0 -1
package/src/graph.ts CHANGED
@@ -15,7 +15,7 @@ import { log } from '@dxos/log';
15
15
  import { type MakeOptional, isNonNullable } from '@dxos/util';
16
16
 
17
17
  import * as Node from './node';
18
- import { Separators, normalizeRelation, shallowEqual } from './util';
18
+ import { normalizeRelation, primaryKey, primaryParts, secondaryKey, secondaryParts, shallowEqual } from './util';
19
19
 
20
20
  const graphSymbol = Symbol('graph');
21
21
 
@@ -183,7 +183,10 @@ class GraphImpl implements WritableGraph {
183
183
  // TODO(wittjosiah): Atom feature request, support for something akin to `ComplexMap` to allow for complex arguments.
184
184
  readonly _connections = Atom.family<string, Atom.Atom<Node.Node[]>>((key) => {
185
185
  return Atom.make((get) => {
186
- if (!key || key.indexOf(Separators.primary) <= 0) {
186
+ const parts = key ? primaryParts(key) : [];
187
+ // Empty id (e.g. from `useConnections(graph, undefined, ...)`) yields a key like `\u0001child\u0002outbound`,
188
+ // which has 2 parts but an empty id — treat as no connections rather than throwing.
189
+ if (parts.length < 2 || !parts[0]) {
187
190
  return [];
188
191
  }
189
192
  const { id, relation } = relationFromConnectionKey(key);
@@ -706,7 +709,7 @@ const expandImpl = <T extends ExpandableGraph | WritableGraph>(
706
709
  ): T => {
707
710
  const internal = getInternal(graph);
708
711
  const normalizedRelation = normalizeRelation(relation);
709
- const key = `${id}${Separators.primary}${relationKey(normalizedRelation)}`;
712
+ const key = primaryKey(id, relationKey(normalizedRelation));
710
713
  const nodeOpt = internal._registry.get(internal._node(id));
711
714
  if (Option.isNone(nodeOpt)) {
712
715
  // Node not yet in graph: record expand to run when the node is added.
@@ -900,11 +903,10 @@ const addNodeImpl = <T extends WritableGraph>(graph: T, nodeArg: Node.NodeArg<an
900
903
  graph.onNodeChanged.emit({ id, node: newNode });
901
904
 
902
905
  // Apply any expands that were deferred because this node did not exist yet.
903
- const prefix = `${id}${Separators.primary}`;
904
- const toApply = [...internal._pendingExpands].filter((k) => k.startsWith(prefix));
906
+ const toApply = [...internal._pendingExpands].filter((k) => primaryParts(k)[0] === id);
905
907
  for (const pendingKey of toApply) {
906
908
  internal._pendingExpands.delete(pendingKey);
907
- const relation = relationFromKey(pendingKey.slice(prefix.length));
909
+ const relation = relationFromKey(primaryParts(pendingKey)[1]);
908
910
  Record.set(internal._expanded, pendingKey, true);
909
911
  internal._onExpand?.(id, relation);
910
912
  }
@@ -1226,26 +1228,22 @@ export const make = (params?: GraphProps): Graph => {
1226
1228
 
1227
1229
  export const relationKey = (relation: Node.RelationInput): string => {
1228
1230
  const normalized = normalizeRelation(relation);
1229
- return `${normalized.kind}${Separators.secondary}${normalized.direction}`;
1231
+ return secondaryKey(normalized.kind, normalized.direction);
1230
1232
  };
1231
1233
 
1232
1234
  export const relationFromKey = (encoded: string): Node.Relation => {
1233
- const separatorIndex = encoded.lastIndexOf(Separators.secondary);
1234
- invariant(separatorIndex > 0 && separatorIndex < encoded.length - 1, `Invalid relation key: ${encoded}`);
1235
- const kind = encoded.slice(0, separatorIndex);
1236
- const directionRaw = encoded.slice(separatorIndex + 1);
1235
+ const parts = secondaryParts(encoded);
1236
+ invariant(parts.length === 2 && parts[0].length > 0 && parts[1].length > 0, `Invalid relation key: ${encoded}`);
1237
+ const [kind, directionRaw] = parts;
1237
1238
  invariant(directionRaw === 'outbound' || directionRaw === 'inbound', `Invalid relation direction: ${directionRaw}`);
1238
1239
  return Node.relation(kind, directionRaw);
1239
1240
  };
1240
1241
 
1241
- const connectionKey = (id: string, relation: Node.RelationInput): string =>
1242
- `${id}${Separators.primary}${relationKey(relation)}`;
1242
+ const connectionKey = (id: string, relation: Node.RelationInput): string => primaryKey(id, relationKey(relation));
1243
1243
 
1244
1244
  const relationFromConnectionKey = (key: string): { id: string; relation: Node.Relation } => {
1245
- const separatorIndex = key.indexOf(Separators.primary);
1246
- invariant(separatorIndex > 0 && separatorIndex < key.length - 1, `Invalid connection key: ${key}`);
1247
- const id = key.slice(0, separatorIndex);
1248
- const encodedRelation = key.slice(separatorIndex + 1);
1245
+ const [id, encodedRelation] = primaryParts(key);
1246
+ invariant(id && encodedRelation, `Invalid connection key: ${key}`);
1249
1247
  return { id, relation: relationFromKey(encodedRelation) };
1250
1248
  };
1251
1249
 
package/src/index.ts CHANGED
@@ -8,6 +8,7 @@ export * as Graph from './graph';
8
8
  export * as GraphBuilder from './graph-builder';
9
9
  export * as Node from './node';
10
10
  export * as NodeMatcher from './node-matcher';
11
+ export { qualifyId, getParentId, getSegmentId } from './util';
11
12
 
12
13
  // TODO(wittjosiah): Direct re-export needed for portable type references.
13
14
  export type { BuilderExtensions } from './graph-builder';
@@ -106,7 +106,10 @@ export const whenNodeType =
106
106
  * });
107
107
  * ```
108
108
  *
109
- * @see {@link whenEchoTypeMatches} - Use instead when composing with whenAll/whenAny.
109
+ * Can be composed directly with {@link whenAll}/{@link whenAny}/{@link whenNot} while
110
+ * preserving the typed entity data in the result.
111
+ *
112
+ * @see {@link whenEchoTypeMatches} - Returns the node instead of data for legacy composition.
110
113
  */
111
114
  export const whenEchoType =
112
115
  <T extends Type.AnyEntity>(type: T): NodeMatcher<Entity.Entity<Schema.Schema.Type<T>>> =>
@@ -124,7 +127,7 @@ export const whenEchoType =
124
127
  * @example
125
128
  * ```ts
126
129
  * GraphBuilder.createExtension({
127
- * id: 'object-settings',
130
+ * id: 'object-properties',
128
131
  * match: NodeMatcher.whenEchoObject,
129
132
  * connector: (object) => {
130
133
  * // `object` is typed as Obj.Unknown
@@ -134,7 +137,10 @@ export const whenEchoType =
134
137
  * });
135
138
  * ```
136
139
  *
137
- * @see {@link whenEchoObjectMatches} - Use instead when composing with whenAll/whenAny.
140
+ * Can be composed directly with {@link whenAll}/{@link whenAny}/{@link whenNot} while
141
+ * preserving the `Obj.Unknown` data type in the result.
142
+ *
143
+ * @see {@link whenEchoObjectMatches} - Returns the node instead of data for legacy composition.
138
144
  */
139
145
  export const whenEchoObject = (node: Node.Node): Option.Option<Obj.Unknown> =>
140
146
  Obj.isObject(node.data) ? Option.some(node.data) : Option.none();
@@ -145,53 +151,75 @@ export const whenEchoObject = (node: Node.Node): Option.Option<Obj.Unknown> =>
145
151
 
146
152
  /**
147
153
  * Composes multiple matchers with AND logic - all matchers must match for success.
148
- * Returns the **node** (not the matched data) to enable further composition.
154
+ * The result data type is the intersection of all matchers' data types.
155
+ * Filter matchers like {@link whenNot} return `unknown`, making them transparent
156
+ * in the intersection (since `T & unknown = T`).
149
157
  *
150
158
  * @param matchers - The matchers to combine. All must return Option.some for success.
151
- * @returns A matcher that returns Option.some(node) if all match, Option.none() otherwise.
159
+ * @returns A matcher whose data type is the intersection of all input matchers' data types.
160
+ * Returns the first matcher's value when all match, Option.none() otherwise.
152
161
  *
153
162
  * @example
154
163
  * ```ts
155
- * // Match ECHO objects that are NOT Channels
164
+ * // Match ECHO objects that are NOT Channels — result is NodeMatcher<Obj.Unknown>.
156
165
  * const whenCommentable = NodeMatcher.whenAll(
157
- * NodeMatcher.whenEchoObjectMatches,
166
+ * NodeMatcher.whenEchoObject,
158
167
  * NodeMatcher.whenNot(NodeMatcher.whenEchoTypeMatches(Channel.Channel)),
159
168
  * );
160
169
  * ```
161
170
  */
162
- export const whenAll =
163
- (...matchers: NodeMatcher[]): NodeMatcher =>
164
- (node: Node.Node): Option.Option<Node.Node> => {
171
+ export const whenAll: {
172
+ <A>(a: NodeMatcher<A>, b: NodeMatcher<unknown>): NodeMatcher<A>;
173
+ <A>(a: NodeMatcher<unknown>, b: NodeMatcher<A>): NodeMatcher<A>;
174
+ <A, B>(a: NodeMatcher<A>, b: NodeMatcher<B>): NodeMatcher<A & B>;
175
+ <A, B, C>(a: NodeMatcher<A>, b: NodeMatcher<B>, c: NodeMatcher<C>): NodeMatcher<A & B & C>;
176
+ <A, B, C, D>(a: NodeMatcher<A>, b: NodeMatcher<B>, c: NodeMatcher<C>, d: NodeMatcher<D>): NodeMatcher<A & B & C & D>;
177
+ (...matchers: NodeMatcher<any>[]): NodeMatcher<any>;
178
+ } =
179
+ (...matchers: NodeMatcher<any>[]): NodeMatcher<any> =>
180
+ (node: Node.Node) => {
181
+ let first: Option.Option<any> = Option.none();
165
182
  for (const candidate of matchers) {
166
- if (Option.isNone(candidate(node))) {
183
+ const result = candidate(node);
184
+ if (Option.isNone(result)) {
167
185
  return Option.none();
168
186
  }
187
+ if (Option.isNone(first)) {
188
+ first = result;
189
+ }
169
190
  }
170
- return Option.some(node);
191
+ return first;
171
192
  };
172
193
 
173
194
  /**
174
195
  * Composes multiple matchers with OR logic - at least one matcher must match.
175
- * Returns the **node** (not the matched data) to enable further composition.
196
+ * The result data type is the union of all matchers' data types.
176
197
  *
177
198
  * @param matchers - The matchers to combine. At least one must return Option.some.
178
- * @returns A matcher that returns Option.some(node) if any match, Option.none() otherwise.
199
+ * @returns A matcher whose data type is the union of all input matchers' data types.
200
+ * Returns the first matching matcher's value, or Option.none() if none match.
179
201
  *
180
202
  * @example
181
203
  * ```ts
182
- * // Match nodes that are either Sequences or Prompts
204
+ * // Match nodes that are either Sequences or Routines
183
205
  * const whenInvocable = NodeMatcher.whenAny(
184
206
  * NodeMatcher.whenEchoTypeMatches(Sequence),
185
- * NodeMatcher.whenEchoTypeMatches(Prompt.Prompt),
207
+ * NodeMatcher.whenEchoTypeMatches(Routine.Routine),
186
208
  * );
187
209
  * ```
188
210
  */
189
- export const whenAny =
190
- (...matchers: NodeMatcher[]): NodeMatcher =>
191
- (node: Node.Node): Option.Option<Node.Node> => {
211
+ export const whenAny: {
212
+ <A, B>(a: NodeMatcher<A>, b: NodeMatcher<B>): NodeMatcher<A | B>;
213
+ <A, B, C>(a: NodeMatcher<A>, b: NodeMatcher<B>, c: NodeMatcher<C>): NodeMatcher<A | B | C>;
214
+ <A, B, C, D>(a: NodeMatcher<A>, b: NodeMatcher<B>, c: NodeMatcher<C>, d: NodeMatcher<D>): NodeMatcher<A | B | C | D>;
215
+ (...matchers: NodeMatcher<any>[]): NodeMatcher<any>;
216
+ } =
217
+ (...matchers: NodeMatcher<any>[]): NodeMatcher<any> =>
218
+ (node: Node.Node) => {
192
219
  for (const candidate of matchers) {
193
- if (Option.isSome(candidate(node))) {
194
- return Option.some(node);
220
+ const result = candidate(node);
221
+ if (Option.isSome(result)) {
222
+ return result;
195
223
  }
196
224
  }
197
225
  return Option.none();
@@ -260,15 +288,19 @@ export const whenEchoObjectMatches = (node: Node.Node): Option.Option<Node.Node>
260
288
  * Negates a matcher - matches when the given matcher does NOT match.
261
289
  * Useful for exclusion patterns like "any object EXCEPT type X".
262
290
  *
291
+ * Returns `NodeMatcher<unknown>` because negation is a filter — it doesn't provide
292
+ * typed data. This makes it transparent in {@link whenAll} intersections
293
+ * (since `T & unknown = T`).
294
+ *
263
295
  * @param matcher - The matcher to negate.
264
296
  * @returns A matcher that returns Option.some(node) if the input matcher returns none,
265
297
  * and Option.none() if the input matcher returns some.
266
298
  *
267
299
  * @example
268
300
  * ```ts
269
- * // Match any ECHO object that is NOT a Channel
301
+ * // Match any ECHO object that is NOT a Channel — result is NodeMatcher<Obj.Unknown>.
270
302
  * const whenCommentable = NodeMatcher.whenAll(
271
- * NodeMatcher.whenEchoObjectMatches,
303
+ * NodeMatcher.whenEchoObject,
272
304
  * NodeMatcher.whenNot(NodeMatcher.whenEchoTypeMatches(Channel.Channel)),
273
305
  * );
274
306
  *
@@ -277,6 +309,6 @@ export const whenEchoObjectMatches = (node: Node.Node): Option.Option<Node.Node>
277
309
  * ```
278
310
  */
279
311
  export const whenNot =
280
- (matcher: NodeMatcher): NodeMatcher =>
281
- (node: Node.Node): Option.Option<Node.Node> =>
312
+ (matcher: NodeMatcher<any>): NodeMatcher<unknown> =>
313
+ (node: Node.Node): Option.Option<unknown> =>
282
314
  Option.isNone(matcher(node)) ? Option.some(node) : Option.none();
package/src/node.ts CHANGED
@@ -15,17 +15,17 @@ export const RootId = 'root';
15
15
  /**
16
16
  * Root node type.
17
17
  */
18
- export const RootType = 'org.dxos.type.graph-root';
18
+ export const RootType = 'org.dxos.type.graphRoot';
19
19
 
20
20
  /**
21
21
  * Action node type.
22
22
  */
23
- export const ActionType = 'org.dxos.type.graph-action';
23
+ export const ActionType = 'org.dxos.type.graphAction';
24
24
 
25
25
  /**
26
26
  * Action group node type.
27
27
  */
28
- export const ActionGroupType = 'org.dxos.type.graph-action-group';
28
+ export const ActionGroupType = 'org.dxos.type.graphActionGroup';
29
29
 
30
30
  /**
31
31
  * Represents a node in the graph.
@@ -150,3 +150,29 @@ export const isActionGroup = (data: unknown): data is ActionGroup =>
150
150
  export type ActionLike = Action | ActionGroup;
151
151
 
152
152
  export const isActionLike = (data: unknown): data is Action | ActionGroup => isAction(data) || isActionGroup(data);
153
+
154
+ //
155
+ // Node Factories
156
+ //
157
+
158
+ /** Typed factory for constructing a NodeArg. Provides auto-complete and type validation. */
159
+ export const make = <TData = any, TProperties extends Record<string, any> = Record<string, any>>(
160
+ arg: NodeArg<TData, TProperties>,
161
+ ): NodeArg<TData, TProperties> => arg;
162
+
163
+ /** Create an action node. Automatically sets `type: ActionType`. */
164
+ export const makeAction = <R = never>(
165
+ arg: Omit<NodeArg<ActionData<R>>, 'type' | 'nodes' | 'edges'>,
166
+ ): NodeArg<ActionData<R>> => ({
167
+ ...arg,
168
+ type: ActionType,
169
+ });
170
+
171
+ /** Create an action group node. Automatically sets `type` and `data`. */
172
+ export const makeActionGroup = (
173
+ arg: Omit<NodeArg<typeof actionGroupSymbol>, 'type' | 'data' | 'nodes' | 'edges'>,
174
+ ): NodeArg<typeof actionGroupSymbol> => ({
175
+ ...arg,
176
+ type: ActionGroupType,
177
+ data: actionGroupSymbol,
178
+ });
@@ -0,0 +1,5 @@
1
+ //
2
+ // Copyright 2026 DXOS.org
3
+ //
4
+
5
+ export { scheduleTask, yieldOrContinue } from 'main-thread-scheduling';
@@ -0,0 +1,17 @@
1
+ //
2
+ // Copyright 2026 DXOS.org
3
+ //
4
+
5
+ // CF Workers / Node fallback: there is no main thread to yield to.
6
+ // scheduleTask runs the callback on the next microtask; yieldOrContinue is a microtask hop.
7
+
8
+ type ScheduleOptions = { strategy?: 'smooth' | 'interactive' | 'idle'; signal?: AbortSignal };
9
+
10
+ export const scheduleTask = async <T>(callback: () => T | Promise<T>, _options?: ScheduleOptions): Promise<T> => {
11
+ await Promise.resolve();
12
+ return callback();
13
+ };
14
+
15
+ export const yieldOrContinue = async (_priority: 'smooth' | 'interactive' | 'idle'): Promise<void> => {
16
+ await Promise.resolve();
17
+ };
@@ -10,14 +10,14 @@ import React, { type PropsWithChildren, useCallback, useContext, useEffect, useM
10
10
 
11
11
  import { Filter, type Space, SpaceState, isSpace } from '@dxos/client/echo';
12
12
  import { Obj, Query } from '@dxos/echo';
13
- import { TestSchema } from '@dxos/echo/testing';
14
13
  import { AtomObj, AtomQuery } from '@dxos/echo-atom';
15
- import { faker } from '@dxos/random';
14
+ import { TestSchema } from '@dxos/echo/testing';
15
+ import { random } from '@dxos/random';
16
16
  import { type Client, useClient } from '@dxos/react-client';
17
17
  import { withClientProvider } from '@dxos/react-client/testing';
18
18
  import { Icon, IconButton, Input, Select } from '@dxos/react-ui';
19
- import { withTheme } from '@dxos/react-ui/testing';
20
19
  import { Path, Tree, type TreeModel } from '@dxos/react-ui-list';
20
+ import { withTheme } from '@dxos/react-ui/testing';
21
21
  import { getSize, mx } from '@dxos/ui-theme';
22
22
  import { safeParseInt } from '@dxos/util';
23
23
 
@@ -25,7 +25,6 @@ import * as CreateAtom from '../atoms';
25
25
  import * as Graph from '../graph';
26
26
  import * as GraphBuilder from '../graph-builder';
27
27
  import * as Node from '../node';
28
-
29
28
  import { JsonTree } from './Tree';
30
29
 
31
30
  const DEFAULT_PERIOD = 500;
@@ -146,8 +145,8 @@ const runAction = async (client: Client, action: Action) => {
146
145
  case Action.RENAME_SPACE: {
147
146
  const space = getRandomSpace(client);
148
147
  if (space) {
149
- Obj.change(space.properties, (p) => {
150
- p.name = faker.commerce.productName();
148
+ Obj.update(space.properties, (obj) => {
149
+ obj.name = random.commerce.productName();
151
150
  });
152
151
  }
153
152
  break;
@@ -157,7 +156,7 @@ const runAction = async (client: Client, action: Action) => {
157
156
  getRandomSpace(client)?.db.add(
158
157
  Obj.make(TestSchema.Expando, {
159
158
  type: 'test',
160
- name: faker.commerce.productName(),
159
+ name: random.commerce.productName(),
161
160
  }),
162
161
  );
163
162
  break;
@@ -176,8 +175,8 @@ const runAction = async (client: Client, action: Action) => {
176
175
  if (space) {
177
176
  const objects = await space.db.query(Filter.type(TestSchema.Expando, { type: 'test' })).run();
178
177
  const object = objects[Math.floor(Math.random() * objects.length)];
179
- Obj.change(object, (o) => {
180
- o.name = faker.commerce.productName();
178
+ Obj.update(object, (object) => {
179
+ object.name = random.commerce.productName();
181
180
  });
182
181
  }
183
182
  break;
@@ -216,7 +215,6 @@ const Controls = ({ children }: PropsWithChildren) => {
216
215
  <Input.Root>
217
216
  <Input.TextInput
218
217
  autoComplete='off'
219
- size={5}
220
218
  classNames='w-[100px] text-right pe-[22px]'
221
219
  placeholder='Interval'
222
220
  value={actionInterval}
@@ -0,0 +1,5 @@
1
+ //
2
+ // Copyright 2026 DXOS.org
3
+ //
4
+
5
+ export * from './setup-graph-builder';
@@ -0,0 +1,41 @@
1
+ //
2
+ // Copyright 2026 DXOS.org
3
+ //
4
+
5
+ import { Registry } from '@effect-atom/atom-react';
6
+ import * as Option from 'effect/Option';
7
+
8
+ import * as Graph from '../graph';
9
+ import * as GraphBuilder from '../graph-builder';
10
+ import * as Node from '../node';
11
+
12
+ export type SetupGraphBuilderOptions = {
13
+ registry?: Registry.Registry;
14
+ extensions?: GraphBuilder.BuilderExtensions;
15
+ };
16
+
17
+ export const setupGraphBuilder = ({ registry = Registry.make(), extensions }: SetupGraphBuilderOptions = {}) => {
18
+ const builder = GraphBuilder.make({ registry });
19
+ const graph = builder.graph;
20
+
21
+ if (extensions) {
22
+ GraphBuilder.addExtension(builder, extensions);
23
+ }
24
+
25
+ return {
26
+ registry,
27
+ builder,
28
+ graph,
29
+ addExtensions: (nextExtensions: GraphBuilder.BuilderExtensions) => {
30
+ GraphBuilder.addExtension(builder, nextExtensions);
31
+ },
32
+ expand: async (id: string, relation: Node.RelationInput = 'child') => {
33
+ Graph.expand(graph, id, relation);
34
+ await GraphBuilder.flush(builder);
35
+ },
36
+ flush: () => GraphBuilder.flush(builder),
37
+ getConnections: (id: string, relation: Node.RelationInput = 'child') =>
38
+ registry.get(graph.connections(id, relation)),
39
+ getNode: (id: string) => Graph.getNode(graph, id).pipe(Option.getOrNull),
40
+ };
41
+ };
package/src/util.ts CHANGED
@@ -6,18 +6,26 @@ import { invariant } from '@dxos/invariant';
6
6
 
7
7
  import * as Node from './node';
8
8
 
9
- /**
10
- * Key separators for compound string keys used across the app-graph package.
11
- * `primary` separates top-level components (e.g., node ID from relation).
12
- * `secondary` separates sub-components within an encoded value (e.g., relation kind from direction).
13
- * `path` separates segments in qualified node IDs (e.g., parent path from local segment).
14
- * Two distinct characters are needed because secondary separators appear inside primary-separated fields.
15
- */
16
- export const Separators = {
17
- primary: '\u0001',
18
- secondary: '\u0002',
19
- path: '/',
20
- } as const;
9
+ // PRIMARY separates top-level components (e.g., node ID from relation) in compound string keys used within the app-graph package.
10
+ const PRIMARY = '\u0001';
11
+
12
+ // SECONDARY separates sub-components within an encoded value (e.g., relation kind from direction) in the same context.
13
+ const SECONDARY = '\u0002';
14
+
15
+ // PATH separates segments in qualified node IDs (e.g., parent path from local segment).
16
+ const PATH = '/';
17
+
18
+ /** Join parts with the primary separator. */
19
+ export const primaryKey = (...parts: string[]): string => parts.join(PRIMARY);
20
+
21
+ /** Split a key on the primary separator. */
22
+ export const primaryParts = (key: string): string[] => key.split(PRIMARY);
23
+
24
+ /** Join parts with the secondary separator. */
25
+ export const secondaryKey = (...parts: string[]): string => parts.join(SECONDARY);
26
+
27
+ /** Split a key on the secondary separator. */
28
+ export const secondaryParts = (key: string): string[] => key.split(SECONDARY);
21
29
 
22
30
  /**
23
31
  * Normalize a relation input to a full Relation object.
@@ -29,8 +37,12 @@ export const normalizeRelation = (relation?: Node.RelationInput): Node.Relation
29
37
  * Shallow-compare two values: same reference, or same own-keys with === values.
30
38
  */
31
39
  export const shallowEqual = (a: unknown, b: unknown): boolean => {
32
- if (a === b) return true;
33
- if (a == null || b == null || typeof a !== 'object' || typeof b !== 'object') return false;
40
+ if (a === b) {
41
+ return true;
42
+ }
43
+ if (a == null || b == null || typeof a !== 'object' || typeof b !== 'object') {
44
+ return false;
45
+ }
34
46
  const keysA = Object.keys(a as Record<string, unknown>);
35
47
  const keysB = Object.keys(b as Record<string, unknown>);
36
48
  if (keysA.length !== keysB.length) {
@@ -41,6 +53,7 @@ export const shallowEqual = (a: unknown, b: unknown): boolean => {
41
53
 
42
54
  /**
43
55
  * Returns true if two NodeArg arrays are semantically identical (same id, type, data, properties per index).
56
+ * Inline child nodes (the `nodes` field) are compared recursively.
44
57
  */
45
58
  export const nodeArgsUnchanged = (prev: Node.NodeArg<any>[], next: Node.NodeArg<any>[]): boolean => {
46
59
  if (prev.length !== next.length) {
@@ -53,19 +66,36 @@ export const nodeArgsUnchanged = (prev: Node.NodeArg<any>[], next: Node.NodeArg<
53
66
  prevNode.id === nextNode.id &&
54
67
  prevNode.type === nextNode.type &&
55
68
  shallowEqual(prevNode.data, nextNode.data) &&
56
- shallowEqual(prevNode.properties, nextNode.properties)
69
+ shallowEqual(prevNode.properties, nextNode.properties) &&
70
+ nodeArgsUnchanged(prevNode.nodes ?? [], nextNode.nodes ?? [])
57
71
  );
58
72
  });
59
73
  };
60
74
 
61
75
  /**
62
- * Build a qualified node ID by joining a parent path with a local segment.
76
+ * Build a qualified node ID by joining path segments.
63
77
  */
64
- export const qualifyId = (parentId: string, segmentId: string): string => `${parentId}${Separators.path}${segmentId}`;
78
+ export const qualifyId = (parentId: string, ...segmentIds: string[]): string => [parentId, ...segmentIds].join(PATH);
65
79
 
66
80
  /**
67
81
  * Validate that a segment ID does not contain the path separator.
68
82
  */
69
83
  export const validateSegmentId = (id: string): void => {
70
- invariant(!id.includes(Separators.path), `Node segment ID must not contain '${Separators.path}': ${id}`);
84
+ invariant(!id.includes(PATH), `Node segment ID must not contain '${PATH}': ${id}`);
85
+ };
86
+
87
+ /**
88
+ * Extract the parent qualified ID (everything before the last path separator).
89
+ * Returns undefined for IDs with no parent (single segment).
90
+ */
91
+ export const getParentId = (qualifiedId: string): string | undefined => {
92
+ const lastSlash = qualifiedId.lastIndexOf(PATH);
93
+ return lastSlash > 0 ? qualifiedId.slice(0, lastSlash) : undefined;
94
+ };
95
+
96
+ /**
97
+ * Extract the last segment of a qualified ID.
98
+ */
99
+ export const getSegmentId = (qualifiedId: string): string => {
100
+ return qualifiedId.split(PATH).pop() ?? qualifiedId;
71
101
  };