-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.ts
78 lines (69 loc) · 2.22 KB
/
index.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
import invariant from "tiny-invariant";
import { type CCComponent, CCComponentStore } from "./component";
import { type CCComponentPin, CCComponentPinStore } from "./componentPin";
import { type CCConnection, CCConnectionStore } from "./connection";
import { registerIntrinsics } from "./intrinsics";
import { type CCNode, CCNodeStore } from "./node";
import { type CCNodePin, CCNodePinStore } from "./nodePin";
import TransactionManager from "./transaction";
/**
* Properties of CCStore from JSON used when restoring store from JSON
*/
export type CCStorePropsFromJson = {
components: CCComponent[];
nodes: CCNode[];
componentPins: CCComponentPin[];
nodePins: CCNodePin[];
connections: CCConnection[];
};
/**
* Store of components, nodes, pins, and connections
*/
export default class CCStore {
components: CCComponentStore;
nodes: CCNodeStore;
componentPins: CCComponentPinStore;
nodePins: CCNodePinStore;
connections: CCConnectionStore;
transactionManager = new TransactionManager();
/**
* Constructor of CCStore
* @param rootComponent root component
* @param props properties of store from JSON used when restoring store from JSON
*/
constructor(props?: CCStorePropsFromJson) {
this.components = new CCComponentStore(this);
this.nodes = new CCNodeStore(this);
this.componentPins = new CCComponentPinStore(this);
this.nodePins = new CCNodePinStore(this);
this.connections = new CCConnectionStore(this);
if (props) {
invariant(props);
const { components, nodes, componentPins, nodePins, connections } = props;
this.components.import(components);
this.nodes.import(nodes);
this.componentPins.import(componentPins);
this.nodePins.import(nodePins);
this.connections.import(connections);
}
registerIntrinsics(this);
this.components.mount();
this.nodes.mount();
this.componentPins.mount();
this.nodePins.mount();
this.connections.mount();
}
/**
* Get the JSON representation of the store
* @returns JSON representation of the store
*/
toJSON() {
return JSON.stringify({
components: this.components.getMany(),
nodes: this.nodes.getMany(),
componentPins: this.componentPins.getMany(),
nodePins: this.nodePins.getMany(),
connections: this.connections.getMany(),
});
}
}