@dxos/graph 0.8.4-main.e098934 → 0.8.4-main.e8ec1fe
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/lib/browser/index.mjs +30 -59
- package/dist/lib/browser/index.mjs.map +3 -3
- package/dist/lib/browser/meta.json +1 -1
- package/dist/lib/node-esm/index.mjs +30 -59
- package/dist/lib/node-esm/index.mjs.map +3 -3
- package/dist/lib/node-esm/meta.json +1 -1
- package/dist/types/src/model.d.ts +1 -4
- package/dist/types/src/model.d.ts.map +1 -1
- package/dist/types/src/types.d.ts +1 -1
- package/dist/types/src/types.d.ts.map +1 -1
- package/dist/types/tsconfig.tsbuildinfo +1 -1
- package/package.json +13 -19
- package/src/model.test.ts +1 -1
- package/src/model.ts +2 -2
- package/src/types.ts +1 -1
|
@@ -3,7 +3,7 @@ import { effect } from "@preact/signals-core";
|
|
|
3
3
|
import { inspectCustom } from "@dxos/debug";
|
|
4
4
|
import { failedInvariant, invariant as invariant2 } from "@dxos/invariant";
|
|
5
5
|
import { live } from "@dxos/live-object";
|
|
6
|
-
import {
|
|
6
|
+
import { isTruthy, removeBy } from "@dxos/util";
|
|
7
7
|
|
|
8
8
|
// src/util.ts
|
|
9
9
|
import { invariant } from "@dxos/invariant";
|
|
@@ -53,23 +53,19 @@ var parseEdgeId = (id) => {
|
|
|
53
53
|
};
|
|
54
54
|
|
|
55
55
|
// src/model.ts
|
|
56
|
-
function _define_property(obj, key, value) {
|
|
57
|
-
if (key in obj) {
|
|
58
|
-
Object.defineProperty(obj, key, {
|
|
59
|
-
value,
|
|
60
|
-
enumerable: true,
|
|
61
|
-
configurable: true,
|
|
62
|
-
writable: true
|
|
63
|
-
});
|
|
64
|
-
} else {
|
|
65
|
-
obj[key] = value;
|
|
66
|
-
}
|
|
67
|
-
return obj;
|
|
68
|
-
}
|
|
69
56
|
var __dxlog_file2 = "/__w/dxos/dxos/packages/common/graph/src/model.ts";
|
|
70
|
-
var _inspectCustom = inspectCustom;
|
|
71
57
|
var ReadonlyGraphModel = class {
|
|
72
|
-
|
|
58
|
+
_graph;
|
|
59
|
+
/**
|
|
60
|
+
* NOTE: Pass in simple Graph or Live.
|
|
61
|
+
*/
|
|
62
|
+
constructor(graph) {
|
|
63
|
+
this._graph = graph ?? {
|
|
64
|
+
nodes: [],
|
|
65
|
+
edges: []
|
|
66
|
+
};
|
|
67
|
+
}
|
|
68
|
+
[inspectCustom]() {
|
|
73
69
|
return this.toJSON();
|
|
74
70
|
}
|
|
75
71
|
/**
|
|
@@ -127,22 +123,12 @@ var ReadonlyGraphModel = class {
|
|
|
127
123
|
visited.add(root.id);
|
|
128
124
|
const targets = this.filterEdges({
|
|
129
125
|
source: root.id
|
|
130
|
-
}).map((edge) => this.getNode(edge.target)).filter(
|
|
126
|
+
}).map((edge) => this.getNode(edge.target)).filter(isTruthy);
|
|
131
127
|
return [
|
|
132
128
|
root,
|
|
133
129
|
...targets.flatMap((target) => this._traverse(target, visited))
|
|
134
130
|
];
|
|
135
131
|
}
|
|
136
|
-
/**
|
|
137
|
-
* NOTE: Pass in simple Graph or Live.
|
|
138
|
-
*/
|
|
139
|
-
constructor(graph) {
|
|
140
|
-
_define_property(this, "_graph", void 0);
|
|
141
|
-
this._graph = graph ?? {
|
|
142
|
-
nodes: [],
|
|
143
|
-
edges: []
|
|
144
|
-
};
|
|
145
|
-
}
|
|
146
132
|
};
|
|
147
133
|
var AbstractGraphModel = class extends ReadonlyGraphModel {
|
|
148
134
|
clear() {
|
|
@@ -252,6 +238,10 @@ var AbstractGraphModel = class extends ReadonlyGraphModel {
|
|
|
252
238
|
}
|
|
253
239
|
};
|
|
254
240
|
var AbstractGraphBuilder = class {
|
|
241
|
+
_model;
|
|
242
|
+
constructor(_model) {
|
|
243
|
+
this._model = _model;
|
|
244
|
+
}
|
|
255
245
|
get model() {
|
|
256
246
|
return this._model;
|
|
257
247
|
}
|
|
@@ -278,10 +268,6 @@ var AbstractGraphBuilder = class {
|
|
|
278
268
|
this._model.addEdges(edges);
|
|
279
269
|
return this;
|
|
280
270
|
}
|
|
281
|
-
constructor(_model) {
|
|
282
|
-
_define_property(this, "_model", void 0);
|
|
283
|
-
this._model = _model;
|
|
284
|
-
}
|
|
285
271
|
};
|
|
286
272
|
var GraphModel = class _GraphModel extends AbstractGraphModel {
|
|
287
273
|
get builder() {
|
|
@@ -303,18 +289,18 @@ var subscribe = (model, cb, fire = false) => {
|
|
|
303
289
|
});
|
|
304
290
|
};
|
|
305
291
|
var ReactiveGraphModel = class _ReactiveGraphModel extends GraphModel {
|
|
306
|
-
copy(graph) {
|
|
307
|
-
return new _ReactiveGraphModel(graph);
|
|
308
|
-
}
|
|
309
|
-
subscribe(cb, fire = false) {
|
|
310
|
-
return subscribe(this, cb, fire);
|
|
311
|
-
}
|
|
312
292
|
constructor(graph) {
|
|
313
293
|
super(live({
|
|
314
294
|
nodes: graph?.nodes ?? [],
|
|
315
295
|
edges: graph?.edges ?? []
|
|
316
296
|
}));
|
|
317
297
|
}
|
|
298
|
+
copy(graph) {
|
|
299
|
+
return new _ReactiveGraphModel(graph);
|
|
300
|
+
}
|
|
301
|
+
subscribe(cb, fire = false) {
|
|
302
|
+
return subscribe(this, cb, fire);
|
|
303
|
+
}
|
|
318
304
|
};
|
|
319
305
|
var GraphBuilder = class extends AbstractGraphBuilder {
|
|
320
306
|
call(cb) {
|
|
@@ -326,21 +312,14 @@ var GraphBuilder = class extends AbstractGraphBuilder {
|
|
|
326
312
|
// src/selection.ts
|
|
327
313
|
import { computed, signal } from "@preact/signals-core";
|
|
328
314
|
import { invariant as invariant3 } from "@dxos/invariant";
|
|
329
|
-
function _define_property2(obj, key, value) {
|
|
330
|
-
if (key in obj) {
|
|
331
|
-
Object.defineProperty(obj, key, {
|
|
332
|
-
value,
|
|
333
|
-
enumerable: true,
|
|
334
|
-
configurable: true,
|
|
335
|
-
writable: true
|
|
336
|
-
});
|
|
337
|
-
} else {
|
|
338
|
-
obj[key] = value;
|
|
339
|
-
}
|
|
340
|
-
return obj;
|
|
341
|
-
}
|
|
342
315
|
var __dxlog_file3 = "/__w/dxos/dxos/packages/common/graph/src/selection.ts";
|
|
343
316
|
var SelectionModel = class {
|
|
317
|
+
_singleSelect;
|
|
318
|
+
_selected = signal(/* @__PURE__ */ new Set());
|
|
319
|
+
_selectedIds = computed(() => Array.from(this._selected.value.values()));
|
|
320
|
+
constructor(_singleSelect = false) {
|
|
321
|
+
this._singleSelect = _singleSelect;
|
|
322
|
+
}
|
|
344
323
|
toJSON() {
|
|
345
324
|
return {
|
|
346
325
|
selected: Array.from(this._selected.value.values())
|
|
@@ -405,18 +384,10 @@ var SelectionModel = class {
|
|
|
405
384
|
});
|
|
406
385
|
this._selected.value = set;
|
|
407
386
|
}
|
|
408
|
-
constructor(_singleSelect = false) {
|
|
409
|
-
_define_property2(this, "_singleSelect", void 0);
|
|
410
|
-
_define_property2(this, "_selected", void 0);
|
|
411
|
-
_define_property2(this, "_selectedIds", void 0);
|
|
412
|
-
this._singleSelect = _singleSelect;
|
|
413
|
-
this._selected = signal(/* @__PURE__ */ new Set());
|
|
414
|
-
this._selectedIds = computed(() => Array.from(this._selected.value.values()));
|
|
415
|
-
}
|
|
416
387
|
};
|
|
417
388
|
|
|
418
389
|
// src/types.ts
|
|
419
|
-
import
|
|
390
|
+
import * as Schema from "effect/Schema";
|
|
420
391
|
var BaseGraphNode = Schema.Struct({
|
|
421
392
|
id: Schema.String,
|
|
422
393
|
type: Schema.optional(Schema.String),
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
|
3
3
|
"sources": ["../../../src/model.ts", "../../../src/util.ts", "../../../src/selection.ts", "../../../src/types.ts"],
|
|
4
|
-
"sourcesContent": ["//\n// Copyright 2024 DXOS.org\n//\n\nimport { effect } from '@preact/signals-core';\n\nimport { inspectCustom } from '@dxos/debug';\nimport { failedInvariant, invariant } from '@dxos/invariant';\nimport { type Live, live } from '@dxos/live-object';\nimport { type MakeOptional, isNotFalsy, removeBy } from '@dxos/util';\n\nimport { type BaseGraphEdge, type BaseGraphNode, type Graph, type GraphEdge, type GraphNode } from './types';\nimport { createEdgeId } from './util';\n\n/**\n * Readonly Graph wrapper.\n */\nexport class ReadonlyGraphModel<\n Node extends BaseGraphNode = BaseGraphNode,\n Edge extends BaseGraphEdge = BaseGraphEdge,\n> {\n protected readonly _graph: Graph;\n\n /**\n * NOTE: Pass in simple Graph or Live.\n */\n constructor(graph?: Graph) {\n this._graph = graph ?? {\n nodes: [],\n edges: [],\n };\n }\n\n [inspectCustom]() {\n return this.toJSON();\n }\n\n /**\n * Return stable sorted JSON representation of graph.\n */\n toJSON() {\n return {\n nodes: this.nodes.length,\n edges: this.edges.length,\n };\n }\n\n get graph(): Graph {\n return this._graph;\n }\n\n get nodes(): Node[] {\n return this._graph.nodes as Node[];\n }\n\n get edges(): Edge[] {\n return this._graph.edges as Edge[];\n }\n\n //\n // Nodes\n //\n\n findNode(id: string): Node | undefined {\n return this.nodes.find((node) => node.id === id);\n }\n\n getNode(id: string): Node {\n return this.findNode(id) ?? failedInvariant();\n }\n\n filterNodes({ type }: Partial<GraphNode.Any> = {}): Node[] {\n return this.nodes.filter((node) => !type || type === node.type);\n }\n\n //\n // Edges\n //\n\n findEdge(id: string): Edge | undefined {\n return this.edges.find((edge) => edge.id === id);\n }\n\n getEdge(id: string): Edge {\n return this.findEdge(id) ?? failedInvariant();\n }\n\n filterEdges({ type, source, target }: Partial<GraphEdge> = {}): Edge[] {\n return this.edges.filter(\n (edge) =>\n (!type || type === edge.type) && (!source || source === edge.source) && (!target || target === edge.target),\n );\n }\n\n //\n // Traverse\n //\n\n traverse(root: Node): Node[] {\n return this._traverse(root);\n }\n\n private _traverse(root: Node, visited: Set<string> = new Set()): Node[] {\n if (visited.has(root.id)) {\n return [];\n }\n\n visited.add(root.id);\n const targets = this.filterEdges({ source: root.id })\n .map((edge) => this.getNode(edge.target))\n .filter(isNotFalsy);\n\n return [root, ...targets.flatMap((target) => this._traverse(target, visited))];\n }\n}\n\n/**\n * Mutable Graph wrapper.\n */\nexport abstract class AbstractGraphModel<\n Node extends BaseGraphNode = BaseGraphNode,\n Edge extends BaseGraphEdge = BaseGraphEdge,\n Model extends AbstractGraphModel<Node, Edge, Model, Builder> = any,\n Builder extends AbstractGraphBuilder<Node, Edge, Model> = AbstractGraphBuilder<Node, Edge, Model>,\n> extends ReadonlyGraphModel<Node, Edge> {\n /**\n * Allows chaining.\n */\n abstract get builder(): Builder;\n\n /**\n * Shallow copy of provided graph.\n */\n abstract copy(graph?: Partial<Graph>): Model;\n\n clear(): this {\n this._graph.nodes.length = 0;\n this._graph.edges.length = 0;\n return this;\n }\n\n addGraph(graph: Model): this {\n this.addNodes(graph.nodes);\n this.addEdges(graph.edges);\n return this;\n }\n\n addGraphs(graphs: Model[]): this {\n graphs.forEach((graph) => {\n this.addNodes(graph.nodes);\n this.addEdges(graph.edges);\n });\n return this;\n }\n\n addNode(node: Node): Node {\n invariant(node.id, 'ID is required');\n invariant(!this.findNode(node.id), `node already exists: ${node.id}`);\n this._graph.nodes.push(node);\n return node;\n }\n\n addNodes(nodes: Node[]): Node[] {\n return nodes.map((node) => this.addNode(node));\n }\n\n addEdge(edge: MakeOptional<Edge, 'id'>): Edge {\n invariant(edge.source);\n invariant(edge.target);\n if (!edge.id) {\n // TODO(burdon): Generate random id.\n edge = { id: createEdgeId(edge), ...edge };\n }\n invariant(!this.findNode(edge.id!));\n this._graph.edges.push(edge as Edge);\n return edge as Edge;\n }\n\n addEdges(edges: Edge[]): Edge[] {\n return edges.map((edge) => this.addEdge(edge));\n }\n\n removeNode(id: string): Model {\n const edges = removeBy<Edge>(this._graph.edges as Edge[], (edge) => edge.source === id || edge.target === id);\n const nodes = removeBy<Node>(this._graph.nodes as Node[], (node) => node.id === id);\n return this.copy({ nodes, edges });\n }\n\n removeNodes(ids: string[]): Model {\n const graphs = ids.map((id) => this.removeNode(id));\n return this.copy().addGraphs(graphs);\n }\n\n removeEdge(id: string): Model {\n const edges = removeBy<Edge>(this._graph.edges as Edge[], (edge) => edge.id === id);\n return this.copy({ nodes: [], edges });\n }\n\n removeEdges(ids: string[]): Model {\n const graphs = ids.map((id) => this.removeEdge(id));\n return this.copy().addGraphs(graphs);\n }\n}\n\n/**\n * Chainable builder wrapper\n */\nexport abstract class AbstractGraphBuilder<\n Node extends BaseGraphNode,\n Edge extends BaseGraphEdge,\n Model extends GraphModel<Node, Edge>,\n> {\n constructor(protected readonly _model: Model) {}\n\n get model(): Model {\n return this._model;\n }\n\n call(cb: (builder: this) => void): this {\n cb(this);\n return this;\n }\n\n getNode(id: string): Node {\n return this.model.getNode(id);\n }\n\n addNode(node: Node): this {\n this._model.addNode(node);\n return this;\n }\n\n addEdge(edge: MakeOptional<Edge, 'id'>): this {\n this._model.addEdge(edge);\n return this;\n }\n\n addNodes(nodes: Node[]): this {\n this._model.addNodes(nodes);\n return this;\n }\n\n addEdges(edges: Edge[]): this {\n this._model.addEdges(edges);\n return this;\n }\n}\n\n/**\n * Basic model.\n */\nexport class GraphModel<\n Node extends BaseGraphNode = BaseGraphNode,\n Edge extends BaseGraphEdge = BaseGraphEdge,\n> extends AbstractGraphModel<Node, Edge, GraphModel<Node, Edge>, GraphBuilder<Node, Edge>> {\n override get builder() {\n return new GraphBuilder<Node, Edge>(this);\n }\n\n override copy(graph?: Partial<Graph>): GraphModel<Node, Edge> {\n return new GraphModel<Node, Edge>({ nodes: graph?.nodes ?? [], edges: graph?.edges ?? [] });\n }\n}\n\nexport type GraphModelSubscription = (model: GraphModel, graph: Live<Graph>) => void;\n\n/**\n * Subscription.\n * NOTE: Requires `registerSignalsRuntime` to be called.\n */\nexport const subscribe = (model: GraphModel, cb: GraphModelSubscription, fire = false) => {\n if (fire) {\n cb(model, model.graph);\n }\n\n return effect(() => {\n cb(model, model.graph); // TODO(burdon): This won't work unless model.graph is reactive.\n });\n};\n\n/**\n * Basic reactive model.\n */\nexport class ReactiveGraphModel<\n Node extends BaseGraphNode = BaseGraphNode,\n Edge extends BaseGraphEdge = BaseGraphEdge,\n> extends GraphModel<Node, Edge> {\n constructor(graph?: Partial<Graph>) {\n super(\n live({\n nodes: graph?.nodes ?? [],\n edges: graph?.edges ?? [],\n }),\n );\n }\n\n override copy(graph?: Partial<Graph>): ReactiveGraphModel<Node, Edge> {\n return new ReactiveGraphModel<Node, Edge>(graph);\n }\n\n subscribe(cb: GraphModelSubscription, fire = false): () => void {\n return subscribe(this, cb, fire);\n }\n}\n\n/**\n * Basic builder.\n */\nexport class GraphBuilder<\n Node extends BaseGraphNode = BaseGraphNode,\n Edge extends BaseGraphEdge = BaseGraphEdge,\n> extends AbstractGraphBuilder<Node, Edge, GraphModel<Node, Edge>> {\n override call(cb: (builder: this) => void): this {\n cb(this);\n return this;\n }\n}\n", "//\n// Copyright 2024 DXOS.org\n//\n\nimport { invariant } from '@dxos/invariant';\n\nconst KEY_REGEX = /\\w+/;\n\n// NOTE: The `relation` is different from the `type`.\ntype EdgeMeta = { source: string; target: string; relation?: string };\n\nexport const createEdgeId = ({ source, target, relation }: EdgeMeta) => {\n invariant(source.match(KEY_REGEX), `invalid source: ${source}`);\n invariant(target.match(KEY_REGEX), `invalid target: ${target}`);\n return [source, relation, target].join('_');\n};\n\nexport const parseEdgeId = (id: string): EdgeMeta => {\n const [source, relation, target] = id.split('_');\n invariant(source.length && target.length);\n return { source, relation: relation.length ? relation : undefined, target };\n};\n", "//\n// Copyright 2024 DXOS.org\n//\n\nimport { type ReadonlySignal, type Signal, computed, signal } from '@preact/signals-core';\n\nimport { invariant } from '@dxos/invariant';\n\n/**\n * Reactive selection model.\n */\nexport class SelectionModel {\n private readonly _selected: Signal<Set<string>> = signal(new Set<string>());\n private readonly _selectedIds = computed(() => Array.from(this._selected.value.values()));\n\n constructor(private readonly _singleSelect: boolean = false) {}\n\n toJSON(): { selected: string[] } {\n return {\n selected: Array.from(this._selected.value.values()),\n };\n }\n\n get size(): number {\n return this._selected.value.size;\n }\n\n get selected(): ReadonlySignal<string[]> {\n return this._selectedIds;\n }\n\n contains(id: string): boolean {\n return this._selected.value.has(id);\n }\n\n clear(): void {\n this._selected.value = new Set();\n }\n\n add(id: string): void {\n invariant(id);\n this._selected.value = new Set<string>(\n this._singleSelect ? [id] : [...Array.from(this._selected.value.values()), id],\n );\n }\n\n remove(id: string): void {\n invariant(id);\n this._selected.value = new Set<string>(Array.from(this._selected.value.values()).filter((_id) => _id !== id));\n }\n\n // TODO(burdon): Handle single select.\n\n setSelected(ids: string[], shift = false): void {\n this._selected.value = new Set([...(shift ? Array.from(this._selected.value.values()) : []), ...ids]);\n }\n\n toggleSelected(ids: string[], shift = false): void {\n const set = new Set<string>(shift ? Array.from(this._selected.value.values()) : undefined);\n ids.forEach((id) => {\n if (this._selected.value.has(id)) {\n set.delete(id);\n } else {\n set.add(id);\n }\n });\n\n this._selected.value = set;\n }\n}\n", "//\n// Copyright 2024 DXOS.org\n//\n\nimport { Schema } from 'effect';\n\nimport { type Specialize } from '@dxos/util';\n\n//\n// Node\n//\n\n// TODO(burdon): Make type extensible (i.e., not dependent on `data` property)?\nexport const BaseGraphNode = Schema.Struct({\n id: Schema.String,\n type: Schema.optional(Schema.String),\n data: Schema.optional(Schema.Any),\n});\n\n/** Raw base type. */\nexport type BaseGraphNode = Schema.Schema.Type<typeof BaseGraphNode>;\n\n/** Typed node data. */\ntype GraphNode<Data = any, Optional extends boolean = false> = Specialize<\n BaseGraphNode,\n Optional extends true ? { data?: Data } : { data: Data }\n>;\n\nexport declare namespace GraphNode {\n export type Any = GraphNode<any, true>;\n export type Optional<Data = any> = GraphNode<Data, true>;\n export type Required<Data = any> = GraphNode<Data, false>;\n}\n\n//\n// Edge\n//\n\nexport const BaseGraphEdge = Schema.Struct({\n id: Schema.String,\n type: Schema.optional(Schema.String),\n source: Schema.String,\n target: Schema.String,\n data: Schema.optional(Schema.Any),\n});\n\n/** Raw base type. */\nexport type BaseGraphEdge = Schema.Schema.Type<typeof BaseGraphEdge>;\n\n/** Typed edge data. */\nexport type GraphEdge<Data = any, Optional extends boolean = false> = Specialize<\n BaseGraphEdge,\n Optional extends true ? { data?: Data } : { data: Data }\n>;\n\nexport declare namespace GraphEdge {\n export type Any = GraphEdge<any, true>;\n export type Optional<Data = any> = GraphEdge<Data, true>;\n export type Required<Data = any> = GraphEdge<Data, false>;\n}\n\n//\n// Graph\n//\n\nexport const Graph = Schema.Struct({\n id: Schema.optional(Schema.String),\n nodes: Schema.mutable(Schema.Array(BaseGraphNode)),\n edges: Schema.mutable(Schema.Array(BaseGraphEdge)),\n});\n\nexport type Graph = Schema.Schema.Type<typeof Graph>;\n"],
|
|
5
|
-
"mappings": ";AAIA,SAASA,cAAc;AAEvB,SAASC,qBAAqB;AAC9B,SAASC,iBAAiBC,aAAAA,kBAAiB;AAC3C,SAAoBC,YAAY;AAChC,SAA4BC,
|
|
6
|
-
"names": ["effect", "inspectCustom", "failedInvariant", "invariant", "live", "
|
|
4
|
+
"sourcesContent": ["//\n// Copyright 2024 DXOS.org\n//\n\nimport { effect } from '@preact/signals-core';\n\nimport { inspectCustom } from '@dxos/debug';\nimport { failedInvariant, invariant } from '@dxos/invariant';\nimport { type Live, live } from '@dxos/live-object';\nimport { type MakeOptional, isTruthy, removeBy } from '@dxos/util';\n\nimport { type BaseGraphEdge, type BaseGraphNode, type Graph, type GraphEdge, type GraphNode } from './types';\nimport { createEdgeId } from './util';\n\n/**\n * Readonly Graph wrapper.\n */\nexport class ReadonlyGraphModel<\n Node extends BaseGraphNode = BaseGraphNode,\n Edge extends BaseGraphEdge = BaseGraphEdge,\n> {\n protected readonly _graph: Graph;\n\n /**\n * NOTE: Pass in simple Graph or Live.\n */\n constructor(graph?: Graph) {\n this._graph = graph ?? {\n nodes: [],\n edges: [],\n };\n }\n\n [inspectCustom]() {\n return this.toJSON();\n }\n\n /**\n * Return stable sorted JSON representation of graph.\n */\n toJSON() {\n return {\n nodes: this.nodes.length,\n edges: this.edges.length,\n };\n }\n\n get graph(): Graph {\n return this._graph;\n }\n\n get nodes(): Node[] {\n return this._graph.nodes as Node[];\n }\n\n get edges(): Edge[] {\n return this._graph.edges as Edge[];\n }\n\n //\n // Nodes\n //\n\n findNode(id: string): Node | undefined {\n return this.nodes.find((node) => node.id === id);\n }\n\n getNode(id: string): Node {\n return this.findNode(id) ?? failedInvariant();\n }\n\n filterNodes({ type }: Partial<GraphNode.Any> = {}): Node[] {\n return this.nodes.filter((node) => !type || type === node.type);\n }\n\n //\n // Edges\n //\n\n findEdge(id: string): Edge | undefined {\n return this.edges.find((edge) => edge.id === id);\n }\n\n getEdge(id: string): Edge {\n return this.findEdge(id) ?? failedInvariant();\n }\n\n filterEdges({ type, source, target }: Partial<GraphEdge> = {}): Edge[] {\n return this.edges.filter(\n (edge) =>\n (!type || type === edge.type) && (!source || source === edge.source) && (!target || target === edge.target),\n );\n }\n\n //\n // Traverse\n //\n\n traverse(root: Node): Node[] {\n return this._traverse(root);\n }\n\n private _traverse(root: Node, visited: Set<string> = new Set()): Node[] {\n if (visited.has(root.id)) {\n return [];\n }\n\n visited.add(root.id);\n const targets = this.filterEdges({ source: root.id })\n .map((edge) => this.getNode(edge.target))\n .filter(isTruthy);\n\n return [root, ...targets.flatMap((target) => this._traverse(target, visited))];\n }\n}\n\n/**\n * Mutable Graph wrapper.\n */\nexport abstract class AbstractGraphModel<\n Node extends BaseGraphNode = BaseGraphNode,\n Edge extends BaseGraphEdge = BaseGraphEdge,\n Model extends AbstractGraphModel<Node, Edge, Model, Builder> = any,\n Builder extends AbstractGraphBuilder<Node, Edge, Model> = AbstractGraphBuilder<Node, Edge, Model>,\n> extends ReadonlyGraphModel<Node, Edge> {\n /**\n * Allows chaining.\n */\n abstract get builder(): Builder;\n\n /**\n * Shallow copy of provided graph.\n */\n abstract copy(graph?: Partial<Graph>): Model;\n\n clear(): this {\n this._graph.nodes.length = 0;\n this._graph.edges.length = 0;\n return this;\n }\n\n addGraph(graph: Model): this {\n this.addNodes(graph.nodes);\n this.addEdges(graph.edges);\n return this;\n }\n\n addGraphs(graphs: Model[]): this {\n graphs.forEach((graph) => {\n this.addNodes(graph.nodes);\n this.addEdges(graph.edges);\n });\n return this;\n }\n\n addNode(node: Node): Node {\n invariant(node.id, 'ID is required');\n invariant(!this.findNode(node.id), `node already exists: ${node.id}`);\n this._graph.nodes.push(node);\n return node;\n }\n\n addNodes(nodes: Node[]): Node[] {\n return nodes.map((node) => this.addNode(node));\n }\n\n addEdge(edge: MakeOptional<Edge, 'id'>): Edge {\n invariant(edge.source);\n invariant(edge.target);\n if (!edge.id) {\n // TODO(burdon): Generate random id.\n edge = { id: createEdgeId(edge), ...edge };\n }\n invariant(!this.findNode(edge.id!));\n this._graph.edges.push(edge as Edge);\n return edge as Edge;\n }\n\n addEdges(edges: Edge[]): Edge[] {\n return edges.map((edge) => this.addEdge(edge));\n }\n\n removeNode(id: string): Model {\n const edges = removeBy<Edge>(this._graph.edges as Edge[], (edge) => edge.source === id || edge.target === id);\n const nodes = removeBy<Node>(this._graph.nodes as Node[], (node) => node.id === id);\n return this.copy({ nodes, edges });\n }\n\n removeNodes(ids: string[]): Model {\n const graphs = ids.map((id) => this.removeNode(id));\n return this.copy().addGraphs(graphs);\n }\n\n removeEdge(id: string): Model {\n const edges = removeBy<Edge>(this._graph.edges as Edge[], (edge) => edge.id === id);\n return this.copy({ nodes: [], edges });\n }\n\n removeEdges(ids: string[]): Model {\n const graphs = ids.map((id) => this.removeEdge(id));\n return this.copy().addGraphs(graphs);\n }\n}\n\n/**\n * Chainable builder wrapper\n */\nexport abstract class AbstractGraphBuilder<\n Node extends BaseGraphNode,\n Edge extends BaseGraphEdge,\n Model extends GraphModel<Node, Edge>,\n> {\n constructor(protected readonly _model: Model) {}\n\n get model(): Model {\n return this._model;\n }\n\n call(cb: (builder: this) => void): this {\n cb(this);\n return this;\n }\n\n getNode(id: string): Node {\n return this.model.getNode(id);\n }\n\n addNode(node: Node): this {\n this._model.addNode(node);\n return this;\n }\n\n addEdge(edge: MakeOptional<Edge, 'id'>): this {\n this._model.addEdge(edge);\n return this;\n }\n\n addNodes(nodes: Node[]): this {\n this._model.addNodes(nodes);\n return this;\n }\n\n addEdges(edges: Edge[]): this {\n this._model.addEdges(edges);\n return this;\n }\n}\n\n/**\n * Basic model.\n */\nexport class GraphModel<\n Node extends BaseGraphNode = BaseGraphNode,\n Edge extends BaseGraphEdge = BaseGraphEdge,\n> extends AbstractGraphModel<Node, Edge, GraphModel<Node, Edge>, GraphBuilder<Node, Edge>> {\n override get builder() {\n return new GraphBuilder<Node, Edge>(this);\n }\n\n override copy(graph?: Partial<Graph>): GraphModel<Node, Edge> {\n return new GraphModel<Node, Edge>({ nodes: graph?.nodes ?? [], edges: graph?.edges ?? [] });\n }\n}\n\nexport type GraphModelSubscription = (model: GraphModel, graph: Live<Graph>) => void;\n\n/**\n * Subscription.\n * NOTE: Requires `registerSignalsRuntime` to be called.\n */\nexport const subscribe = (model: GraphModel, cb: GraphModelSubscription, fire = false) => {\n if (fire) {\n cb(model, model.graph);\n }\n\n return effect(() => {\n cb(model, model.graph); // TODO(burdon): This won't work unless model.graph is reactive.\n });\n};\n\n/**\n * Basic reactive model.\n */\nexport class ReactiveGraphModel<\n Node extends BaseGraphNode = BaseGraphNode,\n Edge extends BaseGraphEdge = BaseGraphEdge,\n> extends GraphModel<Node, Edge> {\n constructor(graph?: Partial<Graph>) {\n super(\n live({\n nodes: graph?.nodes ?? [],\n edges: graph?.edges ?? [],\n }),\n );\n }\n\n override copy(graph?: Partial<Graph>): ReactiveGraphModel<Node, Edge> {\n return new ReactiveGraphModel<Node, Edge>(graph);\n }\n\n subscribe(cb: GraphModelSubscription, fire = false): () => void {\n return subscribe(this, cb, fire);\n }\n}\n\n/**\n * Basic builder.\n */\nexport class GraphBuilder<\n Node extends BaseGraphNode = BaseGraphNode,\n Edge extends BaseGraphEdge = BaseGraphEdge,\n> extends AbstractGraphBuilder<Node, Edge, GraphModel<Node, Edge>> {\n override call(cb: (builder: this) => void): this {\n cb(this);\n return this;\n }\n}\n", "//\n// Copyright 2024 DXOS.org\n//\n\nimport { invariant } from '@dxos/invariant';\n\nconst KEY_REGEX = /\\w+/;\n\n// NOTE: The `relation` is different from the `type`.\ntype EdgeMeta = { source: string; target: string; relation?: string };\n\nexport const createEdgeId = ({ source, target, relation }: EdgeMeta) => {\n invariant(source.match(KEY_REGEX), `invalid source: ${source}`);\n invariant(target.match(KEY_REGEX), `invalid target: ${target}`);\n return [source, relation, target].join('_');\n};\n\nexport const parseEdgeId = (id: string): EdgeMeta => {\n const [source, relation, target] = id.split('_');\n invariant(source.length && target.length);\n return { source, relation: relation.length ? relation : undefined, target };\n};\n", "//\n// Copyright 2024 DXOS.org\n//\n\nimport { type ReadonlySignal, type Signal, computed, signal } from '@preact/signals-core';\n\nimport { invariant } from '@dxos/invariant';\n\n/**\n * Reactive selection model.\n */\nexport class SelectionModel {\n private readonly _selected: Signal<Set<string>> = signal(new Set<string>());\n private readonly _selectedIds = computed(() => Array.from(this._selected.value.values()));\n\n constructor(private readonly _singleSelect: boolean = false) {}\n\n toJSON(): { selected: string[] } {\n return {\n selected: Array.from(this._selected.value.values()),\n };\n }\n\n get size(): number {\n return this._selected.value.size;\n }\n\n get selected(): ReadonlySignal<string[]> {\n return this._selectedIds;\n }\n\n contains(id: string): boolean {\n return this._selected.value.has(id);\n }\n\n clear(): void {\n this._selected.value = new Set();\n }\n\n add(id: string): void {\n invariant(id);\n this._selected.value = new Set<string>(\n this._singleSelect ? [id] : [...Array.from(this._selected.value.values()), id],\n );\n }\n\n remove(id: string): void {\n invariant(id);\n this._selected.value = new Set<string>(Array.from(this._selected.value.values()).filter((_id) => _id !== id));\n }\n\n // TODO(burdon): Handle single select.\n\n setSelected(ids: string[], shift = false): void {\n this._selected.value = new Set([...(shift ? Array.from(this._selected.value.values()) : []), ...ids]);\n }\n\n toggleSelected(ids: string[], shift = false): void {\n const set = new Set<string>(shift ? Array.from(this._selected.value.values()) : undefined);\n ids.forEach((id) => {\n if (this._selected.value.has(id)) {\n set.delete(id);\n } else {\n set.add(id);\n }\n });\n\n this._selected.value = set;\n }\n}\n", "//\n// Copyright 2024 DXOS.org\n//\n\nimport * as Schema from 'effect/Schema';\n\nimport { type Specialize } from '@dxos/util';\n\n//\n// Node\n//\n\n// TODO(burdon): Make type extensible (i.e., not dependent on `data` property)?\nexport const BaseGraphNode = Schema.Struct({\n id: Schema.String,\n type: Schema.optional(Schema.String),\n data: Schema.optional(Schema.Any),\n});\n\n/** Raw base type. */\nexport type BaseGraphNode = Schema.Schema.Type<typeof BaseGraphNode>;\n\n/** Typed node data. */\ntype GraphNode<Data = any, Optional extends boolean = false> = Specialize<\n BaseGraphNode,\n Optional extends true ? { data?: Data } : { data: Data }\n>;\n\nexport declare namespace GraphNode {\n export type Any = GraphNode<any, true>;\n export type Optional<Data = any> = GraphNode<Data, true>;\n export type Required<Data = any> = GraphNode<Data, false>;\n}\n\n//\n// Edge\n//\n\nexport const BaseGraphEdge = Schema.Struct({\n id: Schema.String,\n type: Schema.optional(Schema.String),\n source: Schema.String,\n target: Schema.String,\n data: Schema.optional(Schema.Any),\n});\n\n/** Raw base type. */\nexport type BaseGraphEdge = Schema.Schema.Type<typeof BaseGraphEdge>;\n\n/** Typed edge data. */\nexport type GraphEdge<Data = any, Optional extends boolean = false> = Specialize<\n BaseGraphEdge,\n Optional extends true ? { data?: Data } : { data: Data }\n>;\n\nexport declare namespace GraphEdge {\n export type Any = GraphEdge<any, true>;\n export type Optional<Data = any> = GraphEdge<Data, true>;\n export type Required<Data = any> = GraphEdge<Data, false>;\n}\n\n//\n// Graph\n//\n\nexport const Graph = Schema.Struct({\n id: Schema.optional(Schema.String),\n nodes: Schema.mutable(Schema.Array(BaseGraphNode)),\n edges: Schema.mutable(Schema.Array(BaseGraphEdge)),\n});\n\nexport type Graph = Schema.Schema.Type<typeof Graph>;\n"],
|
|
5
|
+
"mappings": ";AAIA,SAASA,cAAc;AAEvB,SAASC,qBAAqB;AAC9B,SAAS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A;AAqBO,IAAMC,gBAAuBP,cAAO;EACzCC,IAAWC;EACXC,MAAaC,gBAAgBF,aAAM;EACnCM,QAAeN;EACfO,QAAeP;EACfG,MAAaD,gBAAgBE,UAAG;AAClC,CAAA;AAqBO,IAAMI,QAAeV,cAAO;EACjCC,IAAWG,gBAAgBF,aAAM;EACjCS,OAAcC,eAAeC,aAAMd,aAAAA,CAAAA;EACnCe,OAAcF,eAAeC,aAAMN,aAAAA,CAAAA;AACrC,CAAA;",
|
|
6
|
+
"names": ["effect", "inspectCustom", "failedInvariant", "invariant", "live", "isTruthy", "removeBy", "invariant", "KEY_REGEX", "createEdgeId", "source", "target", "relation", "match", "join", "parseEdgeId", "id", "split", "length", "undefined", "ReadonlyGraphModel", "_graph", "graph", "nodes", "edges", "inspectCustom", "toJSON", "length", "findNode", "id", "find", "node", "getNode", "failedInvariant", "filterNodes", "type", "filter", "findEdge", "edge", "getEdge", "filterEdges", "source", "target", "traverse", "root", "_traverse", "visited", "Set", "has", "add", "targets", "map", "isTruthy", "flatMap", "AbstractGraphModel", "clear", "addGraph", "addNodes", "addEdges", "addGraphs", "graphs", "forEach", "addNode", "invariant", "push", "addEdge", "createEdgeId", "removeNode", "removeBy", "copy", "removeNodes", "ids", "removeEdge", "removeEdges", "AbstractGraphBuilder", "_model", "model", "call", "cb", "GraphModel", "builder", "GraphBuilder", "subscribe", "fire", "effect", "ReactiveGraphModel", "live", "computed", "signal", "invariant", "SelectionModel", "_selected", "Set", "_selectedIds", "Array", "from", "value", "values", "_singleSelect", "toJSON", "selected", "size", "contains", "id", "has", "clear", "add", "remove", "filter", "_id", "setSelected", "ids", "shift", "toggleSelected", "set", "undefined", "forEach", "delete", "Schema", "BaseGraphNode", "Struct", "id", "String", "type", "optional", "data", "Any", "BaseGraphEdge", "source", "target", "Graph", "nodes", "mutable", "Array", "edges"]
|
|
7
7
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"inputs":{"src/util.ts":{"bytes":3257,"imports":[{"path":"@dxos/invariant","kind":"import-statement","external":true}],"format":"esm"},"src/model.ts":{"bytes":
|
|
1
|
+
{"inputs":{"src/util.ts":{"bytes":3257,"imports":[{"path":"@dxos/invariant","kind":"import-statement","external":true}],"format":"esm"},"src/model.ts":{"bytes":25651,"imports":[{"path":"@preact/signals-core","kind":"import-statement","external":true},{"path":"@dxos/debug","kind":"import-statement","external":true},{"path":"@dxos/invariant","kind":"import-statement","external":true},{"path":"@dxos/live-object","kind":"import-statement","external":true},{"path":"@dxos/util","kind":"import-statement","external":true},{"path":"src/util.ts","kind":"import-statement","original":"./util"}],"format":"esm"},"src/selection.ts":{"bytes":7321,"imports":[{"path":"@preact/signals-core","kind":"import-statement","external":true},{"path":"@dxos/invariant","kind":"import-statement","external":true}],"format":"esm"},"src/types.ts":{"bytes":4622,"imports":[{"path":"effect/Schema","kind":"import-statement","external":true}],"format":"esm"},"src/index.ts":{"bytes":691,"imports":[{"path":"src/model.ts","kind":"import-statement","original":"./model"},{"path":"src/selection.ts","kind":"import-statement","original":"./selection"},{"path":"src/types.ts","kind":"import-statement","original":"./types"},{"path":"src/util.ts","kind":"import-statement","original":"./util"}],"format":"esm"}},"outputs":{"dist/lib/browser/index.mjs.map":{"imports":[],"exports":[],"inputs":{},"bytes":20657},"dist/lib/browser/index.mjs":{"imports":[{"path":"@preact/signals-core","kind":"import-statement","external":true},{"path":"@dxos/debug","kind":"import-statement","external":true},{"path":"@dxos/invariant","kind":"import-statement","external":true},{"path":"@dxos/live-object","kind":"import-statement","external":true},{"path":"@dxos/util","kind":"import-statement","external":true},{"path":"@dxos/invariant","kind":"import-statement","external":true},{"path":"@preact/signals-core","kind":"import-statement","external":true},{"path":"@dxos/invariant","kind":"import-statement","external":true},{"path":"effect/Schema","kind":"import-statement","external":true}],"exports":["AbstractGraphBuilder","AbstractGraphModel","BaseGraphEdge","BaseGraphNode","Graph","GraphBuilder","GraphModel","ReactiveGraphModel","ReadonlyGraphModel","SelectionModel","createEdgeId","parseEdgeId","subscribe"],"entryPoint":"src/index.ts","inputs":{"src/model.ts":{"bytesInOutput":5708},"src/util.ts":{"bytesInOutput":1014},"src/index.ts":{"bytesInOutput":0},"src/selection.ts":{"bytesInOutput":1865},"src/types.ts":{"bytesInOutput":542}},"bytes":9487}}}
|
|
@@ -5,7 +5,7 @@ import { effect } from "@preact/signals-core";
|
|
|
5
5
|
import { inspectCustom } from "@dxos/debug";
|
|
6
6
|
import { failedInvariant, invariant as invariant2 } from "@dxos/invariant";
|
|
7
7
|
import { live } from "@dxos/live-object";
|
|
8
|
-
import {
|
|
8
|
+
import { isTruthy, removeBy } from "@dxos/util";
|
|
9
9
|
|
|
10
10
|
// src/util.ts
|
|
11
11
|
import { invariant } from "@dxos/invariant";
|
|
@@ -55,23 +55,19 @@ var parseEdgeId = (id) => {
|
|
|
55
55
|
};
|
|
56
56
|
|
|
57
57
|
// src/model.ts
|
|
58
|
-
function _define_property(obj, key, value) {
|
|
59
|
-
if (key in obj) {
|
|
60
|
-
Object.defineProperty(obj, key, {
|
|
61
|
-
value,
|
|
62
|
-
enumerable: true,
|
|
63
|
-
configurable: true,
|
|
64
|
-
writable: true
|
|
65
|
-
});
|
|
66
|
-
} else {
|
|
67
|
-
obj[key] = value;
|
|
68
|
-
}
|
|
69
|
-
return obj;
|
|
70
|
-
}
|
|
71
58
|
var __dxlog_file2 = "/__w/dxos/dxos/packages/common/graph/src/model.ts";
|
|
72
|
-
var _inspectCustom = inspectCustom;
|
|
73
59
|
var ReadonlyGraphModel = class {
|
|
74
|
-
|
|
60
|
+
_graph;
|
|
61
|
+
/**
|
|
62
|
+
* NOTE: Pass in simple Graph or Live.
|
|
63
|
+
*/
|
|
64
|
+
constructor(graph) {
|
|
65
|
+
this._graph = graph ?? {
|
|
66
|
+
nodes: [],
|
|
67
|
+
edges: []
|
|
68
|
+
};
|
|
69
|
+
}
|
|
70
|
+
[inspectCustom]() {
|
|
75
71
|
return this.toJSON();
|
|
76
72
|
}
|
|
77
73
|
/**
|
|
@@ -129,22 +125,12 @@ var ReadonlyGraphModel = class {
|
|
|
129
125
|
visited.add(root.id);
|
|
130
126
|
const targets = this.filterEdges({
|
|
131
127
|
source: root.id
|
|
132
|
-
}).map((edge) => this.getNode(edge.target)).filter(
|
|
128
|
+
}).map((edge) => this.getNode(edge.target)).filter(isTruthy);
|
|
133
129
|
return [
|
|
134
130
|
root,
|
|
135
131
|
...targets.flatMap((target) => this._traverse(target, visited))
|
|
136
132
|
];
|
|
137
133
|
}
|
|
138
|
-
/**
|
|
139
|
-
* NOTE: Pass in simple Graph or Live.
|
|
140
|
-
*/
|
|
141
|
-
constructor(graph) {
|
|
142
|
-
_define_property(this, "_graph", void 0);
|
|
143
|
-
this._graph = graph ?? {
|
|
144
|
-
nodes: [],
|
|
145
|
-
edges: []
|
|
146
|
-
};
|
|
147
|
-
}
|
|
148
134
|
};
|
|
149
135
|
var AbstractGraphModel = class extends ReadonlyGraphModel {
|
|
150
136
|
clear() {
|
|
@@ -254,6 +240,10 @@ var AbstractGraphModel = class extends ReadonlyGraphModel {
|
|
|
254
240
|
}
|
|
255
241
|
};
|
|
256
242
|
var AbstractGraphBuilder = class {
|
|
243
|
+
_model;
|
|
244
|
+
constructor(_model) {
|
|
245
|
+
this._model = _model;
|
|
246
|
+
}
|
|
257
247
|
get model() {
|
|
258
248
|
return this._model;
|
|
259
249
|
}
|
|
@@ -280,10 +270,6 @@ var AbstractGraphBuilder = class {
|
|
|
280
270
|
this._model.addEdges(edges);
|
|
281
271
|
return this;
|
|
282
272
|
}
|
|
283
|
-
constructor(_model) {
|
|
284
|
-
_define_property(this, "_model", void 0);
|
|
285
|
-
this._model = _model;
|
|
286
|
-
}
|
|
287
273
|
};
|
|
288
274
|
var GraphModel = class _GraphModel extends AbstractGraphModel {
|
|
289
275
|
get builder() {
|
|
@@ -305,18 +291,18 @@ var subscribe = (model, cb, fire = false) => {
|
|
|
305
291
|
});
|
|
306
292
|
};
|
|
307
293
|
var ReactiveGraphModel = class _ReactiveGraphModel extends GraphModel {
|
|
308
|
-
copy(graph) {
|
|
309
|
-
return new _ReactiveGraphModel(graph);
|
|
310
|
-
}
|
|
311
|
-
subscribe(cb, fire = false) {
|
|
312
|
-
return subscribe(this, cb, fire);
|
|
313
|
-
}
|
|
314
294
|
constructor(graph) {
|
|
315
295
|
super(live({
|
|
316
296
|
nodes: graph?.nodes ?? [],
|
|
317
297
|
edges: graph?.edges ?? []
|
|
318
298
|
}));
|
|
319
299
|
}
|
|
300
|
+
copy(graph) {
|
|
301
|
+
return new _ReactiveGraphModel(graph);
|
|
302
|
+
}
|
|
303
|
+
subscribe(cb, fire = false) {
|
|
304
|
+
return subscribe(this, cb, fire);
|
|
305
|
+
}
|
|
320
306
|
};
|
|
321
307
|
var GraphBuilder = class extends AbstractGraphBuilder {
|
|
322
308
|
call(cb) {
|
|
@@ -328,21 +314,14 @@ var GraphBuilder = class extends AbstractGraphBuilder {
|
|
|
328
314
|
// src/selection.ts
|
|
329
315
|
import { computed, signal } from "@preact/signals-core";
|
|
330
316
|
import { invariant as invariant3 } from "@dxos/invariant";
|
|
331
|
-
function _define_property2(obj, key, value) {
|
|
332
|
-
if (key in obj) {
|
|
333
|
-
Object.defineProperty(obj, key, {
|
|
334
|
-
value,
|
|
335
|
-
enumerable: true,
|
|
336
|
-
configurable: true,
|
|
337
|
-
writable: true
|
|
338
|
-
});
|
|
339
|
-
} else {
|
|
340
|
-
obj[key] = value;
|
|
341
|
-
}
|
|
342
|
-
return obj;
|
|
343
|
-
}
|
|
344
317
|
var __dxlog_file3 = "/__w/dxos/dxos/packages/common/graph/src/selection.ts";
|
|
345
318
|
var SelectionModel = class {
|
|
319
|
+
_singleSelect;
|
|
320
|
+
_selected = signal(/* @__PURE__ */ new Set());
|
|
321
|
+
_selectedIds = computed(() => Array.from(this._selected.value.values()));
|
|
322
|
+
constructor(_singleSelect = false) {
|
|
323
|
+
this._singleSelect = _singleSelect;
|
|
324
|
+
}
|
|
346
325
|
toJSON() {
|
|
347
326
|
return {
|
|
348
327
|
selected: Array.from(this._selected.value.values())
|
|
@@ -407,18 +386,10 @@ var SelectionModel = class {
|
|
|
407
386
|
});
|
|
408
387
|
this._selected.value = set;
|
|
409
388
|
}
|
|
410
|
-
constructor(_singleSelect = false) {
|
|
411
|
-
_define_property2(this, "_singleSelect", void 0);
|
|
412
|
-
_define_property2(this, "_selected", void 0);
|
|
413
|
-
_define_property2(this, "_selectedIds", void 0);
|
|
414
|
-
this._singleSelect = _singleSelect;
|
|
415
|
-
this._selected = signal(/* @__PURE__ */ new Set());
|
|
416
|
-
this._selectedIds = computed(() => Array.from(this._selected.value.values()));
|
|
417
|
-
}
|
|
418
389
|
};
|
|
419
390
|
|
|
420
391
|
// src/types.ts
|
|
421
|
-
import
|
|
392
|
+
import * as Schema from "effect/Schema";
|
|
422
393
|
var BaseGraphNode = Schema.Struct({
|
|
423
394
|
id: Schema.String,
|
|
424
395
|
type: Schema.optional(Schema.String),
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
|
3
3
|
"sources": ["../../../src/model.ts", "../../../src/util.ts", "../../../src/selection.ts", "../../../src/types.ts"],
|
|
4
|
-
"sourcesContent": ["//\n// Copyright 2024 DXOS.org\n//\n\nimport { effect } from '@preact/signals-core';\n\nimport { inspectCustom } from '@dxos/debug';\nimport { failedInvariant, invariant } from '@dxos/invariant';\nimport { type Live, live } from '@dxos/live-object';\nimport { type MakeOptional, isNotFalsy, removeBy } from '@dxos/util';\n\nimport { type BaseGraphEdge, type BaseGraphNode, type Graph, type GraphEdge, type GraphNode } from './types';\nimport { createEdgeId } from './util';\n\n/**\n * Readonly Graph wrapper.\n */\nexport class ReadonlyGraphModel<\n Node extends BaseGraphNode = BaseGraphNode,\n Edge extends BaseGraphEdge = BaseGraphEdge,\n> {\n protected readonly _graph: Graph;\n\n /**\n * NOTE: Pass in simple Graph or Live.\n */\n constructor(graph?: Graph) {\n this._graph = graph ?? {\n nodes: [],\n edges: [],\n };\n }\n\n [inspectCustom]() {\n return this.toJSON();\n }\n\n /**\n * Return stable sorted JSON representation of graph.\n */\n toJSON() {\n return {\n nodes: this.nodes.length,\n edges: this.edges.length,\n };\n }\n\n get graph(): Graph {\n return this._graph;\n }\n\n get nodes(): Node[] {\n return this._graph.nodes as Node[];\n }\n\n get edges(): Edge[] {\n return this._graph.edges as Edge[];\n }\n\n //\n // Nodes\n //\n\n findNode(id: string): Node | undefined {\n return this.nodes.find((node) => node.id === id);\n }\n\n getNode(id: string): Node {\n return this.findNode(id) ?? failedInvariant();\n }\n\n filterNodes({ type }: Partial<GraphNode.Any> = {}): Node[] {\n return this.nodes.filter((node) => !type || type === node.type);\n }\n\n //\n // Edges\n //\n\n findEdge(id: string): Edge | undefined {\n return this.edges.find((edge) => edge.id === id);\n }\n\n getEdge(id: string): Edge {\n return this.findEdge(id) ?? failedInvariant();\n }\n\n filterEdges({ type, source, target }: Partial<GraphEdge> = {}): Edge[] {\n return this.edges.filter(\n (edge) =>\n (!type || type === edge.type) && (!source || source === edge.source) && (!target || target === edge.target),\n );\n }\n\n //\n // Traverse\n //\n\n traverse(root: Node): Node[] {\n return this._traverse(root);\n }\n\n private _traverse(root: Node, visited: Set<string> = new Set()): Node[] {\n if (visited.has(root.id)) {\n return [];\n }\n\n visited.add(root.id);\n const targets = this.filterEdges({ source: root.id })\n .map((edge) => this.getNode(edge.target))\n .filter(isNotFalsy);\n\n return [root, ...targets.flatMap((target) => this._traverse(target, visited))];\n }\n}\n\n/**\n * Mutable Graph wrapper.\n */\nexport abstract class AbstractGraphModel<\n Node extends BaseGraphNode = BaseGraphNode,\n Edge extends BaseGraphEdge = BaseGraphEdge,\n Model extends AbstractGraphModel<Node, Edge, Model, Builder> = any,\n Builder extends AbstractGraphBuilder<Node, Edge, Model> = AbstractGraphBuilder<Node, Edge, Model>,\n> extends ReadonlyGraphModel<Node, Edge> {\n /**\n * Allows chaining.\n */\n abstract get builder(): Builder;\n\n /**\n * Shallow copy of provided graph.\n */\n abstract copy(graph?: Partial<Graph>): Model;\n\n clear(): this {\n this._graph.nodes.length = 0;\n this._graph.edges.length = 0;\n return this;\n }\n\n addGraph(graph: Model): this {\n this.addNodes(graph.nodes);\n this.addEdges(graph.edges);\n return this;\n }\n\n addGraphs(graphs: Model[]): this {\n graphs.forEach((graph) => {\n this.addNodes(graph.nodes);\n this.addEdges(graph.edges);\n });\n return this;\n }\n\n addNode(node: Node): Node {\n invariant(node.id, 'ID is required');\n invariant(!this.findNode(node.id), `node already exists: ${node.id}`);\n this._graph.nodes.push(node);\n return node;\n }\n\n addNodes(nodes: Node[]): Node[] {\n return nodes.map((node) => this.addNode(node));\n }\n\n addEdge(edge: MakeOptional<Edge, 'id'>): Edge {\n invariant(edge.source);\n invariant(edge.target);\n if (!edge.id) {\n // TODO(burdon): Generate random id.\n edge = { id: createEdgeId(edge), ...edge };\n }\n invariant(!this.findNode(edge.id!));\n this._graph.edges.push(edge as Edge);\n return edge as Edge;\n }\n\n addEdges(edges: Edge[]): Edge[] {\n return edges.map((edge) => this.addEdge(edge));\n }\n\n removeNode(id: string): Model {\n const edges = removeBy<Edge>(this._graph.edges as Edge[], (edge) => edge.source === id || edge.target === id);\n const nodes = removeBy<Node>(this._graph.nodes as Node[], (node) => node.id === id);\n return this.copy({ nodes, edges });\n }\n\n removeNodes(ids: string[]): Model {\n const graphs = ids.map((id) => this.removeNode(id));\n return this.copy().addGraphs(graphs);\n }\n\n removeEdge(id: string): Model {\n const edges = removeBy<Edge>(this._graph.edges as Edge[], (edge) => edge.id === id);\n return this.copy({ nodes: [], edges });\n }\n\n removeEdges(ids: string[]): Model {\n const graphs = ids.map((id) => this.removeEdge(id));\n return this.copy().addGraphs(graphs);\n }\n}\n\n/**\n * Chainable builder wrapper\n */\nexport abstract class AbstractGraphBuilder<\n Node extends BaseGraphNode,\n Edge extends BaseGraphEdge,\n Model extends GraphModel<Node, Edge>,\n> {\n constructor(protected readonly _model: Model) {}\n\n get model(): Model {\n return this._model;\n }\n\n call(cb: (builder: this) => void): this {\n cb(this);\n return this;\n }\n\n getNode(id: string): Node {\n return this.model.getNode(id);\n }\n\n addNode(node: Node): this {\n this._model.addNode(node);\n return this;\n }\n\n addEdge(edge: MakeOptional<Edge, 'id'>): this {\n this._model.addEdge(edge);\n return this;\n }\n\n addNodes(nodes: Node[]): this {\n this._model.addNodes(nodes);\n return this;\n }\n\n addEdges(edges: Edge[]): this {\n this._model.addEdges(edges);\n return this;\n }\n}\n\n/**\n * Basic model.\n */\nexport class GraphModel<\n Node extends BaseGraphNode = BaseGraphNode,\n Edge extends BaseGraphEdge = BaseGraphEdge,\n> extends AbstractGraphModel<Node, Edge, GraphModel<Node, Edge>, GraphBuilder<Node, Edge>> {\n override get builder() {\n return new GraphBuilder<Node, Edge>(this);\n }\n\n override copy(graph?: Partial<Graph>): GraphModel<Node, Edge> {\n return new GraphModel<Node, Edge>({ nodes: graph?.nodes ?? [], edges: graph?.edges ?? [] });\n }\n}\n\nexport type GraphModelSubscription = (model: GraphModel, graph: Live<Graph>) => void;\n\n/**\n * Subscription.\n * NOTE: Requires `registerSignalsRuntime` to be called.\n */\nexport const subscribe = (model: GraphModel, cb: GraphModelSubscription, fire = false) => {\n if (fire) {\n cb(model, model.graph);\n }\n\n return effect(() => {\n cb(model, model.graph); // TODO(burdon): This won't work unless model.graph is reactive.\n });\n};\n\n/**\n * Basic reactive model.\n */\nexport class ReactiveGraphModel<\n Node extends BaseGraphNode = BaseGraphNode,\n Edge extends BaseGraphEdge = BaseGraphEdge,\n> extends GraphModel<Node, Edge> {\n constructor(graph?: Partial<Graph>) {\n super(\n live({\n nodes: graph?.nodes ?? [],\n edges: graph?.edges ?? [],\n }),\n );\n }\n\n override copy(graph?: Partial<Graph>): ReactiveGraphModel<Node, Edge> {\n return new ReactiveGraphModel<Node, Edge>(graph);\n }\n\n subscribe(cb: GraphModelSubscription, fire = false): () => void {\n return subscribe(this, cb, fire);\n }\n}\n\n/**\n * Basic builder.\n */\nexport class GraphBuilder<\n Node extends BaseGraphNode = BaseGraphNode,\n Edge extends BaseGraphEdge = BaseGraphEdge,\n> extends AbstractGraphBuilder<Node, Edge, GraphModel<Node, Edge>> {\n override call(cb: (builder: this) => void): this {\n cb(this);\n return this;\n }\n}\n", "//\n// Copyright 2024 DXOS.org\n//\n\nimport { invariant } from '@dxos/invariant';\n\nconst KEY_REGEX = /\\w+/;\n\n// NOTE: The `relation` is different from the `type`.\ntype EdgeMeta = { source: string; target: string; relation?: string };\n\nexport const createEdgeId = ({ source, target, relation }: EdgeMeta) => {\n invariant(source.match(KEY_REGEX), `invalid source: ${source}`);\n invariant(target.match(KEY_REGEX), `invalid target: ${target}`);\n return [source, relation, target].join('_');\n};\n\nexport const parseEdgeId = (id: string): EdgeMeta => {\n const [source, relation, target] = id.split('_');\n invariant(source.length && target.length);\n return { source, relation: relation.length ? relation : undefined, target };\n};\n", "//\n// Copyright 2024 DXOS.org\n//\n\nimport { type ReadonlySignal, type Signal, computed, signal } from '@preact/signals-core';\n\nimport { invariant } from '@dxos/invariant';\n\n/**\n * Reactive selection model.\n */\nexport class SelectionModel {\n private readonly _selected: Signal<Set<string>> = signal(new Set<string>());\n private readonly _selectedIds = computed(() => Array.from(this._selected.value.values()));\n\n constructor(private readonly _singleSelect: boolean = false) {}\n\n toJSON(): { selected: string[] } {\n return {\n selected: Array.from(this._selected.value.values()),\n };\n }\n\n get size(): number {\n return this._selected.value.size;\n }\n\n get selected(): ReadonlySignal<string[]> {\n return this._selectedIds;\n }\n\n contains(id: string): boolean {\n return this._selected.value.has(id);\n }\n\n clear(): void {\n this._selected.value = new Set();\n }\n\n add(id: string): void {\n invariant(id);\n this._selected.value = new Set<string>(\n this._singleSelect ? [id] : [...Array.from(this._selected.value.values()), id],\n );\n }\n\n remove(id: string): void {\n invariant(id);\n this._selected.value = new Set<string>(Array.from(this._selected.value.values()).filter((_id) => _id !== id));\n }\n\n // TODO(burdon): Handle single select.\n\n setSelected(ids: string[], shift = false): void {\n this._selected.value = new Set([...(shift ? Array.from(this._selected.value.values()) : []), ...ids]);\n }\n\n toggleSelected(ids: string[], shift = false): void {\n const set = new Set<string>(shift ? Array.from(this._selected.value.values()) : undefined);\n ids.forEach((id) => {\n if (this._selected.value.has(id)) {\n set.delete(id);\n } else {\n set.add(id);\n }\n });\n\n this._selected.value = set;\n }\n}\n", "//\n// Copyright 2024 DXOS.org\n//\n\nimport { Schema } from 'effect';\n\nimport { type Specialize } from '@dxos/util';\n\n//\n// Node\n//\n\n// TODO(burdon): Make type extensible (i.e., not dependent on `data` property)?\nexport const BaseGraphNode = Schema.Struct({\n id: Schema.String,\n type: Schema.optional(Schema.String),\n data: Schema.optional(Schema.Any),\n});\n\n/** Raw base type. */\nexport type BaseGraphNode = Schema.Schema.Type<typeof BaseGraphNode>;\n\n/** Typed node data. */\ntype GraphNode<Data = any, Optional extends boolean = false> = Specialize<\n BaseGraphNode,\n Optional extends true ? { data?: Data } : { data: Data }\n>;\n\nexport declare namespace GraphNode {\n export type Any = GraphNode<any, true>;\n export type Optional<Data = any> = GraphNode<Data, true>;\n export type Required<Data = any> = GraphNode<Data, false>;\n}\n\n//\n// Edge\n//\n\nexport const BaseGraphEdge = Schema.Struct({\n id: Schema.String,\n type: Schema.optional(Schema.String),\n source: Schema.String,\n target: Schema.String,\n data: Schema.optional(Schema.Any),\n});\n\n/** Raw base type. */\nexport type BaseGraphEdge = Schema.Schema.Type<typeof BaseGraphEdge>;\n\n/** Typed edge data. */\nexport type GraphEdge<Data = any, Optional extends boolean = false> = Specialize<\n BaseGraphEdge,\n Optional extends true ? { data?: Data } : { data: Data }\n>;\n\nexport declare namespace GraphEdge {\n export type Any = GraphEdge<any, true>;\n export type Optional<Data = any> = GraphEdge<Data, true>;\n export type Required<Data = any> = GraphEdge<Data, false>;\n}\n\n//\n// Graph\n//\n\nexport const Graph = Schema.Struct({\n id: Schema.optional(Schema.String),\n nodes: Schema.mutable(Schema.Array(BaseGraphNode)),\n edges: Schema.mutable(Schema.Array(BaseGraphEdge)),\n});\n\nexport type Graph = Schema.Schema.Type<typeof Graph>;\n"],
|
|
5
|
-
"mappings": ";;;AAIA,SAASA,cAAc;AAEvB,SAASC,qBAAqB;AAC9B,SAASC,iBAAiBC,aAAAA,kBAAiB;AAC3C,SAAoBC,YAAY;AAChC,SAA4BC,
|
|
6
|
-
"names": ["effect", "inspectCustom", "failedInvariant", "invariant", "live", "
|
|
4
|
+
"sourcesContent": ["//\n// Copyright 2024 DXOS.org\n//\n\nimport { effect } from '@preact/signals-core';\n\nimport { inspectCustom } from '@dxos/debug';\nimport { failedInvariant, invariant } from '@dxos/invariant';\nimport { type Live, live } from '@dxos/live-object';\nimport { type MakeOptional, isTruthy, removeBy } from '@dxos/util';\n\nimport { type BaseGraphEdge, type BaseGraphNode, type Graph, type GraphEdge, type GraphNode } from './types';\nimport { createEdgeId } from './util';\n\n/**\n * Readonly Graph wrapper.\n */\nexport class ReadonlyGraphModel<\n Node extends BaseGraphNode = BaseGraphNode,\n Edge extends BaseGraphEdge = BaseGraphEdge,\n> {\n protected readonly _graph: Graph;\n\n /**\n * NOTE: Pass in simple Graph or Live.\n */\n constructor(graph?: Graph) {\n this._graph = graph ?? {\n nodes: [],\n edges: [],\n };\n }\n\n [inspectCustom]() {\n return this.toJSON();\n }\n\n /**\n * Return stable sorted JSON representation of graph.\n */\n toJSON() {\n return {\n nodes: this.nodes.length,\n edges: this.edges.length,\n };\n }\n\n get graph(): Graph {\n return this._graph;\n }\n\n get nodes(): Node[] {\n return this._graph.nodes as Node[];\n }\n\n get edges(): Edge[] {\n return this._graph.edges as Edge[];\n }\n\n //\n // Nodes\n //\n\n findNode(id: string): Node | undefined {\n return this.nodes.find((node) => node.id === id);\n }\n\n getNode(id: string): Node {\n return this.findNode(id) ?? failedInvariant();\n }\n\n filterNodes({ type }: Partial<GraphNode.Any> = {}): Node[] {\n return this.nodes.filter((node) => !type || type === node.type);\n }\n\n //\n // Edges\n //\n\n findEdge(id: string): Edge | undefined {\n return this.edges.find((edge) => edge.id === id);\n }\n\n getEdge(id: string): Edge {\n return this.findEdge(id) ?? failedInvariant();\n }\n\n filterEdges({ type, source, target }: Partial<GraphEdge> = {}): Edge[] {\n return this.edges.filter(\n (edge) =>\n (!type || type === edge.type) && (!source || source === edge.source) && (!target || target === edge.target),\n );\n }\n\n //\n // Traverse\n //\n\n traverse(root: Node): Node[] {\n return this._traverse(root);\n }\n\n private _traverse(root: Node, visited: Set<string> = new Set()): Node[] {\n if (visited.has(root.id)) {\n return [];\n }\n\n visited.add(root.id);\n const targets = this.filterEdges({ source: root.id })\n .map((edge) => this.getNode(edge.target))\n .filter(isTruthy);\n\n return [root, ...targets.flatMap((target) => this._traverse(target, visited))];\n }\n}\n\n/**\n * Mutable Graph wrapper.\n */\nexport abstract class AbstractGraphModel<\n Node extends BaseGraphNode = BaseGraphNode,\n Edge extends BaseGraphEdge = BaseGraphEdge,\n Model extends AbstractGraphModel<Node, Edge, Model, Builder> = any,\n Builder extends AbstractGraphBuilder<Node, Edge, Model> = AbstractGraphBuilder<Node, Edge, Model>,\n> extends ReadonlyGraphModel<Node, Edge> {\n /**\n * Allows chaining.\n */\n abstract get builder(): Builder;\n\n /**\n * Shallow copy of provided graph.\n */\n abstract copy(graph?: Partial<Graph>): Model;\n\n clear(): this {\n this._graph.nodes.length = 0;\n this._graph.edges.length = 0;\n return this;\n }\n\n addGraph(graph: Model): this {\n this.addNodes(graph.nodes);\n this.addEdges(graph.edges);\n return this;\n }\n\n addGraphs(graphs: Model[]): this {\n graphs.forEach((graph) => {\n this.addNodes(graph.nodes);\n this.addEdges(graph.edges);\n });\n return this;\n }\n\n addNode(node: Node): Node {\n invariant(node.id, 'ID is required');\n invariant(!this.findNode(node.id), `node already exists: ${node.id}`);\n this._graph.nodes.push(node);\n return node;\n }\n\n addNodes(nodes: Node[]): Node[] {\n return nodes.map((node) => this.addNode(node));\n }\n\n addEdge(edge: MakeOptional<Edge, 'id'>): Edge {\n invariant(edge.source);\n invariant(edge.target);\n if (!edge.id) {\n // TODO(burdon): Generate random id.\n edge = { id: createEdgeId(edge), ...edge };\n }\n invariant(!this.findNode(edge.id!));\n this._graph.edges.push(edge as Edge);\n return edge as Edge;\n }\n\n addEdges(edges: Edge[]): Edge[] {\n return edges.map((edge) => this.addEdge(edge));\n }\n\n removeNode(id: string): Model {\n const edges = removeBy<Edge>(this._graph.edges as Edge[], (edge) => edge.source === id || edge.target === id);\n const nodes = removeBy<Node>(this._graph.nodes as Node[], (node) => node.id === id);\n return this.copy({ nodes, edges });\n }\n\n removeNodes(ids: string[]): Model {\n const graphs = ids.map((id) => this.removeNode(id));\n return this.copy().addGraphs(graphs);\n }\n\n removeEdge(id: string): Model {\n const edges = removeBy<Edge>(this._graph.edges as Edge[], (edge) => edge.id === id);\n return this.copy({ nodes: [], edges });\n }\n\n removeEdges(ids: string[]): Model {\n const graphs = ids.map((id) => this.removeEdge(id));\n return this.copy().addGraphs(graphs);\n }\n}\n\n/**\n * Chainable builder wrapper\n */\nexport abstract class AbstractGraphBuilder<\n Node extends BaseGraphNode,\n Edge extends BaseGraphEdge,\n Model extends GraphModel<Node, Edge>,\n> {\n constructor(protected readonly _model: Model) {}\n\n get model(): Model {\n return this._model;\n }\n\n call(cb: (builder: this) => void): this {\n cb(this);\n return this;\n }\n\n getNode(id: string): Node {\n return this.model.getNode(id);\n }\n\n addNode(node: Node): this {\n this._model.addNode(node);\n return this;\n }\n\n addEdge(edge: MakeOptional<Edge, 'id'>): this {\n this._model.addEdge(edge);\n return this;\n }\n\n addNodes(nodes: Node[]): this {\n this._model.addNodes(nodes);\n return this;\n }\n\n addEdges(edges: Edge[]): this {\n this._model.addEdges(edges);\n return this;\n }\n}\n\n/**\n * Basic model.\n */\nexport class GraphModel<\n Node extends BaseGraphNode = BaseGraphNode,\n Edge extends BaseGraphEdge = BaseGraphEdge,\n> extends AbstractGraphModel<Node, Edge, GraphModel<Node, Edge>, GraphBuilder<Node, Edge>> {\n override get builder() {\n return new GraphBuilder<Node, Edge>(this);\n }\n\n override copy(graph?: Partial<Graph>): GraphModel<Node, Edge> {\n return new GraphModel<Node, Edge>({ nodes: graph?.nodes ?? [], edges: graph?.edges ?? [] });\n }\n}\n\nexport type GraphModelSubscription = (model: GraphModel, graph: Live<Graph>) => void;\n\n/**\n * Subscription.\n * NOTE: Requires `registerSignalsRuntime` to be called.\n */\nexport const subscribe = (model: GraphModel, cb: GraphModelSubscription, fire = false) => {\n if (fire) {\n cb(model, model.graph);\n }\n\n return effect(() => {\n cb(model, model.graph); // TODO(burdon): This won't work unless model.graph is reactive.\n });\n};\n\n/**\n * Basic reactive model.\n */\nexport class ReactiveGraphModel<\n Node extends BaseGraphNode = BaseGraphNode,\n Edge extends BaseGraphEdge = BaseGraphEdge,\n> extends GraphModel<Node, Edge> {\n constructor(graph?: Partial<Graph>) {\n super(\n live({\n nodes: graph?.nodes ?? [],\n edges: graph?.edges ?? [],\n }),\n );\n }\n\n override copy(graph?: Partial<Graph>): ReactiveGraphModel<Node, Edge> {\n return new ReactiveGraphModel<Node, Edge>(graph);\n }\n\n subscribe(cb: GraphModelSubscription, fire = false): () => void {\n return subscribe(this, cb, fire);\n }\n}\n\n/**\n * Basic builder.\n */\nexport class GraphBuilder<\n Node extends BaseGraphNode = BaseGraphNode,\n Edge extends BaseGraphEdge = BaseGraphEdge,\n> extends AbstractGraphBuilder<Node, Edge, GraphModel<Node, Edge>> {\n override call(cb: (builder: this) => void): this {\n cb(this);\n return this;\n }\n}\n", "//\n// Copyright 2024 DXOS.org\n//\n\nimport { invariant } from '@dxos/invariant';\n\nconst KEY_REGEX = /\\w+/;\n\n// NOTE: The `relation` is different from the `type`.\ntype EdgeMeta = { source: string; target: string; relation?: string };\n\nexport const createEdgeId = ({ source, target, relation }: EdgeMeta) => {\n invariant(source.match(KEY_REGEX), `invalid source: ${source}`);\n invariant(target.match(KEY_REGEX), `invalid target: ${target}`);\n return [source, relation, target].join('_');\n};\n\nexport const parseEdgeId = (id: string): EdgeMeta => {\n const [source, relation, target] = id.split('_');\n invariant(source.length && target.length);\n return { source, relation: relation.length ? relation : undefined, target };\n};\n", "//\n// Copyright 2024 DXOS.org\n//\n\nimport { type ReadonlySignal, type Signal, computed, signal } from '@preact/signals-core';\n\nimport { invariant } from '@dxos/invariant';\n\n/**\n * Reactive selection model.\n */\nexport class SelectionModel {\n private readonly _selected: Signal<Set<string>> = signal(new Set<string>());\n private readonly _selectedIds = computed(() => Array.from(this._selected.value.values()));\n\n constructor(private readonly _singleSelect: boolean = false) {}\n\n toJSON(): { selected: string[] } {\n return {\n selected: Array.from(this._selected.value.values()),\n };\n }\n\n get size(): number {\n return this._selected.value.size;\n }\n\n get selected(): ReadonlySignal<string[]> {\n return this._selectedIds;\n }\n\n contains(id: string): boolean {\n return this._selected.value.has(id);\n }\n\n clear(): void {\n this._selected.value = new Set();\n }\n\n add(id: string): void {\n invariant(id);\n this._selected.value = new Set<string>(\n this._singleSelect ? [id] : [...Array.from(this._selected.value.values()), id],\n );\n }\n\n remove(id: string): void {\n invariant(id);\n this._selected.value = new Set<string>(Array.from(this._selected.value.values()).filter((_id) => _id !== id));\n }\n\n // TODO(burdon): Handle single select.\n\n setSelected(ids: string[], shift = false): void {\n this._selected.value = new Set([...(shift ? Array.from(this._selected.value.values()) : []), ...ids]);\n }\n\n toggleSelected(ids: string[], shift = false): void {\n const set = new Set<string>(shift ? Array.from(this._selected.value.values()) : undefined);\n ids.forEach((id) => {\n if (this._selected.value.has(id)) {\n set.delete(id);\n } else {\n set.add(id);\n }\n });\n\n this._selected.value = set;\n }\n}\n", "//\n// Copyright 2024 DXOS.org\n//\n\nimport * as Schema from 'effect/Schema';\n\nimport { type Specialize } from '@dxos/util';\n\n//\n// Node\n//\n\n// TODO(burdon): Make type extensible (i.e., not dependent on `data` property)?\nexport const BaseGraphNode = Schema.Struct({\n id: Schema.String,\n type: Schema.optional(Schema.String),\n data: Schema.optional(Schema.Any),\n});\n\n/** Raw base type. */\nexport type BaseGraphNode = Schema.Schema.Type<typeof BaseGraphNode>;\n\n/** Typed node data. */\ntype GraphNode<Data = any, Optional extends boolean = false> = Specialize<\n BaseGraphNode,\n Optional extends true ? { data?: Data } : { data: Data }\n>;\n\nexport declare namespace GraphNode {\n export type Any = GraphNode<any, true>;\n export type Optional<Data = any> = GraphNode<Data, true>;\n export type Required<Data = any> = GraphNode<Data, false>;\n}\n\n//\n// Edge\n//\n\nexport const BaseGraphEdge = Schema.Struct({\n id: Schema.String,\n type: Schema.optional(Schema.String),\n source: Schema.String,\n target: Schema.String,\n data: Schema.optional(Schema.Any),\n});\n\n/** Raw base type. */\nexport type BaseGraphEdge = Schema.Schema.Type<typeof BaseGraphEdge>;\n\n/** Typed edge data. */\nexport type GraphEdge<Data = any, Optional extends boolean = false> = Specialize<\n BaseGraphEdge,\n Optional extends true ? { data?: Data } : { data: Data }\n>;\n\nexport declare namespace GraphEdge {\n export type Any = GraphEdge<any, true>;\n export type Optional<Data = any> = GraphEdge<Data, true>;\n export type Required<Data = any> = GraphEdge<Data, false>;\n}\n\n//\n// Graph\n//\n\nexport const Graph = Schema.Struct({\n id: Schema.optional(Schema.String),\n nodes: Schema.mutable(Schema.Array(BaseGraphNode)),\n edges: Schema.mutable(Schema.Array(BaseGraphEdge)),\n});\n\nexport type Graph = Schema.Schema.Type<typeof Graph>;\n"],
|
|
5
|
+
"mappings": ";;;AAIA,SAASA,cAAc;AAEvB,SAASC,qBAAqB;AAC9B,SAASC,iBAAiBC,aAAAA,kBAAiB;AAC3C,SAAoBC,YAAY;AAChC,SAA4BC,UAAUC,gBAAgB;;;ACLtD,SAASC,iBAAiB;;AAE1B,IAAMC,YAAY;AAKX,IAAMC,eAAe,CAAC,EAAEC,QAAQC,QAAQC,SAAQ,MAAY;AACjEL,YAAUG,OAAOG,MAAML,SAAAA,GAAY,mBAAmBE,MAAAA,IAAQ;;;;;;;;;AAC9DH,YAAUI,OAAOE,MAAML,SAAAA,GAAY,mBAAmBG,MAAAA,IAAQ;;;;;;;;;AAC9D,S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"invariant", "live", "isTruthy", "removeBy", "invariant", "KEY_REGEX", "createEdgeId", "source", "target", "relation", "match", "join", "parseEdgeId", "id", "split", "length", "undefined", "ReadonlyGraphModel", "_graph", "graph", "nodes", "edges", "inspectCustom", "toJSON", "length", "findNode", "id", "find", "node", "getNode", "failedInvariant", "filterNodes", "type", "filter", "findEdge", "edge", "getEdge", "filterEdges", "source", "target", "traverse", "root", "_traverse", "visited", "Set", "has", "add", "targets", "map", "isTruthy", "flatMap", "AbstractGraphModel", "clear", "addGraph", "addNodes", "addEdges", "addGraphs", "graphs", "forEach", "addNode", "invariant", "push", "addEdge", "createEdgeId", "removeNode", "removeBy", "copy", "removeNodes", "ids", "removeEdge", "removeEdges", "AbstractGraphBuilder", "_model", "model", "call", "cb", "GraphModel", "builder", "GraphBuilder", "subscribe", "fire", "effect", "ReactiveGraphModel", "live", "computed", "signal", "invariant", "SelectionModel", "_selected", "Set", "_selectedIds", "Array", "from", "value", "values", "_singleSelect", "toJSON", "selected", "size", "contains", "id", "has", "clear", "add", "remove", "filter", "_id", "setSelected", "ids", "shift", "toggleSelected", "set", "undefined", "forEach", "delete", "Schema", "BaseGraphNode", "Struct", "id", "String", "type", "optional", "data", "Any", "BaseGraphEdge", "source", "target", "Graph", "nodes", "mutable", "Array", "edges"]
|
|
7
7
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"inputs":{"src/util.ts":{"bytes":3257,"imports":[{"path":"@dxos/invariant","kind":"import-statement","external":true}],"format":"esm"},"src/model.ts":{"bytes":
|
|
1
|
+
{"inputs":{"src/util.ts":{"bytes":3257,"imports":[{"path":"@dxos/invariant","kind":"import-statement","external":true}],"format":"esm"},"src/model.ts":{"bytes":25651,"imports":[{"path":"@preact/signals-core","kind":"import-statement","external":true},{"path":"@dxos/debug","kind":"import-statement","external":true},{"path":"@dxos/invariant","kind":"import-statement","external":true},{"path":"@dxos/live-object","kind":"import-statement","external":true},{"path":"@dxos/util","kind":"import-statement","external":true},{"path":"src/util.ts","kind":"import-statement","original":"./util"}],"format":"esm"},"src/selection.ts":{"bytes":7321,"imports":[{"path":"@preact/signals-core","kind":"import-statement","external":true},{"path":"@dxos/invariant","kind":"import-statement","external":true}],"format":"esm"},"src/types.ts":{"bytes":4622,"imports":[{"path":"effect/Schema","kind":"import-statement","external":true}],"format":"esm"},"src/index.ts":{"bytes":691,"imports":[{"path":"src/model.ts","kind":"import-statement","original":"./model"},{"path":"src/selection.ts","kind":"import-statement","original":"./selection"},{"path":"src/types.ts","kind":"import-statement","original":"./types"},{"path":"src/util.ts","kind":"import-statement","original":"./util"}],"format":"esm"}},"outputs":{"dist/lib/node-esm/index.mjs.map":{"imports":[],"exports":[],"inputs":{},"bytes":20659},"dist/lib/node-esm/index.mjs":{"imports":[{"path":"@preact/signals-core","kind":"import-statement","external":true},{"path":"@dxos/debug","kind":"import-statement","external":true},{"path":"@dxos/invariant","kind":"import-statement","external":true},{"path":"@dxos/live-object","kind":"import-statement","external":true},{"path":"@dxos/util","kind":"import-statement","external":true},{"path":"@dxos/invariant","kind":"import-statement","external":true},{"path":"@preact/signals-core","kind":"import-statement","external":true},{"path":"@dxos/invariant","kind":"import-statement","external":true},{"path":"effect/Schema","kind":"import-statement","external":true}],"exports":["AbstractGraphBuilder","AbstractGraphModel","BaseGraphEdge","BaseGraphNode","Graph","GraphBuilder","GraphModel","ReactiveGraphModel","ReadonlyGraphModel","SelectionModel","createEdgeId","parseEdgeId","subscribe"],"entryPoint":"src/index.ts","inputs":{"src/model.ts":{"bytesInOutput":5708},"src/util.ts":{"bytesInOutput":1014},"src/index.ts":{"bytesInOutput":0},"src/selection.ts":{"bytesInOutput":1865},"src/types.ts":{"bytesInOutput":542}},"bytes":9580}}}
|
|
@@ -84,10 +84,7 @@ export type GraphModelSubscription = (model: GraphModel, graph: Live<Graph>) =>
|
|
|
84
84
|
* Subscription.
|
|
85
85
|
* NOTE: Requires `registerSignalsRuntime` to be called.
|
|
86
86
|
*/
|
|
87
|
-
export declare const subscribe: (model: GraphModel, cb: GraphModelSubscription, fire?: boolean) =>
|
|
88
|
-
(): void;
|
|
89
|
-
[Symbol.dispose](): void;
|
|
90
|
-
};
|
|
87
|
+
export declare const subscribe: (model: GraphModel, cb: GraphModelSubscription, fire?: boolean) => () => void;
|
|
91
88
|
/**
|
|
92
89
|
* Basic reactive model.
|
|
93
90
|
*/
|