@dxos/graph 0.7.5-main.499c70c
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/LICENSE +8 -0
- package/README.md +3 -0
- package/dist/lib/browser/index.mjs +212 -0
- package/dist/lib/browser/index.mjs.map +7 -0
- package/dist/lib/browser/meta.json +1 -0
- package/dist/lib/node/index.cjs +233 -0
- package/dist/lib/node/index.cjs.map +7 -0
- package/dist/lib/node/meta.json +1 -0
- package/dist/lib/node-esm/index.mjs +214 -0
- package/dist/lib/node-esm/index.mjs.map +7 -0
- package/dist/lib/node-esm/meta.json +1 -0
- package/dist/types/src/buidler.d.ts +16 -0
- package/dist/types/src/buidler.d.ts.map +1 -0
- package/dist/types/src/graph.d.ts +30 -0
- package/dist/types/src/graph.d.ts.map +1 -0
- package/dist/types/src/graph.test.d.ts +2 -0
- package/dist/types/src/graph.test.d.ts.map +1 -0
- package/dist/types/src/index.d.ts +4 -0
- package/dist/types/src/index.d.ts.map +1 -0
- package/dist/types/src/types.d.ts +41 -0
- package/dist/types/src/types.d.ts.map +1 -0
- package/dist/types/src/util.d.ts +2 -0
- package/dist/types/src/util.d.ts.map +1 -0
- package/dist/types/tsconfig.tsbuildinfo +1 -0
- package/package.json +60 -0
- package/src/buidler.ts +62 -0
- package/src/graph.test.ts +56 -0
- package/src/graph.ts +127 -0
- package/src/index.ts +7 -0
- package/src/types.ts +42 -0
- package/src/util.ts +17 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
Copyright (c) 2022 DXOS
|
|
3
|
+
|
|
4
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
|
5
|
+
|
|
6
|
+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
|
7
|
+
|
|
8
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,212 @@
|
|
|
1
|
+
// packages/common/graph/src/buidler.ts
|
|
2
|
+
import { FormatEnum } from "@dxos/echo-schema";
|
|
3
|
+
import { invariant } from "@dxos/invariant";
|
|
4
|
+
import { getSchema } from "@dxos/live-object";
|
|
5
|
+
import { log } from "@dxos/log";
|
|
6
|
+
import { getSchemaProperties } from "@dxos/schema";
|
|
7
|
+
|
|
8
|
+
// packages/common/graph/src/graph.ts
|
|
9
|
+
import { create } from "@dxos/live-object";
|
|
10
|
+
|
|
11
|
+
// packages/common/graph/src/types.ts
|
|
12
|
+
import { S } from "@dxos/echo-schema";
|
|
13
|
+
var BaseGraphNode = S.Struct({
|
|
14
|
+
id: S.String,
|
|
15
|
+
type: S.optional(S.String),
|
|
16
|
+
data: S.optional(S.Any)
|
|
17
|
+
});
|
|
18
|
+
var BaseGraphEdge = S.Struct({
|
|
19
|
+
id: S.String,
|
|
20
|
+
type: S.optional(S.String),
|
|
21
|
+
source: S.String,
|
|
22
|
+
target: S.String,
|
|
23
|
+
data: S.optional(S.Any)
|
|
24
|
+
});
|
|
25
|
+
var Graph = S.Struct({
|
|
26
|
+
nodes: S.mutable(S.Array(BaseGraphNode)),
|
|
27
|
+
edges: S.mutable(S.Array(BaseGraphEdge))
|
|
28
|
+
});
|
|
29
|
+
var emptyGraph = {
|
|
30
|
+
nodes: [],
|
|
31
|
+
edges: []
|
|
32
|
+
};
|
|
33
|
+
|
|
34
|
+
// packages/common/graph/src/util.ts
|
|
35
|
+
var removeElements = (array, predicate) => {
|
|
36
|
+
const deleted = [];
|
|
37
|
+
for (let i = array.length - 1; i >= 0; i--) {
|
|
38
|
+
if (predicate(array[i], i)) {
|
|
39
|
+
deleted.push(...array.splice(i, 1));
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
return deleted;
|
|
43
|
+
};
|
|
44
|
+
|
|
45
|
+
// packages/common/graph/src/graph.ts
|
|
46
|
+
var ReadonlyGraphModel = class {
|
|
47
|
+
constructor({ nodes = [], edges = [] } = {}) {
|
|
48
|
+
this._graph = create(Graph, {
|
|
49
|
+
nodes,
|
|
50
|
+
edges
|
|
51
|
+
});
|
|
52
|
+
}
|
|
53
|
+
toJSON() {
|
|
54
|
+
this._graph.nodes.sort(({ id: a }, { id: b }) => a.localeCompare(b));
|
|
55
|
+
this._graph.edges.sort(({ id: a }, { id: b }) => a.localeCompare(b));
|
|
56
|
+
return JSON.parse(JSON.stringify(this._graph));
|
|
57
|
+
}
|
|
58
|
+
get graph() {
|
|
59
|
+
return this._graph;
|
|
60
|
+
}
|
|
61
|
+
get nodes() {
|
|
62
|
+
return this._graph.nodes;
|
|
63
|
+
}
|
|
64
|
+
get edges() {
|
|
65
|
+
return this._graph.edges;
|
|
66
|
+
}
|
|
67
|
+
getNode(id) {
|
|
68
|
+
return this.nodes.find((node) => node.id === id);
|
|
69
|
+
}
|
|
70
|
+
getNodes({ type }) {
|
|
71
|
+
return this.nodes.filter((node) => !type || type === node.type);
|
|
72
|
+
}
|
|
73
|
+
getEdge(id) {
|
|
74
|
+
return this.edges.find((edge) => edge.id === id);
|
|
75
|
+
}
|
|
76
|
+
getEdges({ type, source, target }) {
|
|
77
|
+
return this.edges.filter((edge) => (!type || type === edge.type) && (!source || source === edge.source) && (!target || target === edge.target));
|
|
78
|
+
}
|
|
79
|
+
};
|
|
80
|
+
var GraphModel = class _GraphModel extends ReadonlyGraphModel {
|
|
81
|
+
clear() {
|
|
82
|
+
this._graph.nodes.length = 0;
|
|
83
|
+
this._graph.edges.length = 0;
|
|
84
|
+
return this;
|
|
85
|
+
}
|
|
86
|
+
addGraph(graph) {
|
|
87
|
+
this.addNodes(graph.nodes);
|
|
88
|
+
this.addEdges(graph.edges);
|
|
89
|
+
return this;
|
|
90
|
+
}
|
|
91
|
+
addGraphs(graphs) {
|
|
92
|
+
graphs.forEach((graph) => {
|
|
93
|
+
this.addNodes(graph.nodes);
|
|
94
|
+
this.addEdges(graph.edges);
|
|
95
|
+
});
|
|
96
|
+
return this;
|
|
97
|
+
}
|
|
98
|
+
addNode(node) {
|
|
99
|
+
this._graph.nodes.push(node);
|
|
100
|
+
return this;
|
|
101
|
+
}
|
|
102
|
+
addNodes(nodes) {
|
|
103
|
+
nodes.forEach((node) => this.addNode(node));
|
|
104
|
+
return this;
|
|
105
|
+
}
|
|
106
|
+
addEdge(edge) {
|
|
107
|
+
this._graph.edges.push(edge);
|
|
108
|
+
return this;
|
|
109
|
+
}
|
|
110
|
+
addEdges(edges) {
|
|
111
|
+
edges.forEach((edge) => this.addEdge(edge));
|
|
112
|
+
return this;
|
|
113
|
+
}
|
|
114
|
+
// TODO(burdon): Return graph.
|
|
115
|
+
removeNode(id) {
|
|
116
|
+
const nodes = removeElements(this._graph.nodes, (node) => node.id === id);
|
|
117
|
+
const edges = removeElements(this._graph.edges, (edge) => edge.source === id || edge.target === id);
|
|
118
|
+
return new _GraphModel({
|
|
119
|
+
nodes,
|
|
120
|
+
edges
|
|
121
|
+
});
|
|
122
|
+
}
|
|
123
|
+
removeNodes(ids) {
|
|
124
|
+
const graphs = ids.map((id) => this.removeNode(id));
|
|
125
|
+
return new _GraphModel().addGraphs(graphs);
|
|
126
|
+
}
|
|
127
|
+
removeEdge(id) {
|
|
128
|
+
const edges = removeElements(this._graph.edges, (edge) => edge.id === id);
|
|
129
|
+
return new _GraphModel({
|
|
130
|
+
edges
|
|
131
|
+
});
|
|
132
|
+
}
|
|
133
|
+
removeEdges(ids) {
|
|
134
|
+
const graphs = ids.map((id) => this.removeEdge(id));
|
|
135
|
+
return new _GraphModel().addGraphs(graphs);
|
|
136
|
+
}
|
|
137
|
+
};
|
|
138
|
+
|
|
139
|
+
// packages/common/graph/src/buidler.ts
|
|
140
|
+
var __dxlog_file = "/home/runner/work/dxos/dxos/packages/common/graph/src/buidler.ts";
|
|
141
|
+
var createEdgeId = ({ source, target, relation }) => `${source}-${relation}-${target}`;
|
|
142
|
+
var parseEdgeId = (id) => {
|
|
143
|
+
const [source, relation, target] = id.split("-");
|
|
144
|
+
invariant(source && target && relation, void 0, {
|
|
145
|
+
F: __dxlog_file,
|
|
146
|
+
L: 22,
|
|
147
|
+
S: void 0,
|
|
148
|
+
A: [
|
|
149
|
+
"source && target && relation",
|
|
150
|
+
""
|
|
151
|
+
]
|
|
152
|
+
});
|
|
153
|
+
return {
|
|
154
|
+
source,
|
|
155
|
+
relation,
|
|
156
|
+
target
|
|
157
|
+
};
|
|
158
|
+
};
|
|
159
|
+
var createGraph = (objects) => {
|
|
160
|
+
const graph = new GraphModel();
|
|
161
|
+
objects.forEach((object) => {
|
|
162
|
+
graph.addNode({
|
|
163
|
+
id: object.id,
|
|
164
|
+
type: object.typename,
|
|
165
|
+
data: object
|
|
166
|
+
});
|
|
167
|
+
});
|
|
168
|
+
objects.forEach((object) => {
|
|
169
|
+
const schema = getSchema(object);
|
|
170
|
+
if (!schema) {
|
|
171
|
+
log.info("no schema for object", {
|
|
172
|
+
id: object.id.slice(0, 8)
|
|
173
|
+
}, {
|
|
174
|
+
F: __dxlog_file,
|
|
175
|
+
L: 41,
|
|
176
|
+
S: void 0,
|
|
177
|
+
C: (f, a) => f(...a)
|
|
178
|
+
});
|
|
179
|
+
return;
|
|
180
|
+
}
|
|
181
|
+
for (const prop of getSchemaProperties(schema.ast, object)) {
|
|
182
|
+
if (prop.format === FormatEnum.Ref) {
|
|
183
|
+
const source = object;
|
|
184
|
+
const target = object[prop.name]?.target;
|
|
185
|
+
if (target) {
|
|
186
|
+
graph.addEdge({
|
|
187
|
+
id: createEdgeId({
|
|
188
|
+
source: source.id,
|
|
189
|
+
target: target.id,
|
|
190
|
+
relation: String(prop.name)
|
|
191
|
+
}),
|
|
192
|
+
source: source.id,
|
|
193
|
+
target: target.id
|
|
194
|
+
});
|
|
195
|
+
}
|
|
196
|
+
}
|
|
197
|
+
}
|
|
198
|
+
});
|
|
199
|
+
return graph;
|
|
200
|
+
};
|
|
201
|
+
export {
|
|
202
|
+
BaseGraphEdge,
|
|
203
|
+
BaseGraphNode,
|
|
204
|
+
Graph,
|
|
205
|
+
GraphModel,
|
|
206
|
+
ReadonlyGraphModel,
|
|
207
|
+
createEdgeId,
|
|
208
|
+
createGraph,
|
|
209
|
+
emptyGraph,
|
|
210
|
+
parseEdgeId
|
|
211
|
+
};
|
|
212
|
+
//# sourceMappingURL=index.mjs.map
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
{
|
|
2
|
+
"version": 3,
|
|
3
|
+
"sources": ["../../../src/buidler.ts", "../../../src/graph.ts", "../../../src/types.ts", "../../../src/util.ts"],
|
|
4
|
+
"sourcesContent": ["//\n// Copyright 2023 DXOS.org\n//\n\nimport { type ReactiveEchoObject } from '@dxos/echo-db';\nimport { FormatEnum } from '@dxos/echo-schema';\nimport { invariant } from '@dxos/invariant';\nimport { getSchema } from '@dxos/live-object';\nimport { log } from '@dxos/log';\nimport { getSchemaProperties } from '@dxos/schema';\n\nimport { GraphModel } from './graph';\nimport { type GraphNode } from './types';\n\n// NOTE: The `relation` is different from the `type`.\ntype Edge = { source: string; target: string; relation: string };\n\nexport const createEdgeId = ({ source, target, relation }: Edge) => `${source}-${relation}-${target}`;\n\nexport const parseEdgeId = (id: string): Edge => {\n const [source, relation, target] = id.split('-');\n invariant(source && target && relation);\n return { source, relation, target };\n};\n\n/**\n * Maps an ECHO object graph onto a layout graph.\n */\nexport const createGraph = (objects: ReactiveEchoObject<any>[]): GraphModel<GraphNode<ReactiveEchoObject<any>>> => {\n const graph = new GraphModel<GraphNode<ReactiveEchoObject<any>>>();\n\n // Map objects.\n objects.forEach((object) => {\n graph.addNode({ id: object.id, type: object.typename, data: object });\n });\n\n // Find references.\n objects.forEach((object) => {\n const schema = getSchema(object);\n if (!schema) {\n log.info('no schema for object', { id: object.id.slice(0, 8) });\n return;\n }\n\n // Parse schema to follow referenced objects.\n for (const prop of getSchemaProperties(schema.ast, object)) {\n if (prop.format === FormatEnum.Ref) {\n const source = object;\n const target = object[prop.name]?.target;\n if (target) {\n graph.addEdge({\n id: createEdgeId({ source: source.id, target: target.id, relation: String(prop.name) }),\n source: source.id,\n target: target.id,\n });\n }\n }\n }\n });\n\n return graph;\n};\n", "//\n// Copyright 2024 DXOS.org\n//\n\nimport { create } from '@dxos/live-object';\n\nimport { Graph, type GraphNode, type GraphEdge } from './types';\nimport { removeElements } from './util';\n\n// TODO(burdon): Traversal/visitor.\n\n/**\n * Typed reactive object graph.\n */\nexport class ReadonlyGraphModel<Node extends GraphNode = any, Edge extends GraphEdge = any> {\n protected readonly _graph: Graph;\n\n constructor({ nodes = [], edges = [] }: Partial<Graph> = {}) {\n this._graph = create(Graph, { nodes, edges });\n }\n\n toJSON() {\n // Sort to enable stable comparisons.\n this._graph.nodes.sort(({ id: a }, { id: b }) => a.localeCompare(b));\n this._graph.edges.sort(({ id: a }, { id: b }) => a.localeCompare(b));\n return JSON.parse(JSON.stringify(this._graph));\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 getNode(id: string): Node | undefined {\n return this.nodes.find((node) => node.id === id);\n }\n\n getNodes({ type }: Partial<Node>): Node[] {\n return this.nodes.filter((node) => !type || type === node.type);\n }\n\n getEdge(id: string): Edge | undefined {\n return this.edges.find((edge) => edge.id === id);\n }\n\n getEdges({ type, source, target }: Partial<Edge>): 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\nexport class GraphModel<Node extends GraphNode = any, Edge extends GraphEdge = any> extends ReadonlyGraphModel<\n Node,\n Edge\n> {\n clear(): this {\n this._graph.nodes.length = 0;\n this._graph.edges.length = 0;\n return this;\n }\n\n addGraph(graph: GraphModel<Node, Edge>): this {\n this.addNodes(graph.nodes);\n this.addEdges(graph.edges);\n return this;\n }\n\n addGraphs(graphs: GraphModel<Node, Edge>[]): 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): this {\n this._graph.nodes.push(node);\n return this;\n }\n\n addNodes(nodes: Node[]): this {\n nodes.forEach((node) => this.addNode(node));\n return this;\n }\n\n addEdge(edge: Edge): this {\n this._graph.edges.push(edge);\n return this;\n }\n\n addEdges(edges: Edge[]): this {\n edges.forEach((edge) => this.addEdge(edge));\n return this;\n }\n\n // TODO(burdon): Return graph.\n\n removeNode(id: string): GraphModel<Node, Edge> {\n const nodes = removeElements(this._graph.nodes, (node) => node.id === id);\n const edges = removeElements(this._graph.edges, (edge) => edge.source === id || edge.target === id);\n return new GraphModel<Node, Edge>({ nodes, edges });\n }\n\n removeNodes(ids: string[]): GraphModel<Node, Edge> {\n const graphs = ids.map((id) => this.removeNode(id));\n return new GraphModel<Node, Edge>().addGraphs(graphs);\n }\n\n removeEdge(id: string): GraphModel<Node, Edge> {\n const edges = removeElements(this._graph.edges, (edge) => edge.id === id);\n return new GraphModel<Node, Edge>({ edges });\n }\n\n removeEdges(ids: string[]): GraphModel<Node, Edge> {\n const graphs = ids.map((id) => this.removeEdge(id));\n return new GraphModel<Node, Edge>().addGraphs(graphs);\n }\n}\n", "//\n// Copyright 2024 DXOS.org\n//\n\nimport { S } from '@dxos/echo-schema';\n\n// Prior art:\n// - https://graphology.github.io (TS, tree-shakable, multiple packages for extensions)\n// - https://github.com/dagrejs/graphlib (mature, extensive)\n// - https://github.com/avoidwork/tiny-graph\n\nexport const BaseGraphNode = S.Struct({\n id: S.String,\n type: S.optional(S.String),\n data: S.optional(S.Any),\n});\n\nexport type BaseGraphNode = S.Schema.Type<typeof BaseGraphNode>;\nexport type GraphNode<Data extends object | void = void> = BaseGraphNode & { data: Data };\n\nexport const BaseGraphEdge = S.Struct({\n id: S.String,\n type: S.optional(S.String),\n source: S.String,\n target: S.String,\n data: S.optional(S.Any),\n});\n\nexport type BaseGraphEdge = S.Schema.Type<typeof BaseGraphEdge>;\nexport type GraphEdge<Data extends object | void = void> = BaseGraphEdge & { data: Data };\n\n/**\n * Generic graph abstraction.\n */\nexport const Graph = S.Struct({\n nodes: S.mutable(S.Array(BaseGraphNode)),\n edges: S.mutable(S.Array(BaseGraphEdge)),\n});\n\nexport type Graph = S.Schema.Type<typeof Graph>;\n\nexport const emptyGraph: Graph = { nodes: [], edges: [] };\n", "//\n// Copyright 2024 DXOS.org\n//\n\n// TODO(burdon): GraphBuilder.\n\n// TODO(burdon): Move to @dxos/live-object?\nexport const removeElements = <T>(array: T[], predicate: (element: T, index: number) => boolean): T[] => {\n const deleted: T[] = [];\n for (let i = array.length - 1; i >= 0; i--) {\n if (predicate(array[i], i)) {\n deleted.push(...array.splice(i, 1));\n }\n }\n\n return deleted;\n};\n"],
|
|
5
|
+
"mappings": ";AAKA,SAASA,kBAAkB;AAC3B,SAASC,iBAAiB;AAC1B,SAASC,iBAAiB;AAC1B,SAASC,WAAW;AACpB,SAASC,2BAA2B;;;ACLpC,SAASC,cAAc;;;ACAvB,SAASC,SAAS;AAOX,IAAMC,gBAAgBC,EAAEC,OAAO;EACpCC,IAAIF,EAAEG;EACNC,MAAMJ,EAAEK,SAASL,EAAEG,MAAM;EACzBG,MAAMN,EAAEK,SAASL,EAAEO,GAAG;AACxB,CAAA;AAKO,IAAMC,gBAAgBR,EAAEC,OAAO;EACpC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AAO;AACpC;AAKO,IAAMM,cAAc,CAACC,YAAAA;AAC1B,QAAMC,QAAQ,IAAIC,WAAAA;AAGlBF,UAAQG,QAAQ,CAACC,WAAAA;AACfH,UAAMI,QAAQ;MAAET,IAAIQ,OAAOR;MAAIU,MAAMF,OAAOG;MAAUC,MAAMJ;IAAO,CAAA;EACrE,CAAA;AAGAJ,UAAQG,QAAQ,CAACC,WAAAA;AACf,UAAMK,SAASC,UAAUN,MAAAA;AACzB,QAAI,CAACK,QAAQ;AACXE,UAAIC,KAAK,wBAAwB;QAAEhB,IAAIQ,OAAOR,GAAGiB,MAAM,GAAG,CAAA;MAAG,GAAA;;;;;;AAC7D;IACF;AAGA,eAAWC,QAAQC,oBAAoBN,OAAOO,KAAKZ,MAAAA,GAAS;AAC1D,UAAIU,KAAKG,WAAWC,WAAWC,KAAK;AAClC,cAAM3B,SAASY;AACf,cAAMX,SAASW,OAAOU,KAAKM,IAAI,GAAG3B;AAClC,YAAIA,QAAQ;AACVQ,gBAAMoB,QAAQ;YACZzB,IAAIL,aAAa;cAAEC,QAAQA,OAAOI;cAAIH,QAAQA,OAAOG;cAAIF,UAAU4B,OAAOR,KAAKM,IAAI;YAAE,CAAA;YACrF5B,QAAQA,OAAOI;YACfH,QAAQA,OAAOG;UACjB,CAAA;QACF;MACF;IACF;EACF,CAAA;AAEA,SAAOK;AACT;",
|
|
6
|
+
"names": ["FormatEnum", "invariant", "getSchema", "log", "getSchemaProperties", "create", "S", "BaseGraphNode", "S", "Struct", "id", "String", "type", "optional", "data", "Any", "BaseGraphEdge", "source", "target", "Graph", "nodes", "mutable", "Array", "edges", "emptyGraph", "removeElements", "array", "predicate", "deleted", "i", "length", "push", "splice", "ReadonlyGraphModel", "constructor", "nodes", "edges", "_graph", "create", "Graph", "toJSON", "sort", "id", "a", "b", "localeCompare", "JSON", "parse", "stringify", "graph", "getNode", "find", "node", "getNodes", "type", "filter", "getEdge", "edge", "getEdges", "source", "target", "GraphModel", "clear", "length", "addGraph", "addNodes", "addEdges", "addGraphs", "graphs", "forEach", "addNode", "push", "addEdge", "removeNode", "removeElements", "removeNodes", "ids", "map", "removeEdge", "removeEdges", "createEdgeId", "source", "target", "relation", "parseEdgeId", "id", "split", "invariant", "createGraph", "objects", "graph", "GraphModel", "forEach", "object", "addNode", "type", "typename", "data", "schema", "getSchema", "log", "info", "slice", "prop", "getSchemaProperties", "ast", "format", "FormatEnum", "Ref", "name", "addEdge", "String"]
|
|
7
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"inputs":{"packages/common/graph/src/types.ts":{"bytes":3825,"imports":[{"path":"@dxos/echo-schema","kind":"import-statement","external":true}],"format":"esm"},"packages/common/graph/src/util.ts":{"bytes":1705,"imports":[],"format":"esm"},"packages/common/graph/src/graph.ts":{"bytes":12508,"imports":[{"path":"@dxos/live-object","kind":"import-statement","external":true},{"path":"packages/common/graph/src/types.ts","kind":"import-statement","original":"./types"},{"path":"packages/common/graph/src/util.ts","kind":"import-statement","original":"./util"}],"format":"esm"},"packages/common/graph/src/buidler.ts":{"bytes":7346,"imports":[{"path":"@dxos/echo-schema","kind":"import-statement","external":true},{"path":"@dxos/invariant","kind":"import-statement","external":true},{"path":"@dxos/live-object","kind":"import-statement","external":true},{"path":"@dxos/log","kind":"import-statement","external":true},{"path":"@dxos/schema","kind":"import-statement","external":true},{"path":"packages/common/graph/src/graph.ts","kind":"import-statement","original":"./graph"}],"format":"esm"},"packages/common/graph/src/index.ts":{"bytes":654,"imports":[{"path":"packages/common/graph/src/buidler.ts","kind":"import-statement","original":"./buidler"},{"path":"packages/common/graph/src/graph.ts","kind":"import-statement","original":"./graph"},{"path":"packages/common/graph/src/types.ts","kind":"import-statement","original":"./types"}],"format":"esm"}},"outputs":{"packages/common/graph/dist/lib/browser/index.mjs.map":{"imports":[],"exports":[],"inputs":{},"bytes":12611},"packages/common/graph/dist/lib/browser/index.mjs":{"imports":[{"path":"@dxos/echo-schema","kind":"import-statement","external":true},{"path":"@dxos/invariant","kind":"import-statement","external":true},{"path":"@dxos/live-object","kind":"import-statement","external":true},{"path":"@dxos/log","kind":"import-statement","external":true},{"path":"@dxos/schema","kind":"import-statement","external":true},{"path":"@dxos/live-object","kind":"import-statement","external":true},{"path":"@dxos/echo-schema","kind":"import-statement","external":true}],"exports":["BaseGraphEdge","BaseGraphNode","Graph","GraphModel","ReadonlyGraphModel","createEdgeId","createGraph","emptyGraph","parseEdgeId"],"entryPoint":"packages/common/graph/src/index.ts","inputs":{"packages/common/graph/src/buidler.ts":{"bytesInOutput":1716},"packages/common/graph/src/graph.ts":{"bytesInOutput":2460},"packages/common/graph/src/types.ts":{"bytesInOutput":454},"packages/common/graph/src/util.ts":{"bytesInOutput":223},"packages/common/graph/src/index.ts":{"bytesInOutput":0}},"bytes":5274}}}
|
|
@@ -0,0 +1,233 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
var node_exports = {};
|
|
20
|
+
__export(node_exports, {
|
|
21
|
+
BaseGraphEdge: () => BaseGraphEdge,
|
|
22
|
+
BaseGraphNode: () => BaseGraphNode,
|
|
23
|
+
Graph: () => Graph,
|
|
24
|
+
GraphModel: () => GraphModel,
|
|
25
|
+
ReadonlyGraphModel: () => ReadonlyGraphModel,
|
|
26
|
+
createEdgeId: () => createEdgeId,
|
|
27
|
+
createGraph: () => createGraph,
|
|
28
|
+
emptyGraph: () => emptyGraph,
|
|
29
|
+
parseEdgeId: () => parseEdgeId
|
|
30
|
+
});
|
|
31
|
+
module.exports = __toCommonJS(node_exports);
|
|
32
|
+
var import_echo_schema = require("@dxos/echo-schema");
|
|
33
|
+
var import_invariant = require("@dxos/invariant");
|
|
34
|
+
var import_live_object = require("@dxos/live-object");
|
|
35
|
+
var import_log = require("@dxos/log");
|
|
36
|
+
var import_schema = require("@dxos/schema");
|
|
37
|
+
var import_live_object2 = require("@dxos/live-object");
|
|
38
|
+
var import_echo_schema2 = require("@dxos/echo-schema");
|
|
39
|
+
var BaseGraphNode = import_echo_schema2.S.Struct({
|
|
40
|
+
id: import_echo_schema2.S.String,
|
|
41
|
+
type: import_echo_schema2.S.optional(import_echo_schema2.S.String),
|
|
42
|
+
data: import_echo_schema2.S.optional(import_echo_schema2.S.Any)
|
|
43
|
+
});
|
|
44
|
+
var BaseGraphEdge = import_echo_schema2.S.Struct({
|
|
45
|
+
id: import_echo_schema2.S.String,
|
|
46
|
+
type: import_echo_schema2.S.optional(import_echo_schema2.S.String),
|
|
47
|
+
source: import_echo_schema2.S.String,
|
|
48
|
+
target: import_echo_schema2.S.String,
|
|
49
|
+
data: import_echo_schema2.S.optional(import_echo_schema2.S.Any)
|
|
50
|
+
});
|
|
51
|
+
var Graph = import_echo_schema2.S.Struct({
|
|
52
|
+
nodes: import_echo_schema2.S.mutable(import_echo_schema2.S.Array(BaseGraphNode)),
|
|
53
|
+
edges: import_echo_schema2.S.mutable(import_echo_schema2.S.Array(BaseGraphEdge))
|
|
54
|
+
});
|
|
55
|
+
var emptyGraph = {
|
|
56
|
+
nodes: [],
|
|
57
|
+
edges: []
|
|
58
|
+
};
|
|
59
|
+
var removeElements = (array, predicate) => {
|
|
60
|
+
const deleted = [];
|
|
61
|
+
for (let i = array.length - 1; i >= 0; i--) {
|
|
62
|
+
if (predicate(array[i], i)) {
|
|
63
|
+
deleted.push(...array.splice(i, 1));
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
return deleted;
|
|
67
|
+
};
|
|
68
|
+
var ReadonlyGraphModel = class {
|
|
69
|
+
constructor({ nodes = [], edges = [] } = {}) {
|
|
70
|
+
this._graph = (0, import_live_object2.create)(Graph, {
|
|
71
|
+
nodes,
|
|
72
|
+
edges
|
|
73
|
+
});
|
|
74
|
+
}
|
|
75
|
+
toJSON() {
|
|
76
|
+
this._graph.nodes.sort(({ id: a }, { id: b }) => a.localeCompare(b));
|
|
77
|
+
this._graph.edges.sort(({ id: a }, { id: b }) => a.localeCompare(b));
|
|
78
|
+
return JSON.parse(JSON.stringify(this._graph));
|
|
79
|
+
}
|
|
80
|
+
get graph() {
|
|
81
|
+
return this._graph;
|
|
82
|
+
}
|
|
83
|
+
get nodes() {
|
|
84
|
+
return this._graph.nodes;
|
|
85
|
+
}
|
|
86
|
+
get edges() {
|
|
87
|
+
return this._graph.edges;
|
|
88
|
+
}
|
|
89
|
+
getNode(id) {
|
|
90
|
+
return this.nodes.find((node) => node.id === id);
|
|
91
|
+
}
|
|
92
|
+
getNodes({ type }) {
|
|
93
|
+
return this.nodes.filter((node) => !type || type === node.type);
|
|
94
|
+
}
|
|
95
|
+
getEdge(id) {
|
|
96
|
+
return this.edges.find((edge) => edge.id === id);
|
|
97
|
+
}
|
|
98
|
+
getEdges({ type, source, target }) {
|
|
99
|
+
return this.edges.filter((edge) => (!type || type === edge.type) && (!source || source === edge.source) && (!target || target === edge.target));
|
|
100
|
+
}
|
|
101
|
+
};
|
|
102
|
+
var GraphModel = class _GraphModel extends ReadonlyGraphModel {
|
|
103
|
+
clear() {
|
|
104
|
+
this._graph.nodes.length = 0;
|
|
105
|
+
this._graph.edges.length = 0;
|
|
106
|
+
return this;
|
|
107
|
+
}
|
|
108
|
+
addGraph(graph) {
|
|
109
|
+
this.addNodes(graph.nodes);
|
|
110
|
+
this.addEdges(graph.edges);
|
|
111
|
+
return this;
|
|
112
|
+
}
|
|
113
|
+
addGraphs(graphs) {
|
|
114
|
+
graphs.forEach((graph) => {
|
|
115
|
+
this.addNodes(graph.nodes);
|
|
116
|
+
this.addEdges(graph.edges);
|
|
117
|
+
});
|
|
118
|
+
return this;
|
|
119
|
+
}
|
|
120
|
+
addNode(node) {
|
|
121
|
+
this._graph.nodes.push(node);
|
|
122
|
+
return this;
|
|
123
|
+
}
|
|
124
|
+
addNodes(nodes) {
|
|
125
|
+
nodes.forEach((node) => this.addNode(node));
|
|
126
|
+
return this;
|
|
127
|
+
}
|
|
128
|
+
addEdge(edge) {
|
|
129
|
+
this._graph.edges.push(edge);
|
|
130
|
+
return this;
|
|
131
|
+
}
|
|
132
|
+
addEdges(edges) {
|
|
133
|
+
edges.forEach((edge) => this.addEdge(edge));
|
|
134
|
+
return this;
|
|
135
|
+
}
|
|
136
|
+
// TODO(burdon): Return graph.
|
|
137
|
+
removeNode(id) {
|
|
138
|
+
const nodes = removeElements(this._graph.nodes, (node) => node.id === id);
|
|
139
|
+
const edges = removeElements(this._graph.edges, (edge) => edge.source === id || edge.target === id);
|
|
140
|
+
return new _GraphModel({
|
|
141
|
+
nodes,
|
|
142
|
+
edges
|
|
143
|
+
});
|
|
144
|
+
}
|
|
145
|
+
removeNodes(ids) {
|
|
146
|
+
const graphs = ids.map((id) => this.removeNode(id));
|
|
147
|
+
return new _GraphModel().addGraphs(graphs);
|
|
148
|
+
}
|
|
149
|
+
removeEdge(id) {
|
|
150
|
+
const edges = removeElements(this._graph.edges, (edge) => edge.id === id);
|
|
151
|
+
return new _GraphModel({
|
|
152
|
+
edges
|
|
153
|
+
});
|
|
154
|
+
}
|
|
155
|
+
removeEdges(ids) {
|
|
156
|
+
const graphs = ids.map((id) => this.removeEdge(id));
|
|
157
|
+
return new _GraphModel().addGraphs(graphs);
|
|
158
|
+
}
|
|
159
|
+
};
|
|
160
|
+
var __dxlog_file = "/home/runner/work/dxos/dxos/packages/common/graph/src/buidler.ts";
|
|
161
|
+
var createEdgeId = ({ source, target, relation }) => `${source}-${relation}-${target}`;
|
|
162
|
+
var parseEdgeId = (id) => {
|
|
163
|
+
const [source, relation, target] = id.split("-");
|
|
164
|
+
(0, import_invariant.invariant)(source && target && relation, void 0, {
|
|
165
|
+
F: __dxlog_file,
|
|
166
|
+
L: 22,
|
|
167
|
+
S: void 0,
|
|
168
|
+
A: [
|
|
169
|
+
"source && target && relation",
|
|
170
|
+
""
|
|
171
|
+
]
|
|
172
|
+
});
|
|
173
|
+
return {
|
|
174
|
+
source,
|
|
175
|
+
relation,
|
|
176
|
+
target
|
|
177
|
+
};
|
|
178
|
+
};
|
|
179
|
+
var createGraph = (objects) => {
|
|
180
|
+
const graph = new GraphModel();
|
|
181
|
+
objects.forEach((object) => {
|
|
182
|
+
graph.addNode({
|
|
183
|
+
id: object.id,
|
|
184
|
+
type: object.typename,
|
|
185
|
+
data: object
|
|
186
|
+
});
|
|
187
|
+
});
|
|
188
|
+
objects.forEach((object) => {
|
|
189
|
+
const schema = (0, import_live_object.getSchema)(object);
|
|
190
|
+
if (!schema) {
|
|
191
|
+
import_log.log.info("no schema for object", {
|
|
192
|
+
id: object.id.slice(0, 8)
|
|
193
|
+
}, {
|
|
194
|
+
F: __dxlog_file,
|
|
195
|
+
L: 41,
|
|
196
|
+
S: void 0,
|
|
197
|
+
C: (f, a) => f(...a)
|
|
198
|
+
});
|
|
199
|
+
return;
|
|
200
|
+
}
|
|
201
|
+
for (const prop of (0, import_schema.getSchemaProperties)(schema.ast, object)) {
|
|
202
|
+
if (prop.format === import_echo_schema.FormatEnum.Ref) {
|
|
203
|
+
const source = object;
|
|
204
|
+
const target = object[prop.name]?.target;
|
|
205
|
+
if (target) {
|
|
206
|
+
graph.addEdge({
|
|
207
|
+
id: createEdgeId({
|
|
208
|
+
source: source.id,
|
|
209
|
+
target: target.id,
|
|
210
|
+
relation: String(prop.name)
|
|
211
|
+
}),
|
|
212
|
+
source: source.id,
|
|
213
|
+
target: target.id
|
|
214
|
+
});
|
|
215
|
+
}
|
|
216
|
+
}
|
|
217
|
+
}
|
|
218
|
+
});
|
|
219
|
+
return graph;
|
|
220
|
+
};
|
|
221
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
222
|
+
0 && (module.exports = {
|
|
223
|
+
BaseGraphEdge,
|
|
224
|
+
BaseGraphNode,
|
|
225
|
+
Graph,
|
|
226
|
+
GraphModel,
|
|
227
|
+
ReadonlyGraphModel,
|
|
228
|
+
createEdgeId,
|
|
229
|
+
createGraph,
|
|
230
|
+
emptyGraph,
|
|
231
|
+
parseEdgeId
|
|
232
|
+
});
|
|
233
|
+
//# sourceMappingURL=index.cjs.map
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
{
|
|
2
|
+
"version": 3,
|
|
3
|
+
"sources": ["../../../src/buidler.ts", "../../../src/graph.ts", "../../../src/types.ts", "../../../src/util.ts"],
|
|
4
|
+
"sourcesContent": ["//\n// Copyright 2023 DXOS.org\n//\n\nimport { type ReactiveEchoObject } from '@dxos/echo-db';\nimport { FormatEnum } from '@dxos/echo-schema';\nimport { invariant } from '@dxos/invariant';\nimport { getSchema } from '@dxos/live-object';\nimport { log } from '@dxos/log';\nimport { getSchemaProperties } from '@dxos/schema';\n\nimport { GraphModel } from './graph';\nimport { type GraphNode } from './types';\n\n// NOTE: The `relation` is different from the `type`.\ntype Edge = { source: string; target: string; relation: string };\n\nexport const createEdgeId = ({ source, target, relation }: Edge) => `${source}-${relation}-${target}`;\n\nexport const parseEdgeId = (id: string): Edge => {\n const [source, relation, target] = id.split('-');\n invariant(source && target && relation);\n return { source, relation, target };\n};\n\n/**\n * Maps an ECHO object graph onto a layout graph.\n */\nexport const createGraph = (objects: ReactiveEchoObject<any>[]): GraphModel<GraphNode<ReactiveEchoObject<any>>> => {\n const graph = new GraphModel<GraphNode<ReactiveEchoObject<any>>>();\n\n // Map objects.\n objects.forEach((object) => {\n graph.addNode({ id: object.id, type: object.typename, data: object });\n });\n\n // Find references.\n objects.forEach((object) => {\n const schema = getSchema(object);\n if (!schema) {\n log.info('no schema for object', { id: object.id.slice(0, 8) });\n return;\n }\n\n // Parse schema to follow referenced objects.\n for (const prop of getSchemaProperties(schema.ast, object)) {\n if (prop.format === FormatEnum.Ref) {\n const source = object;\n const target = object[prop.name]?.target;\n if (target) {\n graph.addEdge({\n id: createEdgeId({ source: source.id, target: target.id, relation: String(prop.name) }),\n source: source.id,\n target: target.id,\n });\n }\n }\n }\n });\n\n return graph;\n};\n", "//\n// Copyright 2024 DXOS.org\n//\n\nimport { create } from '@dxos/live-object';\n\nimport { Graph, type GraphNode, type GraphEdge } from './types';\nimport { removeElements } from './util';\n\n// TODO(burdon): Traversal/visitor.\n\n/**\n * Typed reactive object graph.\n */\nexport class ReadonlyGraphModel<Node extends GraphNode = any, Edge extends GraphEdge = any> {\n protected readonly _graph: Graph;\n\n constructor({ nodes = [], edges = [] }: Partial<Graph> = {}) {\n this._graph = create(Graph, { nodes, edges });\n }\n\n toJSON() {\n // Sort to enable stable comparisons.\n this._graph.nodes.sort(({ id: a }, { id: b }) => a.localeCompare(b));\n this._graph.edges.sort(({ id: a }, { id: b }) => a.localeCompare(b));\n return JSON.parse(JSON.stringify(this._graph));\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 getNode(id: string): Node | undefined {\n return this.nodes.find((node) => node.id === id);\n }\n\n getNodes({ type }: Partial<Node>): Node[] {\n return this.nodes.filter((node) => !type || type === node.type);\n }\n\n getEdge(id: string): Edge | undefined {\n return this.edges.find((edge) => edge.id === id);\n }\n\n getEdges({ type, source, target }: Partial<Edge>): 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\nexport class GraphModel<Node extends GraphNode = any, Edge extends GraphEdge = any> extends ReadonlyGraphModel<\n Node,\n Edge\n> {\n clear(): this {\n this._graph.nodes.length = 0;\n this._graph.edges.length = 0;\n return this;\n }\n\n addGraph(graph: GraphModel<Node, Edge>): this {\n this.addNodes(graph.nodes);\n this.addEdges(graph.edges);\n return this;\n }\n\n addGraphs(graphs: GraphModel<Node, Edge>[]): 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): this {\n this._graph.nodes.push(node);\n return this;\n }\n\n addNodes(nodes: Node[]): this {\n nodes.forEach((node) => this.addNode(node));\n return this;\n }\n\n addEdge(edge: Edge): this {\n this._graph.edges.push(edge);\n return this;\n }\n\n addEdges(edges: Edge[]): this {\n edges.forEach((edge) => this.addEdge(edge));\n return this;\n }\n\n // TODO(burdon): Return graph.\n\n removeNode(id: string): GraphModel<Node, Edge> {\n const nodes = removeElements(this._graph.nodes, (node) => node.id === id);\n const edges = removeElements(this._graph.edges, (edge) => edge.source === id || edge.target === id);\n return new GraphModel<Node, Edge>({ nodes, edges });\n }\n\n removeNodes(ids: string[]): GraphModel<Node, Edge> {\n const graphs = ids.map((id) => this.removeNode(id));\n return new GraphModel<Node, Edge>().addGraphs(graphs);\n }\n\n removeEdge(id: string): GraphModel<Node, Edge> {\n const edges = removeElements(this._graph.edges, (edge) => edge.id === id);\n return new GraphModel<Node, Edge>({ edges });\n }\n\n removeEdges(ids: string[]): GraphModel<Node, Edge> {\n const graphs = ids.map((id) => this.removeEdge(id));\n return new GraphModel<Node, Edge>().addGraphs(graphs);\n }\n}\n", "//\n// Copyright 2024 DXOS.org\n//\n\nimport { S } from '@dxos/echo-schema';\n\n// Prior art:\n// - https://graphology.github.io (TS, tree-shakable, multiple packages for extensions)\n// - https://github.com/dagrejs/graphlib (mature, extensive)\n// - https://github.com/avoidwork/tiny-graph\n\nexport const BaseGraphNode = S.Struct({\n id: S.String,\n type: S.optional(S.String),\n data: S.optional(S.Any),\n});\n\nexport type BaseGraphNode = S.Schema.Type<typeof BaseGraphNode>;\nexport type GraphNode<Data extends object | void = void> = BaseGraphNode & { data: Data };\n\nexport const BaseGraphEdge = S.Struct({\n id: S.String,\n type: S.optional(S.String),\n source: S.String,\n target: S.String,\n data: S.optional(S.Any),\n});\n\nexport type BaseGraphEdge = S.Schema.Type<typeof BaseGraphEdge>;\nexport type GraphEdge<Data extends object | void = void> = BaseGraphEdge & { data: Data };\n\n/**\n * Generic graph abstraction.\n */\nexport const Graph = S.Struct({\n nodes: S.mutable(S.Array(BaseGraphNode)),\n edges: S.mutable(S.Array(BaseGraphEdge)),\n});\n\nexport type Graph = S.Schema.Type<typeof Graph>;\n\nexport const emptyGraph: Graph = { nodes: [], edges: [] };\n", "//\n// Copyright 2024 DXOS.org\n//\n\n// TODO(burdon): GraphBuilder.\n\n// TODO(burdon): Move to @dxos/live-object?\nexport const removeElements = <T>(array: T[], predicate: (element: T, index: number) => boolean): T[] => {\n const deleted: T[] = [];\n for (let i = array.length - 1; i >= 0; i--) {\n if (predicate(array[i], i)) {\n deleted.push(...array.splice(i, 1));\n }\n }\n\n return deleted;\n};\n"],
|
|
5
|
+
"mappings": ";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAKA,yBAA2B;AAC3B,uBAA0B;AAC1B,yBAA0B;AAC1B,iBAAoB;AACpB,oBAAoC;ACLpC,IAAAA,sBAAuB;ACAvB,IAAAC,sBAAkB;AAOX,IAAMC,gBAAgBC,sBAAEC,OAAO;EACpCC,IAAIF,sBAAEG;EACNC,MAAMJ,sBAAEK,SAASL,sBAAEG,MAAM;EACzBG,MAAMN,sBAAEK,SAASL,sBAAEO,GAAG;AACxB,CAAA;AAKO,IAAMC,gBAAgBR,sBAAEC,OAAO;EACpCC,IAAIF,sBAAEG;EACNC,MAAMJ,sBA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tD;IAAQsD;IAAUrD;EAAO;AACpC;AAKO,IAAMyD,cAAc,CAACC,YAAAA;AAC1B,QAAM/B,QAAQ,IAAIS,WAAAA;AAGlBsB,UAAQf,QAAQ,CAACgB,WAAAA;AACfhC,UAAMiB,QAAQ;MAAEpD,IAAImE,OAAOnE;MAAIE,MAAMiE,OAAOC;MAAUhE,MAAM+D;IAAO,CAAA;EACrE,CAAA;AAGAD,UAAQf,QAAQ,CAACgB,WAAAA;AACf,UAAME,aAASC,8BAAUH,MAAAA;AACzB,QAAI,CAACE,QAAQ;AACXE,qBAAIC,KAAK,wBAAwB;QAAExE,IAAImE,OAAOnE,GAAGyE,MAAM,GAAG,CAAA;MAAG,GAAA;;;;;;AAC7D;IACF;AAGA,eAAWC,YAAQC,mCAAoBN,OAAOO,KAAKT,MAAAA,GAAS;AAC1D,UAAIO,KAAKG,WAAWC,8BAAWC,KAAK;AAClC,cAAMxE,SAAS4D;AACf,cAAM3D,SAAS2D,OAAOO,KAAKM,IAAI,GAAGxE;AAClC,YAAIA,QAAQ;AACV2B,gBAAMkB,QAAQ;YACZrD,IAAI4D,aAAa;cAAErD,QAAQA,OAAOP;cAAIQ,QAAQA,OAAOR;cAAI6D,UAAU5D,OAAOyE,KAAKM,IAAI;YAAE,CAAA;YACrFzE,QAAQA,OAAOP;YACfQ,QAAQA,OAAOR;UACjB,CAAA;QACF;MACF;IACF;EACF,CAAA;AAEA,SAAOmC;AACT;",
|
|
6
|
+
"names": ["import_live_object", "import_echo_schema", "BaseGraphNode", "S", "Struct", "id", "String", "type", "optional", "data", "Any", "BaseGraphEdge", "source", "target", "Graph", "nodes", "mutable", "Array", "edges", "emptyGraph", "removeElements", "array", "predicate", "deleted", "i", "length", "push", "splice", "ReadonlyGraphModel", "constructor", "_graph", "create", "toJSON", "sort", "a", "b", "localeCompare", "JSON", "parse", "stringify", "graph", "getNode", "find", "node", "getNodes", "filter", "getEdge", "edge", "getEdges", "GraphModel", "clear", "addGraph", "addNodes", "addEdges", "addGraphs", "graphs", "forEach", "addNode", "addEdge", "removeNode", "removeNodes", "ids", "map", "removeEdge", "removeEdges", "createEdgeId", "relation", "parseEdgeId", "split", "invariant", "createGraph", "objects", "object", "typename", "schema", "getSchema", "log", "info", "slice", "prop", "getSchemaProperties", "ast", "format", "FormatEnum", "Ref", "name"]
|
|
7
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"inputs":{"packages/common/graph/src/types.ts":{"bytes":3825,"imports":[{"path":"@dxos/echo-schema","kind":"import-statement","external":true}],"format":"esm"},"packages/common/graph/src/util.ts":{"bytes":1705,"imports":[],"format":"esm"},"packages/common/graph/src/graph.ts":{"bytes":12508,"imports":[{"path":"@dxos/live-object","kind":"import-statement","external":true},{"path":"packages/common/graph/src/types.ts","kind":"import-statement","original":"./types"},{"path":"packages/common/graph/src/util.ts","kind":"import-statement","original":"./util"}],"format":"esm"},"packages/common/graph/src/buidler.ts":{"bytes":7346,"imports":[{"path":"@dxos/echo-schema","kind":"import-statement","external":true},{"path":"@dxos/invariant","kind":"import-statement","external":true},{"path":"@dxos/live-object","kind":"import-statement","external":true},{"path":"@dxos/log","kind":"import-statement","external":true},{"path":"@dxos/schema","kind":"import-statement","external":true},{"path":"packages/common/graph/src/graph.ts","kind":"import-statement","original":"./graph"}],"format":"esm"},"packages/common/graph/src/index.ts":{"bytes":654,"imports":[{"path":"packages/common/graph/src/buidler.ts","kind":"import-statement","original":"./buidler"},{"path":"packages/common/graph/src/graph.ts","kind":"import-statement","original":"./graph"},{"path":"packages/common/graph/src/types.ts","kind":"import-statement","original":"./types"}],"format":"esm"}},"outputs":{"packages/common/graph/dist/lib/node/index.cjs.map":{"imports":[],"exports":[],"inputs":{},"bytes":12611},"packages/common/graph/dist/lib/node/index.cjs":{"imports":[{"path":"@dxos/echo-schema","kind":"import-statement","external":true},{"path":"@dxos/invariant","kind":"import-statement","external":true},{"path":"@dxos/live-object","kind":"import-statement","external":true},{"path":"@dxos/log","kind":"import-statement","external":true},{"path":"@dxos/schema","kind":"import-statement","external":true},{"path":"@dxos/live-object","kind":"import-statement","external":true},{"path":"@dxos/echo-schema","kind":"import-statement","external":true}],"exports":["BaseGraphEdge","BaseGraphNode","Graph","GraphModel","ReadonlyGraphModel","createEdgeId","createGraph","emptyGraph","parseEdgeId"],"entryPoint":"packages/common/graph/src/index.ts","inputs":{"packages/common/graph/src/buidler.ts":{"bytesInOutput":1716},"packages/common/graph/src/graph.ts":{"bytesInOutput":2460},"packages/common/graph/src/types.ts":{"bytesInOutput":454},"packages/common/graph/src/util.ts":{"bytesInOutput":223},"packages/common/graph/src/index.ts":{"bytesInOutput":0}},"bytes":5274}}}
|