-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathLocalStorageGraph.js
More file actions
195 lines (195 loc) · 6.79 KB
/
Copy pathLocalStorageGraph.js
File metadata and controls
195 lines (195 loc) · 6.79 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
import { GraphBase } from './GraphBase';
import { GraphEdge, GraphNode } from './graph';
import { setCompressedToStorage, getCompressedFromStorage } from '../utils/Compression';
export class LocalStorageGraph extends GraphBase {
constructor(desc, nodes = [], edges = [], storage = sessionStorage) {
super(desc, nodes, edges);
this.updateHandler = (event) => {
const s = event.target;
if (s instanceof GraphNode) {
this.updateNode(s);
}
if (s instanceof GraphEdge) {
this.updateEdge(s);
}
};
this.storage = storage;
const { uid } = this;
if (nodes.length > 0 || edges.length > 0) {
this.setItem(`${uid}.nodes`, JSON.stringify(nodes.map((d) => d.id)));
nodes.forEach((n) => {
this.setItem(`${uid}.node.${n.id}`, JSON.stringify(n.persist()));
n.on('setAttr', this.updateHandler);
});
this.setItem(`${uid}.edges`, JSON.stringify(edges.map((d) => d.id)));
edges.forEach((e) => {
this.setItem(`${uid}.edge.${e.id}`, JSON.stringify(e.persist()));
e.on('setAttr', this.updateHandler);
});
}
}
setItem(key, value) {
setCompressedToStorage(this.storage, key, value);
}
getItem(key) {
return getCompressedFromStorage(this.storage, key, null);
}
static migrate(graph, storage = sessionStorage) {
return Promise.resolve(graph.migrate()).then(({ nodes, edges }) => {
return new LocalStorageGraph(graph.desc, nodes, edges, storage);
});
}
migrate() {
this.nodes.forEach((n) => n.off('setAttr', this.updateHandler));
this.edges.forEach((n) => n.off('setAttr', this.updateHandler));
return super.migrate();
}
static load(desc, factory, storage = sessionStorage, reset = false) {
const r = new LocalStorageGraph(desc, [], [], storage);
if (!reset) {
r.load(factory);
}
return r;
}
static clone(graph, factory, storage = sessionStorage) {
const r = new LocalStorageGraph(graph.desc, [], [], storage);
r.restoreDump(graph.persist(), factory);
return r;
}
get uid() {
return `graph${this.desc.id}`;
}
load(factory) {
const { uid } = this;
if (this.getItem(`${uid}.nodes`) == null) {
return;
}
const nodeIds = this.getItem(`${uid}.nodes`);
const lookup = new Map();
nodeIds.forEach((id) => {
const n = this.getItem(`${uid}.node.${id}`);
const nn = factory.makeNode(n);
lookup.set(nn.id, nn);
nn.on('setAttr', this.updateHandler);
super.addNode(nn);
});
const edgeIds = this.getItem(`${uid}.edges`);
edgeIds.forEach((id) => {
const n = this.getItem(`${uid}.edge.${id}`);
const nn = factory.makeEdge(n, lookup.get.bind(lookup));
nn.on('setAttr', this.updateHandler);
super.addEdge(nn);
});
this.fire('loaded');
}
static delete(desc, storage = sessionStorage) {
const uid = `graph${desc.id}`;
JSON.parse(storage.getItem(`${uid}.nodes`) || '[]').forEach((id) => {
storage.removeItem(`${uid}.node.${id}`);
});
storage.removeItem(`${uid}.nodes`);
JSON.parse(storage.getItem(`${uid}.edges`) || '[]').forEach((id) => {
storage.removeItem(`${uid}.edge.${id}`);
});
storage.removeItem(`${uid}.edges`);
return true;
}
static update(desc, storage = sessionStorage) {
const uid = `graph${desc.id}`;
}
restoreDump(persisted, factory) {
const lookup = new Map();
persisted.nodes.forEach((p) => {
const n = factory.makeNode(p);
lookup.set(n.id, n);
this.addNode(n);
});
persisted.edges.forEach((p) => {
const n = factory.makeEdge(p, lookup.get.bind(lookup));
this.addEdge(n);
});
return this;
}
addNode(n) {
super.addNode(n);
const { uid } = this;
this.setItem(`${uid}.node.${n.id}`, n.persist());
this.setItem(`${uid}.nodes`, this.nodes.map((d) => d.id));
n.on('setAttr', this.updateHandler);
return this;
}
updateNode(n) {
super.updateNode(n);
const { uid } = this;
this.setItem(`${uid}.node.${n.id}`, n.persist());
return this;
}
removeNode(n) {
if (!super.removeNode(n)) {
return null;
}
const { uid } = this;
this.setItem(`${uid}.nodes`, this.nodes.map((d) => d.id));
this.storage.removeItem(`${uid}.node.${n.id}`);
n.off('setAttr', this.updateHandler);
return this;
}
addEdge(edgeOrSource, type, t) {
if (edgeOrSource instanceof GraphEdge) {
super.addEdge(edgeOrSource);
const e = edgeOrSource;
const { uid } = this;
this.setItem(`${uid}.edges`, this.edges.map((d) => d.id));
this.setItem(`${uid}.edge.${e.id}`, e.persist());
e.on('setAttr', this.updateHandler);
return this;
}
return super.addEdge(edgeOrSource, type, t);
}
removeEdge(e) {
if (!super.removeEdge(e)) {
return null;
}
// need to shift all
const { uid } = this;
this.setItem(`${uid}.edges`, this.edges.map((d) => d.id));
this.storage.removeItem(`${uid}.edge.${e.id}`);
e.off('setAttr', this.updateHandler);
return this;
}
updateEdge(e) {
super.updateEdge(e);
const { uid } = this;
this.setItem(`${uid}.edge.${e.id}`, e.persist());
return this;
}
clear() {
const { nnodes } = this;
const { nedges } = this;
if (nnodes === 0 && nedges === 0) {
return Promise.resolve(this);
}
this.nodes.forEach((n) => n.off('setAttr', this.updateHandler));
this.edges.forEach((n) => n.off('setAttr', this.updateHandler));
super.clear();
const { uid } = this;
this.getItem(`${uid}.nodes`).forEach((id) => {
this.storage.removeItem(`${uid}.node.${id}`);
});
this.storage.removeItem(`${uid}.nodes`);
this.getItem(`${uid}.edges`).forEach((id) => {
this.storage.removeItem(`${uid}.edge.${id}`);
});
this.storage.removeItem(`${uid}.edges`);
return Promise.resolve(this);
}
persist() {
const r = {
root: this.desc.id,
};
r.nodes = this.nodes.map((s) => s.persist());
r.edges = this.edges.map((l) => l.persist());
return r;
}
}
//# sourceMappingURL=LocalStorageGraph.js.map