@dxos/graph 0.8.4-main.84f28bd → 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.
@@ -1 +0,0 @@
1
- {"version":3,"file":"model.d.ts","sourceRoot":"","sources":["../../../src/model.ts"],"names":[],"mappings":"AAMA,OAAO,EAAE,aAAa,EAAE,MAAM,aAAa,CAAC;AAE5C,OAAO,EAAE,KAAK,IAAI,EAAQ,MAAM,mBAAmB,CAAC;AACpD,OAAO,EAAE,KAAK,YAAY,EAAwB,MAAM,YAAY,CAAC;AAErE,OAAO,EAAE,KAAK,aAAa,EAAE,KAAK,aAAa,EAAE,KAAK,KAAK,EAAE,KAAK,SAAS,EAAE,KAAK,SAAS,EAAE,MAAM,SAAS,CAAC;AAG7G;;GAEG;AACH,qBAAa,kBAAkB,CAC7B,IAAI,SAAS,aAAa,GAAG,aAAa,EAC1C,IAAI,SAAS,aAAa,GAAG,aAAa;IAE1C,SAAS,CAAC,QAAQ,CAAC,MAAM,EAAE,KAAK,CAAC;IAEjC;;OAEG;gBACS,KAAK,CAAC,EAAE,KAAK;IAOzB,CAAC,aAAa,CAAC;;;;IAIf;;OAEG;IACH,MAAM;;;;IAON,IAAI,KAAK,IAAI,KAAK,CAEjB;IAED,IAAI,KAAK,IAAI,IAAI,EAAE,CAElB;IAED,IAAI,KAAK,IAAI,IAAI,EAAE,CAElB;IAMD,QAAQ,CAAC,EAAE,EAAE,MAAM,GAAG,IAAI,GAAG,SAAS;IAItC,OAAO,CAAC,EAAE,EAAE,MAAM,GAAG,IAAI;IAIzB,WAAW,CAAC,EAAE,IAAI,EAAE,GAAE,OAAO,CAAC,SAAS,CAAC,GAAG,CAAM,GAAG,IAAI,EAAE;IAQ1D,QAAQ,CAAC,EAAE,EAAE,MAAM,GAAG,IAAI,GAAG,SAAS;IAItC,OAAO,CAAC,EAAE,EAAE,MAAM,GAAG,IAAI;IAIzB,WAAW,CAAC,EAAE,IAA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,OAAO,UAAU,EAAE,IAAI,sBAAsB,EAAE,cAAY;;;CAQpF,CAAC;AAEF;;GAEG;AACH,qBAAa,kBAAkB,CAC7B,IAAI,SAAS,aAAa,GAAG,aAAa,EAC1C,IAAI,SAAS,aAAa,GAAG,aAAa,CAC1C,SAAQ,UAAU,CAAC,IAAI,EAAE,IAAI,CAAC;gBAClB,KAAK,CAAC,EAAE,OAAO,CAAC,KAAK,CAAC;IASzB,IAAI,CAAC,KAAK,CAAC,EAAE,OAAO,CAAC,KAAK,CAAC,GAAG,kBAAkB,CAAC,IAAI,EAAE,IAAI,CAAC;IAIrE,SAAS,CAAC,EAAE,EAAE,sBAAsB,EAAE,IAAI,UAAQ,GAAG,MAAM,IAAI;CAGhE;AAED;;GAEG;AACH,qBAAa,YAAY,CACvB,IAAI,SAAS,aAAa,GAAG,aAAa,EAC1C,IAAI,SAAS,aAAa,GAAG,aAAa,CAC1C,SAAQ,oBAAoB,CAAC,IAAI,EAAE,IAAI,EAAE,UAAU,CAAC,IAAI,EAAE,IAAI,CAAC,CAAC;IACvD,IAAI,CAAC,EAAE,EAAE,CAAC,OAAO,EAAE,IAAI,KAAK,IAAI,GAAG,IAAI;CAIjD"}
@@ -1,2 +0,0 @@
1
- export {};
2
- //# sourceMappingURL=model.test.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"model.test.d.ts","sourceRoot":"","sources":["../../../src/model.test.ts"],"names":[],"mappings":""}
@@ -1,58 +0,0 @@
1
- import { Schema } from 'effect';
2
- import { type Specialize } from '@dxos/util';
3
- export declare const BaseGraphNode: Schema.Struct<{
4
- id: typeof Schema.String;
5
- type: Schema.optional<typeof Schema.String>;
6
- data: Schema.optional<typeof Schema.Any>;
7
- }>;
8
- /** Raw base type. */
9
- export type BaseGraphNode = Schema.Schema.Type<typeof BaseGraphNode>;
10
- /** Typed node data. */
11
- type GraphNode<Data = any, Optional extends boolean = false> = Specialize<BaseGraphNode, Optional extends true ? {
12
- data?: Data;
13
- } : {
14
- data: Data;
15
- }>;
16
- export declare namespace GraphNode {
17
- type Any = GraphNode<any, true>;
18
- type Optional<Data = any> = GraphNode<Data, true>;
19
- type Required<Data = any> = GraphNode<Data, false>;
20
- }
21
- export declare const BaseGraphEdge: Schema.Struct<{
22
- id: typeof Schema.String;
23
- type: Schema.optional<typeof Schema.String>;
24
- source: typeof Schema.String;
25
- target: typeof Schema.String;
26
- data: Schema.optional<typeof Schema.Any>;
27
- }>;
28
- /** Raw base type. */
29
- export type BaseGraphEdge = Schema.Schema.Type<typeof BaseGraphEdge>;
30
- /** Typed edge data. */
31
- export type GraphEdge<Data = any, Optional extends boolean = false> = Specialize<BaseGraphEdge, Optional extends true ? {
32
- data?: Data;
33
- } : {
34
- data: Data;
35
- }>;
36
- export declare namespace GraphEdge {
37
- type Any = GraphEdge<any, true>;
38
- type Optional<Data = any> = GraphEdge<Data, true>;
39
- type Required<Data = any> = GraphEdge<Data, false>;
40
- }
41
- export declare const Graph: Schema.Struct<{
42
- id: Schema.optional<typeof Schema.String>;
43
- nodes: Schema.mutable<Schema.Array$<Schema.Struct<{
44
- id: typeof Schema.String;
45
- type: Schema.optional<typeof Schema.String>;
46
- data: Schema.optional<typeof Schema.Any>;
47
- }>>>;
48
- edges: Schema.mutable<Schema.Array$<Schema.Struct<{
49
- id: typeof Schema.String;
50
- type: Schema.optional<typeof Schema.String>;
51
- source: typeof Schema.String;
52
- target: typeof Schema.String;
53
- data: Schema.optional<typeof Schema.Any>;
54
- }>>>;
55
- }>;
56
- export type Graph = Schema.Schema.Type<typeof Graph>;
57
- export {};
58
- //# sourceMappingURL=types.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../../../src/types.ts"],"names":[],"mappings":"AAIA,OAAO,EAAE,MAAM,EAAE,MAAM,QAAQ,CAAC;AAEhC,OAAO,EAAE,KAAK,UAAU,EAAE,MAAM,YAAY,CAAC;AAO7C,eAAO,MAAM,aAAa;;;;EAIxB,CAAC;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I,CAAC,CAAC;IACzD,KAAY,QAAQ,CAAC,IAAI,GAAG,GAAG,IAAI,SAAS,CAAC,IAAI,EAAE,KAAK,CAAC,CAAC;CAC3D;AAMD,eAAO,MAAM,KAAK;;;;;;;;;;;;;;EAIhB,CAAC;AAEH,MAAM,MAAM,KAAK,GAAG,MAAM,CAAC,MAAM,CAAC,IAAI,CAAC,OAAO,KAAK,CAAC,CAAC"}
@@ -1,9 +0,0 @@
1
- type EdgeMeta = {
2
- source: string;
3
- target: string;
4
- relation?: string;
5
- };
6
- export declare const createEdgeId: ({ source, target, relation }: EdgeMeta) => string;
7
- export declare const parseEdgeId: (id: string) => EdgeMeta;
8
- export {};
9
- //# sourceMappingURL=util.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"util.d.ts","sourceRoot":"","sources":["../../../src/util.ts"],"names":[],"mappings":"AASA,KAAK,QAAQ,GAAG;IAAE,MAAM,EAAE,MAAM,CAAC;IAAC,MAAM,EAAE,MAAM,CAAC;IAAC,QAAQ,CAAC,EAAE,MAAM,CAAA;CAAE,CAAC;AAEtE,eAAO,MAAM,YAAY,GAAI,8BAA8B,QAAQ,WAIlE,CAAC;AAEF,eAAO,MAAM,WAAW,GAAI,IAAI,MAAM,KAAG,QAIxC,CAAC"}
package/src/model.test.ts DELETED
@@ -1,188 +0,0 @@
1
- //
2
- // Copyright 2024 DXOS.org
3
- //
4
-
5
- import { effect } from '@preact/signals-core';
6
- import { Schema } from 'effect';
7
- import { describe, test } from 'vitest';
8
-
9
- import { Trigger } from '@dxos/async';
10
- import { registerSignalsRuntime } from '@dxos/echo-signals';
11
- import { live } from '@dxos/live-object';
12
-
13
- import { GraphModel, ReactiveGraphModel } from './model';
14
- import { BaseGraphNode, type Graph, type GraphNode } from './types';
15
-
16
- registerSignalsRuntime();
17
-
18
- const TestNode = Schema.extend(
19
- BaseGraphNode,
20
- Schema.Struct({
21
- value: Schema.String,
22
- }),
23
- );
24
-
25
- type TestNode = Schema.Schema.Type<typeof TestNode>;
26
-
27
- type TestData = { value: string };
28
-
29
- describe('Graph', () => {
30
- test('empty', ({ expect }) => {
31
- const graph = new GraphModel();
32
- expect(graph.nodes).to.have.length(0);
33
- expect(graph.edges).to.have.length(0);
34
- expect(graph.toJSON()).to.deep.eq({ nodes: 0, edges: 0 });
35
- });
36
-
37
- test('extended', ({ expect }) => {
38
- const graph = new GraphModel<TestNode>();
39
- const node = graph.addNode({ id: 'test', value: 'test' });
40
- expect(node.value.length).to.eq(4);
41
- });
42
-
43
- test('reactive', async ({ expect }) => {
44
- const graph = new GraphModel(live({ nodes: [], edges: [] }));
45
-
46
- const done = new Trigger<Graph>();
47
-
48
- // NOTE: Requires `registerSignalsRuntime` to be called.
49
- const unsubscribe = effect(() => {
50
- if (graph.edges.length === 2) {
51
- done.wake(graph.graph);
52
- }
53
- });
54
-
55
- setTimeout(() => {
56
- graph.builder.addNode({ id: 'node-1' });
57
- graph.builder.addNode({ id: 'node-2' });
58
- graph.builder.addNode({ id: 'node-3' });
59
- });
60
-
61
- setTimeout(() => {
62
- graph.builder.addEdge({ source: 'node-1', target: 'node-2' });
63
- graph.builder.addEdge({ source: 'node-2', target: 'node-3' });
64
- });
65
-
66
- {
67
- const graph = await done.wait();
68
- expect(graph.nodes).to.have.length(3);
69
- expect(graph.edges).to.have.length(2);
70
- }
71
-
72
- unsubscribe();
73
- });
74
-
75
- test('reactive model', async ({ expect }) => {
76
- const graph = new ReactiveGraphModel();
77
-
78
- const done = new Trigger<Graph>();
79
- const unsubscribe = graph.subscribe((graph) => {
80
- if (graph.edges.length === 2) {
81
- done.wake(graph.graph);
82
- }
83
- });
84
-
85
- setTimeout(() => {
86
- graph.builder.addNode({ id: 'node-1' });
87
- graph.builder.addNode({ id: 'node-2' });
88
- graph.builder.addNode({ id: 'node-3' });
89
- });
90
-
91
- setTimeout(() => {
92
- graph.builder.addEdge({ source: 'node-1', target: 'node-2' });
93
- graph.builder.addEdge({ source: 'node-2', target: 'node-3' });
94
- });
95
-
96
- {
97
- const graph = await done.wait();
98
- expect(graph.nodes).to.have.length(3);
99
- expect(graph.edges).to.have.length(2);
100
- }
101
-
102
- unsubscribe();
103
- });
104
-
105
- test('optional', ({ expect }) => {
106
- {
107
- const graph = new GraphModel<GraphNode.Required<string>>();
108
- const node = graph.addNode({ id: 'test', data: 'test' });
109
- expect(node.data.length).to.eq(4);
110
- }
111
-
112
- {
113
- const graph = new GraphModel<GraphNode.Optional<string>>();
114
- const node = graph.addNode({ id: 'test' });
115
- expect(node.data?.length).to.be.undefined;
116
- }
117
- });
118
-
119
- test('add and remove subgraphs', ({ expect }) => {
120
- const graph = new GraphModel<GraphNode.Required<TestData>>();
121
- graph.builder
122
- .addNode({ id: 'node1', data: { value: 'test' } })
123
- .addNode({ id: 'node2', data: { value: 'test' } })
124
- .addNode({ id: 'node3', data: { value: 'test' } })
125
- .addEdge({ source: 'node1', target: 'node2' })
126
- .addEdge({ source: 'node2', target: 'node3' });
127
- expect(graph.nodes).to.have.length(3);
128
- expect(graph.edges).to.have.length(2);
129
- const pre = graph.toJSON();
130
-
131
- const node = graph.findNode('node2');
132
- expect(node).to.exist;
133
-
134
- const removed = graph.removeNode('node2');
135
- expect(removed.nodes).to.have.length(1);
136
- expect(removed.edges).to.have.length(2);
137
- expect(graph.nodes).to.have.length(2);
138
- expect(graph.edges).to.have.length(0);
139
-
140
- graph.addGraph(removed);
141
- const post = graph.toJSON();
142
- expect(pre).to.deep.eq(post);
143
-
144
- graph.clear();
145
- expect(graph.nodes).to.have.length(0);
146
- expect(graph.edges).to.have.length(0);
147
- });
148
-
149
- test('traverse', ({ expect }) => {
150
- const graph = new GraphModel();
151
- graph.builder
152
- .addNode({ id: 'a' })
153
- .addNode({ id: 'b' })
154
- .addNode({ id: 'c' })
155
- .addNode({ id: 'd' })
156
- .addNode({ id: 'e' })
157
- .addNode({ id: 'f' })
158
- .addNode({ id: 'g' })
159
- .addNode({ id: 'h' })
160
- // Sub-graph 1.
161
- .addEdge({ source: 'a', target: 'b' })
162
- .addEdge({ source: 'a', target: 'c' })
163
- .addEdge({ source: 'c', target: 'd' })
164
- .addEdge({ source: 'd', target: 'e' })
165
- .addEdge({ source: 'd', target: 'a' })
166
- // Sub-graph 2.
167
- .addEdge({ source: 'f', target: 'g' })
168
- .addEdge({ source: 'g', target: 'h' });
169
-
170
- const count = graph.nodes.length;
171
-
172
- {
173
- // Sub-graph 1.
174
- const nodes = graph.traverse(graph.getNode('a'));
175
- expect(nodes).to.have.length(5);
176
- }
177
-
178
- {
179
- // Sub-graph 2.
180
- const nodes = graph.traverse(graph.getNode('f'));
181
- expect(nodes).to.have.length(3);
182
-
183
- // Remove sub-graph.
184
- graph.removeNodes(nodes.map((node) => node.id));
185
- expect(graph.nodes).to.have.length(count - 3);
186
- }
187
- });
188
- });
package/src/model.ts DELETED
@@ -1,317 +0,0 @@
1
- //
2
- // Copyright 2024 DXOS.org
3
- //
4
-
5
- import { effect } from '@preact/signals-core';
6
-
7
- import { inspectCustom } from '@dxos/debug';
8
- import { failedInvariant, invariant } from '@dxos/invariant';
9
- import { type Live, live } from '@dxos/live-object';
10
- import { type MakeOptional, isNotFalsy, removeBy } from '@dxos/util';
11
-
12
- import { type BaseGraphEdge, type BaseGraphNode, type Graph, type GraphEdge, type GraphNode } from './types';
13
- import { createEdgeId } from './util';
14
-
15
- /**
16
- * Readonly Graph wrapper.
17
- */
18
- export class ReadonlyGraphModel<
19
- Node extends BaseGraphNode = BaseGraphNode,
20
- Edge extends BaseGraphEdge = BaseGraphEdge,
21
- > {
22
- protected readonly _graph: Graph;
23
-
24
- /**
25
- * NOTE: Pass in simple Graph or Live.
26
- */
27
- constructor(graph?: Graph) {
28
- this._graph = graph ?? {
29
- nodes: [],
30
- edges: [],
31
- };
32
- }
33
-
34
- [inspectCustom]() {
35
- return this.toJSON();
36
- }
37
-
38
- /**
39
- * Return stable sorted JSON representation of graph.
40
- */
41
- toJSON() {
42
- return {
43
- nodes: this.nodes.length,
44
- edges: this.edges.length,
45
- };
46
- }
47
-
48
- get graph(): Graph {
49
- return this._graph;
50
- }
51
-
52
- get nodes(): Node[] {
53
- return this._graph.nodes as Node[];
54
- }
55
-
56
- get edges(): Edge[] {
57
- return this._graph.edges as Edge[];
58
- }
59
-
60
- //
61
- // Nodes
62
- //
63
-
64
- findNode(id: string): Node | undefined {
65
- return this.nodes.find((node) => node.id === id);
66
- }
67
-
68
- getNode(id: string): Node {
69
- return this.findNode(id) ?? failedInvariant();
70
- }
71
-
72
- filterNodes({ type }: Partial<GraphNode.Any> = {}): Node[] {
73
- return this.nodes.filter((node) => !type || type === node.type);
74
- }
75
-
76
- //
77
- // Edges
78
- //
79
-
80
- findEdge(id: string): Edge | undefined {
81
- return this.edges.find((edge) => edge.id === id);
82
- }
83
-
84
- getEdge(id: string): Edge {
85
- return this.findEdge(id) ?? failedInvariant();
86
- }
87
-
88
- filterEdges({ type, source, target }: Partial<GraphEdge> = {}): Edge[] {
89
- return this.edges.filter(
90
- (edge) =>
91
- (!type || type === edge.type) && (!source || source === edge.source) && (!target || target === edge.target),
92
- );
93
- }
94
-
95
- //
96
- // Traverse
97
- //
98
-
99
- traverse(root: Node): Node[] {
100
- return this._traverse(root);
101
- }
102
-
103
- private _traverse(root: Node, visited: Set<string> = new Set()): Node[] {
104
- if (visited.has(root.id)) {
105
- return [];
106
- }
107
-
108
- visited.add(root.id);
109
- const targets = this.filterEdges({ source: root.id })
110
- .map((edge) => this.getNode(edge.target))
111
- .filter(isNotFalsy);
112
-
113
- return [root, ...targets.flatMap((target) => this._traverse(target, visited))];
114
- }
115
- }
116
-
117
- /**
118
- * Mutable Graph wrapper.
119
- */
120
- export abstract class AbstractGraphModel<
121
- Node extends BaseGraphNode = BaseGraphNode,
122
- Edge extends BaseGraphEdge = BaseGraphEdge,
123
- Model extends AbstractGraphModel<Node, Edge, Model, Builder> = any,
124
- Builder extends AbstractGraphBuilder<Node, Edge, Model> = AbstractGraphBuilder<Node, Edge, Model>,
125
- > extends ReadonlyGraphModel<Node, Edge> {
126
- /**
127
- * Allows chaining.
128
- */
129
- abstract get builder(): Builder;
130
-
131
- /**
132
- * Shallow copy of provided graph.
133
- */
134
- abstract copy(graph?: Partial<Graph>): Model;
135
-
136
- clear(): this {
137
- this._graph.nodes.length = 0;
138
- this._graph.edges.length = 0;
139
- return this;
140
- }
141
-
142
- addGraph(graph: Model): this {
143
- this.addNodes(graph.nodes);
144
- this.addEdges(graph.edges);
145
- return this;
146
- }
147
-
148
- addGraphs(graphs: Model[]): this {
149
- graphs.forEach((graph) => {
150
- this.addNodes(graph.nodes);
151
- this.addEdges(graph.edges);
152
- });
153
- return this;
154
- }
155
-
156
- addNode(node: Node): Node {
157
- invariant(node.id, 'ID is required');
158
- invariant(!this.findNode(node.id), `node already exists: ${node.id}`);
159
- this._graph.nodes.push(node);
160
- return node;
161
- }
162
-
163
- addNodes(nodes: Node[]): Node[] {
164
- return nodes.map((node) => this.addNode(node));
165
- }
166
-
167
- addEdge(edge: MakeOptional<Edge, 'id'>): Edge {
168
- invariant(edge.source);
169
- invariant(edge.target);
170
- if (!edge.id) {
171
- // TODO(burdon): Generate random id.
172
- edge = { id: createEdgeId(edge), ...edge };
173
- }
174
- invariant(!this.findNode(edge.id!));
175
- this._graph.edges.push(edge as Edge);
176
- return edge as Edge;
177
- }
178
-
179
- addEdges(edges: Edge[]): Edge[] {
180
- return edges.map((edge) => this.addEdge(edge));
181
- }
182
-
183
- removeNode(id: string): Model {
184
- const edges = removeBy<Edge>(this._graph.edges as Edge[], (edge) => edge.source === id || edge.target === id);
185
- const nodes = removeBy<Node>(this._graph.nodes as Node[], (node) => node.id === id);
186
- return this.copy({ nodes, edges });
187
- }
188
-
189
- removeNodes(ids: string[]): Model {
190
- const graphs = ids.map((id) => this.removeNode(id));
191
- return this.copy().addGraphs(graphs);
192
- }
193
-
194
- removeEdge(id: string): Model {
195
- const edges = removeBy<Edge>(this._graph.edges as Edge[], (edge) => edge.id === id);
196
- return this.copy({ nodes: [], edges });
197
- }
198
-
199
- removeEdges(ids: string[]): Model {
200
- const graphs = ids.map((id) => this.removeEdge(id));
201
- return this.copy().addGraphs(graphs);
202
- }
203
- }
204
-
205
- /**
206
- * Chainable builder wrapper
207
- */
208
- export abstract class AbstractGraphBuilder<
209
- Node extends BaseGraphNode,
210
- Edge extends BaseGraphEdge,
211
- Model extends GraphModel<Node, Edge>,
212
- > {
213
- constructor(protected readonly _model: Model) {}
214
-
215
- get model(): Model {
216
- return this._model;
217
- }
218
-
219
- call(cb: (builder: this) => void): this {
220
- cb(this);
221
- return this;
222
- }
223
-
224
- getNode(id: string): Node {
225
- return this.model.getNode(id);
226
- }
227
-
228
- addNode(node: Node): this {
229
- this._model.addNode(node);
230
- return this;
231
- }
232
-
233
- addEdge(edge: MakeOptional<Edge, 'id'>): this {
234
- this._model.addEdge(edge);
235
- return this;
236
- }
237
-
238
- addNodes(nodes: Node[]): this {
239
- this._model.addNodes(nodes);
240
- return this;
241
- }
242
-
243
- addEdges(edges: Edge[]): this {
244
- this._model.addEdges(edges);
245
- return this;
246
- }
247
- }
248
-
249
- /**
250
- * Basic model.
251
- */
252
- export class GraphModel<
253
- Node extends BaseGraphNode = BaseGraphNode,
254
- Edge extends BaseGraphEdge = BaseGraphEdge,
255
- > extends AbstractGraphModel<Node, Edge, GraphModel<Node, Edge>, GraphBuilder<Node, Edge>> {
256
- override get builder() {
257
- return new GraphBuilder<Node, Edge>(this);
258
- }
259
-
260
- override copy(graph?: Partial<Graph>): GraphModel<Node, Edge> {
261
- return new GraphModel<Node, Edge>({ nodes: graph?.nodes ?? [], edges: graph?.edges ?? [] });
262
- }
263
- }
264
-
265
- export type GraphModelSubscription = (model: GraphModel, graph: Live<Graph>) => void;
266
-
267
- /**
268
- * Subscription.
269
- * NOTE: Requires `registerSignalsRuntime` to be called.
270
- */
271
- export const subscribe = (model: GraphModel, cb: GraphModelSubscription, fire = false) => {
272
- if (fire) {
273
- cb(model, model.graph);
274
- }
275
-
276
- return effect(() => {
277
- cb(model, model.graph); // TODO(burdon): This won't work unless model.graph is reactive.
278
- });
279
- };
280
-
281
- /**
282
- * Basic reactive model.
283
- */
284
- export class ReactiveGraphModel<
285
- Node extends BaseGraphNode = BaseGraphNode,
286
- Edge extends BaseGraphEdge = BaseGraphEdge,
287
- > extends GraphModel<Node, Edge> {
288
- constructor(graph?: Partial<Graph>) {
289
- super(
290
- live({
291
- nodes: graph?.nodes ?? [],
292
- edges: graph?.edges ?? [],
293
- }),
294
- );
295
- }
296
-
297
- override copy(graph?: Partial<Graph>): ReactiveGraphModel<Node, Edge> {
298
- return new ReactiveGraphModel<Node, Edge>(graph);
299
- }
300
-
301
- subscribe(cb: GraphModelSubscription, fire = false): () => void {
302
- return subscribe(this, cb, fire);
303
- }
304
- }
305
-
306
- /**
307
- * Basic builder.
308
- */
309
- export class GraphBuilder<
310
- Node extends BaseGraphNode = BaseGraphNode,
311
- Edge extends BaseGraphEdge = BaseGraphEdge,
312
- > extends AbstractGraphBuilder<Node, Edge, GraphModel<Node, Edge>> {
313
- override call(cb: (builder: this) => void): this {
314
- cb(this);
315
- return this;
316
- }
317
- }
package/src/types.ts DELETED
@@ -1,72 +0,0 @@
1
- //
2
- // Copyright 2024 DXOS.org
3
- //
4
-
5
- import { Schema } from 'effect';
6
-
7
- import { type Specialize } from '@dxos/util';
8
-
9
- //
10
- // Node
11
- //
12
-
13
- // TODO(burdon): Make type extensible (i.e., not dependent on `data` property)?
14
- export const BaseGraphNode = Schema.Struct({
15
- id: Schema.String,
16
- type: Schema.optional(Schema.String),
17
- data: Schema.optional(Schema.Any),
18
- });
19
-
20
- /** Raw base type. */
21
- export type BaseGraphNode = Schema.Schema.Type<typeof BaseGraphNode>;
22
-
23
- /** Typed node data. */
24
- type GraphNode<Data = any, Optional extends boolean = false> = Specialize<
25
- BaseGraphNode,
26
- Optional extends true ? { data?: Data } : { data: Data }
27
- >;
28
-
29
- export declare namespace GraphNode {
30
- export type Any = GraphNode<any, true>;
31
- export type Optional<Data = any> = GraphNode<Data, true>;
32
- export type Required<Data = any> = GraphNode<Data, false>;
33
- }
34
-
35
- //
36
- // Edge
37
- //
38
-
39
- export const BaseGraphEdge = Schema.Struct({
40
- id: Schema.String,
41
- type: Schema.optional(Schema.String),
42
- source: Schema.String,
43
- target: Schema.String,
44
- data: Schema.optional(Schema.Any),
45
- });
46
-
47
- /** Raw base type. */
48
- export type BaseGraphEdge = Schema.Schema.Type<typeof BaseGraphEdge>;
49
-
50
- /** Typed edge data. */
51
- export type GraphEdge<Data = any, Optional extends boolean = false> = Specialize<
52
- BaseGraphEdge,
53
- Optional extends true ? { data?: Data } : { data: Data }
54
- >;
55
-
56
- export declare namespace GraphEdge {
57
- export type Any = GraphEdge<any, true>;
58
- export type Optional<Data = any> = GraphEdge<Data, true>;
59
- export type Required<Data = any> = GraphEdge<Data, false>;
60
- }
61
-
62
- //
63
- // Graph
64
- //
65
-
66
- export const Graph = Schema.Struct({
67
- id: Schema.optional(Schema.String),
68
- nodes: Schema.mutable(Schema.Array(BaseGraphNode)),
69
- edges: Schema.mutable(Schema.Array(BaseGraphEdge)),
70
- });
71
-
72
- export type Graph = Schema.Schema.Type<typeof Graph>;
package/src/util.ts DELETED
@@ -1,22 +0,0 @@
1
- //
2
- // Copyright 2024 DXOS.org
3
- //
4
-
5
- import { invariant } from '@dxos/invariant';
6
-
7
- const KEY_REGEX = /\w+/;
8
-
9
- // NOTE: The `relation` is different from the `type`.
10
- type EdgeMeta = { source: string; target: string; relation?: string };
11
-
12
- export const createEdgeId = ({ source, target, relation }: EdgeMeta) => {
13
- invariant(source.match(KEY_REGEX), `invalid source: ${source}`);
14
- invariant(target.match(KEY_REGEX), `invalid target: ${target}`);
15
- return [source, relation, target].join('_');
16
- };
17
-
18
- export const parseEdgeId = (id: string): EdgeMeta => {
19
- const [source, relation, target] = id.split('_');
20
- invariant(source.length && target.length);
21
- return { source, relation: relation.length ? relation : undefined, target };
22
- };