@dxos/app-graph 0.8.4-main.fffef41 → 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.
Files changed (71) hide show
  1. package/LICENSE +102 -5
  2. package/README.md +1 -1
  3. package/dist/lib/neutral/chunk-7BYCDV55.mjs +1484 -0
  4. package/dist/lib/neutral/chunk-7BYCDV55.mjs.map +7 -0
  5. package/dist/lib/neutral/chunk-J5LGTIGS.mjs +10 -0
  6. package/dist/lib/neutral/chunk-J5LGTIGS.mjs.map +7 -0
  7. package/dist/lib/neutral/index.mjs +40 -0
  8. package/dist/lib/neutral/index.mjs.map +7 -0
  9. package/dist/lib/neutral/meta.json +1 -0
  10. package/dist/lib/neutral/scheduler.mjs +15 -0
  11. package/dist/lib/neutral/scheduler.mjs.map +7 -0
  12. package/dist/lib/neutral/testing/index.mjs +40 -0
  13. package/dist/lib/neutral/testing/index.mjs.map +7 -0
  14. package/dist/types/src/atoms.d.ts +8 -0
  15. package/dist/types/src/atoms.d.ts.map +1 -0
  16. package/dist/types/src/graph-builder.d.ts +110 -65
  17. package/dist/types/src/graph-builder.d.ts.map +1 -1
  18. package/dist/types/src/graph.d.ts +179 -213
  19. package/dist/types/src/graph.d.ts.map +1 -1
  20. package/dist/types/src/index.d.ts +7 -3
  21. package/dist/types/src/index.d.ts.map +1 -1
  22. package/dist/types/src/node-matcher.d.ts +243 -0
  23. package/dist/types/src/node-matcher.d.ts.map +1 -0
  24. package/dist/types/src/node-matcher.test.d.ts +2 -0
  25. package/dist/types/src/node-matcher.test.d.ts.map +1 -0
  26. package/dist/types/src/node.d.ts +50 -5
  27. package/dist/types/src/node.d.ts.map +1 -1
  28. package/dist/types/src/scheduler.browser.d.ts +2 -0
  29. package/dist/types/src/scheduler.browser.d.ts.map +1 -0
  30. package/dist/types/src/scheduler.d.ts +8 -0
  31. package/dist/types/src/scheduler.d.ts.map +1 -0
  32. package/dist/types/src/stories/EchoGraph.stories.d.ts.map +1 -1
  33. package/dist/types/src/testing/index.d.ts +2 -0
  34. package/dist/types/src/testing/index.d.ts.map +1 -0
  35. package/dist/types/src/testing/setup-graph-builder.d.ts +31 -0
  36. package/dist/types/src/testing/setup-graph-builder.d.ts.map +1 -0
  37. package/dist/types/src/util.d.ts +40 -0
  38. package/dist/types/src/util.d.ts.map +1 -0
  39. package/dist/types/tsconfig.tsbuildinfo +1 -1
  40. package/package.json +52 -40
  41. package/src/atoms.ts +25 -0
  42. package/src/graph-builder.test.ts +1153 -115
  43. package/src/graph-builder.ts +740 -285
  44. package/src/graph.test.ts +448 -120
  45. package/src/graph.ts +1022 -413
  46. package/src/index.ts +10 -3
  47. package/src/node-matcher.test.ts +301 -0
  48. package/src/node-matcher.ts +313 -0
  49. package/src/node.ts +82 -8
  50. package/src/scheduler.browser.ts +5 -0
  51. package/src/scheduler.ts +17 -0
  52. package/src/stories/EchoGraph.stories.tsx +150 -121
  53. package/src/stories/Tree.tsx +2 -2
  54. package/src/testing/index.ts +5 -0
  55. package/src/testing/setup-graph-builder.ts +41 -0
  56. package/src/util.ts +101 -0
  57. package/dist/lib/browser/index.mjs +0 -836
  58. package/dist/lib/browser/index.mjs.map +0 -7
  59. package/dist/lib/browser/meta.json +0 -1
  60. package/dist/lib/node-esm/index.mjs +0 -838
  61. package/dist/lib/node-esm/index.mjs.map +0 -7
  62. package/dist/lib/node-esm/meta.json +0 -1
  63. package/dist/types/src/experimental/graph-projections.test.d.ts +0 -25
  64. package/dist/types/src/experimental/graph-projections.test.d.ts.map +0 -1
  65. package/dist/types/src/signals-integration.test.d.ts +0 -2
  66. package/dist/types/src/signals-integration.test.d.ts.map +0 -1
  67. package/dist/types/src/testing.d.ts +0 -5
  68. package/dist/types/src/testing.d.ts.map +0 -1
  69. package/src/experimental/graph-projections.test.ts +0 -56
  70. package/src/signals-integration.test.ts +0 -219
  71. package/src/testing.ts +0 -20
package/src/index.ts CHANGED
@@ -2,6 +2,13 @@
2
2
  // Copyright 2023 DXOS.org
3
3
  //
4
4
 
5
- export * from './graph';
6
- export * from './graph-builder';
7
- export * from './node';
5
+ // TODO(wittjosiah): What is a good name for this module?
6
+ export * as CreateAtom from './atoms';
7
+ export * as Graph from './graph';
8
+ export * as GraphBuilder from './graph-builder';
9
+ export * as Node from './node';
10
+ export * as NodeMatcher from './node-matcher';
11
+ export { qualifyId, getParentId, getSegmentId } from './util';
12
+
13
+ // TODO(wittjosiah): Direct re-export needed for portable type references.
14
+ export type { BuilderExtensions } from './graph-builder';
@@ -0,0 +1,301 @@
1
+ //
2
+ // Copyright 2025 DXOS.org
3
+ //
4
+
5
+ import * as Option from 'effect/Option';
6
+ import { describe, expect, test } from 'vitest';
7
+
8
+ import { Obj } from '@dxos/echo';
9
+ import { TestSchema } from '@dxos/echo/testing';
10
+
11
+ import * as Node from './node';
12
+ import * as NodeMatcher from './node-matcher';
13
+
14
+ describe('NodeMatcher', () => {
15
+ describe('whenRoot', () => {
16
+ test('matches root node', () => {
17
+ const rootNode: Node.Node = {
18
+ id: Node.RootId,
19
+ type: Node.RootType,
20
+ properties: {},
21
+ data: null,
22
+ };
23
+ const result = NodeMatcher.whenRoot(rootNode);
24
+ expect(Option.isSome(result)).to.be.true;
25
+ expect(Option.getOrNull(result)).to.equal(rootNode);
26
+ });
27
+
28
+ test('does not match non-root node', () => {
29
+ const node: Node.Node = {
30
+ id: 'other',
31
+ type: 'test',
32
+ properties: {},
33
+ data: null,
34
+ };
35
+ const result = NodeMatcher.whenRoot(node);
36
+ expect(Option.isNone(result)).to.be.true;
37
+ });
38
+ });
39
+
40
+ describe('whenId', () => {
41
+ test('matches node by ID', () => {
42
+ const node: Node.Node = {
43
+ id: 'test-id',
44
+ type: 'test',
45
+ properties: {},
46
+ data: null,
47
+ };
48
+ const matcher = NodeMatcher.whenId('test-id');
49
+ const result = matcher(node);
50
+ expect(Option.isSome(result)).to.be.true;
51
+ expect(Option.getOrNull(result)).to.equal(node);
52
+ });
53
+
54
+ test('does not match different ID', () => {
55
+ const node: Node.Node = {
56
+ id: 'test-id',
57
+ type: 'test',
58
+ properties: {},
59
+ data: null,
60
+ };
61
+ const matcher = NodeMatcher.whenId('other-id');
62
+ const result = matcher(node);
63
+ expect(Option.isNone(result)).to.be.true;
64
+ });
65
+ });
66
+
67
+ describe('whenNodeType', () => {
68
+ test('matches node by type', () => {
69
+ const node: Node.Node = {
70
+ id: 'test',
71
+ type: 'test-type',
72
+ properties: {},
73
+ data: null,
74
+ };
75
+ const matcher = NodeMatcher.whenNodeType('test-type');
76
+ const result = matcher(node);
77
+ expect(Option.isSome(result)).to.be.true;
78
+ expect(Option.getOrNull(result)).to.equal(node);
79
+ });
80
+
81
+ test('does not match different type', () => {
82
+ const node: Node.Node = {
83
+ id: 'test',
84
+ type: 'test-type',
85
+ properties: {},
86
+ data: null,
87
+ };
88
+ const matcher = NodeMatcher.whenNodeType('other-type');
89
+ const result = matcher(node);
90
+ expect(Option.isNone(result)).to.be.true;
91
+ });
92
+ });
93
+
94
+ describe('whenEchoType', () => {
95
+ test('creates matcher function', () => {
96
+ const matcher = NodeMatcher.whenEchoType(TestSchema.Person);
97
+ expect(typeof matcher).to.equal('function');
98
+ });
99
+
100
+ test('returns none for non-instance', () => {
101
+ const node: Node.Node = {
102
+ id: 'test',
103
+ type: 'test',
104
+ properties: {},
105
+ data: { name: 'Test' },
106
+ };
107
+ const matcher = NodeMatcher.whenEchoType(TestSchema.Person);
108
+ const result = matcher(node);
109
+ expect(Option.isNone(result)).to.be.true;
110
+ });
111
+
112
+ test('returns some for instance of type', () => {
113
+ const testObject = Obj.make(TestSchema.Person, { name: 'Test' });
114
+ const node: Node.Node = {
115
+ id: 'test',
116
+ type: 'test',
117
+ properties: {},
118
+ data: testObject,
119
+ };
120
+ const matcher = NodeMatcher.whenEchoType(TestSchema.Person);
121
+ const result = matcher(node);
122
+ expect(Option.isSome(result)).to.be.true;
123
+ expect(Option.getOrNull(result)).to.equal(testObject);
124
+ });
125
+ });
126
+
127
+ describe('whenEchoObject', () => {
128
+ test('creates matcher function', () => {
129
+ const node: Node.Node = {
130
+ id: 'test',
131
+ type: 'test',
132
+ properties: {},
133
+ data: { name: 'Test' },
134
+ };
135
+ const result = NodeMatcher.whenEchoObject(node);
136
+ // Just verify the function works - actual object matching depends on Obj.isObject implementation.
137
+ expect(Option.isNone(result) || Option.isSome(result)).to.be.true;
138
+ });
139
+
140
+ test('does not match non-object data', () => {
141
+ const node: Node.Node = {
142
+ id: 'test',
143
+ type: 'test',
144
+ properties: {},
145
+ data: 'string',
146
+ };
147
+ const result = NodeMatcher.whenEchoObject(node);
148
+ expect(Option.isNone(result)).to.be.true;
149
+ });
150
+ });
151
+
152
+ describe('whenEchoTypeMatches', () => {
153
+ test('returns node for matching type', () => {
154
+ const testObject = Obj.make(TestSchema.Person, { name: 'Test' });
155
+ const node: Node.Node = {
156
+ id: 'test',
157
+ type: 'test',
158
+ properties: {},
159
+ data: testObject,
160
+ };
161
+ const matcher = NodeMatcher.whenEchoTypeMatches(TestSchema.Person);
162
+ const result = matcher(node);
163
+ expect(Option.isSome(result)).to.be.true;
164
+ expect(Option.getOrNull(result)).to.equal(node);
165
+ });
166
+
167
+ test('returns none for non-matching type', () => {
168
+ const node: Node.Node = {
169
+ id: 'test',
170
+ type: 'test',
171
+ properties: {},
172
+ data: { name: 'Test' },
173
+ };
174
+ const matcher = NodeMatcher.whenEchoTypeMatches(TestSchema.Person);
175
+ const result = matcher(node);
176
+ expect(Option.isNone(result)).to.be.true;
177
+ });
178
+ });
179
+
180
+ describe('whenEchoObjectMatches', () => {
181
+ test('returns node for ECHO object', () => {
182
+ const testObject = Obj.make(TestSchema.Person, { name: 'Test' });
183
+ const node: Node.Node = {
184
+ id: 'test',
185
+ type: 'test',
186
+ properties: {},
187
+ data: testObject,
188
+ };
189
+ const result = NodeMatcher.whenEchoObjectMatches(node);
190
+ expect(Option.isSome(result)).to.be.true;
191
+ expect(Option.getOrNull(result)).to.equal(node);
192
+ });
193
+
194
+ test('returns none for non-ECHO object', () => {
195
+ const node: Node.Node = {
196
+ id: 'test',
197
+ type: 'test',
198
+ properties: {},
199
+ data: 'string',
200
+ };
201
+ const result = NodeMatcher.whenEchoObjectMatches(node);
202
+ expect(Option.isNone(result)).to.be.true;
203
+ });
204
+ });
205
+
206
+ describe('whenNot', () => {
207
+ test('negates a matching matcher', () => {
208
+ const rootNode: Node.Node = {
209
+ id: Node.RootId,
210
+ type: Node.RootType,
211
+ properties: {},
212
+ data: null,
213
+ };
214
+ const matcher = NodeMatcher.whenNot(NodeMatcher.whenRoot);
215
+ const result = matcher(rootNode);
216
+ expect(Option.isNone(result)).to.be.true;
217
+ });
218
+
219
+ test('returns node when matcher does not match', () => {
220
+ const node: Node.Node = {
221
+ id: 'other',
222
+ type: 'test',
223
+ properties: {},
224
+ data: null,
225
+ };
226
+ const matcher = NodeMatcher.whenNot(NodeMatcher.whenRoot);
227
+ const result = matcher(node);
228
+ expect(Option.isSome(result)).to.be.true;
229
+ expect(Option.getOrNull(result)).to.equal(node);
230
+ });
231
+
232
+ test('works with whenAll for complex patterns', () => {
233
+ const testObject = Obj.make(TestSchema.Person, { name: 'Test' });
234
+ const node: Node.Node = {
235
+ id: 'test',
236
+ type: 'test',
237
+ properties: {},
238
+ data: testObject,
239
+ };
240
+ // Match ECHO objects that are NOT Person type.
241
+ const matcher = NodeMatcher.whenAll(
242
+ NodeMatcher.whenEchoObjectMatches,
243
+ NodeMatcher.whenNot(NodeMatcher.whenEchoTypeMatches(TestSchema.Person)),
244
+ );
245
+ const result = matcher(node);
246
+ expect(Option.isNone(result)).to.be.true;
247
+ });
248
+ });
249
+
250
+ describe('whenAll', () => {
251
+ test('matches when all matchers match', () => {
252
+ const node: Node.Node = {
253
+ id: 'test-id',
254
+ type: 'test-type',
255
+ properties: {},
256
+ data: null,
257
+ };
258
+ const matcher = NodeMatcher.whenAll(NodeMatcher.whenId('test-id'), NodeMatcher.whenNodeType('test-type'));
259
+ const result = matcher(node);
260
+ expect(Option.isSome(result)).to.be.true;
261
+ });
262
+
263
+ test('does not match when any matcher fails', () => {
264
+ const node: Node.Node = {
265
+ id: 'test-id',
266
+ type: 'test-type',
267
+ properties: {},
268
+ data: null,
269
+ };
270
+ const matcher = NodeMatcher.whenAll(NodeMatcher.whenId('test-id'), NodeMatcher.whenNodeType('other-type'));
271
+ const result = matcher(node);
272
+ expect(Option.isNone(result)).to.be.true;
273
+ });
274
+ });
275
+
276
+ describe('whenAny', () => {
277
+ test('matches when any matcher matches', () => {
278
+ const node: Node.Node = {
279
+ id: 'test-id',
280
+ type: 'test-type',
281
+ properties: {},
282
+ data: null,
283
+ };
284
+ const matcher = NodeMatcher.whenAny(NodeMatcher.whenId('other-id'), NodeMatcher.whenNodeType('test-type'));
285
+ const result = matcher(node);
286
+ expect(Option.isSome(result)).to.be.true;
287
+ });
288
+
289
+ test('does not match when all matchers fail', () => {
290
+ const node: Node.Node = {
291
+ id: 'test-id',
292
+ type: 'test-type',
293
+ properties: {},
294
+ data: null,
295
+ };
296
+ const matcher = NodeMatcher.whenAny(NodeMatcher.whenId('other-id'), NodeMatcher.whenNodeType('other-type'));
297
+ const result = matcher(node);
298
+ expect(Option.isNone(result)).to.be.true;
299
+ });
300
+ });
301
+ });
@@ -0,0 +1,313 @@
1
+ //
2
+ // Copyright 2025 DXOS.org
3
+ //
4
+
5
+ import * as Option from 'effect/Option';
6
+
7
+ import { Entity, Obj, type Type } from '@dxos/echo';
8
+
9
+ import * as Node from './node';
10
+
11
+ /**
12
+ * Type for a node matcher function that returns an Option of the matched data.
13
+ * Matchers are used to filter and transform nodes in the app graph.
14
+ *
15
+ * @template TData - The type of data returned when the matcher succeeds.
16
+ * Defaults to Node.Node, but can be a more specific type (e.g., an ECHO entity).
17
+ */
18
+ export type NodeMatcher<TData = Node.Node> = (node: Node.Node) => Option.Option<TData>;
19
+
20
+ //
21
+ // Basic Node Matchers
22
+ //
23
+
24
+ /**
25
+ * Matches the root node of the graph.
26
+ *
27
+ * @returns Option.some(node) if the node is the root, Option.none() otherwise.
28
+ *
29
+ * @example
30
+ * ```ts
31
+ * GraphBuilder.createExtension({
32
+ * id: 'myExtension',
33
+ * match: NodeMatcher.whenRoot,
34
+ * connector: (node) => Effect.succeed([...]),
35
+ * });
36
+ * ```
37
+ */
38
+ export const whenRoot = (node: Node.Node): Option.Option<Node.Node> =>
39
+ node.id === Node.RootId ? Option.some(node) : Option.none();
40
+
41
+ /**
42
+ * Matches a node by its exact ID.
43
+ *
44
+ * @param id - The node ID to match against.
45
+ * @returns A matcher that returns Option.some(node) if IDs match, Option.none() otherwise.
46
+ *
47
+ * @example
48
+ * ```ts
49
+ * GraphBuilder.createExtension({
50
+ * id: 'spacesExtension',
51
+ * match: NodeMatcher.whenId('spaces'),
52
+ * connector: (node) => Effect.succeed([...]),
53
+ * });
54
+ * ```
55
+ */
56
+ export const whenId =
57
+ (id: string) =>
58
+ (node: Node.Node): Option.Option<Node.Node> =>
59
+ node.id === id ? Option.some(node) : Option.none();
60
+
61
+ /**
62
+ * Matches a node by its type string (the `node.type` property).
63
+ *
64
+ * @param type - The node type string to match against.
65
+ * @returns A matcher that returns Option.some(node) if types match, Option.none() otherwise.
66
+ *
67
+ * @example
68
+ * ```ts
69
+ * GraphBuilder.createExtension({
70
+ * id: 'spaceSettingsExtension',
71
+ * match: NodeMatcher.whenNodeType('org.dxos.plugin.space.settings'),
72
+ * connector: (node) => Effect.succeed([...]),
73
+ * });
74
+ * ```
75
+ */
76
+ export const whenNodeType =
77
+ (type: string) =>
78
+ (node: Node.Node): Option.Option<Node.Node> =>
79
+ node.type === type ? Option.some(node) : Option.none();
80
+
81
+ //
82
+ // ECHO Data Matchers
83
+ //
84
+
85
+ /**
86
+ * Matches a node whose data is an instance of the given ECHO schema type.
87
+ * Returns the **typed entity data** (not the node) for direct use in callbacks.
88
+ *
89
+ * Use this when you need to work directly with the typed ECHO entity in your
90
+ * connector or actions callback.
91
+ *
92
+ * @template T - The ECHO schema type to match against.
93
+ * @param type - The ECHO schema (e.g., `Collection.Collection`, `Document.Document`).
94
+ * @returns A matcher that returns Option.some(entity) if the data matches, Option.none() otherwise.
95
+ *
96
+ * @example
97
+ * ```ts
98
+ * GraphBuilder.createExtension({
99
+ * id: 'collectionExtension',
100
+ * match: NodeMatcher.whenEchoType(Collection.Collection),
101
+ * connector: (collection) => {
102
+ * // `collection` is typed as Collection.Collection
103
+ * return Effect.succeed(collection.objects.map(...));
104
+ * },
105
+ * });
106
+ * ```
107
+ *
108
+ * Can be composed directly with {@link whenAll}/{@link whenAny}/{@link whenNot} while
109
+ * preserving the typed entity data in the result.
110
+ *
111
+ * @see {@link whenEchoTypeMatches} - Returns the node instead of data for legacy composition.
112
+ */
113
+ export const whenEchoType =
114
+ <T extends Type.AnyEntity>(type: T): NodeMatcher<Type.InstanceType<T>> =>
115
+ (node: Node.Node): Option.Option<Type.InstanceType<T>> =>
116
+ Entity.instanceOf(type, node.data) ? Option.some(node.data) : Option.none();
117
+
118
+ /**
119
+ * Matches a node whose data is any ECHO object.
120
+ * Returns the **object data** (not the node) for direct use in callbacks.
121
+ *
122
+ * Use this when you need to work with any ECHO object regardless of its specific type.
123
+ *
124
+ * @returns Option.some(object) if the node's data is an ECHO object, Option.none() otherwise.
125
+ *
126
+ * @example
127
+ * ```ts
128
+ * GraphBuilder.createExtension({
129
+ * id: 'objectProperties',
130
+ * match: NodeMatcher.whenEchoObject,
131
+ * connector: (object) => {
132
+ * // `object` is typed as Obj.Unknown
133
+ * const id = Obj.getURI(object);
134
+ * return Effect.succeed([{ id: `${id}.settings`, ... }]);
135
+ * },
136
+ * });
137
+ * ```
138
+ *
139
+ * Can be composed directly with {@link whenAll}/{@link whenAny}/{@link whenNot} while
140
+ * preserving the `Obj.Unknown` data type in the result.
141
+ *
142
+ * @see {@link whenEchoObjectMatches} - Returns the node instead of data for legacy composition.
143
+ */
144
+ export const whenEchoObject = (node: Node.Node): Option.Option<Obj.Unknown> =>
145
+ Obj.isObject(node.data) ? Option.some(node.data) : Option.none();
146
+
147
+ //
148
+ // Composition Matchers
149
+ //
150
+
151
+ /**
152
+ * Composes multiple matchers with AND logic - all matchers must match for success.
153
+ * The result data type is the intersection of all matchers' data types.
154
+ * Filter matchers like {@link whenNot} return `unknown`, making them transparent
155
+ * in the intersection (since `T & unknown = T`).
156
+ *
157
+ * @param matchers - The matchers to combine. All must return Option.some for success.
158
+ * @returns A matcher whose data type is the intersection of all input matchers' data types.
159
+ * Returns the first matcher's value when all match, Option.none() otherwise.
160
+ *
161
+ * @example
162
+ * ```ts
163
+ * // Match ECHO objects that are NOT Channels — result is NodeMatcher<Obj.Unknown>.
164
+ * const whenCommentable = NodeMatcher.whenAll(
165
+ * NodeMatcher.whenEchoObject,
166
+ * NodeMatcher.whenNot(NodeMatcher.whenEchoTypeMatches(Channel.Channel)),
167
+ * );
168
+ * ```
169
+ */
170
+ export const whenAll: {
171
+ <A>(a: NodeMatcher<A>, b: NodeMatcher<unknown>): NodeMatcher<A>;
172
+ <A>(a: NodeMatcher<unknown>, b: NodeMatcher<A>): NodeMatcher<A>;
173
+ <A, B>(a: NodeMatcher<A>, b: NodeMatcher<B>): NodeMatcher<A & B>;
174
+ <A, B, C>(a: NodeMatcher<A>, b: NodeMatcher<B>, c: NodeMatcher<C>): NodeMatcher<A & B & C>;
175
+ <A, B, C, D>(a: NodeMatcher<A>, b: NodeMatcher<B>, c: NodeMatcher<C>, d: NodeMatcher<D>): NodeMatcher<A & B & C & D>;
176
+ (...matchers: NodeMatcher<any>[]): NodeMatcher<any>;
177
+ } =
178
+ (...matchers: NodeMatcher<any>[]): NodeMatcher<any> =>
179
+ (node: Node.Node) => {
180
+ let first: Option.Option<any> = Option.none();
181
+ for (const candidate of matchers) {
182
+ const result = candidate(node);
183
+ if (Option.isNone(result)) {
184
+ return Option.none();
185
+ }
186
+ if (Option.isNone(first)) {
187
+ first = result;
188
+ }
189
+ }
190
+ return first;
191
+ };
192
+
193
+ /**
194
+ * Composes multiple matchers with OR logic - at least one matcher must match.
195
+ * The result data type is the union of all matchers' data types.
196
+ *
197
+ * @param matchers - The matchers to combine. At least one must return Option.some.
198
+ * @returns A matcher whose data type is the union of all input matchers' data types.
199
+ * Returns the first matching matcher's value, or Option.none() if none match.
200
+ *
201
+ * @example
202
+ * ```ts
203
+ * // Match nodes that are either Sequences or Routines
204
+ * const whenInvocable = NodeMatcher.whenAny(
205
+ * NodeMatcher.whenEchoTypeMatches(Sequence),
206
+ * NodeMatcher.whenEchoTypeMatches(Routine.Routine),
207
+ * );
208
+ * ```
209
+ */
210
+ export const whenAny: {
211
+ <A, B>(a: NodeMatcher<A>, b: NodeMatcher<B>): NodeMatcher<A | B>;
212
+ <A, B, C>(a: NodeMatcher<A>, b: NodeMatcher<B>, c: NodeMatcher<C>): NodeMatcher<A | B | C>;
213
+ <A, B, C, D>(a: NodeMatcher<A>, b: NodeMatcher<B>, c: NodeMatcher<C>, d: NodeMatcher<D>): NodeMatcher<A | B | C | D>;
214
+ (...matchers: NodeMatcher<any>[]): NodeMatcher<any>;
215
+ } =
216
+ (...matchers: NodeMatcher<any>[]): NodeMatcher<any> =>
217
+ (node: Node.Node) => {
218
+ for (const candidate of matchers) {
219
+ const result = candidate(node);
220
+ if (Option.isSome(result)) {
221
+ return result;
222
+ }
223
+ }
224
+ return Option.none();
225
+ };
226
+
227
+ /**
228
+ * Matches a node whose data is an instance of the given ECHO schema type.
229
+ * Returns the **node** (not the data) to enable composition with whenAll/whenAny/whenNot.
230
+ *
231
+ * Use this instead of {@link whenEchoType} when you need to combine matchers.
232
+ * The difference is what's returned:
233
+ * - `whenEchoType` returns the typed entity (for direct use)
234
+ * - `whenEchoTypeMatches` returns the node (for composition)
235
+ *
236
+ * @template T - The ECHO schema type to match against.
237
+ * @param type - The ECHO schema (e.g., `Channel.Channel`, `Document.Document`).
238
+ * @returns A matcher that returns Option.some(node) if the data matches, Option.none() otherwise.
239
+ *
240
+ * @example
241
+ * ```ts
242
+ * // Use with whenAny for OR logic
243
+ * const whenPresentable = NodeMatcher.whenAny(
244
+ * NodeMatcher.whenEchoTypeMatches(Collection.Collection),
245
+ * NodeMatcher.whenEchoTypeMatches(Markdown.Document),
246
+ * );
247
+ *
248
+ * // Use with whenNot for exclusion
249
+ * const whenNotChannel = NodeMatcher.whenNot(
250
+ * NodeMatcher.whenEchoTypeMatches(Channel.Channel),
251
+ * );
252
+ * ```
253
+ *
254
+ * @see {@link whenEchoType} - Use instead when you need the typed entity directly.
255
+ */
256
+ export const whenEchoTypeMatches =
257
+ <T extends Type.AnyObj | Type.AnyRelation>(type: T): NodeMatcher =>
258
+ (node: Node.Node): Option.Option<Node.Node> =>
259
+ Entity.instanceOf(type, node.data) ? Option.some(node) : Option.none();
260
+
261
+ /**
262
+ * Matches a node whose data is any ECHO object.
263
+ * Returns the **node** (not the data) to enable composition with whenAll/whenAny/whenNot.
264
+ *
265
+ * Use this instead of {@link whenEchoObject} when you need to combine matchers.
266
+ * The difference is what's returned:
267
+ * - `whenEchoObject` returns the object data (for direct use)
268
+ * - `whenEchoObjectMatches` returns the node (for composition)
269
+ *
270
+ * @returns Option.some(node) if the node's data is an ECHO object, Option.none() otherwise.
271
+ *
272
+ * @example
273
+ * ```ts
274
+ * // Match ECHO objects that are not system types
275
+ * const whenUserObject = NodeMatcher.whenAll(
276
+ * NodeMatcher.whenEchoObjectMatches,
277
+ * NodeMatcher.whenNot(NodeMatcher.whenEchoTypeMatches(SystemType)),
278
+ * );
279
+ * ```
280
+ *
281
+ * @see {@link whenEchoObject} - Use instead when you need the object data directly.
282
+ */
283
+ export const whenEchoObjectMatches = (node: Node.Node): Option.Option<Node.Node> =>
284
+ Obj.isObject(node.data) ? Option.some(node) : Option.none();
285
+
286
+ /**
287
+ * Negates a matcher - matches when the given matcher does NOT match.
288
+ * Useful for exclusion patterns like "any object EXCEPT type X".
289
+ *
290
+ * Returns `NodeMatcher<unknown>` because negation is a filter — it doesn't provide
291
+ * typed data. This makes it transparent in {@link whenAll} intersections
292
+ * (since `T & unknown = T`).
293
+ *
294
+ * @param matcher - The matcher to negate.
295
+ * @returns A matcher that returns Option.some(node) if the input matcher returns none,
296
+ * and Option.none() if the input matcher returns some.
297
+ *
298
+ * @example
299
+ * ```ts
300
+ * // Match any ECHO object that is NOT a Channel — result is NodeMatcher<Obj.Unknown>.
301
+ * const whenCommentable = NodeMatcher.whenAll(
302
+ * NodeMatcher.whenEchoObject,
303
+ * NodeMatcher.whenNot(NodeMatcher.whenEchoTypeMatches(Channel.Channel)),
304
+ * );
305
+ *
306
+ * // Match any node that is NOT the root
307
+ * const whenNotRoot = NodeMatcher.whenNot(NodeMatcher.whenRoot);
308
+ * ```
309
+ */
310
+ export const whenNot =
311
+ (matcher: NodeMatcher<any>): NodeMatcher<unknown> =>
312
+ (node: Node.Node): Option.Option<unknown> =>
313
+ Option.isNone(matcher(node)) ? Option.some(node) : Option.none();