-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnode.ts
149 lines (132 loc) · 3.55 KB
/
node.ts
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
139
140
141
142
143
144
145
146
147
148
149
import EventEmitter from "eventemitter3";
import nullthrows from "nullthrows";
import invariant from "tiny-invariant";
import type { Opaque } from "type-fest";
import type CCStore from ".";
import type { Vector2 } from "../common/vector2";
import type { CCComponentId } from "./component";
export type CCNodeId = Opaque<string, "CCNodeId">;
export type CCNode = {
readonly id: CCNodeId;
readonly parentComponentId: CCComponentId;
readonly componentId: CCComponentId;
position: Vector2;
};
export type CCNodeStoreEvents = {
didRegister(node: CCNode): void;
willUnregister(node: CCNode): void;
didUnregister(node: CCNode): void;
didUpdate(node: CCNode): void;
};
/**
* Store of nodes
*/
export class CCNodeStore extends EventEmitter<CCNodeStoreEvents> {
#store: CCStore;
#nodes: Map<CCNodeId, CCNode> = new Map();
/**
* Constructor of CCNodeStore
* @param store store
* @param nodes initial nodes
*/
constructor(store: CCStore) {
super();
this.#store = store;
}
import(nodes: CCNode[]): void {
for (const node of nodes) {
node.position = { x: node.position.x, y: node.position.y };
this.register(node);
}
}
mount() {}
/**
* 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.#nodes.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.#nodes.get(id)));
await this.#store.transactionManager.runInTransaction(() => {
for (const node of nodes) {
this.emit("willUnregister", node);
this.#nodes.delete(node.id);
}
});
for (const node of nodes) {
this.emit("didUnregister", node);
}
}
/**
* Get a node by id
* @param id id of node
* @returns node of `id`
*/
get(id: CCNodeId): CCNode | undefined {
return this.#nodes.get(id);
}
/**
* Get all of nodes by parent component id
* @param parentComponentId id of parent component
* @returns nodes of parent component
* @deprecated in favor of {@link getManyByParentComponentId}
*/
getNodeIdsByParentComponentId(parentComponentId: CCComponentId): CCNodeId[] {
return this.getManyByParentComponentId(parentComponentId).map(
(node) => node.id,
);
}
getManyByParentComponentId(parentComponentId: CCComponentId): CCNode[] {
return [...this.#nodes.values()].filter(
(node) => node.parentComponentId === parentComponentId,
);
}
getManyByComponentId(componentId: CCComponentId): CCNode[] {
return [...this.#nodes.values()].filter(
(node) => node.componentId === componentId,
);
}
/**
* Update position of node
* @param id id of node
* @param value new position
*/
update(id: CCNodeId, value: Pick<CCNode, "position">): void {
const node = this.#nodes.get(id);
invariant(node);
this.#nodes.set(id, { ...node, ...value });
this.emit("didUpdate", node);
}
/**
* Create node
* @param partialNode node without `id`
* @returns new node
*/
static create(partialNode: Omit<CCNode, "id">): CCNode {
// invariant(
// hasVariablePinCount(partialNode.componentId)
// ? partialNode.intrinsicVariablePinCount !== null
// : partialNode.intrinsicVariablePinCount === null
// );
return {
id: crypto.randomUUID() as CCNodeId,
...partialNode,
};
}
/**
* Get array of nodes
* @returns array of nodes
*/
getMany(): CCNode[] {
return [...this.#nodes.values()];
}
}