-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconnection.ts
More file actions
218 lines (200 loc) · 6.39 KB
/
Copy pathconnection.ts
File metadata and controls
218 lines (200 loc) · 6.39 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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
import EventEmitter from "eventemitter3";
import nullthrows from "nullthrows";
import type { Opaque } from "type-fest";
import type CCStore from ".";
import type { CCComponentId } from "./component";
import type { CCComponentPinId } from "./componentPin";
import type { CCNodeId } from "./node";
import type { CCNodePinId } from "./nodePin";
export type CCConnectionId = Opaque<string, "CCConnectionId">;
export type CCConnectionEndpoint = {
readonly nodeId: CCNodeId;
readonly pinId: CCComponentPinId;
};
export type CCConnection = {
readonly id: CCConnectionId;
readonly from: CCNodePinId;
readonly to: CCNodePinId;
readonly parentComponentId: CCComponentId;
bentPortion: number;
};
export type CCConnectionStoreEvents = {
didRegister(Connection: CCConnection): void;
willUnregister(Connection: CCConnection): void;
didUnregister(Connection: CCConnection): void;
};
export const ccConnectionStoreChangeEventTypes: (keyof CCConnectionStoreEvents)[] =
["didRegister", "didUnregister"];
/**
* Store of connections
*/
export class CCConnectionStore extends EventEmitter<CCConnectionStoreEvents> {
#store: CCStore;
#connections: Map<CCConnectionId, CCConnection> = new Map();
/**
* Constructor of CCConnectionStore
* @param store store
* @param connections initial connections
*/
constructor(store: CCStore) {
super();
this.#store = store;
}
import(connections: CCConnection[]): void {
for (const connection of connections) {
this.#connections.set(connection.id, connection);
}
}
mount() {
this.#store.nodePins.on("willUnregister", (nodePin) => {
const connections = this.getConnectionsByNodePinId(nodePin.id);
if (connections.length > 0) {
this.unregister(connections.map((connection) => connection.id));
}
});
// A config change can change the bit width of a pin (e.g. the resolution of a
// display), which invalidates the connections that were valid when they were made.
// CCStore mounts CCNodePinStore first, so its bit width cache is already dropped.
this.#store.nodes.on("didUpdateConfig", (node) => {
// TODO: Dropping a connection can in turn invalidate another one. Resolving that
// needs a repeated sweep, which the re-validation on `didRegister` below lacks too.
const invalidatedConnections = this.getMany().filter(
(connection) =>
connection.parentComponentId === node.parentComponentId &&
!this.#store.nodePins.hasCompatibleBitWidths(
connection.from,
connection.to,
),
);
if (invalidatedConnections.length > 0) {
this.unregister(
invalidatedConnections.map((connection) => connection.id),
);
}
});
this.#store.connections.on("didRegister", (connection) => {
const component = nullthrows(
this.#store.components.get(connection.parentComponentId),
);
const nodes = this.#store.nodes.getManyByComponentId(component.id);
for (const node of nodes) {
const nodePins = this.#store.nodePins.getManyByNodeId(node.id);
for (const nodePin of nodePins) {
const connections = this.getConnectionsByNodePinId(nodePin.id);
for (const connection of connections) {
const parentComponentId = connection.parentComponentId;
const from = connection.from;
const to = connection.to;
this.unregister([connection.id]).then(() => {
if (this.#store.nodePins.isConnectable(from, to)) {
this.register(
CCConnectionStore.create({
from,
to,
parentComponentId,
bentPortion: 0.5,
}),
);
}
});
}
}
}
});
}
/**
* Register a connection
* @param connection connection to be registered
*/
register(connection: CCConnection): void {
const fromNodePinId = connection.from;
const toNodePinId = connection.to;
if (!this.#store.nodePins.isConnectable(fromNodePinId, toNodePinId)) {
return;
}
this.#connections.set(connection.id, connection);
this.emit("didRegister", connection);
}
/**
* Unregister a connection
* @param ids ids of connections to be unregistered
*/
async unregister(ids: CCConnectionId[]): Promise<void> {
const connections = ids.map((id) => nullthrows(this.#connections.get(id)));
await this.#store.transactionManager.runInTransaction(() => {
for (const connection of connections) {
this.emit("willUnregister", connection);
this.#connections.delete(connection.id);
}
});
for (const connection of connections) {
this.emit("didUnregister", connection);
}
}
/**
* Get a connection by CCConnectionId
* @param id id of connection
* @returns connection of `id`
*/
get(id: CCConnectionId): CCConnection | undefined {
return this.#connections.get(id);
}
/**
* Get all of connections
* @returns map of id and connection (read only)
*/
getConnectionIdsByParentComponentId(
parentComponentId: CCComponentId,
): CCConnectionId[] {
return [...this.#connections.values()]
.filter(
(connection) => connection.parentComponentId === parentComponentId,
)
.map((connection) => connection.id);
}
getManyByParentComponentId(parentComponentId: CCComponentId): CCConnection[] {
return [...this.#connections.values()].filter(
(connection) => connection.parentComponentId === parentComponentId,
);
}
/**
* Get connections by id of node and pin
* @param nodeId id of node
* @param pinId id of pin
* @returns connections connected to the pin of the node
*/
getConnectionsByNodePinId(nodePinId: CCNodePinId): CCConnection[] {
return [...this.#connections.values()].filter(
(connection) =>
connection.from === nodePinId || connection.to === nodePinId,
);
}
/**
* Check if no connection of the pin of the node existsno connection of the pin of the node exists
* @param nodeId id of node
* @param pinId id of pin
* @returns if no connection of the pin of the node exists, `true` returns (otherwise `false`)
* @deprecated in favor of {@link getConnectionsByNodePinId}
*/
hasNoConnectionOf(nodePinId: CCNodePinId): boolean {
return this.getConnectionsByNodePinId(nodePinId).length === 0;
}
/**
* Create a new connection
* @param partialConnection connection without `id`
* @returns a new connection
*/
static create(partialConnection: Omit<CCConnection, "id">): CCConnection {
return {
id: crypto.randomUUID() as CCConnectionId,
...partialConnection,
};
}
/**
* Get array of connections
* @returns array of connections
*/
getMany(): CCConnection[] {
return [...this.#connections.values()];
}
}