@dxos/app-graph 0.3.3-main.052ad73

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (40) hide show
  1. package/LICENSE +8 -0
  2. package/README.md +13 -0
  3. package/dist/lib/browser/index.mjs +252 -0
  4. package/dist/lib/browser/index.mjs.map +7 -0
  5. package/dist/lib/browser/meta.json +1 -0
  6. package/dist/lib/browser/testing.mjs +98 -0
  7. package/dist/lib/browser/testing.mjs.map +7 -0
  8. package/dist/lib/node/index.cjs +290 -0
  9. package/dist/lib/node/index.cjs.map +7 -0
  10. package/dist/lib/node/meta.json +1 -0
  11. package/dist/lib/node/testing.cjs +124 -0
  12. package/dist/lib/node/testing.cjs.map +7 -0
  13. package/dist/types/src/action.d.ts +53 -0
  14. package/dist/types/src/action.d.ts.map +1 -0
  15. package/dist/types/src/graph-builder.d.ts +31 -0
  16. package/dist/types/src/graph-builder.d.ts.map +1 -0
  17. package/dist/types/src/graph.d.ts +45 -0
  18. package/dist/types/src/graph.d.ts.map +1 -0
  19. package/dist/types/src/graph.test.d.ts +2 -0
  20. package/dist/types/src/graph.test.d.ts.map +1 -0
  21. package/dist/types/src/index.d.ts +5 -0
  22. package/dist/types/src/index.d.ts.map +1 -0
  23. package/dist/types/src/node.d.ts +71 -0
  24. package/dist/types/src/node.d.ts.map +1 -0
  25. package/dist/types/src/stories/EchoGraph.stories.d.ts +10 -0
  26. package/dist/types/src/stories/EchoGraph.stories.d.ts.map +1 -0
  27. package/dist/types/src/stories/Tree.d.ts +14 -0
  28. package/dist/types/src/stories/Tree.d.ts.map +1 -0
  29. package/dist/types/src/testing.d.ts +52 -0
  30. package/dist/types/src/testing.d.ts.map +1 -0
  31. package/package.json +74 -0
  32. package/src/action.ts +69 -0
  33. package/src/graph-builder.ts +200 -0
  34. package/src/graph.test.ts +220 -0
  35. package/src/graph.ts +110 -0
  36. package/src/index.ts +8 -0
  37. package/src/node.ts +101 -0
  38. package/src/stories/EchoGraph.stories.tsx +233 -0
  39. package/src/stories/Tree.tsx +79 -0
  40. package/src/testing.ts +152 -0
@@ -0,0 +1,71 @@
1
+ import type { IconProps } from '@phosphor-icons/react';
2
+ import { type FC } from 'react';
3
+ import type { UnsubscribeCallback } from '@dxos/async';
4
+ import { type Action, type Label } from './action';
5
+ /**
6
+ * Called when a node is added to the graph, allowing other node builders to add children, actions or properties.
7
+ */
8
+ export type NodeBuilder = (parent: Node) => UnsubscribeCallback | void;
9
+ /**
10
+ * Represents a node in the graph.
11
+ */
12
+ export type Node<TData = any, TProperties extends Record<string, any> = Record<string, any>> = {
13
+ /**
14
+ * Globally unique ID.
15
+ */
16
+ id: string;
17
+ /**
18
+ * Parent node in the graph.
19
+ */
20
+ parent: Node | null;
21
+ /**
22
+ * Label to be used when displaying the node.
23
+ * For default labels, use a translated string.
24
+ *
25
+ * @example 'My Node'
26
+ * @example ['unknown node label, { ns: 'example-plugin' }]
27
+ */
28
+ label: Label;
29
+ /**
30
+ * Description to be used when displaying a detailed view of the node.
31
+ * For default descriptions, use a translated string.
32
+ */
33
+ description?: Label;
34
+ /**
35
+ * Icon to be used when displaying the node.
36
+ */
37
+ icon?: FC<IconProps>;
38
+ /**
39
+ * Properties of the node relevant to displaying the node.
40
+ *
41
+ * @example { index: 'a1' }
42
+ */
43
+ properties: TProperties;
44
+ /**
45
+ * Data the node represents.
46
+ */
47
+ data: TData;
48
+ /**
49
+ * Children of the node stored by their id.
50
+ */
51
+ childrenMap: Record<string, Node>;
52
+ /**
53
+ * Actions of the node stored by their id.
54
+ */
55
+ actionsMap: Record<string, Action>;
56
+ /**
57
+ * Children of the node in default order.
58
+ */
59
+ get children(): Node[];
60
+ /**
61
+ * Actions of the node in default order.
62
+ */
63
+ get actions(): Action[];
64
+ addProperty(key: string, value: any): void;
65
+ removeProperty(key: string): void;
66
+ addNode<TChildData = null, TChildProperties extends Record<string, any> = Record<string, any>>(id: string, ...node: (Pick<Node, 'id' | 'label'> & Partial<Node<TChildData, TChildProperties>>)[]): Node<TChildData, TChildProperties>[];
67
+ removeNode(id: string): Node;
68
+ addAction<TActionProperties extends Record<string, any> = Record<string, any>>(...action: (Pick<Action, 'id' | 'label' | 'invoke'> & Partial<Action<TActionProperties>>)[]): Action<TActionProperties>[];
69
+ removeAction(id: string): Action;
70
+ };
71
+ //# sourceMappingURL=node.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"node.d.ts","sourceRoot":"","sources":["../../../src/node.ts"],"names":[],"mappings":"AAIA,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,uBAAuB,CAAC;AACvD,OAAO,EAAE,KAAK,EAAE,EAAE,MAAM,OAAO,CAAC;AAEhC,OAAO,KAAK,EAAE,mBAAmB,EAAE,MAAM,aAAa,CAAC;AAEvD,OAAO,EAAE,KAAK,MAAM,EAAE,KAAK,KAAK,EAAE,MAAM,UAAU,CAAC;AAEnD;;GAEG;AACH,MAAM,MAAM,WAAW,GAAG,CAAC,MAAM,EAAE,IAAI,KAAK,mBAAmB,GAAG,IAAI,CAAC;AAEvE;;GAEG;AACH,MAAM,MAAM,IAAI,CAAC,KAAK,GAAG,GAAG,EAAE,WAAW,SAAS,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,GAAG,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,IAAI;IAC7F;;OAEG;IACH,EAAE,EAAE,MAAM,CAAC;IAEX;;OAEG;IACH,MAAM,EAAE,IAAI,GAAG,IAAI,CAAC;IAEpB;;;;;;OAMG;IACH,KAAK,EAAE,KAAK,CAAC;IAEb;;;OAGG;IACH,WAAW,CAAC,EAAE,KAAK,CAAC;IAEpB;;OAEG;IACH,IAAI,CAAC,EAAE,EAAE,CAAC,SAAS,CAAC,CAAC;IAErB;;;;OAIG;IAEH,UAAU,EAAE,WAAW,CAAC;IAExB;;OAEG;IAIH,IAAI,EAAE,KAAK,CAAC;IAEZ;;OAEG;IAEH,WAAW,EAAE,MAAM,CAAC,MAAM,EAAE,IAAI,CAAC,CAAC;IAElC;;OAEG;IACH,UAAU,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;IAEnC;;OAEG;IACH,IAAI,QAAQ,IAAI,IAAI,EAAE,CAAC;IAEvB;;OAEG;IACH,IAAI,OAAO,IAAI,MAAM,EAAE,CAAC;IAExB,WAAW,CAAC,GAAG,EAAE,MAAM,EAAE,KAAK,EAAE,GAAG,GAAG,IAAI,CAAC;IAC3C,cAAc,CAAC,GAAG,EAAE,MAAM,GAAG,IAAI,CAAC;IAElC,OAAO,CAAC,UAAU,GAAG,IAAI,EAAE,gBAAgB,SAAS,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,GAAG,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,EAC3F,EAAE,EAAE,MAAM,EACV,GAAG,IAAI,EAAE,CAAC,IAAI,CAAC,IAAI,EAAE,IAAI,GAAG,OAAO,CAAC,GAAG,OAAO,CAAC,IAAI,CAAC,UAAU,EAAE,gBAAgB,CAAC,CAAC,CAAC,EAAE,GACpF,IAAI,CAAC,UAAU,EAAE,gBAAgB,CAAC,EAAE,CAAC;IACxC,UAAU,CAAC,EAAE,EAAE,MAAM,GAAG,IAAI,CAAC;IAE7B,SAAS,CAAC,iBAAiB,SAAS,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,GAAG,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,EAC3E,GAAG,MAAM,EAAE,CAAC,IAAI,CAAC,MAAM,EAAE,IAAI,GAAG,OAAO,GAAG,QAAQ,CAAC,GAAG,OAAO,CAAC,MAAM,CAAC,iBAAiB,CAAC,CAAC,CAAC,EAAE,GAC1F,MAAM,CAAC,iBAAiB,CAAC,EAAE,CAAC;IAC/B,YAAY,CAAC,EAAE,EAAE,MAAM,GAAG,MAAM,CAAC;CAClC,CAAC"}
@@ -0,0 +1,10 @@
1
+ import '@dxosTheme';
2
+ declare const _default: {
3
+ title: string;
4
+ };
5
+ export default _default;
6
+ export declare const EchoGraph: {
7
+ render: () => JSX.Element;
8
+ decorators: import("@storybook/csf").DecoratorFunction<import("@storybook/react/dist/types-0a347bb9").R, any>[];
9
+ };
10
+ //# sourceMappingURL=EchoGraph.stories.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"EchoGraph.stories.d.ts","sourceRoot":"","sources":["../../../../src/stories/EchoGraph.stories.tsx"],"names":[],"mappings":"AAIA,OAAO,YAAY,CAAC;;;;AAkBpB,wBAEE;AA6MF,eAAO,MAAM,SAAS;;;CAGrB,CAAC"}
@@ -0,0 +1,14 @@
1
+ import { type FC } from 'react';
2
+ export declare const Tree: FC<{
3
+ data?: object;
4
+ }>;
5
+ export declare const Node: FC<{
6
+ data?: any;
7
+ root?: boolean;
8
+ }>;
9
+ export declare const KeyValue: FC<{
10
+ label: string;
11
+ data?: any;
12
+ className?: string;
13
+ }>;
14
+ //# sourceMappingURL=Tree.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"Tree.d.ts","sourceRoot":"","sources":["../../../../src/stories/Tree.tsx"],"names":[],"mappings":"AAIA,OAAc,EAAE,KAAK,EAAE,EAAiC,MAAM,OAAO,CAAC;AAMtE,eAAO,MAAM,IAAI,EAAE,EAAE,CAAC;IAAE,IAAI,CAAC,EAAE,MAAM,CAAA;CAAE,CAMtC,CAAC;AAEF,eAAO,MAAM,IAAI,EAAE,EAAE,CAAC;IAAE,IAAI,CAAC,EAAE,GAAG,CAAC;IAAC,IAAI,CAAC,EAAE,OAAO,CAAA;CAAE,CAsBnD,CAAC;AAEF,eAAO,MAAM,QAAQ,EAAE,EAAE,CAAC;IAAE,KAAK,EAAE,MAAM,CAAC;IAAC,IAAI,CAAC,EAAE,GAAG,CAAC;IAAC,SAAS,CAAC,EAAE,MAAM,CAAA;CAAE,CAiB1E,CAAC"}
@@ -0,0 +1,52 @@
1
+ import { type Action } from './action';
2
+ import { type Graph } from './graph';
3
+ import { type NodeBuilder, type Node } from './node';
4
+ /**
5
+ * Create a test node builder that always adds nodes and actions to the specified depth.
6
+ *
7
+ * @param id The id of the node builder, used to identify nodes created by this builder.
8
+ * @param depth The depth at which to add nodes and actions.
9
+ * @default 1
10
+ *
11
+ * @returns A test node builder
12
+ */
13
+ export declare const createTestNodeBuilder: (id: string, depth?: number) => {
14
+ nodeBuilder: NodeBuilder;
15
+ addNode: (parentId: string, node: Pick<Node, 'id' | 'label'> & Partial<Node>) => Node<any, Record<string, any>> | undefined;
16
+ removeNode: (parentId: string, id: string) => Node<any, Record<string, any>> | undefined;
17
+ addAction: (parentId: string, action: Pick<Action, 'id' | 'label' | 'invoke'> & Partial<Action>) => Action<Record<string, any>>[] | undefined;
18
+ removeAction: (parentId: string, id: string) => Action | undefined;
19
+ addProperty: (parentId: string, key: string, value: any) => void;
20
+ removeProperty: (parentId: string, key: string) => void;
21
+ };
22
+ /**
23
+ * Build a graph from a nested list of nodes.
24
+ *
25
+ * @param graph Graph to add nodes to.
26
+ * @param nodes Nodes to add to the
27
+ *
28
+ * @example
29
+ * const graph = new GraphStore();
30
+ * buildGraph(graph, [
31
+ * {
32
+ * id: 'test1',
33
+ * label: 'test1',
34
+ * children: [
35
+ * {
36
+ * id: 'test1.1',
37
+ * label: 'test1.1',
38
+ * },
39
+ * {
40
+ * id: 'test1.2',
41
+ * label: 'test1.2',
42
+ * },
43
+ * ],
44
+ * },
45
+ * {
46
+ * id: 'test2',
47
+ * label: 'test2',
48
+ * },
49
+ * ]);
50
+ */
51
+ export declare const buildGraph: (graph: Graph, id: string, nodes: any[]) => Graph;
52
+ //# sourceMappingURL=testing.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"testing.d.ts","sourceRoot":"","sources":["../../../src/testing.ts"],"names":[],"mappings":"AAIA,OAAO,EAAE,KAAK,MAAM,EAAE,MAAM,UAAU,CAAC;AACvC,OAAO,EAAE,KAAK,KAAK,EAAE,MAAM,SAAS,CAAC;AACrC,OAAO,EAAE,KAAK,WAAW,EAAE,KAAK,IAAI,EAAE,MAAM,QAAQ,CAAC;AAErD;;;;;;;;GAQG;AAEH,eAAO,MAAM,qBAAqB,OAAQ,MAAM;;wBAwBnB,MAAM,QAAQ,KAAK,IAAI,EAAE,IAAI,GAAG,OAAO,CAAC,GAAG,QAAQ,IAAI,CAAC;2BAWrD,MAAM,MAAM,MAAM;0BASnB,MAAM,UAAU,KAAK,MAAM,EAAE,IAAI,GAAG,OAAO,GAAG,QAAQ,CAAC,GAAG,QAAQ,MAAM,CAAC;6BAStE,MAAM,MAAM,MAAM;4BASnB,MAAM,OAAO,MAAM,SAAS,GAAG;+BAS5B,MAAM,OAAO,MAAM;CAUtD,CAAC;AAEF;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA4BG;AAGH,eAAO,MAAM,UAAU,UAAW,KAAK,MAAM,MAAM,SAAS,GAAG,EAAE,UAGhE,CAAC"}
package/package.json ADDED
@@ -0,0 +1,74 @@
1
+ {
2
+ "name": "@dxos/app-graph",
3
+ "version": "0.3.3-main.052ad73",
4
+ "description": "Constructs knowledge graphs for the purpose of building applications on top of",
5
+ "homepage": "https://dxos.org",
6
+ "bugs": "https://github.com/dxos/dxos/issues",
7
+ "license": "MIT",
8
+ "author": "DXOS.org",
9
+ "exports": {
10
+ ".": {
11
+ "browser": "./dist/lib/browser/index.mjs",
12
+ "import": "./dist/lib/browser/index.mjs",
13
+ "require": "./dist/lib/node/index.cjs",
14
+ "node": "./dist/lib/node/index.cjs"
15
+ },
16
+ "./testing": {
17
+ "browser": "./dist/lib/browser/testing.mjs",
18
+ "import": "./dist/lib/browser/testing.mjs",
19
+ "require": "./dist/lib/node/testing.cjs",
20
+ "node": "./dist/lib/node/testing.cjs"
21
+ }
22
+ },
23
+ "types": "dist/types/src/index.d.ts",
24
+ "typesVersions": {
25
+ "*": {
26
+ "testing": [
27
+ "dist/types/src/testing.d.ts"
28
+ ]
29
+ }
30
+ },
31
+ "files": [
32
+ "dist",
33
+ "src"
34
+ ],
35
+ "dependencies": {
36
+ "@preact/signals-react": "^1.3.6",
37
+ "deepsignal": "^1.3.6",
38
+ "lodash.get": "^4.4.2",
39
+ "lodash.set": "^4.3.2",
40
+ "mousetrap": "^1.6.5",
41
+ "@dxos/async": "0.3.3-main.052ad73",
42
+ "@dxos/debug": "0.3.3-main.052ad73",
43
+ "@dxos/invariant": "0.3.3-main.052ad73",
44
+ "@dxos/util": "0.3.3-main.052ad73"
45
+ },
46
+ "devDependencies": {
47
+ "@faker-js/faker": "^8.0.2",
48
+ "@phosphor-icons/react": "^2.0.5",
49
+ "@tldraw/indices": "2.0.0-alpha.14",
50
+ "@types/lodash.get": "^4.4.7",
51
+ "@types/lodash.set": "^4.3.7",
52
+ "@types/mousetrap": "^1.6.11",
53
+ "@types/react": "^18.0.21",
54
+ "@types/react-dom": "^18.0.6",
55
+ "react": "^18.2.0",
56
+ "react-dom": "^18.2.0",
57
+ "vite": "^4.3.9",
58
+ "@dxos/react-client": "0.3.3-main.052ad73",
59
+ "@dxos/react-surface": "0.3.3-main.052ad73",
60
+ "@dxos/react-ui": "0.3.3-main.052ad73",
61
+ "@dxos/react-ui-theme": "0.3.3-main.052ad73"
62
+ },
63
+ "peerDependencies": {
64
+ "@phosphor-icons/react": "^2.0.5",
65
+ "react": "^18.2.0",
66
+ "react-dom": "^18.2.0",
67
+ "@dxos/react-surface": "0.3.3-main.052ad73",
68
+ "@dxos/react-ui": "0.3.3-main.052ad73",
69
+ "@dxos/react-ui-theme": "0.3.3-main.052ad73"
70
+ },
71
+ "publishConfig": {
72
+ "access": "public"
73
+ }
74
+ }
package/src/action.ts ADDED
@@ -0,0 +1,69 @@
1
+ //
2
+ // Copyright 2023 DXOS.org
3
+ //
4
+
5
+ import type { IconProps } from '@phosphor-icons/react';
6
+ import type { FC } from 'react';
7
+
8
+ import type { MaybePromise } from '@dxos/util';
9
+
10
+ // TODO(thure): `Parameters<TFunction>` causes typechecking issues because `TFunction` has so many signatures.
11
+ export type Label = string | [string, { ns: string; count?: number }];
12
+
13
+ /**
14
+ * An action on a node in the graph which may be invoked by sending the associated intent.
15
+ */
16
+ export type Action<TProperties extends Record<string, any> = Record<string, any>> = {
17
+ /**
18
+ * Locally unique ID.
19
+ */
20
+ id: string;
21
+
22
+ /**
23
+ * Label to be used when displaying the node.
24
+ * For default labels, use a translated string.
25
+ *
26
+ * @example 'Test Action'
27
+ * @example ['test action label, { ns: 'example-plugin' }]
28
+ */
29
+ label: Label;
30
+
31
+ /**
32
+ * Icon to be used when displaying the node.
33
+ */
34
+ icon?: FC<IconProps>;
35
+
36
+ /**
37
+ * Key binding.
38
+ * NOTE: Alphanumeric characters should be declared in lowercase.
39
+ */
40
+ keyBinding?: string;
41
+
42
+ /**
43
+ * Properties of the node relevant to displaying the action.
44
+ *
45
+ * @example { index: 'a1' }
46
+ */
47
+ properties: TProperties;
48
+
49
+ /**
50
+ * Sub-actions of the node stored by their id.
51
+ */
52
+ actionsMap: Record<string, Action>;
53
+
54
+ /**
55
+ * Actions of the node in default order.
56
+ */
57
+ // TODO(burdon): Why get vs. prop?
58
+ get actions(): Action[];
59
+
60
+ invoke: () => MaybePromise<any>;
61
+
62
+ addProperty(key: string, value: any): void;
63
+ removeProperty(key: string): void;
64
+
65
+ addAction<TActionProperties extends Record<string, any> = Record<string, any>>(
66
+ ...action: (Pick<Action, 'id' | 'label' | 'invoke'> & Partial<Action<TActionProperties>>)[]
67
+ ): Action<TActionProperties>[];
68
+ removeAction(id: string): Action;
69
+ };
@@ -0,0 +1,200 @@
1
+ //
2
+ // Copyright 2023 DXOS.org
3
+ //
4
+
5
+ import { untracked } from '@preact/signals-react';
6
+ import { type RevertDeepSignal, deepSignal } from 'deepsignal/react';
7
+ import Mousetrap from 'mousetrap';
8
+
9
+ import { EventSubscriptions } from '@dxos/async';
10
+
11
+ import { type Action } from './action';
12
+ import { Graph } from './graph';
13
+ import { type Node, type NodeBuilder } from './node';
14
+
15
+ /**
16
+ * The builder...
17
+ */
18
+ export class GraphBuilder {
19
+ private readonly _nodeBuilders = new Map<string, NodeBuilder>();
20
+ private readonly _unsubscribe = new Map<string, EventSubscriptions>();
21
+
22
+ /**
23
+ * Register a node builder which will be called in order to construct the graph.
24
+ */
25
+ addNodeBuilder(id: string, builder: NodeBuilder): GraphBuilder {
26
+ this._nodeBuilders.set(id, builder);
27
+ return this;
28
+ }
29
+
30
+ /**
31
+ * Remove a node builder from the graph builder.
32
+ */
33
+ removeNodeBuilder(id: string): GraphBuilder {
34
+ this._nodeBuilders.delete(id);
35
+ return this;
36
+ }
37
+
38
+ /**
39
+ * Construct the graph, starting by calling all registered node builders on the root node.
40
+ * Node builders will be filtered out as they are used such that they are only used once on any given path.
41
+ * @param root
42
+ * @param path
43
+ */
44
+ build(root?: Node, path: string[] = []): Graph {
45
+ const graph: Graph = new Graph(root ?? this._createNode(() => graph, { id: 'root', label: 'Root' }));
46
+ return this._build(graph, graph.root, path);
47
+ }
48
+
49
+ /**
50
+ * Called recursively.
51
+ */
52
+ private _build(graph: Graph, node: Node, path: string[] = [], ignoreBuilders: string[] = []): Graph {
53
+ // TODO(wittjosiah): Should this support multiple paths to the same node?
54
+ graph._setPath(node.id, path);
55
+
56
+ // TODO(burdon): Document.
57
+ const subscriptions = this._unsubscribe.get(node.id) ?? new EventSubscriptions();
58
+ subscriptions.clear();
59
+
60
+ Array.from(this._nodeBuilders.entries())
61
+ .filter(([id]) => ignoreBuilders.findIndex((ignore) => ignore === id) === -1)
62
+ .forEach(([_, builder]) => {
63
+ const unsubscribe = builder(node);
64
+ unsubscribe && subscriptions.add(unsubscribe);
65
+ });
66
+
67
+ this._unsubscribe.set(node.id, subscriptions);
68
+
69
+ return graph;
70
+ }
71
+
72
+ private _createNode<TData = null, TProperties extends Record<string, any> = Record<string, any>>(
73
+ getGraph: () => Graph,
74
+ partial: Pick<Node, 'id' | 'label'> & Partial<Node<TData, TProperties>>,
75
+ path: string[] = [],
76
+ ignoreBuilders: string[] = [],
77
+ ): Node<TData, TProperties> {
78
+ // TODO(burdon): Document implications and rationale of deepSignal.
79
+ const node: Node<TData, TProperties> = deepSignal({
80
+ parent: null,
81
+ data: null as TData, // TODO(burdon): Allow null property?
82
+ properties: {} as TProperties,
83
+ childrenMap: {},
84
+ actionsMap: {},
85
+ // TODO(burdon): Document.
86
+ ...partial,
87
+
88
+ // TODO(wittjosiah): Default sort.
89
+ get children() {
90
+ return Object.values(node.childrenMap);
91
+ },
92
+ get actions() {
93
+ return Object.values(node.actionsMap);
94
+ },
95
+
96
+ addProperty: (key, value) => {
97
+ untracked(() => {
98
+ (node.properties as Record<string, any>)[key] = value;
99
+ });
100
+ },
101
+ removeProperty: (key) => {
102
+ untracked(() => {
103
+ delete (node.properties as Record<string, any>)[key];
104
+ });
105
+ },
106
+
107
+ addNode: (builder, ...partials) => {
108
+ return untracked(() => {
109
+ return partials.map((partial) => {
110
+ const builders = [...ignoreBuilders, builder];
111
+ const childPath = [...path, 'childrenMap', partial.id];
112
+ const child = this._createNode(getGraph, { ...partial, parent: node }, childPath, builders);
113
+ node.childrenMap[child.id] = child;
114
+ // TOOD(burdon): Defer triggering recursive updates until task has completed.
115
+ this._build(getGraph(), child, childPath, builders);
116
+ return child;
117
+ });
118
+ });
119
+ },
120
+ removeNode: (id) => {
121
+ return untracked(() => {
122
+ const child = node.childrenMap[id];
123
+ delete node.childrenMap[id];
124
+ return child;
125
+ });
126
+ },
127
+
128
+ addAction: (...partials) => {
129
+ return untracked(() => {
130
+ return partials.map((partial) => {
131
+ const action = this._createAction(partial);
132
+ if (action.keyBinding) {
133
+ Mousetrap.bind(action.keyBinding, () => {
134
+ action.invoke();
135
+ });
136
+ }
137
+
138
+ node.actionsMap[action.id] = action;
139
+ return action;
140
+ });
141
+ });
142
+ },
143
+ removeAction: (id) => {
144
+ return untracked(() => {
145
+ const action = node.actionsMap[id];
146
+ if (action.keyBinding) {
147
+ Mousetrap.unbind(action.keyBinding);
148
+ }
149
+
150
+ delete node.actionsMap[id];
151
+ return action;
152
+ });
153
+ },
154
+ }) as RevertDeepSignal<Node<TData, TProperties>>;
155
+
156
+ return node;
157
+ }
158
+
159
+ private _createAction<TProperties extends Record<string, any> = Record<string, any>>(
160
+ partial: Pick<Action, 'id' | 'label' | 'invoke'> & Partial<Action<TProperties>>,
161
+ ): Action<TProperties> {
162
+ const action: Action<TProperties> = deepSignal({
163
+ properties: {} as TProperties,
164
+ actionsMap: {},
165
+ ...partial,
166
+ // TODO(wittjosiah): Default sort.
167
+ get actions() {
168
+ return Object.values(action.actionsMap);
169
+ },
170
+ addAction: (...partials) => {
171
+ return untracked(() => {
172
+ return partials.map((partial) => {
173
+ const subAction = this._createAction(partial);
174
+ action.actionsMap[subAction.id] = subAction;
175
+ return subAction;
176
+ });
177
+ });
178
+ },
179
+ removeAction: (id) => {
180
+ return untracked(() => {
181
+ const subAction = action.actionsMap[id];
182
+ delete action.actionsMap[id];
183
+ return subAction;
184
+ });
185
+ },
186
+ addProperty: (key, value) => {
187
+ return untracked(() => {
188
+ (action.properties as Record<string, any>)[key] = value;
189
+ });
190
+ },
191
+ removeProperty: (key) => {
192
+ return untracked(() => {
193
+ delete (action.properties as Record<string, any>)[key];
194
+ });
195
+ },
196
+ }) as RevertDeepSignal<Action<TProperties>>;
197
+
198
+ return action;
199
+ }
200
+ }