ar_sync 1.0.5 → 1.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.github/workflows/test.yml +27 -0
- data/Gemfile +1 -0
- data/Gemfile.lock +26 -28
- data/core/ArSyncApi.d.ts +8 -2
- data/core/ArSyncApi.js +11 -1
- data/core/ArSyncStore.js +192 -138
- data/core/ConnectionManager.d.ts +1 -1
- data/core/ConnectionManager.js +2 -0
- data/core/DataType.d.ts +14 -9
- data/core/hooks.d.ts +1 -0
- data/core/hooks.js +9 -6
- data/gemfiles/Gemfile-rails-6 +9 -0
- data/gemfiles/Gemfile-rails-7 +9 -0
- data/index.js +2 -2
- data/lib/ar_sync/class_methods.rb +64 -23
- data/lib/ar_sync/collection.rb +23 -19
- data/lib/ar_sync/core.rb +3 -3
- data/lib/ar_sync/instance_methods.rb +1 -1
- data/lib/ar_sync/rails.rb +1 -1
- data/lib/ar_sync/version.rb +1 -1
- data/package-lock.json +1706 -227
- data/package.json +1 -1
- data/src/core/ArSyncApi.ts +20 -7
- data/src/core/ArSyncStore.ts +177 -125
- data/src/core/ConnectionManager.ts +1 -0
- data/src/core/DataType.ts +14 -15
- data/src/core/hooks.ts +5 -5
- metadata +9 -7
- data/lib/ar_sync/field.rb +0 -96
data/core/ArSyncStore.js
CHANGED
@@ -32,51 +32,55 @@ var __spreadArrays = (this && this.__spreadArrays) || function () {
|
|
32
32
|
};
|
33
33
|
Object.defineProperty(exports, "__esModule", { value: true });
|
34
34
|
var ArSyncApi_1 = require("./ArSyncApi");
|
35
|
-
var ModelBatchRequest = {
|
36
|
-
|
37
|
-
|
38
|
-
|
35
|
+
var ModelBatchRequest = /** @class */ (function () {
|
36
|
+
function ModelBatchRequest() {
|
37
|
+
this.timer = null;
|
38
|
+
this.apiRequests = new Map();
|
39
|
+
}
|
40
|
+
ModelBatchRequest.prototype.fetch = function (api, query, id) {
|
39
41
|
var _this = this;
|
40
42
|
this.setTimer();
|
41
|
-
return new Promise(function (resolve) {
|
43
|
+
return new Promise(function (resolve, reject) {
|
42
44
|
var queryJSON = JSON.stringify(query);
|
43
|
-
var apiRequest = _this.apiRequests
|
44
|
-
|
45
|
-
|
46
|
-
|
45
|
+
var apiRequest = _this.apiRequests.get(api);
|
46
|
+
if (!apiRequest)
|
47
|
+
_this.apiRequests.set(api, apiRequest = new Map());
|
48
|
+
var queryRequests = apiRequest.get(queryJSON);
|
49
|
+
if (!queryRequests)
|
50
|
+
apiRequest.set(queryJSON, queryRequests = { query: query, requests: new Map() });
|
51
|
+
var request = queryRequests.requests.get(id);
|
52
|
+
if (!request)
|
53
|
+
queryRequests.requests.set(id, request = { id: id, callbacks: [] });
|
54
|
+
request.callbacks.push({ resolve: resolve, reject: reject });
|
47
55
|
});
|
48
|
-
}
|
49
|
-
batchFetch
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
var ids = Object.values(requests).map(function (_a) {
|
55
|
-
var id = _a.id;
|
56
|
-
return id;
|
57
|
-
});
|
56
|
+
};
|
57
|
+
ModelBatchRequest.prototype.batchFetch = function () {
|
58
|
+
this.apiRequests.forEach(function (apiRequest, api) {
|
59
|
+
apiRequest.forEach(function (_a) {
|
60
|
+
var query = _a.query, requests = _a.requests;
|
61
|
+
var ids = Array.from(requests.keys());
|
58
62
|
ArSyncApi_1.default.syncFetch({ api: api, query: query, params: { ids: ids } }).then(function (models) {
|
59
63
|
for (var _i = 0, models_1 = models; _i < models_1.length; _i++) {
|
60
64
|
var model = models_1[_i];
|
61
|
-
requests
|
62
|
-
|
63
|
-
|
64
|
-
var _c = _b[_a], model = _c.model, callbacks = _c.callbacks;
|
65
|
-
for (var _d = 0, callbacks_1 = callbacks; _d < callbacks_1.length; _d++) {
|
66
|
-
var callback = callbacks_1[_d];
|
67
|
-
callback(model);
|
68
|
-
}
|
65
|
+
var req = requests.get(model.id);
|
66
|
+
if (req)
|
67
|
+
req.model = model;
|
69
68
|
}
|
69
|
+
requests.forEach(function (_a) {
|
70
|
+
var model = _a.model, callbacks = _a.callbacks;
|
71
|
+
callbacks.forEach(function (cb) { return cb.resolve(model); });
|
72
|
+
});
|
73
|
+
}).catch(function (e) {
|
74
|
+
requests.forEach(function (_a) {
|
75
|
+
var callbacks = _a.callbacks;
|
76
|
+
callbacks.forEach(function (cb) { return cb.reject(e); });
|
77
|
+
});
|
70
78
|
});
|
71
|
-
};
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
}
|
77
|
-
this.apiRequests = {};
|
78
|
-
},
|
79
|
-
setTimer: function () {
|
79
|
+
});
|
80
|
+
});
|
81
|
+
this.apiRequests.clear();
|
82
|
+
};
|
83
|
+
ModelBatchRequest.prototype.setTimer = function () {
|
80
84
|
var _this = this;
|
81
85
|
if (this.timer)
|
82
86
|
return;
|
@@ -84,8 +88,10 @@ var ModelBatchRequest = {
|
|
84
88
|
_this.timer = null;
|
85
89
|
_this.batchFetch();
|
86
90
|
}, 20);
|
87
|
-
}
|
88
|
-
|
91
|
+
};
|
92
|
+
return ModelBatchRequest;
|
93
|
+
}());
|
94
|
+
var modelBatchRequest = new ModelBatchRequest;
|
89
95
|
var ArSyncContainerBase = /** @class */ (function () {
|
90
96
|
function ArSyncContainerBase() {
|
91
97
|
this.listeners = [];
|
@@ -94,9 +100,14 @@ var ArSyncContainerBase = /** @class */ (function () {
|
|
94
100
|
ArSyncContainerBase.prototype.initForReload = function (request) {
|
95
101
|
var _this = this;
|
96
102
|
this.networkSubscriber = ArSyncStore.connectionManager.subscribeNetwork(function (state) {
|
97
|
-
if (state) {
|
98
|
-
|
99
|
-
|
103
|
+
if (!state) {
|
104
|
+
if (_this.onConnectionChange)
|
105
|
+
_this.onConnectionChange(false);
|
106
|
+
return;
|
107
|
+
}
|
108
|
+
if (request.id != null) {
|
109
|
+
modelBatchRequest.fetch(request.api, request.query, request.id).then(function (data) {
|
110
|
+
if (_this.data && data) {
|
100
111
|
_this.replaceData(data);
|
101
112
|
if (_this.onConnectionChange)
|
102
113
|
_this.onConnectionChange(true);
|
@@ -106,8 +117,17 @@ var ArSyncContainerBase = /** @class */ (function () {
|
|
106
117
|
});
|
107
118
|
}
|
108
119
|
else {
|
109
|
-
|
110
|
-
_this.
|
120
|
+
ArSyncApi_1.default.syncFetch(request).then(function (data) {
|
121
|
+
if (_this.data && data) {
|
122
|
+
_this.replaceData(data);
|
123
|
+
if (_this.onConnectionChange)
|
124
|
+
_this.onConnectionChange(true);
|
125
|
+
if (_this.onChange)
|
126
|
+
_this.onChange([], _this.data);
|
127
|
+
}
|
128
|
+
}).catch(function (e) {
|
129
|
+
console.error("failed to reload. " + e);
|
130
|
+
});
|
111
131
|
}
|
112
132
|
});
|
113
133
|
};
|
@@ -154,7 +174,7 @@ var ArSyncContainerBase = /** @class */ (function () {
|
|
154
174
|
return [true, false];
|
155
175
|
if (keys.length === 1)
|
156
176
|
return [keys[0], false];
|
157
|
-
return [keys];
|
177
|
+
return [keys, false];
|
158
178
|
}
|
159
179
|
var needsEscape = attrs['attributes'] || attrs['params'] || attrs['as'];
|
160
180
|
if (keys.length === 0)
|
@@ -180,13 +200,8 @@ var ArSyncContainerBase = /** @class */ (function () {
|
|
180
200
|
result.attributes = attributes;
|
181
201
|
return result;
|
182
202
|
}
|
183
|
-
|
184
|
-
|
185
|
-
return result === true ? {} : result;
|
186
|
-
}
|
187
|
-
catch (e) {
|
188
|
-
throw JSON.stringify(query) + e.stack;
|
189
|
-
}
|
203
|
+
var result = compactQuery(query);
|
204
|
+
return result === true ? {} : result;
|
190
205
|
};
|
191
206
|
ArSyncContainerBase.parseQuery = function (query, attrsonly) {
|
192
207
|
var attributes = {};
|
@@ -233,13 +248,21 @@ var ArSyncContainerBase = /** @class */ (function () {
|
|
233
248
|
var api = _a.api, id = _a.id, params = _a.params, query = _a.query;
|
234
249
|
var parsedQuery = ArSyncRecord.parseQuery(query);
|
235
250
|
var compactQuery = ArSyncRecord.compactQuery(parsedQuery);
|
236
|
-
if (id) {
|
237
|
-
return
|
251
|
+
if (id != null) {
|
252
|
+
return modelBatchRequest.fetch(api, compactQuery, id).then(function (data) {
|
253
|
+
if (!data)
|
254
|
+
throw { retry: false };
|
255
|
+
var request = { api: api, id: id, query: compactQuery };
|
256
|
+
return new ArSyncRecord(parsedQuery, data, request, root);
|
257
|
+
});
|
238
258
|
}
|
239
259
|
else {
|
240
260
|
var request_1 = { api: api, query: compactQuery, params: params };
|
241
261
|
return ArSyncApi_1.default.syncFetch(request_1).then(function (response) {
|
242
|
-
if (response
|
262
|
+
if (!response) {
|
263
|
+
throw { retry: false };
|
264
|
+
}
|
265
|
+
else if (response.collection && response.order) {
|
243
266
|
return new ArSyncCollection(response.sync_keys, 'collection', parsedQuery, response, request_1, root);
|
244
267
|
}
|
245
268
|
else if (response instanceof Array) {
|
@@ -278,16 +301,14 @@ var ArSyncRecord = /** @class */ (function (_super) {
|
|
278
301
|
if (request)
|
279
302
|
_this.initForReload(request);
|
280
303
|
_this.query = query;
|
304
|
+
_this.queryAttributes = query.attributes || {};
|
281
305
|
_this.data = {};
|
282
306
|
_this.children = {};
|
283
307
|
_this.replaceData(data);
|
284
308
|
return _this;
|
285
309
|
}
|
286
310
|
ArSyncRecord.prototype.setSyncKeys = function (sync_keys) {
|
287
|
-
this.sync_keys = sync_keys;
|
288
|
-
if (!this.sync_keys) {
|
289
|
-
this.sync_keys = [];
|
290
|
-
}
|
311
|
+
this.sync_keys = sync_keys !== null && sync_keys !== void 0 ? sync_keys : [];
|
291
312
|
};
|
292
313
|
ArSyncRecord.prototype.replaceData = function (data) {
|
293
314
|
this.setSyncKeys(data.sync_keys);
|
@@ -297,8 +318,8 @@ var ArSyncRecord = /** @class */ (function (_super) {
|
|
297
318
|
this.data.id = data.id;
|
298
319
|
}
|
299
320
|
this.paths = [];
|
300
|
-
for (var key in this.
|
301
|
-
var subQuery = this.
|
321
|
+
for (var key in this.queryAttributes) {
|
322
|
+
var subQuery = this.queryAttributes[key];
|
302
323
|
var aliasName = subQuery.as || key;
|
303
324
|
var subData = data[aliasName];
|
304
325
|
var child = this.children[aliasName];
|
@@ -345,9 +366,9 @@ var ArSyncRecord = /** @class */ (function (_super) {
|
|
345
366
|
}
|
346
367
|
}
|
347
368
|
}
|
348
|
-
if (this.
|
369
|
+
if (this.queryAttributes['*']) {
|
349
370
|
for (var key in data) {
|
350
|
-
if (!this.
|
371
|
+
if (!this.queryAttributes[key] && this.data[key] !== data[key]) {
|
351
372
|
this.mark();
|
352
373
|
this.data[key] = data[key];
|
353
374
|
}
|
@@ -357,8 +378,8 @@ var ArSyncRecord = /** @class */ (function (_super) {
|
|
357
378
|
};
|
358
379
|
ArSyncRecord.prototype.onNotify = function (notifyData, path) {
|
359
380
|
var _this = this;
|
360
|
-
var action = notifyData.action,
|
361
|
-
var query = path && this.
|
381
|
+
var action = notifyData.action, className = notifyData.class_name, id = notifyData.id;
|
382
|
+
var query = path && this.queryAttributes[path];
|
362
383
|
var aliasName = (query && query.as) || path;
|
363
384
|
if (action === 'remove') {
|
364
385
|
var child = this.children[aliasName];
|
@@ -372,7 +393,7 @@ var ArSyncRecord = /** @class */ (function (_super) {
|
|
372
393
|
else if (action === 'add') {
|
373
394
|
if (this.data[aliasName] && this.data[aliasName].id === id)
|
374
395
|
return;
|
375
|
-
|
396
|
+
modelBatchRequest.fetch(className, ArSyncRecord.compactQuery(query), id).then(function (data) {
|
376
397
|
if (!data || !_this.data)
|
377
398
|
return;
|
378
399
|
var model = new ArSyncRecord(query, data, null, _this.root);
|
@@ -385,16 +406,21 @@ var ArSyncRecord = /** @class */ (function (_super) {
|
|
385
406
|
model.parentModel = _this;
|
386
407
|
model.parentKey = aliasName;
|
387
408
|
_this.onChange([aliasName], model.data);
|
409
|
+
}).catch(function (e) {
|
410
|
+
console.error("failed to load " + className + ":" + id + " " + e);
|
388
411
|
});
|
389
412
|
}
|
390
413
|
else {
|
391
414
|
var field = notifyData.field;
|
392
415
|
var query_2 = field ? this.patchQuery(field) : this.reloadQuery();
|
393
|
-
if (query_2)
|
394
|
-
|
395
|
-
|
396
|
-
|
397
|
-
|
416
|
+
if (!query_2)
|
417
|
+
return;
|
418
|
+
modelBatchRequest.fetch(className, query_2, id).then(function (data) {
|
419
|
+
if (_this.data)
|
420
|
+
_this.update(data);
|
421
|
+
}).catch(function (e) {
|
422
|
+
console.error("failed to load patch " + className + ":" + id + " " + e);
|
423
|
+
});
|
398
424
|
}
|
399
425
|
};
|
400
426
|
ArSyncRecord.prototype.subscribeAll = function () {
|
@@ -404,7 +430,7 @@ var ArSyncRecord = /** @class */ (function (_super) {
|
|
404
430
|
var key = _a[_i];
|
405
431
|
this.subscribe(key, callback);
|
406
432
|
}
|
407
|
-
var
|
433
|
+
var _loop_1 = function (path) {
|
408
434
|
var pathCallback = function (data) { return _this.onNotify(data, path); };
|
409
435
|
for (var _i = 0, _a = this_1.sync_keys; _i < _a.length; _i++) {
|
410
436
|
var key = _a[_i];
|
@@ -414,11 +440,11 @@ var ArSyncRecord = /** @class */ (function (_super) {
|
|
414
440
|
var this_1 = this;
|
415
441
|
for (var _b = 0, _c = this.paths; _b < _c.length; _b++) {
|
416
442
|
var path = _c[_b];
|
417
|
-
|
443
|
+
_loop_1(path);
|
418
444
|
}
|
419
445
|
};
|
420
446
|
ArSyncRecord.prototype.patchQuery = function (key) {
|
421
|
-
var val = this.
|
447
|
+
var val = this.queryAttributes[key];
|
422
448
|
if (!val)
|
423
449
|
return;
|
424
450
|
var attributes = val.attributes, as = val.as, params = val.params;
|
@@ -440,10 +466,10 @@ var ArSyncRecord = /** @class */ (function (_super) {
|
|
440
466
|
if (this.reloadQueryCache)
|
441
467
|
return this.reloadQueryCache;
|
442
468
|
var reloadQuery = this.reloadQueryCache = { attributes: [] };
|
443
|
-
for (var key in this.
|
469
|
+
for (var key in this.queryAttributes) {
|
444
470
|
if (key === 'sync_keys')
|
445
471
|
continue;
|
446
|
-
var val = this.
|
472
|
+
var val = this.queryAttributes[key];
|
447
473
|
if (!val || !val.attributes) {
|
448
474
|
reloadQuery.attributes.push(key);
|
449
475
|
}
|
@@ -455,7 +481,7 @@ var ArSyncRecord = /** @class */ (function (_super) {
|
|
455
481
|
};
|
456
482
|
ArSyncRecord.prototype.update = function (data) {
|
457
483
|
for (var key in data) {
|
458
|
-
var subQuery = this.
|
484
|
+
var subQuery = this.queryAttributes[key];
|
459
485
|
if (subQuery && subQuery.attributes && Object.keys(subQuery.attributes).length > 0)
|
460
486
|
continue;
|
461
487
|
if (this.data[key] === data[key])
|
@@ -483,42 +509,39 @@ var ArSyncCollection = /** @class */ (function (_super) {
|
|
483
509
|
__extends(ArSyncCollection, _super);
|
484
510
|
function ArSyncCollection(sync_keys, path, query, data, request, root) {
|
485
511
|
var _this = _super.call(this) || this;
|
486
|
-
_this.
|
512
|
+
_this.ordering = { orderBy: 'id', direction: 'asc' };
|
487
513
|
_this.aliasOrderKey = 'id';
|
488
514
|
_this.root = root;
|
489
515
|
_this.path = path;
|
490
516
|
_this.query = query;
|
517
|
+
_this.queryAttributes = query.attributes || {};
|
491
518
|
_this.compactQuery = ArSyncRecord.compactQuery(query);
|
492
519
|
if (request)
|
493
520
|
_this.initForReload(request);
|
494
|
-
if (query.params
|
495
|
-
_this.setOrdering(query.params
|
521
|
+
if (query.params) {
|
522
|
+
_this.setOrdering(query.params);
|
496
523
|
}
|
497
524
|
_this.data = [];
|
498
525
|
_this.children = [];
|
499
526
|
_this.replaceData(data, sync_keys);
|
500
527
|
return _this;
|
501
528
|
}
|
502
|
-
ArSyncCollection.prototype.setOrdering = function (
|
503
|
-
var
|
504
|
-
var
|
505
|
-
|
506
|
-
|
507
|
-
|
508
|
-
|
509
|
-
|
510
|
-
|
511
|
-
|
512
|
-
|
513
|
-
|
514
|
-
|
515
|
-
|
516
|
-
|
517
|
-
|
518
|
-
throw 'limit with custom order key is not supported';
|
519
|
-
var subQuery = this.query.attributes[key];
|
520
|
-
this.aliasOrderKey = (subQuery && subQuery.as) || key;
|
521
|
-
this.order = { limit: limitNumber, mode: mode, key: key };
|
529
|
+
ArSyncCollection.prototype.setOrdering = function (ordering) {
|
530
|
+
var direction = 'asc';
|
531
|
+
var orderBy = 'id';
|
532
|
+
var first = undefined;
|
533
|
+
var last = undefined;
|
534
|
+
if (ordering.direction === 'desc')
|
535
|
+
direction = ordering.direction;
|
536
|
+
if (typeof ordering.orderBy === 'string')
|
537
|
+
orderBy = ordering.orderBy;
|
538
|
+
if (typeof ordering.first === 'number')
|
539
|
+
first = ordering.first;
|
540
|
+
if (typeof ordering.last === 'number')
|
541
|
+
last = ordering.last;
|
542
|
+
var subQuery = this.queryAttributes[orderBy];
|
543
|
+
this.aliasOrderKey = (subQuery && subQuery.as) || orderBy;
|
544
|
+
this.ordering = { first: first, last: last, direction: direction, orderBy: orderBy };
|
522
545
|
};
|
523
546
|
ArSyncCollection.prototype.setSyncKeys = function (sync_keys) {
|
524
547
|
var _this = this;
|
@@ -531,31 +554,31 @@ var ArSyncCollection = /** @class */ (function (_super) {
|
|
531
554
|
};
|
532
555
|
ArSyncCollection.prototype.replaceData = function (data, sync_keys) {
|
533
556
|
this.setSyncKeys(sync_keys);
|
534
|
-
var existings =
|
557
|
+
var existings = new Map();
|
535
558
|
for (var _i = 0, _a = this.children; _i < _a.length; _i++) {
|
536
559
|
var child = _a[_i];
|
537
|
-
existings
|
560
|
+
existings.set(child.data.id, child);
|
538
561
|
}
|
539
562
|
var collection;
|
540
|
-
if (
|
541
|
-
collection = data
|
542
|
-
this.setOrdering(data.order.limit, data.order.mode);
|
563
|
+
if (Array.isArray(data)) {
|
564
|
+
collection = data;
|
543
565
|
}
|
544
566
|
else {
|
545
|
-
collection = data;
|
567
|
+
collection = data.collection;
|
568
|
+
this.setOrdering(data.ordering);
|
546
569
|
}
|
547
570
|
var newChildren = [];
|
548
571
|
var newData = [];
|
549
572
|
for (var _b = 0, collection_1 = collection; _b < collection_1.length; _b++) {
|
550
573
|
var subData = collection_1[_b];
|
551
|
-
var model =
|
552
|
-
if (typeof (subData) === 'object' && subData && '
|
553
|
-
model = existings
|
574
|
+
var model = undefined;
|
575
|
+
if (typeof (subData) === 'object' && subData && 'sync_keys' in subData)
|
576
|
+
model = existings.get(subData.id);
|
554
577
|
var data_1 = subData;
|
555
578
|
if (model) {
|
556
579
|
model.replaceData(subData);
|
557
580
|
}
|
558
|
-
else if (subData.
|
581
|
+
else if (subData.sync_keys) {
|
559
582
|
model = new ArSyncRecord(this.query, subData, null, this.root);
|
560
583
|
model.parentModel = this;
|
561
584
|
model.parentKey = subData.id;
|
@@ -568,7 +591,7 @@ var ArSyncCollection = /** @class */ (function (_super) {
|
|
568
591
|
}
|
569
592
|
while (this.children.length) {
|
570
593
|
var child = this.children.pop();
|
571
|
-
if (!existings
|
594
|
+
if (!existings.has(child.data.id))
|
572
595
|
child.release();
|
573
596
|
}
|
574
597
|
if (this.data.length || newChildren.length)
|
@@ -587,63 +610,94 @@ var ArSyncCollection = /** @class */ (function (_super) {
|
|
587
610
|
};
|
588
611
|
ArSyncCollection.prototype.consumeAdd = function (className, id) {
|
589
612
|
var _this = this;
|
613
|
+
var _a = this.ordering, first = _a.first, last = _a.last, direction = _a.direction;
|
614
|
+
var limit = first || last;
|
590
615
|
if (this.data.findIndex(function (a) { return a.id === id; }) >= 0)
|
591
616
|
return;
|
592
|
-
if (
|
593
|
-
|
594
|
-
|
595
|
-
|
596
|
-
|
617
|
+
if (limit && limit <= this.data.length) {
|
618
|
+
var lastItem = this.data[this.data.length - 1];
|
619
|
+
var firstItem = this.data[0];
|
620
|
+
if (direction === 'asc') {
|
621
|
+
if (first) {
|
622
|
+
if (lastItem && lastItem.id < id)
|
623
|
+
return;
|
624
|
+
}
|
625
|
+
else {
|
626
|
+
if (firstItem && id < firstItem.id)
|
627
|
+
return;
|
628
|
+
}
|
597
629
|
}
|
598
630
|
else {
|
599
|
-
|
600
|
-
|
601
|
-
|
631
|
+
if (first) {
|
632
|
+
if (lastItem && id < lastItem.id)
|
633
|
+
return;
|
634
|
+
}
|
635
|
+
else {
|
636
|
+
if (firstItem && firstItem.id < id)
|
637
|
+
return;
|
638
|
+
}
|
602
639
|
}
|
603
640
|
}
|
604
|
-
|
641
|
+
modelBatchRequest.fetch(className, this.compactQuery, id).then(function (data) {
|
605
642
|
if (!data || !_this.data)
|
606
643
|
return;
|
607
644
|
var model = new ArSyncRecord(_this.query, data, null, _this.root);
|
608
645
|
model.parentModel = _this;
|
609
646
|
model.parentKey = id;
|
610
|
-
var overflow =
|
647
|
+
var overflow = limit && limit <= _this.data.length;
|
611
648
|
var rmodel;
|
612
649
|
_this.mark();
|
613
650
|
var orderKey = _this.aliasOrderKey;
|
614
|
-
|
615
|
-
|
616
|
-
|
617
|
-
|
618
|
-
|
619
|
-
_this.
|
620
|
-
|
621
|
-
|
622
|
-
|
623
|
-
_this.
|
651
|
+
var firstItem = _this.data[0];
|
652
|
+
var lastItem = _this.data[_this.data.length - 1];
|
653
|
+
if (direction === 'asc') {
|
654
|
+
if (firstItem && data[orderKey] < firstItem[orderKey]) {
|
655
|
+
_this.children.unshift(model);
|
656
|
+
_this.data.unshift(model.data);
|
657
|
+
}
|
658
|
+
else {
|
659
|
+
var skipSort = lastItem && lastItem[orderKey] < data[orderKey];
|
660
|
+
_this.children.push(model);
|
661
|
+
_this.data.push(model.data);
|
662
|
+
if (!skipSort)
|
663
|
+
_this.markAndSort();
|
624
664
|
}
|
625
665
|
}
|
626
666
|
else {
|
627
|
-
|
628
|
-
|
629
|
-
|
630
|
-
|
631
|
-
|
632
|
-
|
667
|
+
if (firstItem && data[orderKey] > firstItem[orderKey]) {
|
668
|
+
_this.children.unshift(model);
|
669
|
+
_this.data.unshift(model.data);
|
670
|
+
}
|
671
|
+
else {
|
672
|
+
var skipSort = lastItem && lastItem[orderKey] > data[orderKey];
|
673
|
+
_this.children.push(model);
|
674
|
+
_this.data.push(model.data);
|
675
|
+
if (!skipSort)
|
676
|
+
_this.markAndSort();
|
677
|
+
}
|
678
|
+
}
|
679
|
+
if (overflow) {
|
680
|
+
if (first) {
|
633
681
|
rmodel = _this.children.pop();
|
634
|
-
rmodel.release();
|
635
682
|
_this.data.pop();
|
636
683
|
}
|
684
|
+
else {
|
685
|
+
rmodel = _this.children.shift();
|
686
|
+
_this.data.shift();
|
687
|
+
}
|
688
|
+
rmodel.release();
|
637
689
|
}
|
638
690
|
_this.onChange([model.id], model.data);
|
639
691
|
if (rmodel)
|
640
692
|
_this.onChange([rmodel.id], null);
|
693
|
+
}).catch(function (e) {
|
694
|
+
console.error("failed to load " + className + ":" + id + " " + e);
|
641
695
|
});
|
642
696
|
};
|
643
697
|
ArSyncCollection.prototype.markAndSort = function () {
|
644
698
|
this.mark();
|
645
699
|
var orderKey = this.aliasOrderKey;
|
646
|
-
if (this.
|
700
|
+
if (this.ordering.direction === 'asc') {
|
647
701
|
this.children.sort(function (a, b) { return a.data[orderKey] < b.data[orderKey] ? -1 : +1; });
|
648
702
|
this.data.sort(function (a, b) { return a[orderKey] < b[orderKey] ? -1 : +1; });
|
649
703
|
}
|
data/core/ConnectionManager.d.ts
CHANGED
data/core/ConnectionManager.js
CHANGED
@@ -40,6 +40,8 @@ var ConnectionManager = /** @class */ (function () {
|
|
40
40
|
};
|
41
41
|
ConnectionManager.prototype.subscribe = function (key, func) {
|
42
42
|
var _this = this;
|
43
|
+
if (!this.networkStatus)
|
44
|
+
return { unsubscribe: function () { } };
|
43
45
|
var subscription = this.connect(key);
|
44
46
|
var id = subscription.serial++;
|
45
47
|
subscription.ref++;
|
data/core/DataType.d.ts
CHANGED
@@ -3,9 +3,7 @@ declare type RecordType = {
|
|
3
3
|
query: any;
|
4
4
|
};
|
5
5
|
};
|
6
|
-
declare type Values<T> = T
|
7
|
-
[K in keyof T]: infer U;
|
8
|
-
} ? U : never;
|
6
|
+
declare type Values<T> = T[keyof T];
|
9
7
|
declare type AddNullable<Test, Type> = null extends Test ? Type | null : Type;
|
10
8
|
declare type DataTypeExtractField<BaseType, Key extends keyof BaseType> = Exclude<BaseType[Key], null> extends RecordType ? AddNullable<BaseType[Key], {}> : BaseType[Key] extends RecordType[] ? {}[] : BaseType[Key];
|
11
9
|
declare type DataTypeExtractFieldsFromQuery<BaseType, Fields> = '*' extends Fields ? {
|
@@ -33,17 +31,24 @@ declare type CheckAttributesField<P, Q> = Q extends {
|
|
33
31
|
declare type IsAnyCompareLeftType = {
|
34
32
|
__any: never;
|
35
33
|
};
|
36
|
-
declare type CollectExtraFields<Type,
|
37
|
-
|
38
|
-
|
39
|
-
}
|
34
|
+
declare type CollectExtraFields<Type, Key> = IsAnyCompareLeftType extends Type ? never : Type extends ExtraFieldErrorType ? Key : Type extends (infer R)[] ? {
|
35
|
+
0: Values<{
|
36
|
+
[key in keyof R]: CollectExtraFields<R[key], key>;
|
37
|
+
}>;
|
38
|
+
1: never;
|
39
|
+
}[R extends object ? 0 : 1] : {
|
40
|
+
0: Values<{
|
41
|
+
[key in keyof Type]: CollectExtraFields<Type[key], key>;
|
42
|
+
}>;
|
43
|
+
1: never;
|
44
|
+
}[Type extends object ? 0 : 1];
|
40
45
|
declare type SelectString<T> = T extends string ? T : never;
|
41
46
|
declare type _ValidateDataTypeExtraFileds<Extra, Type> = SelectString<Extra> extends never ? Type : {
|
42
47
|
error: {
|
43
|
-
extraFields:
|
48
|
+
extraFields: Extra;
|
44
49
|
};
|
45
50
|
};
|
46
|
-
declare type ValidateDataTypeExtraFileds<Type> = _ValidateDataTypeExtraFileds<CollectExtraFields<Type,
|
51
|
+
declare type ValidateDataTypeExtraFileds<Type> = _ValidateDataTypeExtraFileds<CollectExtraFields<Type, never>, Type>;
|
47
52
|
declare type RequestBase = {
|
48
53
|
api: string;
|
49
54
|
query: any;
|
data/core/hooks.d.ts
CHANGED