Repository navigation
Expand file tree
/
Copy pathnode.ts
More file actions
138 lines (126 loc) · 3.71 KB
/
Copy pathnode.ts
File metadata and controls
138 lines (126 loc) · 3.71 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
import nullthrows from "nullthrows";
import invariant from "tiny-invariant";
import type { Vector2 } from "../common/vector2";
import { CCObjectStoreBase, type CCObjectStoreEvents } from "./base";
import type { CCComponentId } from "./component";
import { definitionByComponentId } from "./intrinsics/definitions";
import type { CCIntrinsicComponentSpec } from "./intrinsics/types";
import type { CCObjectId } from "./types";
export type CCNodeId = CCObjectId<"nodes">;
export type CCNode<
Spec extends CCIntrinsicComponentSpec = CCIntrinsicComponentSpec,
> = {
readonly id: CCNodeId;
readonly parentComponentId: CCComponentId;
readonly componentId: CCComponentId;
position: Vector2;
config: Spec["config"];
};
export type CCNodeStoreEvents = CCObjectStoreEvents<CCNode> & {
didUpdate(node: CCNode): void;
/**
* Emitted in addition to `didUpdate` when the config of a node changed, so that
* listeners depending on the config are not woken up by frequent position updates.
*/
didUpdateConfig(node: CCNode): void;
};
export const ccNodeStoreChangeEventTypes: (keyof CCNodeStoreEvents)[] = [
"didRegister",
"didUnregister",
"didUpdate",
];
/**
* Store of nodes
*/
export class CCNodeStore extends CCObjectStoreBase<"nodes", CCNodeStoreEvents> {
readonly references = {
parentComponentId: "components",
componentId: "components",
} as const;
import(nodes: CCNode[]): void {
for (const node of nodes) {
node.position = { x: node.position.x, y: node.position.y };
this.register(node);
}
}
mount() {
this.store.components.on("willUnregister", (component) => {
const ids = [...this.objects.values()]
.filter(
(node) =>
node.parentComponentId === component.id ||
node.componentId === component.id,
)
.map((node) => node.id);
if (ids.length > 0) {
this.unregister(ids);
}
});
}
/**
* Register a node
* @param node node to be registered
*/
register(node: CCNode): void {
invariant(this.store.components.get(node.componentId));
invariant(this.store.components.get(node.parentComponentId));
this.objects.set(node.id, node);
this.emit("didRegister", node);
}
/**
* Unregister nodes
* @param ids ids of nodes to be unregistered
*/
async unregister(ids: CCNodeId[]): Promise<void> {
const nodes = ids.map((id) => nullthrows(this.objects.get(id)));
await this.store.transactionManager.runInTransaction(() => {
for (const node of nodes) {
this.emit("willUnregister", node);
this.objects.delete(node.id);
}
});
for (const node of nodes) {
this.emit("didUnregister", node);
}
}
getManyByParentComponentId(parentComponentId: CCComponentId): CCNode[] {
return [...this.objects.values()].filter(
(node) => node.parentComponentId === parentComponentId,
);
}
getManyByComponentId(componentId: CCComponentId): CCNode[] {
return [...this.objects.values()].filter(
(node) => node.componentId === componentId,
);
}
/**
* Update position of node
* @param id id of node
* @param value new position
*/
update(
id: CCNodeId,
value: Partial<Pick<CCNode, "position" | "config">>,
): void {
const existingNode = nullthrows(this.objects.get(id));
const newNode = { ...existingNode, ...value };
this.objects.set(id, newNode);
if ("config" in value && value.config !== existingNode.config) {
this.emit("didUpdateConfig", newNode);
}
this.emit("didUpdate", newNode);
}
/**
* Create node
* @param partialNode node without `id`
* @returns new node
*/
static create(partialNode: Omit<CCNode, "id" | "config">): CCNode {
const definition = definitionByComponentId.get(partialNode.componentId);
return {
id: crypto.randomUUID() as CCNodeId,
config: definition ? definition.initialConfig : null,
...partialNode,
};
}
}