-
Notifications
You must be signed in to change notification settings - Fork 0
/
view.js
340 lines (264 loc) · 7.1 KB
/
view.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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
import has from 'underscore/modules/_has.js';
import { nativeCreate, nativeIsArray } from 'underscore/modules/_setup.js';
import isFunction from 'underscore/modules/isFunction.js';
import isString from 'underscore/modules/isString.js';
import { applyTo } from 'helpers/apply';
import { array } from 'helpers/array';
import { indexBy } from 'helpers/arrayBy';
import { buffer } from 'helpers/buffer';
import { debug, log, warn } from 'helpers/log';
import { portion } from 'helpers/portion';
import { push } from 'helpers/push';
import { remove } from 'helpers/remove';
import { collection, data, model } from './data';
import { Expeditor } from './expeditor';
import { single } from './model';
var autoId = 0;
const views = data.views;
export class View {
constructor(o) {
this.init(o);
if (this.initialize) {
this.initialize(o);
}
}
init(o) {
this.model = model(o.model).aka;
this.id = [ 'view', this.model, ++autoId ].join('-');
this.generation = 0;
this.dependencies = {};
views[this.id] = this;
this.fields = [ 'id' ];
this.annexes = [];
this.addFields(o.fields);
this.addAnnexes(o.annexes);
applyTo(this, o, 'name');
}
destroy() {
views[this.id] = null;
log(this, 'destroyed', this.destroyed = true);
}
addFields(fields) {
array(fields).forEach(field => {
if (field) {
push(this, 'fields', field, 'uniq');
}
});
}
addAnnexes(annexes) {
array(annexes).forEach(func => {
this.annexes.push(
isFunction(func) ? { func } : func
);
});
}
async load(params = {}, data) {
log(this, 'load', JSON.stringify(params));
if (this.loading) {
warn(this, 'is already loading');
}
delete this.rawData;
delete this.data;
delete this.map;
var expeditor = this.getExpeditor(params);
try {
this.loading = true;
data = this.rawData = // debug
await this.sequent(expeditor, data);
data = this.data = // debug
await this.produce(expeditor, data);
}
finally {
this.loading = false;
}
if (!expeditor.aborted()) {
this.complete(expeditor, data);
}
}
getExpeditor(params, range) {
if (params.minYmd || params.maxYmd) {
range = range || [
params.minYmd || params.maxYmd,
params.maxYmd || params.minYmd,
];
delete params.minYmd;
delete params.maxYmd;
}
var fields = single(this.fields);
// skip annexes
this.annexes.forEach(annex => {
// field is calculated
remove(fields, annex.annex, 'all');
// todo: add annex.path to fields
});
return new Expeditor({
aborted: this.getAbortedFn(),
name: this.name,
model: this.model,
fields,
params,
range,
});
}
getAbortedFn() {
var generation = ++this.generation;
return (message) => {
if (this.generation !== generation) {
var s = 'aborted';
}
else if (this.destroyed) {
s = 'destroyed';
}
else {
return;
}
if (isString(message)) {
warn(this, message);
}
return s;
};
}
sequent(expeditor, data) {
return expeditor.sequent(data);
}
async produce(expeditor, data) {
var total = data.length;
var rows = new Array(total);
await portion.call(this, 'apply rows', total, expeditor.aborted, (i) => {
rows[i] = this.applyRow(data[i]);
});
return rows;
}
applyRow(src, dst) {
dst = src.produce(this.fields, dst);
for (var i = 0; i < this.annexes.length; i++) {
var annex = this.annexes[i];
annex.func.call(annex.scope || this, dst, annex);
}
return dst;
}
complete(expeditor, data) {
this.expeditor = expeditor;
this.map = indexBy(data, 'id');
this.finalize(expeditor, data, 'refresh');
}
finalize(expeditor, data, event) {
log(this, 'finalize', event, { data, expeditor });
this.addDependency(expeditor);
this.monCollections();
this.fireEvent(event, this, data, expeditor);
if (!this.ready) {
this.ready = true;
this.fireEvent('ready', this, data, expeditor);
}
}
fireEvent() {
// implementation
}
addDependency(expeditor) {
var deps = this.dependencies;
if (!isString(deps[expeditor.model])) {
push(deps, expeditor.model, expeditor.inversed);
}
if (expeditor.children) {
expeditor.children.forEach(this.addDependency, this);
}
}
monCollections() {
var deps = this.dependencies;
var v, c;
for (var aka in deps) {
if (nativeIsArray(v = deps[aka])) {
if ((c = collection(aka))) {
var counter = v.map(inversed => inversed.length);
var shortest = Math.min.apply(Math, counter);
shortest = counter.indexOf(shortest);
deps[aka] = v[shortest].join('.');
this.mon(c, 'change', this.applyChanges, this);
}
else {
warn(this, 'monitor skip non-loaded collection', aka);
}
}
}
log(this, 'monitor reverse fields', { dependencies: deps });
}
mon() {
// implementation
}
applyChanges(record) {
if (nativeIsArray(record)) {
record.forEach(this.applyChanges, this);
}
else {
this.monBuffer(record);
}
}
monBuffer(record) {
if (this.expeditor.aborted('buffer record in denied state')) {
return;
}
var aka = record.aka;
var field = this.dependencies[aka];
if (!field) {
warn(this, 'buffer field is not found in dependencies', aka);
return;
}
if (nativeIsArray(field)) {
warn(this, 'buffer field expected to be a string, array given for', aka);
return;
}
debug(this, 'update', aka, record.id);
record.query(field).forEach(id => {
buffer(this.id, id, this.processBuffer, this);
});
}
async processBuffer(ids) {
var expeditor = nativeCreate(this.expeditor);
if (expeditor.aborted('process buffer in denied state')) {
return;
}
var data = collection(this.model).find(ids);
data = await this.sequent(expeditor, data);
data = await this.produce(expeditor, data);
if (expeditor.aborted('update in denied state')) {
return;
}
var filtered = indexBy(data, 'id');
var rows = [];
ids.forEach(id => {
var src = filtered[id];
var dst = this.map[id];
var changed;
if (src) {
if (dst) {
// compare field values to make the decision "row is updated"
for (var f in dst) {
if (!(changed = has(dst, f))) {
break;
}
if (dst[f] !== src[f]) {
break;
}
}
debug(this, 'update row', id, changed ? 'changed' : 'skipped');
if (!changed) {
return;
}
}
else {
debug(this, 'add row', id);
}
dst = this.map[id] = src;
}
if (dst) {
rows.push(dst);
if (!src) {
debug(this, 'remove row', id);
delete this.map[id];
}
}
});
this.finalize(expeditor, rows, 'update');
}
}