-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconnection.ts
171 lines (153 loc) · 4.75 KB
/
connection.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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
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 { 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;
};
/**
* 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));
}
});
}
/**
* Register a connection
* @param connection connection to be registered
*/
register(connection: CCConnection): void {
const fromNodeId = nullthrows(
this.#store.nodePins.get(connection.from),
).nodeId;
const toNodeId = nullthrows(this.#store.nodePins.get(connection.to)).nodeId;
const fromNode = this.#store.nodes.get(fromNodeId);
const toNode = this.#store.nodes.get(toNodeId);
invariant(fromNode && toNode);
invariant(fromNode.parentComponentId === toNode.parentComponentId);
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()];
}
}