-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathauto.js
More file actions
242 lines (202 loc) · 5.63 KB
/
Copy pathauto.js
File metadata and controls
242 lines (202 loc) · 5.63 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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
const assert = require('assert');
const diff = require('./diff');
const patch = require('./patch');
const reverse = require('./reverse');
const { _clone, _crc, _configure, _config } = require('./helpers');
let HISTORY_LENGTH = 1000;
const meta = new WeakMap();
const _cid = _id();
class AutoPigeon {
constructor() {
meta.set(this, {
history: [],
stash: [],
warning: null,
gids: {},
});
}
static from(data, cid=_cid) {
let doc = new AutoPigeon();
meta.get(doc).cid = cid;
doc = AutoPigeon.change(doc, doc => Object.assign(doc, data));
return doc;
}
static _forge(data, cid=_cid) {
let doc = new AutoPigeon();
meta.get(doc).cid = cid;
Object.assign(doc, _clone(data));
return doc;
}
static alias(doc) {
let alias = new AutoPigeon();
meta.set(alias, meta.get(doc));
Object.assign(alias, doc);
return alias;
}
static init() {
return AutoPigeon.from({});
}
static clone(doc, historyLength=HISTORY_LENGTH) {
const clone = AutoPigeon._forge(doc);
meta.get(clone).history = meta.get(doc).history;
meta.get(clone).gids = _clone(meta.get(doc).gids);
AutoPigeon.pruneHistory(meta.get(clone), historyLength)
return clone;
}
static pruneHistory(meta, historyLength) {
const docHistoryLength = meta.history.length;
if (docHistoryLength > historyLength) {
const prunedHistory = meta.history.slice(0, docHistoryLength - historyLength);
for (const item of prunedHistory) {
delete meta.gids[item.gid];
}
}
meta.history = meta.history.slice(-historyLength);
}
static getChanges(left, right) {
const _diff = diff(left, right);
const changes = {
diff: _diff,
cid: meta.get(left).cid,
ts: _config.getTimestamp(),
seq: _seq(),
gid: _id(),
}
return changes;
}
static rewindChanges(doc, ts, cid) {
const { history } = meta.get(doc);
while (true) {
if (history.length <= 1) break;
const change = history[history.length - 1];
if (change.ts > ts || (change.ts == ts && change.cid > cid)) {
const c = meta.get(doc).history.pop();
patch(doc, reverse(c.diff));
delete meta.get(doc).gids[c.gid];
meta.get(doc).stash.push(c);
continue;
}
break;
}
}
static fastForwardChanges(doc) {
const { stash, history } = meta.get(doc);
let change;
while (change = stash.pop()) {
patch(doc, change.diff);
meta.get(doc).gids[change.gid] = 1;
history.push(change);
}
}
static applyChangesInPlace(doc, changes) {
return AutoPigeon.applyChanges(doc, changes, true);
}
static applyChanges(doc, changes, inplace) {
meta.get(doc).warning = null;
const newDoc = inplace ? doc : AutoPigeon.clone(doc);
if (meta.get(doc).gids[changes.gid]) {
return newDoc;
}
try {
AutoPigeon.rewindChanges(newDoc, changes.ts, changes.cid);
} catch (e) {
meta.get(newDoc).warning = 'rewind failed: ' + e;
}
try {
patch(newDoc, changes.diff);
meta.get(newDoc).gids[changes.gid] = 1;
} catch (e) {
meta.get(newDoc).warning = 'patch failed: ' + e;
}
try {
AutoPigeon.fastForwardChanges(newDoc);
} catch (e) {
meta.get(newDoc).warning = 'forward failed: ' + e;
}
const history = meta.get(newDoc).history;
let idx = history.length;
while (idx > 1 && history[idx - 1].ts > changes.ts) idx--;
history.splice(idx, 0, changes);
return newDoc;
}
static change(doc, fn) {
assert(doc instanceof AutoPigeon);
assert(fn instanceof Function);
const tmp = _clone(doc);
fn(tmp);
const changes = AutoPigeon.getChanges(doc, tmp);
return AutoPigeon.applyChanges(doc, changes);
}
static getHistory(doc) {
return meta.get(doc).history;
}
static merge(doc1, doc2) {
let doc = AutoPigeon.from({});
const history1 = AutoPigeon.getHistory(doc1);
const history2 = AutoPigeon.getHistory(doc2);
const changes = [];
while (history1.length || history2.length) {
if (!history2.length) {
changes.push(history1.shift());
} else if (!history1.length) {
changes.push(history2.shift());
} else if (history1[0].gid === history2[0].gid) {
changes.push(history1.shift() && history2.shift());
} else if (history1[0].ts < history2[0].ts) {
changes.push(history1.shift());
} else if (history1[0].ts == history2[0].ts) {
if (history1[0].seq < history2[0].seq) {
changes.push(history1.shift());
} else {
changes.push(history2.shift());
}
} else {
changes.push(history2.shift());
}
}
for (const c of changes) {
doc = AutoPigeon.applyChanges(doc, c);
}
return doc;
}
static getWarning(doc) {
return meta.get(doc).warning;
}
static getMissingDeps(doc) {
return false;
}
static setHistoryLength(len) {
HISTORY_LENGTH = len;
}
static setTimestamp(fn) {
_config.getTimestamp = fn;
}
static crc(doc) {
return _crc(doc);
}
static load(str, historyLength=HISTORY_LENGTH) {
const { meta: _meta, data } = JSON.parse(str);
AutoPigeon.pruneHistory(_meta, historyLength);
const doc = AutoPigeon.from(data);
Object.assign(meta.get(doc), _meta);
return doc;
}
static save(doc) {
const { cid, ..._meta } = meta.get(doc);
return JSON.stringify({
meta: _meta,
data: doc,
});
}
static configure(options) {
_configure(options);
}
}
function _id() {
return Math.random().toString(36).substring(2);
}
let seq = 0;
function _seq() {
return seq++;
}
module.exports = AutoPigeon;