-
Notifications
You must be signed in to change notification settings - Fork 0
/
skyrocket.js
218 lines (203 loc) · 5.2 KB
/
skyrocket.js
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
'use strict';
var assign = require('assignment');
var state = require('./lib/state');
var reactors = [];
var skyrocket = {
configure: configure,
op: op,
applyChanges: applyChanges,
scope: scope,
react: react
};
var rooms = [];
var queue = { join: [], leave: [] };
var queueTimer;
var operationHandlers = {
push: pushOp,
unshift: unshiftOp,
remove: manipOp,
edit: manipOp
};
var bound;
function configure (options) {
state.configure(options);
if (!bound) {
bound = true;
state.taunus.gradual.on('data', react);
}
}
function op (name, fn) {
operationHandlers[name] = fn;
}
function joining (reactor) {
state.taunus.track(reactor.container, reactor, { implodes: true });
}
function scope (container, viewModel) {
return assign({ on: on }, skyrocket);
function on (room, options, reaction) {
if (arguments.length === 2) {
reaction = options;
options = void 0;
}
var o = options || {};
var reactor = {
container: container,
viewModel: viewModel,
room: room,
applyChanges: o.applyChanges || applyChanges,
reaction: reaction || noop,
destroy: destroy
};
reactors.push(reactor);
enqueue('join', room);
joining(reactor);
return reactor;
function destroy () {
reactors.splice(reactors.indexOf(reactor), 1);
enqueue('leave', room);
}
function enqueue (method, room) {
var joins = method === 'join';
if (joins) {
move(queue.leave, queue.join);
} else {
move(queue.join, queue.leave);
}
if (queueTimer) {
clearTimeout(queueTimer);
}
queueTimer = setTimeout(flush, 0);
function move (from, to) {
var index = from.indexOf(room);
if (index !== -1) {
from.splice(index, 1);
}
to.push(room);
}
function flush () {
flushQueue('leave', true);
flushQueue('join', false);
}
function flushQueue (type, existing) {
var flushable = queue[type].filter(canFlush);
if (flushable.length) {
state.revolve(type, flushable);
flushable.forEach(update);
queue[type] = [];
}
function canFlush (room) {
var exists = rooms.indexOf(room) !== -1;
return exists === existing;
}
function update (room) {
var index = rooms.indexOf(room);
var exists = index !== -1;
if (exists === existing) {
if (type === 'join') {
rooms.push(room);
} else {
rooms.splice(room, 1);
}
}
}
}
}
}
}
function react (data) {
if (data.updates) {
data.updates.forEach(handleUpdate);
}
function handleUpdate (update) {
update.rooms.forEach(reactInRoom);
function reactInRoom (room) {
reactors.filter(byRoom).forEach(reactToUpdate);
function byRoom (reactor) {
return reactor.room === room;
}
}
function reactToUpdate (reactor) {
reactor.applyChanges(reactor.viewModel, update);
reactor.reaction(update);
}
}
}
function applyChanges (viewModel, update) {
var operations = update.operations || [];
assign(viewModel, update.model);
applyOperations(viewModel, operations);
}
function applyOperations (viewModel, operations) {
operations.forEach(seek);
function seek (operation) {
var lastTarget;
var lastCrumb;
var target = viewModel;
var crumbs = (operation.concern || '').split('.');
var crumb = crumbs.shift();
while (crumb && target) {
lastTarget = target;
lastCrumb = crumb;
target = target[crumb];
crumb = crumbs.shift();
}
applyChange(target, operation);
function applyChange (target, operation) {
var result;
var handler = operationHandlers[operation.op];
if (handler) {
result = handler(target, operation);
if (result !== void 0) {
if (lastTarget) {
lastTarget[lastCrumb] = result;
} else {
throw new Error('Forbidden attempt to replace the entire model');
}
}
} else {
throw new Error('Forbidden attempt to apply an unknown model change operation');
}
}
}
}
function noop () {}
function pushOp (target, operation) {
if (Array.isArray(target)) {
target.push(operation.model);
}
}
function unshiftOp (target, operation) {
if (Array.isArray(target)) {
target.unshift(operation.model);
}
}
function manipOp (target, operation) {
if (!Array.isArray(target)) {
return target;
}
var needle = lookup();
if (needle) {
operation.context = needle.item;
if (operation.op === 'edit') {
applyChanges(needle.item, operation);
} else if (operation.op === 'remove') {
target.splice(needle.index, 1);
}
}
function lookup () {
var query = operation.query;
var keys = Object.keys(query);
var i;
for (i = 0; i < target.length; i++) {
if (keys.every(matches(target[i]))) {
return { index: i, item: target[i] };
}
}
function matches (item) {
return function compare (key) { // assumes primitive values in query
return item[key] === query[key];
};
}
}
}
module.exports = skyrocket;