ar_sync 1.1.0 → 1.1.2
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 +4 -3
- data/.gitignore +2 -0
- data/Gemfile +1 -0
- data/ar_sync.gemspec +1 -1
- data/bin/console +2 -2
- data/core/ArSyncModel.d.ts +10 -12
- data/core/ArSyncModel.js +7 -5
- data/core/ArSyncStore.d.ts +11 -3
- data/core/ArSyncStore.js +112 -112
- data/core/hooks.d.ts +1 -0
- data/core/hooks.js +5 -4
- data/gemfiles/Gemfile-rails-6 +4 -3
- data/gemfiles/{Gemfile-rails-7 → Gemfile-rails-7-0} +4 -3
- data/gemfiles/Gemfile-rails-7-1 +10 -0
- data/lib/ar_sync/class_methods.rb +1 -5
- data/lib/ar_sync/core.rb +2 -6
- data/lib/ar_sync/instance_methods.rb +16 -1
- data/lib/ar_sync/rails.rb +14 -17
- data/lib/ar_sync/version.rb +1 -1
- data/src/core/ArSyncModel.ts +9 -8
- data/src/core/ArSyncStore.ts +128 -121
- data/src/core/hooks.ts +6 -5
- metadata +5 -19
- data/Gemfile.lock +0 -54
data/core/ArSyncStore.js
CHANGED
@@ -31,6 +31,7 @@ var __spreadArrays = (this && this.__spreadArrays) || function () {
|
|
31
31
|
return r;
|
32
32
|
};
|
33
33
|
Object.defineProperty(exports, "__esModule", { value: true });
|
34
|
+
exports.ArSyncStore = void 0;
|
34
35
|
var ArSyncApi_1 = require("./ArSyncApi");
|
35
36
|
var ModelBatchRequest = /** @class */ (function () {
|
36
37
|
function ModelBatchRequest() {
|
@@ -62,7 +63,7 @@ var ModelBatchRequest = /** @class */ (function () {
|
|
62
63
|
ArSyncApi_1.default.syncFetch({ api: api, query: query, params: { ids: ids } }).then(function (models) {
|
63
64
|
for (var _i = 0, models_1 = models; _i < models_1.length; _i++) {
|
64
65
|
var model = models_1[_i];
|
65
|
-
var req = requests.get(model.id);
|
66
|
+
var req = requests.get(model._sync.id);
|
66
67
|
if (req)
|
67
68
|
req.model = model;
|
68
69
|
}
|
@@ -95,8 +96,9 @@ var modelBatchRequest = new ModelBatchRequest;
|
|
95
96
|
var ArSyncContainerBase = /** @class */ (function () {
|
96
97
|
function ArSyncContainerBase() {
|
97
98
|
this.listeners = [];
|
99
|
+
this.parentModel = null;
|
98
100
|
}
|
99
|
-
ArSyncContainerBase.prototype.replaceData = function (_data,
|
101
|
+
ArSyncContainerBase.prototype.replaceData = function (_data, _parentSyncKeys) { };
|
100
102
|
ArSyncContainerBase.prototype.initForReload = function (request) {
|
101
103
|
var _this = this;
|
102
104
|
this.networkSubscriber = ArSyncStore.connectionManager.subscribeNetwork(function (state) {
|
@@ -156,7 +158,7 @@ var ArSyncContainerBase = /** @class */ (function () {
|
|
156
158
|
}
|
157
159
|
this.listeners = [];
|
158
160
|
};
|
159
|
-
ArSyncContainerBase.
|
161
|
+
ArSyncContainerBase.compactQueryAttributes = function (query) {
|
160
162
|
function compactAttributes(attributes) {
|
161
163
|
var attrs = {};
|
162
164
|
var keys = [];
|
@@ -201,6 +203,8 @@ var ArSyncContainerBase = /** @class */ (function () {
|
|
201
203
|
return result;
|
202
204
|
}
|
203
205
|
var result = compactQuery(query);
|
206
|
+
if (typeof result === 'object' && 'attributes' in result)
|
207
|
+
return result.attributes;
|
204
208
|
return result === true ? {} : result;
|
205
209
|
};
|
206
210
|
ArSyncContainerBase.parseQuery = function (query, attrsonly) {
|
@@ -244,59 +248,43 @@ var ArSyncContainerBase = /** @class */ (function () {
|
|
244
248
|
return attributes;
|
245
249
|
return { attributes: attributes, as: column, params: params };
|
246
250
|
};
|
247
|
-
ArSyncContainerBase.
|
251
|
+
ArSyncContainerBase.load = function (_a, root) {
|
248
252
|
var api = _a.api, id = _a.id, params = _a.params, query = _a.query;
|
249
253
|
var parsedQuery = ArSyncRecord.parseQuery(query);
|
250
|
-
var
|
254
|
+
var compactQueryAttributes = ArSyncRecord.compactQueryAttributes(parsedQuery);
|
251
255
|
if (id != null) {
|
252
|
-
return modelBatchRequest.fetch(api,
|
256
|
+
return modelBatchRequest.fetch(api, compactQueryAttributes, id).then(function (data) {
|
253
257
|
if (!data)
|
254
258
|
throw { retry: false };
|
255
|
-
var request = { api: api, id: id, query:
|
256
|
-
return new ArSyncRecord(parsedQuery, data, request, root);
|
259
|
+
var request = { api: api, id: id, query: compactQueryAttributes };
|
260
|
+
return new ArSyncRecord(parsedQuery, data, request, root, null, null);
|
257
261
|
});
|
258
262
|
}
|
259
263
|
else {
|
260
|
-
var request_1 = { api: api, query:
|
264
|
+
var request_1 = { api: api, query: compactQueryAttributes, params: params };
|
261
265
|
return ArSyncApi_1.default.syncFetch(request_1).then(function (response) {
|
262
266
|
if (!response) {
|
263
267
|
throw { retry: false };
|
264
268
|
}
|
265
|
-
else if (response.collection && response.order) {
|
266
|
-
return new ArSyncCollection(response.
|
269
|
+
else if (response.collection && response.order && response._sync) {
|
270
|
+
return new ArSyncCollection(response._sync.keys, 'collection', parsedQuery, response, request_1, root, null, null);
|
267
271
|
}
|
268
272
|
else if (response instanceof Array) {
|
269
|
-
return new ArSyncCollection([], '', parsedQuery, response, request_1, root);
|
273
|
+
return new ArSyncCollection([], '', parsedQuery, response, request_1, root, null, null);
|
270
274
|
}
|
271
275
|
else {
|
272
|
-
return new ArSyncRecord(parsedQuery, response, request_1, root);
|
276
|
+
return new ArSyncRecord(parsedQuery, response, request_1, root, null, null);
|
273
277
|
}
|
274
278
|
});
|
275
279
|
}
|
276
280
|
};
|
277
|
-
ArSyncContainerBase.load = function (apiParams, root) {
|
278
|
-
var _this = this;
|
279
|
-
if (!(apiParams instanceof Array))
|
280
|
-
return this._load(apiParams, root);
|
281
|
-
return new Promise(function (resolve, _reject) {
|
282
|
-
var resultModels = [];
|
283
|
-
var countdown = apiParams.length;
|
284
|
-
apiParams.forEach(function (param, i) {
|
285
|
-
_this._load(param, root).then(function (model) {
|
286
|
-
resultModels[i] = model;
|
287
|
-
countdown--;
|
288
|
-
if (countdown === 0)
|
289
|
-
resolve(resultModels);
|
290
|
-
});
|
291
|
-
});
|
292
|
-
});
|
293
|
-
};
|
294
281
|
return ArSyncContainerBase;
|
295
282
|
}());
|
296
283
|
var ArSyncRecord = /** @class */ (function (_super) {
|
297
284
|
__extends(ArSyncRecord, _super);
|
298
|
-
function ArSyncRecord(query, data, request, root) {
|
285
|
+
function ArSyncRecord(query, data, request, root, parentModel, parentKey) {
|
299
286
|
var _this = _super.call(this) || this;
|
287
|
+
_this.fetching = new Set();
|
300
288
|
_this.root = root;
|
301
289
|
if (request)
|
302
290
|
_this.initForReload(request);
|
@@ -304,54 +292,49 @@ var ArSyncRecord = /** @class */ (function (_super) {
|
|
304
292
|
_this.queryAttributes = query.attributes || {};
|
305
293
|
_this.data = {};
|
306
294
|
_this.children = {};
|
295
|
+
_this.rootRecord = !parentModel;
|
296
|
+
_this.id = data._sync.id;
|
297
|
+
_this.syncKeys = data._sync.keys;
|
307
298
|
_this.replaceData(data);
|
299
|
+
_this.parentModel = parentModel;
|
300
|
+
_this.parentKey = parentKey;
|
308
301
|
return _this;
|
309
302
|
}
|
310
|
-
ArSyncRecord.prototype.setSyncKeys = function (sync_keys) {
|
311
|
-
this.sync_keys = sync_keys !== null && sync_keys !== void 0 ? sync_keys : [];
|
312
|
-
};
|
313
303
|
ArSyncRecord.prototype.replaceData = function (data) {
|
314
|
-
this.
|
304
|
+
this.id = data._sync.id;
|
305
|
+
this.syncKeys = data._sync.keys;
|
315
306
|
this.unsubscribeAll();
|
316
|
-
if (this.data.id !== data.id) {
|
317
|
-
this.mark();
|
318
|
-
this.data.id = data.id;
|
319
|
-
}
|
320
307
|
this.paths = [];
|
321
308
|
for (var key in this.queryAttributes) {
|
322
309
|
var subQuery = this.queryAttributes[key];
|
323
310
|
var aliasName = subQuery.as || key;
|
324
311
|
var subData = data[aliasName];
|
325
312
|
var child = this.children[aliasName];
|
326
|
-
if (key === '
|
313
|
+
if (key === '_sync')
|
327
314
|
continue;
|
328
|
-
if (subData instanceof Array || (subData && subData.collection && subData.order)) {
|
315
|
+
if (subData instanceof Array || (subData && subData.collection && subData.order && subData._sync)) {
|
329
316
|
if (child) {
|
330
|
-
child.replaceData(subData, this.
|
317
|
+
child.replaceData(subData, this.syncKeys);
|
331
318
|
}
|
332
319
|
else {
|
333
|
-
var collection = new ArSyncCollection(this.
|
320
|
+
var collection = new ArSyncCollection(this.syncKeys, key, subQuery, subData, null, this.root, this, aliasName);
|
334
321
|
this.mark();
|
335
322
|
this.children[aliasName] = collection;
|
336
323
|
this.data[aliasName] = collection.data;
|
337
|
-
collection.parentModel = this;
|
338
|
-
collection.parentKey = aliasName;
|
339
324
|
}
|
340
325
|
}
|
341
326
|
else {
|
342
327
|
if (subQuery.attributes && Object.keys(subQuery.attributes).length > 0)
|
343
328
|
this.paths.push(key);
|
344
|
-
if (subData && subData.
|
329
|
+
if (subData && subData._sync) {
|
345
330
|
if (child) {
|
346
331
|
child.replaceData(subData);
|
347
332
|
}
|
348
333
|
else {
|
349
|
-
var model = new ArSyncRecord(subQuery, subData, null, this.root);
|
334
|
+
var model = new ArSyncRecord(subQuery, subData, null, this.root, this, aliasName);
|
350
335
|
this.mark();
|
351
336
|
this.children[aliasName] = model;
|
352
337
|
this.data[aliasName] = model.data;
|
353
|
-
model.parentModel = this;
|
354
|
-
model.parentKey = aliasName;
|
355
338
|
}
|
356
339
|
}
|
357
340
|
else {
|
@@ -368,6 +351,8 @@ var ArSyncRecord = /** @class */ (function (_super) {
|
|
368
351
|
}
|
369
352
|
if (this.queryAttributes['*']) {
|
370
353
|
for (var key in data) {
|
354
|
+
if (key === '_sync')
|
355
|
+
continue;
|
371
356
|
if (!this.queryAttributes[key] && this.data[key] !== data[key]) {
|
372
357
|
this.mark();
|
373
358
|
this.data[key] = data[key];
|
@@ -378,11 +363,12 @@ var ArSyncRecord = /** @class */ (function (_super) {
|
|
378
363
|
};
|
379
364
|
ArSyncRecord.prototype.onNotify = function (notifyData, path) {
|
380
365
|
var _this = this;
|
381
|
-
var action = notifyData.action, className = notifyData.
|
366
|
+
var action = notifyData.action, className = notifyData.class, id = notifyData.id;
|
382
367
|
var query = path && this.queryAttributes[path];
|
383
368
|
var aliasName = (query && query.as) || path;
|
384
369
|
if (action === 'remove') {
|
385
370
|
var child = this.children[aliasName];
|
371
|
+
this.fetching.delete(aliasName + ":" + id); // To cancel consumeAdd
|
386
372
|
if (child)
|
387
373
|
child.release();
|
388
374
|
this.children[aliasName] = null;
|
@@ -391,20 +377,25 @@ var ArSyncRecord = /** @class */ (function (_super) {
|
|
391
377
|
this.onChange([aliasName], null);
|
392
378
|
}
|
393
379
|
else if (action === 'add') {
|
394
|
-
|
380
|
+
var child = this.children[aliasName];
|
381
|
+
if (child instanceof ArSyncRecord && child.id === id)
|
395
382
|
return;
|
396
|
-
|
383
|
+
var fetchKey_1 = aliasName + ":" + id;
|
384
|
+
this.fetching.add(fetchKey_1);
|
385
|
+
modelBatchRequest.fetch(className, ArSyncRecord.compactQueryAttributes(query), id).then(function (data) {
|
386
|
+
// Record already removed
|
387
|
+
if (!_this.fetching.has(fetchKey_1))
|
388
|
+
return;
|
389
|
+
_this.fetching.delete(fetchKey_1);
|
397
390
|
if (!data || !_this.data)
|
398
391
|
return;
|
399
|
-
var model = new ArSyncRecord(query, data, null, _this.root);
|
392
|
+
var model = new ArSyncRecord(query, data, null, _this.root, _this, aliasName);
|
400
393
|
var child = _this.children[aliasName];
|
401
394
|
if (child)
|
402
395
|
child.release();
|
403
396
|
_this.children[aliasName] = model;
|
404
397
|
_this.mark();
|
405
398
|
_this.data[aliasName] = model.data;
|
406
|
-
model.parentModel = _this;
|
407
|
-
model.parentKey = aliasName;
|
408
399
|
_this.onChange([aliasName], model.data);
|
409
400
|
}).catch(function (e) {
|
410
401
|
console.error("failed to load " + className + ":" + id + " " + e);
|
@@ -426,13 +417,13 @@ var ArSyncRecord = /** @class */ (function (_super) {
|
|
426
417
|
ArSyncRecord.prototype.subscribeAll = function () {
|
427
418
|
var _this = this;
|
428
419
|
var callback = function (data) { return _this.onNotify(data); };
|
429
|
-
for (var _i = 0, _a = this.
|
420
|
+
for (var _i = 0, _a = this.syncKeys; _i < _a.length; _i++) {
|
430
421
|
var key = _a[_i];
|
431
422
|
this.subscribe(key, callback);
|
432
423
|
}
|
433
424
|
var _loop_1 = function (path) {
|
434
425
|
var pathCallback = function (data) { return _this.onNotify(data, path); };
|
435
|
-
for (var _i = 0, _a = this_1.
|
426
|
+
for (var _i = 0, _a = this_1.syncKeys; _i < _a.length; _i++) {
|
436
427
|
var key = _a[_i];
|
437
428
|
this_1.subscribe(key + path, pathCallback);
|
438
429
|
}
|
@@ -442,45 +433,42 @@ var ArSyncRecord = /** @class */ (function (_super) {
|
|
442
433
|
var path = _c[_b];
|
443
434
|
_loop_1(path);
|
444
435
|
}
|
436
|
+
if (this.rootRecord) {
|
437
|
+
var key = this.syncKeys[0];
|
438
|
+
if (key)
|
439
|
+
this.subscribe(key + '_destroy', function () { return _this.root.handleDestroy(); });
|
440
|
+
}
|
445
441
|
};
|
446
442
|
ArSyncRecord.prototype.patchQuery = function (key) {
|
447
|
-
var
|
448
|
-
|
449
|
-
|
450
|
-
|
451
|
-
if (attributes && Object.keys(val.attributes).length === 0)
|
452
|
-
attributes = null;
|
453
|
-
if (!attributes && !as && !params)
|
454
|
-
return key;
|
455
|
-
var result = {};
|
456
|
-
if (attributes)
|
457
|
-
result.attributes = attributes;
|
458
|
-
if (as)
|
459
|
-
result.as = as;
|
460
|
-
if (params)
|
461
|
-
result.params = params;
|
462
|
-
return result;
|
443
|
+
var _a;
|
444
|
+
var subQuery = this.queryAttributes[key];
|
445
|
+
if (subQuery)
|
446
|
+
return _a = {}, _a[key] = subQuery, _a;
|
463
447
|
};
|
464
448
|
ArSyncRecord.prototype.reloadQuery = function () {
|
465
|
-
var _a;
|
466
449
|
if (this.reloadQueryCache)
|
467
450
|
return this.reloadQueryCache;
|
468
|
-
var
|
451
|
+
var arrayQuery = [];
|
452
|
+
var hashQuery = {};
|
469
453
|
for (var key in this.queryAttributes) {
|
470
|
-
if (key === '
|
454
|
+
if (key === '_sync')
|
471
455
|
continue;
|
472
456
|
var val = this.queryAttributes[key];
|
473
457
|
if (!val || !val.attributes) {
|
474
|
-
|
458
|
+
arrayQuery === null || arrayQuery === void 0 ? void 0 : arrayQuery.push(key);
|
459
|
+
hashQuery[key] = true;
|
475
460
|
}
|
476
461
|
else if (!val.params && Object.keys(val.attributes).length === 0) {
|
477
|
-
|
462
|
+
arrayQuery = null;
|
463
|
+
hashQuery[key] = val;
|
478
464
|
}
|
479
465
|
}
|
480
|
-
return
|
466
|
+
return this.reloadQueryCache = arrayQuery || hashQuery;
|
481
467
|
};
|
482
468
|
ArSyncRecord.prototype.update = function (data) {
|
483
469
|
for (var key in data) {
|
470
|
+
if (key === '_sync')
|
471
|
+
continue;
|
484
472
|
var subQuery = this.queryAttributes[key];
|
485
473
|
if (subQuery && subQuery.attributes && Object.keys(subQuery.attributes).length > 0)
|
486
474
|
continue;
|
@@ -500,22 +488,23 @@ var ArSyncRecord = /** @class */ (function (_super) {
|
|
500
488
|
return;
|
501
489
|
this.data = __assign({}, this.data);
|
502
490
|
this.root.mark(this.data);
|
503
|
-
if (this.parentModel)
|
491
|
+
if (this.parentModel && this.parentKey)
|
504
492
|
this.parentModel.markAndSet(this.parentKey, this.data);
|
505
493
|
};
|
506
494
|
return ArSyncRecord;
|
507
495
|
}(ArSyncContainerBase));
|
508
496
|
var ArSyncCollection = /** @class */ (function (_super) {
|
509
497
|
__extends(ArSyncCollection, _super);
|
510
|
-
function ArSyncCollection(
|
498
|
+
function ArSyncCollection(parentSyncKeys, path, query, data, request, root, parentModel, parentKey) {
|
511
499
|
var _this = _super.call(this) || this;
|
512
500
|
_this.ordering = { orderBy: 'id', direction: 'asc' };
|
513
501
|
_this.aliasOrderKey = 'id';
|
502
|
+
_this.fetching = new Set();
|
514
503
|
_this.root = root;
|
515
504
|
_this.path = path;
|
516
505
|
_this.query = query;
|
517
506
|
_this.queryAttributes = query.attributes || {};
|
518
|
-
_this.
|
507
|
+
_this.compactQueryAttributes = ArSyncRecord.compactQueryAttributes(query);
|
519
508
|
if (request)
|
520
509
|
_this.initForReload(request);
|
521
510
|
if (query.params) {
|
@@ -523,7 +512,9 @@ var ArSyncCollection = /** @class */ (function (_super) {
|
|
523
512
|
}
|
524
513
|
_this.data = [];
|
525
514
|
_this.children = [];
|
526
|
-
_this.replaceData(data,
|
515
|
+
_this.replaceData(data, parentSyncKeys);
|
516
|
+
_this.parentModel = parentModel;
|
517
|
+
_this.parentKey = parentKey;
|
527
518
|
return _this;
|
528
519
|
}
|
529
520
|
ArSyncCollection.prototype.setOrdering = function (ordering) {
|
@@ -543,21 +534,21 @@ var ArSyncCollection = /** @class */ (function (_super) {
|
|
543
534
|
this.aliasOrderKey = (subQuery && subQuery.as) || orderBy;
|
544
535
|
this.ordering = { first: first, last: last, direction: direction, orderBy: orderBy };
|
545
536
|
};
|
546
|
-
ArSyncCollection.prototype.setSyncKeys = function (
|
537
|
+
ArSyncCollection.prototype.setSyncKeys = function (parentSyncKeys) {
|
547
538
|
var _this = this;
|
548
|
-
if (
|
549
|
-
this.
|
539
|
+
if (parentSyncKeys) {
|
540
|
+
this.syncKeys = parentSyncKeys.map(function (key) { return key + _this.path; });
|
550
541
|
}
|
551
542
|
else {
|
552
|
-
this.
|
543
|
+
this.syncKeys = [];
|
553
544
|
}
|
554
545
|
};
|
555
|
-
ArSyncCollection.prototype.replaceData = function (data,
|
556
|
-
this.setSyncKeys(
|
546
|
+
ArSyncCollection.prototype.replaceData = function (data, parentSyncKeys) {
|
547
|
+
this.setSyncKeys(parentSyncKeys);
|
557
548
|
var existings = new Map();
|
558
549
|
for (var _i = 0, _a = this.children; _i < _a.length; _i++) {
|
559
550
|
var child = _a[_i];
|
560
|
-
existings.set(child.
|
551
|
+
existings.set(child.id, child);
|
561
552
|
}
|
562
553
|
var collection;
|
563
554
|
if (Array.isArray(data)) {
|
@@ -572,16 +563,14 @@ var ArSyncCollection = /** @class */ (function (_super) {
|
|
572
563
|
for (var _b = 0, collection_1 = collection; _b < collection_1.length; _b++) {
|
573
564
|
var subData = collection_1[_b];
|
574
565
|
var model = undefined;
|
575
|
-
if (typeof (subData) === 'object' && subData && '
|
576
|
-
model = existings.get(subData.id);
|
566
|
+
if (typeof (subData) === 'object' && subData && '_sync' in subData)
|
567
|
+
model = existings.get(subData._sync.id);
|
577
568
|
var data_1 = subData;
|
578
569
|
if (model) {
|
579
570
|
model.replaceData(subData);
|
580
571
|
}
|
581
|
-
else if (subData.
|
582
|
-
model = new ArSyncRecord(this.query, subData, null, this.root);
|
583
|
-
model.parentModel = this;
|
584
|
-
model.parentKey = subData.id;
|
572
|
+
else if (subData._sync) {
|
573
|
+
model = new ArSyncRecord(this.query, subData, null, this.root, this, subData._sync.id);
|
585
574
|
}
|
586
575
|
if (model) {
|
587
576
|
newChildren.push(model);
|
@@ -591,7 +580,7 @@ var ArSyncCollection = /** @class */ (function (_super) {
|
|
591
580
|
}
|
592
581
|
while (this.children.length) {
|
593
582
|
var child = this.children.pop();
|
594
|
-
if (!existings.has(child.
|
583
|
+
if (!existings.has(child.id))
|
595
584
|
child.release();
|
596
585
|
}
|
597
586
|
if (this.data.length || newChildren.length)
|
@@ -612,11 +601,11 @@ var ArSyncCollection = /** @class */ (function (_super) {
|
|
612
601
|
var _this = this;
|
613
602
|
var _a = this.ordering, first = _a.first, last = _a.last, direction = _a.direction;
|
614
603
|
var limit = first || last;
|
615
|
-
if (this.
|
604
|
+
if (this.children.find(function (a) { return a.id === id; }))
|
616
605
|
return;
|
617
|
-
if (limit && limit <= this.
|
618
|
-
var lastItem = this.
|
619
|
-
var firstItem = this.
|
606
|
+
if (limit && limit <= this.children.length) {
|
607
|
+
var lastItem = this.children[this.children.length - 1];
|
608
|
+
var firstItem = this.children[0];
|
620
609
|
if (direction === 'asc') {
|
621
610
|
if (first) {
|
622
611
|
if (lastItem && lastItem.id < id)
|
@@ -638,12 +627,15 @@ var ArSyncCollection = /** @class */ (function (_super) {
|
|
638
627
|
}
|
639
628
|
}
|
640
629
|
}
|
641
|
-
|
630
|
+
this.fetching.add(id);
|
631
|
+
modelBatchRequest.fetch(className, this.compactQueryAttributes, id).then(function (data) {
|
632
|
+
// Record already removed
|
633
|
+
if (!_this.fetching.has(id))
|
634
|
+
return;
|
635
|
+
_this.fetching.delete(id);
|
642
636
|
if (!data || !_this.data)
|
643
637
|
return;
|
644
|
-
var model = new ArSyncRecord(_this.query, data, null, _this.root);
|
645
|
-
model.parentModel = _this;
|
646
|
-
model.parentKey = id;
|
638
|
+
var model = new ArSyncRecord(_this.query, data, null, _this.root, _this, id);
|
647
639
|
var overflow = limit && limit <= _this.data.length;
|
648
640
|
var rmodel;
|
649
641
|
_this.mark();
|
@@ -707,7 +699,8 @@ var ArSyncCollection = /** @class */ (function (_super) {
|
|
707
699
|
}
|
708
700
|
};
|
709
701
|
ArSyncCollection.prototype.consumeRemove = function (id) {
|
710
|
-
var idx = this.
|
702
|
+
var idx = this.children.findIndex(function (a) { return a.id === id; });
|
703
|
+
this.fetching.delete(id); // To cancel consumeAdd
|
711
704
|
if (idx < 0)
|
712
705
|
return;
|
713
706
|
this.mark();
|
@@ -718,7 +711,7 @@ var ArSyncCollection = /** @class */ (function (_super) {
|
|
718
711
|
};
|
719
712
|
ArSyncCollection.prototype.onNotify = function (notifyData) {
|
720
713
|
if (notifyData.action === 'add') {
|
721
|
-
this.consumeAdd(notifyData.
|
714
|
+
this.consumeAdd(notifyData.class, notifyData.id);
|
722
715
|
}
|
723
716
|
else if (notifyData.action === 'remove') {
|
724
717
|
this.consumeRemove(notifyData.id);
|
@@ -727,7 +720,7 @@ var ArSyncCollection = /** @class */ (function (_super) {
|
|
727
720
|
ArSyncCollection.prototype.subscribeAll = function () {
|
728
721
|
var _this = this;
|
729
722
|
var callback = function (data) { return _this.onNotify(data); };
|
730
|
-
for (var _i = 0, _a = this.
|
723
|
+
for (var _i = 0, _a = this.syncKeys; _i < _a.length; _i++) {
|
731
724
|
var key = _a[_i];
|
732
725
|
this.subscribe(key, callback);
|
733
726
|
}
|
@@ -739,7 +732,7 @@ var ArSyncCollection = /** @class */ (function (_super) {
|
|
739
732
|
};
|
740
733
|
ArSyncCollection.prototype.markAndSet = function (id, data) {
|
741
734
|
this.mark();
|
742
|
-
var idx = this.
|
735
|
+
var idx = this.children.findIndex(function (a) { return a.id === id; });
|
743
736
|
if (idx >= 0)
|
744
737
|
this.data[idx] = data;
|
745
738
|
};
|
@@ -748,7 +741,7 @@ var ArSyncCollection = /** @class */ (function (_super) {
|
|
748
741
|
return;
|
749
742
|
this.data = __spreadArrays(this.data);
|
750
743
|
this.root.mark(this.data);
|
751
|
-
if (this.parentModel)
|
744
|
+
if (this.parentModel && this.parentKey)
|
752
745
|
this.parentModel.markAndSet(this.parentKey, this.data);
|
753
746
|
};
|
754
747
|
return ArSyncCollection;
|
@@ -756,15 +749,22 @@ var ArSyncCollection = /** @class */ (function (_super) {
|
|
756
749
|
var ArSyncStore = /** @class */ (function () {
|
757
750
|
function ArSyncStore(request, _a) {
|
758
751
|
var immutable = (_a === void 0 ? {} : _a).immutable;
|
752
|
+
this.complete = false;
|
753
|
+
this.destroyed = false;
|
759
754
|
this.immutable = !!immutable;
|
760
755
|
this.markedForFreezeObjects = [];
|
761
756
|
this.changes = [];
|
762
757
|
this.eventListeners = { events: {}, serial: 0 };
|
763
758
|
this.request = request;
|
764
|
-
this.complete = false;
|
765
759
|
this.data = null;
|
766
760
|
this.load(0);
|
767
761
|
}
|
762
|
+
ArSyncStore.prototype.handleDestroy = function () {
|
763
|
+
this.release();
|
764
|
+
this.data = null;
|
765
|
+
this.destroyed = true;
|
766
|
+
this.trigger('destroy');
|
767
|
+
};
|
768
768
|
ArSyncStore.prototype.load = function (retryCount) {
|
769
769
|
var _this = this;
|
770
770
|
ArSyncContainerBase.load(this.request, this).then(function (container) {
|
@@ -862,4 +862,4 @@ var ArSyncStore = /** @class */ (function () {
|
|
862
862
|
};
|
863
863
|
return ArSyncStore;
|
864
864
|
}());
|
865
|
-
exports.
|
865
|
+
exports.ArSyncStore = ArSyncStore;
|
data/core/hooks.d.ts
CHANGED
data/core/hooks.js
CHANGED
@@ -18,7 +18,7 @@ function checkHooks() {
|
|
18
18
|
if (!useState)
|
19
19
|
throw 'uninitialized. needs `initializeHooks({ useState, useEffect, useMemo, useRef })`';
|
20
20
|
}
|
21
|
-
var initialResult = [null, { complete: false, notfound: undefined, connected: true }];
|
21
|
+
var initialResult = [null, { complete: false, notfound: undefined, connected: true, destroyed: false }];
|
22
22
|
function useArSyncModel(request) {
|
23
23
|
var _a;
|
24
24
|
checkHooks();
|
@@ -33,13 +33,13 @@ function useArSyncModel(request) {
|
|
33
33
|
}
|
34
34
|
var model = new ArSyncModel_1.default(request, { immutable: true });
|
35
35
|
function update() {
|
36
|
-
var complete = model.complete, notfound = model.notfound, connected = model.connected, data = model.data;
|
36
|
+
var complete = model.complete, notfound = model.notfound, connected = model.connected, destroyed = model.destroyed, data = model.data;
|
37
37
|
setResult(function (resultWas) {
|
38
38
|
var dataWas = resultWas[0], statusWas = resultWas[1];
|
39
|
-
var statusPersisted = statusWas.complete === complete && statusWas.notfound === notfound && statusWas.connected === connected;
|
39
|
+
var statusPersisted = statusWas.complete === complete && statusWas.notfound === notfound && statusWas.connected === connected && statusWas.destroyed === destroyed;
|
40
40
|
if (dataWas === data && statusPersisted)
|
41
41
|
return resultWas;
|
42
|
-
var status = statusPersisted ? statusWas : { complete: complete, notfound: notfound, connected: connected };
|
42
|
+
var status = statusPersisted ? statusWas : { complete: complete, notfound: notfound, connected: connected, destroyed: destroyed };
|
43
43
|
return [data, status];
|
44
44
|
});
|
45
45
|
}
|
@@ -51,6 +51,7 @@ function useArSyncModel(request) {
|
|
51
51
|
}
|
52
52
|
model.subscribe('load', update);
|
53
53
|
model.subscribe('change', update);
|
54
|
+
model.subscribe('destroy', update);
|
54
55
|
model.subscribe('connection', update);
|
55
56
|
return function () { return model.release(); };
|
56
57
|
}, [requestString]);
|
data/gemfiles/Gemfile-rails-6
CHANGED
@@ -1,9 +1,10 @@
|
|
1
|
-
source
|
1
|
+
source 'https://rubygems.org'
|
2
2
|
|
3
3
|
git_source(:github) {|repo_name| "https://github.com/#{repo_name}" }
|
4
4
|
|
5
5
|
# Specify your gem's dependencies in ar_sync.gemspec
|
6
|
-
gemspec path:
|
6
|
+
gemspec path: '..'
|
7
7
|
|
8
|
-
gem
|
8
|
+
gem 'sqlite3', '~> 1.4'
|
9
|
+
gem 'activerecord', '~> 6.0'
|
9
10
|
gem 'ar_serializer', github: 'tompng/ar_serializer'
|
@@ -1,9 +1,10 @@
|
|
1
|
-
source
|
1
|
+
source 'https://rubygems.org'
|
2
2
|
|
3
3
|
git_source(:github) {|repo_name| "https://github.com/#{repo_name}" }
|
4
4
|
|
5
5
|
# Specify your gem's dependencies in ar_sync.gemspec
|
6
|
-
gemspec path:
|
6
|
+
gemspec path: '..'
|
7
7
|
|
8
|
-
gem
|
8
|
+
gem 'sqlite3', '~> 1.4'
|
9
|
+
gem 'activerecord', '~> 7.0.0'
|
9
10
|
gem 'ar_serializer', github: 'tompng/ar_serializer'
|
@@ -0,0 +1,10 @@
|
|
1
|
+
source 'https://rubygems.org'
|
2
|
+
|
3
|
+
git_source(:github) {|repo_name| "https://github.com/#{repo_name}" }
|
4
|
+
|
5
|
+
# Specify your gem's dependencies in ar_sync.gemspec
|
6
|
+
gemspec path: '..'
|
7
|
+
|
8
|
+
gem 'sqlite3', '~> 1.4'
|
9
|
+
gem 'activerecord', '~> 7.1.0'
|
10
|
+
gem 'ar_serializer', github: 'tompng/ar_serializer'
|
@@ -173,12 +173,8 @@ module ArSync::ModelBase::ClassMethods
|
|
173
173
|
|
174
174
|
_sync_define :id
|
175
175
|
|
176
|
-
_sync_define :sync_keys, type: [:string] do |current_user|
|
177
|
-
ArSync.sync_keys self, current_user
|
178
|
-
end
|
179
|
-
|
180
176
|
serializer_defaults namespace: :sync do |current_user|
|
181
|
-
{
|
177
|
+
{ _sync: _sync_field(current_user) }
|
182
178
|
end
|
183
179
|
|
184
180
|
after_initialize do
|
data/lib/ar_sync/core.rb
CHANGED
@@ -41,7 +41,7 @@ module ArSync
|
|
41
41
|
e = { action: action }
|
42
42
|
e[:field] = field if field
|
43
43
|
if model
|
44
|
-
e[:
|
44
|
+
e[:class] = model.class.base_class.name
|
45
45
|
e[:id] = model.id
|
46
46
|
end
|
47
47
|
event = ["#{key}#{path}", e]
|
@@ -53,10 +53,6 @@ module ArSync
|
|
53
53
|
end
|
54
54
|
end
|
55
55
|
|
56
|
-
def self.sync_keys(model, user)
|
57
|
-
[sync_key(model), sync_key(model, user)]
|
58
|
-
end
|
59
|
-
|
60
56
|
def self.sync_key(model, to_user = nil, signature: true)
|
61
57
|
if model.is_a? ArSync::Collection
|
62
58
|
key = [to_user&.id, model.klass.name, model.name].join '/'
|
@@ -91,7 +87,7 @@ module ArSync
|
|
91
87
|
serialized = ArSerializer.serialize target, query, context: user, use: :sync
|
92
88
|
return serialized if target.is_a? ArSync::ModelBase
|
93
89
|
{
|
94
|
-
|
90
|
+
_sync: { keys: [ArSync.sync_key(target), ArSync.sync_key(target, user)] },
|
95
91
|
ordering: target.ordering,
|
96
92
|
collection: serialized
|
97
93
|
}
|