ar_sync 1.0.4 → 1.0.5
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/Gemfile.lock +1 -1
- data/core/ActionCableAdapter.js +19 -17
- data/core/ArSyncApi.js +112 -48
- data/core/ArSyncModel.js +69 -60
- data/core/ArSyncStore.js +401 -314
- data/core/ConnectionManager.js +43 -38
- data/core/hooks.d.ts +4 -0
- data/core/hooks.js +61 -36
- data/lib/ar_sync/type_script.rb +9 -8
- data/lib/ar_sync/version.rb +1 -1
- data/src/core/DataType.ts +1 -1
- data/src/core/hooks.ts +29 -5
- data/tsconfig.json +2 -2
- metadata +2 -2
data/core/ArSyncStore.js
CHANGED
@@ -1,98 +1,147 @@
|
|
1
1
|
"use strict";
|
2
|
+
var __extends = (this && this.__extends) || (function () {
|
3
|
+
var extendStatics = function (d, b) {
|
4
|
+
extendStatics = Object.setPrototypeOf ||
|
5
|
+
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
|
6
|
+
function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
|
7
|
+
return extendStatics(d, b);
|
8
|
+
};
|
9
|
+
return function (d, b) {
|
10
|
+
extendStatics(d, b);
|
11
|
+
function __() { this.constructor = d; }
|
12
|
+
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
|
13
|
+
};
|
14
|
+
})();
|
15
|
+
var __assign = (this && this.__assign) || function () {
|
16
|
+
__assign = Object.assign || function(t) {
|
17
|
+
for (var s, i = 1, n = arguments.length; i < n; i++) {
|
18
|
+
s = arguments[i];
|
19
|
+
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
|
20
|
+
t[p] = s[p];
|
21
|
+
}
|
22
|
+
return t;
|
23
|
+
};
|
24
|
+
return __assign.apply(this, arguments);
|
25
|
+
};
|
26
|
+
var __spreadArrays = (this && this.__spreadArrays) || function () {
|
27
|
+
for (var s = 0, i = 0, il = arguments.length; i < il; i++) s += arguments[i].length;
|
28
|
+
for (var r = Array(s), k = 0, i = 0; i < il; i++)
|
29
|
+
for (var a = arguments[i], j = 0, jl = a.length; j < jl; j++, k++)
|
30
|
+
r[k] = a[j];
|
31
|
+
return r;
|
32
|
+
};
|
2
33
|
Object.defineProperty(exports, "__esModule", { value: true });
|
3
|
-
|
4
|
-
|
34
|
+
var ArSyncApi_1 = require("./ArSyncApi");
|
35
|
+
var ModelBatchRequest = {
|
5
36
|
timer: null,
|
6
37
|
apiRequests: {},
|
7
|
-
fetch(api, query, id) {
|
38
|
+
fetch: function (api, query, id) {
|
39
|
+
var _this = this;
|
8
40
|
this.setTimer();
|
9
|
-
return new Promise(resolve
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
41
|
+
return new Promise(function (resolve) {
|
42
|
+
var queryJSON = JSON.stringify(query);
|
43
|
+
var apiRequest = _this.apiRequests[api] = _this.apiRequests[api] || {};
|
44
|
+
var queryRequests = apiRequest[queryJSON] = apiRequest[queryJSON] || { query: query, requests: {} };
|
45
|
+
var request = queryRequests.requests[id] = queryRequests.requests[id] || { id: id, callbacks: [] };
|
14
46
|
request.callbacks.push(resolve);
|
15
47
|
});
|
16
48
|
},
|
17
|
-
batchFetch() {
|
18
|
-
|
19
|
-
for (
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
49
|
+
batchFetch: function () {
|
50
|
+
var apiRequests = this.apiRequests;
|
51
|
+
for (var api in apiRequests) {
|
52
|
+
var apiRequest = apiRequests[api];
|
53
|
+
var _loop_1 = function (query, requests) {
|
54
|
+
var ids = Object.values(requests).map(function (_a) {
|
55
|
+
var id = _a.id;
|
56
|
+
return id;
|
57
|
+
});
|
58
|
+
ArSyncApi_1.default.syncFetch({ api: api, query: query, params: { ids: ids } }).then(function (models) {
|
59
|
+
for (var _i = 0, models_1 = models; _i < models_1.length; _i++) {
|
60
|
+
var model = models_1[_i];
|
25
61
|
requests[model.id].model = model;
|
26
|
-
|
27
|
-
|
62
|
+
}
|
63
|
+
for (var _a = 0, _b = Object.values(requests); _a < _b.length; _a++) {
|
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];
|
28
67
|
callback(model);
|
68
|
+
}
|
29
69
|
}
|
30
70
|
});
|
71
|
+
};
|
72
|
+
for (var _i = 0, _a = Object.values(apiRequest); _i < _a.length; _i++) {
|
73
|
+
var _b = _a[_i], query = _b.query, requests = _b.requests;
|
74
|
+
_loop_1(query, requests);
|
31
75
|
}
|
32
76
|
}
|
33
77
|
this.apiRequests = {};
|
34
78
|
},
|
35
|
-
setTimer() {
|
79
|
+
setTimer: function () {
|
80
|
+
var _this = this;
|
36
81
|
if (this.timer)
|
37
82
|
return;
|
38
|
-
this.timer = setTimeout(()
|
39
|
-
|
40
|
-
|
83
|
+
this.timer = setTimeout(function () {
|
84
|
+
_this.timer = null;
|
85
|
+
_this.batchFetch();
|
41
86
|
}, 20);
|
42
87
|
}
|
43
88
|
};
|
44
|
-
|
45
|
-
|
89
|
+
var ArSyncContainerBase = /** @class */ (function () {
|
90
|
+
function ArSyncContainerBase() {
|
46
91
|
this.listeners = [];
|
47
92
|
}
|
48
|
-
replaceData(_data, _sync_keys) { }
|
49
|
-
initForReload(request) {
|
50
|
-
|
93
|
+
ArSyncContainerBase.prototype.replaceData = function (_data, _sync_keys) { };
|
94
|
+
ArSyncContainerBase.prototype.initForReload = function (request) {
|
95
|
+
var _this = this;
|
96
|
+
this.networkSubscriber = ArSyncStore.connectionManager.subscribeNetwork(function (state) {
|
51
97
|
if (state) {
|
52
|
-
ArSyncApi_1.default.syncFetch(request).then(data
|
53
|
-
if (
|
54
|
-
|
55
|
-
if (
|
56
|
-
|
57
|
-
if (
|
58
|
-
|
98
|
+
ArSyncApi_1.default.syncFetch(request).then(function (data) {
|
99
|
+
if (_this.data) {
|
100
|
+
_this.replaceData(data);
|
101
|
+
if (_this.onConnectionChange)
|
102
|
+
_this.onConnectionChange(true);
|
103
|
+
if (_this.onChange)
|
104
|
+
_this.onChange([], _this.data);
|
59
105
|
}
|
60
106
|
});
|
61
107
|
}
|
62
108
|
else {
|
63
|
-
if (
|
64
|
-
|
109
|
+
if (_this.onConnectionChange)
|
110
|
+
_this.onConnectionChange(false);
|
65
111
|
}
|
66
112
|
});
|
67
|
-
}
|
68
|
-
release() {
|
113
|
+
};
|
114
|
+
ArSyncContainerBase.prototype.release = function () {
|
69
115
|
if (this.networkSubscriber)
|
70
116
|
this.networkSubscriber.unsubscribe();
|
71
117
|
this.unsubscribeAll();
|
72
|
-
for (
|
118
|
+
for (var _i = 0, _a = Object.values(this.children); _i < _a.length; _i++) {
|
119
|
+
var child = _a[_i];
|
73
120
|
if (child)
|
74
121
|
child.release();
|
75
122
|
}
|
76
123
|
this.data = null;
|
77
|
-
}
|
78
|
-
onChange(path, data) {
|
124
|
+
};
|
125
|
+
ArSyncContainerBase.prototype.onChange = function (path, data) {
|
79
126
|
if (this.parentModel)
|
80
|
-
this.parentModel.onChange([this.parentKey,
|
81
|
-
}
|
82
|
-
subscribe(key, listener) {
|
127
|
+
this.parentModel.onChange(__spreadArrays([this.parentKey], path), data);
|
128
|
+
};
|
129
|
+
ArSyncContainerBase.prototype.subscribe = function (key, listener) {
|
83
130
|
this.listeners.push(ArSyncStore.connectionManager.subscribe(key, listener));
|
84
|
-
}
|
85
|
-
unsubscribeAll() {
|
86
|
-
for (
|
131
|
+
};
|
132
|
+
ArSyncContainerBase.prototype.unsubscribeAll = function () {
|
133
|
+
for (var _i = 0, _a = this.listeners; _i < _a.length; _i++) {
|
134
|
+
var l = _a[_i];
|
87
135
|
l.unsubscribe();
|
136
|
+
}
|
88
137
|
this.listeners = [];
|
89
|
-
}
|
90
|
-
|
138
|
+
};
|
139
|
+
ArSyncContainerBase.compactQuery = function (query) {
|
91
140
|
function compactAttributes(attributes) {
|
92
|
-
|
93
|
-
|
94
|
-
for (
|
95
|
-
|
141
|
+
var attrs = {};
|
142
|
+
var keys = [];
|
143
|
+
for (var key in attributes) {
|
144
|
+
var c = compactQuery(attributes[key]);
|
96
145
|
if (c === true) {
|
97
146
|
keys.push(key);
|
98
147
|
}
|
@@ -107,22 +156,22 @@ class ArSyncContainerBase {
|
|
107
156
|
return [keys[0], false];
|
108
157
|
return [keys];
|
109
158
|
}
|
110
|
-
|
159
|
+
var needsEscape = attrs['attributes'] || attrs['params'] || attrs['as'];
|
111
160
|
if (keys.length === 0)
|
112
161
|
return [attrs, needsEscape];
|
113
|
-
return [
|
162
|
+
return [__spreadArrays(keys, [attrs]), needsEscape];
|
114
163
|
}
|
115
164
|
function compactQuery(query) {
|
116
165
|
if (!('attributes' in query))
|
117
166
|
return true;
|
118
|
-
|
119
|
-
|
167
|
+
var as = query.as, params = query.params;
|
168
|
+
var _a = compactAttributes(query.attributes), attributes = _a[0], needsEscape = _a[1];
|
120
169
|
if (as == null && params == null) {
|
121
170
|
if (needsEscape)
|
122
|
-
return { attributes };
|
171
|
+
return { attributes: attributes };
|
123
172
|
return attributes;
|
124
173
|
}
|
125
|
-
|
174
|
+
var result = {};
|
126
175
|
if (as)
|
127
176
|
result.as = as;
|
128
177
|
if (params)
|
@@ -132,35 +181,36 @@ class ArSyncContainerBase {
|
|
132
181
|
return result;
|
133
182
|
}
|
134
183
|
try {
|
135
|
-
|
184
|
+
var result = compactQuery(query);
|
136
185
|
return result === true ? {} : result;
|
137
186
|
}
|
138
187
|
catch (e) {
|
139
188
|
throw JSON.stringify(query) + e.stack;
|
140
189
|
}
|
141
|
-
}
|
142
|
-
|
143
|
-
|
144
|
-
|
145
|
-
|
190
|
+
};
|
191
|
+
ArSyncContainerBase.parseQuery = function (query, attrsonly) {
|
192
|
+
var attributes = {};
|
193
|
+
var column = null;
|
194
|
+
var params = null;
|
146
195
|
if (!query)
|
147
196
|
query = [];
|
148
197
|
if (query.constructor !== Array)
|
149
198
|
query = [query];
|
150
|
-
for (
|
199
|
+
for (var _i = 0, query_1 = query; _i < query_1.length; _i++) {
|
200
|
+
var arg = query_1[_i];
|
151
201
|
if (typeof (arg) === 'string') {
|
152
202
|
attributes[arg] = {};
|
153
203
|
}
|
154
204
|
else if (typeof (arg) === 'object') {
|
155
|
-
for (
|
156
|
-
|
205
|
+
for (var key in arg) {
|
206
|
+
var value = arg[key];
|
157
207
|
if (attrsonly) {
|
158
208
|
attributes[key] = this.parseQuery(value);
|
159
209
|
continue;
|
160
210
|
}
|
161
211
|
if (key === 'attributes') {
|
162
|
-
|
163
|
-
for (
|
212
|
+
var child = this.parseQuery(value, true);
|
213
|
+
for (var k in child)
|
164
214
|
attributes[k] = child[k];
|
165
215
|
}
|
166
216
|
else if (key === 'as') {
|
@@ -177,37 +227,39 @@ class ArSyncContainerBase {
|
|
177
227
|
}
|
178
228
|
if (attrsonly)
|
179
229
|
return attributes;
|
180
|
-
return { attributes, as: column, params };
|
181
|
-
}
|
182
|
-
|
183
|
-
|
184
|
-
|
230
|
+
return { attributes: attributes, as: column, params: params };
|
231
|
+
};
|
232
|
+
ArSyncContainerBase._load = function (_a, root) {
|
233
|
+
var api = _a.api, id = _a.id, params = _a.params, query = _a.query;
|
234
|
+
var parsedQuery = ArSyncRecord.parseQuery(query);
|
235
|
+
var compactQuery = ArSyncRecord.compactQuery(parsedQuery);
|
185
236
|
if (id) {
|
186
|
-
return ModelBatchRequest.fetch(api, compactQuery, id).then(data
|
237
|
+
return ModelBatchRequest.fetch(api, compactQuery, id).then(function (data) { return new ArSyncRecord(parsedQuery, data, null, root); });
|
187
238
|
}
|
188
239
|
else {
|
189
|
-
|
190
|
-
return ArSyncApi_1.default.syncFetch(
|
240
|
+
var request_1 = { api: api, query: compactQuery, params: params };
|
241
|
+
return ArSyncApi_1.default.syncFetch(request_1).then(function (response) {
|
191
242
|
if (response.collection && response.order) {
|
192
|
-
return new ArSyncCollection(response.sync_keys, 'collection', parsedQuery, response,
|
243
|
+
return new ArSyncCollection(response.sync_keys, 'collection', parsedQuery, response, request_1, root);
|
193
244
|
}
|
194
245
|
else if (response instanceof Array) {
|
195
|
-
return new ArSyncCollection([], '', parsedQuery, response,
|
246
|
+
return new ArSyncCollection([], '', parsedQuery, response, request_1, root);
|
196
247
|
}
|
197
248
|
else {
|
198
|
-
return new ArSyncRecord(parsedQuery, response,
|
249
|
+
return new ArSyncRecord(parsedQuery, response, request_1, root);
|
199
250
|
}
|
200
251
|
});
|
201
252
|
}
|
202
|
-
}
|
203
|
-
|
253
|
+
};
|
254
|
+
ArSyncContainerBase.load = function (apiParams, root) {
|
255
|
+
var _this = this;
|
204
256
|
if (!(apiParams instanceof Array))
|
205
257
|
return this._load(apiParams, root);
|
206
|
-
return new Promise((resolve, _reject)
|
207
|
-
|
208
|
-
|
209
|
-
apiParams.forEach((param, i)
|
210
|
-
|
258
|
+
return new Promise(function (resolve, _reject) {
|
259
|
+
var resultModels = [];
|
260
|
+
var countdown = apiParams.length;
|
261
|
+
apiParams.forEach(function (param, i) {
|
262
|
+
_this._load(param, root).then(function (model) {
|
211
263
|
resultModels[i] = model;
|
212
264
|
countdown--;
|
213
265
|
if (countdown === 0)
|
@@ -215,26 +267,29 @@ class ArSyncContainerBase {
|
|
215
267
|
});
|
216
268
|
});
|
217
269
|
});
|
218
|
-
}
|
219
|
-
|
220
|
-
|
221
|
-
|
222
|
-
|
223
|
-
|
270
|
+
};
|
271
|
+
return ArSyncContainerBase;
|
272
|
+
}());
|
273
|
+
var ArSyncRecord = /** @class */ (function (_super) {
|
274
|
+
__extends(ArSyncRecord, _super);
|
275
|
+
function ArSyncRecord(query, data, request, root) {
|
276
|
+
var _this = _super.call(this) || this;
|
277
|
+
_this.root = root;
|
224
278
|
if (request)
|
225
|
-
|
226
|
-
|
227
|
-
|
228
|
-
|
229
|
-
|
230
|
-
|
231
|
-
|
279
|
+
_this.initForReload(request);
|
280
|
+
_this.query = query;
|
281
|
+
_this.data = {};
|
282
|
+
_this.children = {};
|
283
|
+
_this.replaceData(data);
|
284
|
+
return _this;
|
285
|
+
}
|
286
|
+
ArSyncRecord.prototype.setSyncKeys = function (sync_keys) {
|
232
287
|
this.sync_keys = sync_keys;
|
233
288
|
if (!this.sync_keys) {
|
234
289
|
this.sync_keys = [];
|
235
290
|
}
|
236
|
-
}
|
237
|
-
replaceData(data) {
|
291
|
+
};
|
292
|
+
ArSyncRecord.prototype.replaceData = function (data) {
|
238
293
|
this.setSyncKeys(data.sync_keys);
|
239
294
|
this.unsubscribeAll();
|
240
295
|
if (this.data.id !== data.id) {
|
@@ -242,11 +297,11 @@ class ArSyncRecord extends ArSyncContainerBase {
|
|
242
297
|
this.data.id = data.id;
|
243
298
|
}
|
244
299
|
this.paths = [];
|
245
|
-
for (
|
246
|
-
|
247
|
-
|
248
|
-
|
249
|
-
|
300
|
+
for (var key in this.query.attributes) {
|
301
|
+
var subQuery = this.query.attributes[key];
|
302
|
+
var aliasName = subQuery.as || key;
|
303
|
+
var subData = data[aliasName];
|
304
|
+
var child = this.children[aliasName];
|
250
305
|
if (key === 'sync_keys')
|
251
306
|
continue;
|
252
307
|
if (subData instanceof Array || (subData && subData.collection && subData.order)) {
|
@@ -254,7 +309,7 @@ class ArSyncRecord extends ArSyncContainerBase {
|
|
254
309
|
child.replaceData(subData, this.sync_keys);
|
255
310
|
}
|
256
311
|
else {
|
257
|
-
|
312
|
+
var collection = new ArSyncCollection(this.sync_keys, key, subQuery, subData, null, this.root);
|
258
313
|
this.mark();
|
259
314
|
this.children[aliasName] = collection;
|
260
315
|
this.data[aliasName] = collection.data;
|
@@ -270,7 +325,7 @@ class ArSyncRecord extends ArSyncContainerBase {
|
|
270
325
|
child.replaceData(subData);
|
271
326
|
}
|
272
327
|
else {
|
273
|
-
|
328
|
+
var model = new ArSyncRecord(subQuery, subData, null, this.root);
|
274
329
|
this.mark();
|
275
330
|
this.children[aliasName] = model;
|
276
331
|
this.data[aliasName] = model.data;
|
@@ -291,7 +346,7 @@ class ArSyncRecord extends ArSyncContainerBase {
|
|
291
346
|
}
|
292
347
|
}
|
293
348
|
if (this.query.attributes['*']) {
|
294
|
-
for (
|
349
|
+
for (var key in data) {
|
295
350
|
if (!this.query.attributes[key] && this.data[key] !== data[key]) {
|
296
351
|
this.mark();
|
297
352
|
this.data[key] = data[key];
|
@@ -299,13 +354,14 @@ class ArSyncRecord extends ArSyncContainerBase {
|
|
299
354
|
}
|
300
355
|
}
|
301
356
|
this.subscribeAll();
|
302
|
-
}
|
303
|
-
onNotify(notifyData, path) {
|
304
|
-
|
305
|
-
|
306
|
-
|
357
|
+
};
|
358
|
+
ArSyncRecord.prototype.onNotify = function (notifyData, path) {
|
359
|
+
var _this = this;
|
360
|
+
var action = notifyData.action, class_name = notifyData.class_name, id = notifyData.id;
|
361
|
+
var query = path && this.query.attributes[path];
|
362
|
+
var aliasName = (query && query.as) || path;
|
307
363
|
if (action === 'remove') {
|
308
|
-
|
364
|
+
var child = this.children[aliasName];
|
309
365
|
if (child)
|
310
366
|
child.release();
|
311
367
|
this.children[aliasName] = null;
|
@@ -316,52 +372,61 @@ class ArSyncRecord extends ArSyncContainerBase {
|
|
316
372
|
else if (action === 'add') {
|
317
373
|
if (this.data[aliasName] && this.data[aliasName].id === id)
|
318
374
|
return;
|
319
|
-
ModelBatchRequest.fetch(class_name, ArSyncRecord.compactQuery(query), id).then(data
|
320
|
-
if (!data || !
|
375
|
+
ModelBatchRequest.fetch(class_name, ArSyncRecord.compactQuery(query), id).then(function (data) {
|
376
|
+
if (!data || !_this.data)
|
321
377
|
return;
|
322
|
-
|
323
|
-
|
378
|
+
var model = new ArSyncRecord(query, data, null, _this.root);
|
379
|
+
var child = _this.children[aliasName];
|
324
380
|
if (child)
|
325
381
|
child.release();
|
326
|
-
|
327
|
-
|
328
|
-
|
329
|
-
model.parentModel =
|
382
|
+
_this.children[aliasName] = model;
|
383
|
+
_this.mark();
|
384
|
+
_this.data[aliasName] = model.data;
|
385
|
+
model.parentModel = _this;
|
330
386
|
model.parentKey = aliasName;
|
331
|
-
|
387
|
+
_this.onChange([aliasName], model.data);
|
332
388
|
});
|
333
389
|
}
|
334
390
|
else {
|
335
|
-
|
336
|
-
|
337
|
-
if (
|
338
|
-
ModelBatchRequest.fetch(class_name,
|
339
|
-
if (
|
340
|
-
|
391
|
+
var field = notifyData.field;
|
392
|
+
var query_2 = field ? this.patchQuery(field) : this.reloadQuery();
|
393
|
+
if (query_2)
|
394
|
+
ModelBatchRequest.fetch(class_name, query_2, id).then(function (data) {
|
395
|
+
if (_this.data)
|
396
|
+
_this.update(data);
|
341
397
|
});
|
342
398
|
}
|
343
|
-
}
|
344
|
-
subscribeAll() {
|
345
|
-
|
346
|
-
|
399
|
+
};
|
400
|
+
ArSyncRecord.prototype.subscribeAll = function () {
|
401
|
+
var _this = this;
|
402
|
+
var callback = function (data) { return _this.onNotify(data); };
|
403
|
+
for (var _i = 0, _a = this.sync_keys; _i < _a.length; _i++) {
|
404
|
+
var key = _a[_i];
|
347
405
|
this.subscribe(key, callback);
|
348
406
|
}
|
349
|
-
|
350
|
-
|
351
|
-
for (
|
352
|
-
|
353
|
-
|
354
|
-
|
355
|
-
|
356
|
-
|
407
|
+
var _loop_2 = function (path) {
|
408
|
+
var pathCallback = function (data) { return _this.onNotify(data, path); };
|
409
|
+
for (var _i = 0, _a = this_1.sync_keys; _i < _a.length; _i++) {
|
410
|
+
var key = _a[_i];
|
411
|
+
this_1.subscribe(key + path, pathCallback);
|
412
|
+
}
|
413
|
+
};
|
414
|
+
var this_1 = this;
|
415
|
+
for (var _b = 0, _c = this.paths; _b < _c.length; _b++) {
|
416
|
+
var path = _c[_b];
|
417
|
+
_loop_2(path);
|
418
|
+
}
|
419
|
+
};
|
420
|
+
ArSyncRecord.prototype.patchQuery = function (key) {
|
421
|
+
var val = this.query.attributes[key];
|
357
422
|
if (!val)
|
358
423
|
return;
|
359
|
-
|
424
|
+
var attributes = val.attributes, as = val.as, params = val.params;
|
360
425
|
if (attributes && Object.keys(val.attributes).length === 0)
|
361
426
|
attributes = null;
|
362
427
|
if (!attributes && !as && !params)
|
363
428
|
return key;
|
364
|
-
|
429
|
+
var result = {};
|
365
430
|
if (attributes)
|
366
431
|
result.attributes = attributes;
|
367
432
|
if (as)
|
@@ -369,27 +434,28 @@ class ArSyncRecord extends ArSyncContainerBase {
|
|
369
434
|
if (params)
|
370
435
|
result.params = params;
|
371
436
|
return result;
|
372
|
-
}
|
373
|
-
reloadQuery() {
|
437
|
+
};
|
438
|
+
ArSyncRecord.prototype.reloadQuery = function () {
|
439
|
+
var _a;
|
374
440
|
if (this.reloadQueryCache)
|
375
441
|
return this.reloadQueryCache;
|
376
|
-
|
377
|
-
for (
|
442
|
+
var reloadQuery = this.reloadQueryCache = { attributes: [] };
|
443
|
+
for (var key in this.query.attributes) {
|
378
444
|
if (key === 'sync_keys')
|
379
445
|
continue;
|
380
|
-
|
446
|
+
var val = this.query.attributes[key];
|
381
447
|
if (!val || !val.attributes) {
|
382
448
|
reloadQuery.attributes.push(key);
|
383
449
|
}
|
384
450
|
else if (!val.params && Object.keys(val.attributes).length === 0) {
|
385
|
-
reloadQuery.attributes.push({ [key]
|
451
|
+
reloadQuery.attributes.push((_a = {}, _a[key] = val, _a));
|
386
452
|
}
|
387
453
|
}
|
388
454
|
return reloadQuery;
|
389
|
-
}
|
390
|
-
update(data) {
|
391
|
-
for (
|
392
|
-
|
455
|
+
};
|
456
|
+
ArSyncRecord.prototype.update = function (data) {
|
457
|
+
for (var key in data) {
|
458
|
+
var subQuery = this.query.attributes[key];
|
393
459
|
if (subQuery && subQuery.attributes && Object.keys(subQuery.attributes).length > 0)
|
394
460
|
continue;
|
395
461
|
if (this.data[key] === data[key])
|
@@ -398,73 +464,79 @@ class ArSyncRecord extends ArSyncContainerBase {
|
|
398
464
|
this.data[key] = data[key];
|
399
465
|
this.onChange([key], data[key]);
|
400
466
|
}
|
401
|
-
}
|
402
|
-
markAndSet(key, data) {
|
467
|
+
};
|
468
|
+
ArSyncRecord.prototype.markAndSet = function (key, data) {
|
403
469
|
this.mark();
|
404
470
|
this.data[key] = data;
|
405
|
-
}
|
406
|
-
mark() {
|
471
|
+
};
|
472
|
+
ArSyncRecord.prototype.mark = function () {
|
407
473
|
if (!this.root || !this.root.immutable || !Object.isFrozen(this.data))
|
408
474
|
return;
|
409
|
-
this.data =
|
475
|
+
this.data = __assign({}, this.data);
|
410
476
|
this.root.mark(this.data);
|
411
477
|
if (this.parentModel)
|
412
478
|
this.parentModel.markAndSet(this.parentKey, this.data);
|
413
|
-
}
|
414
|
-
|
415
|
-
|
416
|
-
|
417
|
-
|
418
|
-
|
419
|
-
|
420
|
-
|
421
|
-
|
422
|
-
|
423
|
-
|
479
|
+
};
|
480
|
+
return ArSyncRecord;
|
481
|
+
}(ArSyncContainerBase));
|
482
|
+
var ArSyncCollection = /** @class */ (function (_super) {
|
483
|
+
__extends(ArSyncCollection, _super);
|
484
|
+
function ArSyncCollection(sync_keys, path, query, data, request, root) {
|
485
|
+
var _this = _super.call(this) || this;
|
486
|
+
_this.order = { limit: null, mode: 'asc', key: 'id' };
|
487
|
+
_this.aliasOrderKey = 'id';
|
488
|
+
_this.root = root;
|
489
|
+
_this.path = path;
|
490
|
+
_this.query = query;
|
491
|
+
_this.compactQuery = ArSyncRecord.compactQuery(query);
|
424
492
|
if (request)
|
425
|
-
|
493
|
+
_this.initForReload(request);
|
426
494
|
if (query.params && (query.params.order || query.params.limit)) {
|
427
|
-
|
495
|
+
_this.setOrdering(query.params.limit, query.params.order);
|
428
496
|
}
|
429
|
-
|
430
|
-
|
431
|
-
|
497
|
+
_this.data = [];
|
498
|
+
_this.children = [];
|
499
|
+
_this.replaceData(data, sync_keys);
|
500
|
+
return _this;
|
432
501
|
}
|
433
|
-
setOrdering(limit, order) {
|
434
|
-
|
435
|
-
|
502
|
+
ArSyncCollection.prototype.setOrdering = function (limit, order) {
|
503
|
+
var mode = 'asc';
|
504
|
+
var key = 'id';
|
436
505
|
if (order === 'asc' || order === 'desc') {
|
437
506
|
mode = order;
|
438
507
|
}
|
439
508
|
else if (typeof order === 'object' && order) {
|
440
|
-
|
509
|
+
var keys = Object.keys(order);
|
441
510
|
if (keys.length > 1)
|
442
511
|
throw 'multiple order keys are not supported';
|
443
512
|
if (keys.length === 1)
|
444
513
|
key = keys[0];
|
445
514
|
mode = order[key] === 'asc' ? 'asc' : 'desc';
|
446
515
|
}
|
447
|
-
|
516
|
+
var limitNumber = (typeof limit === 'number') ? limit : null;
|
448
517
|
if (limitNumber !== null && key !== 'id')
|
449
518
|
throw 'limit with custom order key is not supported';
|
450
|
-
|
519
|
+
var subQuery = this.query.attributes[key];
|
451
520
|
this.aliasOrderKey = (subQuery && subQuery.as) || key;
|
452
|
-
this.order = { limit: limitNumber, mode, key };
|
453
|
-
}
|
454
|
-
setSyncKeys(sync_keys) {
|
521
|
+
this.order = { limit: limitNumber, mode: mode, key: key };
|
522
|
+
};
|
523
|
+
ArSyncCollection.prototype.setSyncKeys = function (sync_keys) {
|
524
|
+
var _this = this;
|
455
525
|
if (sync_keys) {
|
456
|
-
this.sync_keys = sync_keys.map(key
|
526
|
+
this.sync_keys = sync_keys.map(function (key) { return key + _this.path; });
|
457
527
|
}
|
458
528
|
else {
|
459
529
|
this.sync_keys = [];
|
460
530
|
}
|
461
|
-
}
|
462
|
-
replaceData(data, sync_keys) {
|
531
|
+
};
|
532
|
+
ArSyncCollection.prototype.replaceData = function (data, sync_keys) {
|
463
533
|
this.setSyncKeys(sync_keys);
|
464
|
-
|
465
|
-
for (
|
534
|
+
var existings = {};
|
535
|
+
for (var _i = 0, _a = this.children; _i < _a.length; _i++) {
|
536
|
+
var child = _a[_i];
|
466
537
|
existings[child.data.id] = child;
|
467
|
-
|
538
|
+
}
|
539
|
+
var collection;
|
468
540
|
if ('collection' in data && 'order' in data) {
|
469
541
|
collection = data.collection;
|
470
542
|
this.setOrdering(data.order.limit, data.order.mode);
|
@@ -472,13 +544,14 @@ class ArSyncCollection extends ArSyncContainerBase {
|
|
472
544
|
else {
|
473
545
|
collection = data;
|
474
546
|
}
|
475
|
-
|
476
|
-
|
477
|
-
for (
|
478
|
-
|
547
|
+
var newChildren = [];
|
548
|
+
var newData = [];
|
549
|
+
for (var _b = 0, collection_1 = collection; _b < collection_1.length; _b++) {
|
550
|
+
var subData = collection_1[_b];
|
551
|
+
var model = null;
|
479
552
|
if (typeof (subData) === 'object' && subData && 'id' in subData)
|
480
553
|
model = existings[subData.id];
|
481
|
-
|
554
|
+
var data_1 = subData;
|
482
555
|
if (model) {
|
483
556
|
model.replaceData(subData);
|
484
557
|
}
|
@@ -489,12 +562,12 @@ class ArSyncCollection extends ArSyncContainerBase {
|
|
489
562
|
}
|
490
563
|
if (model) {
|
491
564
|
newChildren.push(model);
|
492
|
-
|
565
|
+
data_1 = model.data;
|
493
566
|
}
|
494
|
-
newData.push(
|
567
|
+
newData.push(data_1);
|
495
568
|
}
|
496
569
|
while (this.children.length) {
|
497
|
-
|
570
|
+
var child = this.children.pop();
|
498
571
|
if (!existings[child.data.id])
|
499
572
|
child.release();
|
500
573
|
}
|
@@ -502,80 +575,85 @@ class ArSyncCollection extends ArSyncContainerBase {
|
|
502
575
|
this.mark();
|
503
576
|
while (this.data.length)
|
504
577
|
this.data.pop();
|
505
|
-
for (
|
578
|
+
for (var _c = 0, newChildren_1 = newChildren; _c < newChildren_1.length; _c++) {
|
579
|
+
var child = newChildren_1[_c];
|
506
580
|
this.children.push(child);
|
507
|
-
|
581
|
+
}
|
582
|
+
for (var _d = 0, newData_1 = newData; _d < newData_1.length; _d++) {
|
583
|
+
var el = newData_1[_d];
|
508
584
|
this.data.push(el);
|
585
|
+
}
|
509
586
|
this.subscribeAll();
|
510
|
-
}
|
511
|
-
consumeAdd(className, id) {
|
512
|
-
|
587
|
+
};
|
588
|
+
ArSyncCollection.prototype.consumeAdd = function (className, id) {
|
589
|
+
var _this = this;
|
590
|
+
if (this.data.findIndex(function (a) { return a.id === id; }) >= 0)
|
513
591
|
return;
|
514
592
|
if (this.order.limit === this.data.length) {
|
515
593
|
if (this.order.mode === 'asc') {
|
516
|
-
|
594
|
+
var last = this.data[this.data.length - 1];
|
517
595
|
if (last && last.id < id)
|
518
596
|
return;
|
519
597
|
}
|
520
598
|
else {
|
521
|
-
|
599
|
+
var last = this.data[this.data.length - 1];
|
522
600
|
if (last && last.id > id)
|
523
601
|
return;
|
524
602
|
}
|
525
603
|
}
|
526
|
-
ModelBatchRequest.fetch(className, this.compactQuery, id).then((data)
|
527
|
-
if (!data || !
|
604
|
+
ModelBatchRequest.fetch(className, this.compactQuery, id).then(function (data) {
|
605
|
+
if (!data || !_this.data)
|
528
606
|
return;
|
529
|
-
|
530
|
-
model.parentModel =
|
607
|
+
var model = new ArSyncRecord(_this.query, data, null, _this.root);
|
608
|
+
model.parentModel = _this;
|
531
609
|
model.parentKey = id;
|
532
|
-
|
533
|
-
|
534
|
-
|
535
|
-
|
536
|
-
if (
|
537
|
-
|
538
|
-
|
539
|
-
|
610
|
+
var overflow = _this.order.limit && _this.order.limit === _this.data.length;
|
611
|
+
var rmodel;
|
612
|
+
_this.mark();
|
613
|
+
var orderKey = _this.aliasOrderKey;
|
614
|
+
if (_this.order.mode === 'asc') {
|
615
|
+
var last = _this.data[_this.data.length - 1];
|
616
|
+
_this.children.push(model);
|
617
|
+
_this.data.push(model.data);
|
540
618
|
if (last && last[orderKey] > data[orderKey])
|
541
|
-
|
619
|
+
_this.markAndSort();
|
542
620
|
if (overflow) {
|
543
|
-
rmodel =
|
621
|
+
rmodel = _this.children.shift();
|
544
622
|
rmodel.release();
|
545
|
-
|
623
|
+
_this.data.shift();
|
546
624
|
}
|
547
625
|
}
|
548
626
|
else {
|
549
|
-
|
550
|
-
|
551
|
-
|
627
|
+
var first = _this.data[0];
|
628
|
+
_this.children.unshift(model);
|
629
|
+
_this.data.unshift(model.data);
|
552
630
|
if (first && first[orderKey] > data[orderKey])
|
553
|
-
|
631
|
+
_this.markAndSort();
|
554
632
|
if (overflow) {
|
555
|
-
rmodel =
|
633
|
+
rmodel = _this.children.pop();
|
556
634
|
rmodel.release();
|
557
|
-
|
635
|
+
_this.data.pop();
|
558
636
|
}
|
559
637
|
}
|
560
|
-
|
638
|
+
_this.onChange([model.id], model.data);
|
561
639
|
if (rmodel)
|
562
|
-
|
640
|
+
_this.onChange([rmodel.id], null);
|
563
641
|
});
|
564
|
-
}
|
565
|
-
markAndSort() {
|
642
|
+
};
|
643
|
+
ArSyncCollection.prototype.markAndSort = function () {
|
566
644
|
this.mark();
|
567
|
-
|
645
|
+
var orderKey = this.aliasOrderKey;
|
568
646
|
if (this.order.mode === 'asc') {
|
569
|
-
this.children.sort((a, b)
|
570
|
-
this.data.sort((a, b)
|
647
|
+
this.children.sort(function (a, b) { return a.data[orderKey] < b.data[orderKey] ? -1 : +1; });
|
648
|
+
this.data.sort(function (a, b) { return a[orderKey] < b[orderKey] ? -1 : +1; });
|
571
649
|
}
|
572
650
|
else {
|
573
|
-
this.children.sort((a, b)
|
574
|
-
this.data.sort((a, b)
|
651
|
+
this.children.sort(function (a, b) { return a.data[orderKey] > b.data[orderKey] ? -1 : +1; });
|
652
|
+
this.data.sort(function (a, b) { return a[orderKey] > b[orderKey] ? -1 : +1; });
|
575
653
|
}
|
576
|
-
}
|
577
|
-
consumeRemove(id) {
|
578
|
-
|
654
|
+
};
|
655
|
+
ArSyncCollection.prototype.consumeRemove = function (id) {
|
656
|
+
var idx = this.data.findIndex(function (a) { return a.id === id; });
|
579
657
|
if (idx < 0)
|
580
658
|
return;
|
581
659
|
this.mark();
|
@@ -583,42 +661,47 @@ class ArSyncCollection extends ArSyncContainerBase {
|
|
583
661
|
this.children.splice(idx, 1);
|
584
662
|
this.data.splice(idx, 1);
|
585
663
|
this.onChange([id], null);
|
586
|
-
}
|
587
|
-
onNotify(notifyData) {
|
664
|
+
};
|
665
|
+
ArSyncCollection.prototype.onNotify = function (notifyData) {
|
588
666
|
if (notifyData.action === 'add') {
|
589
667
|
this.consumeAdd(notifyData.class_name, notifyData.id);
|
590
668
|
}
|
591
669
|
else if (notifyData.action === 'remove') {
|
592
670
|
this.consumeRemove(notifyData.id);
|
593
671
|
}
|
594
|
-
}
|
595
|
-
subscribeAll() {
|
596
|
-
|
597
|
-
|
672
|
+
};
|
673
|
+
ArSyncCollection.prototype.subscribeAll = function () {
|
674
|
+
var _this = this;
|
675
|
+
var callback = function (data) { return _this.onNotify(data); };
|
676
|
+
for (var _i = 0, _a = this.sync_keys; _i < _a.length; _i++) {
|
677
|
+
var key = _a[_i];
|
598
678
|
this.subscribe(key, callback);
|
599
|
-
|
600
|
-
|
601
|
-
|
679
|
+
}
|
680
|
+
};
|
681
|
+
ArSyncCollection.prototype.onChange = function (path, data) {
|
682
|
+
_super.prototype.onChange.call(this, path, data);
|
602
683
|
if (path[1] === this.aliasOrderKey)
|
603
684
|
this.markAndSort();
|
604
|
-
}
|
605
|
-
markAndSet(id, data) {
|
685
|
+
};
|
686
|
+
ArSyncCollection.prototype.markAndSet = function (id, data) {
|
606
687
|
this.mark();
|
607
|
-
|
688
|
+
var idx = this.data.findIndex(function (a) { return a.id === id; });
|
608
689
|
if (idx >= 0)
|
609
690
|
this.data[idx] = data;
|
610
|
-
}
|
611
|
-
mark() {
|
691
|
+
};
|
692
|
+
ArSyncCollection.prototype.mark = function () {
|
612
693
|
if (!this.root || !this.root.immutable || !Object.isFrozen(this.data))
|
613
694
|
return;
|
614
|
-
this.data =
|
695
|
+
this.data = __spreadArrays(this.data);
|
615
696
|
this.root.mark(this.data);
|
616
697
|
if (this.parentModel)
|
617
698
|
this.parentModel.markAndSet(this.parentKey, this.data);
|
618
|
-
}
|
619
|
-
|
620
|
-
|
621
|
-
|
699
|
+
};
|
700
|
+
return ArSyncCollection;
|
701
|
+
}(ArSyncContainerBase));
|
702
|
+
var ArSyncStore = /** @class */ (function () {
|
703
|
+
function ArSyncStore(request, _a) {
|
704
|
+
var immutable = (_a === void 0 ? {} : _a).immutable;
|
622
705
|
this.immutable = !!immutable;
|
623
706
|
this.markedForFreezeObjects = [];
|
624
707
|
this.changes = [];
|
@@ -628,87 +711,90 @@ class ArSyncStore {
|
|
628
711
|
this.data = null;
|
629
712
|
this.load(0);
|
630
713
|
}
|
631
|
-
load(retryCount) {
|
632
|
-
|
633
|
-
|
714
|
+
ArSyncStore.prototype.load = function (retryCount) {
|
715
|
+
var _this = this;
|
716
|
+
ArSyncContainerBase.load(this.request, this).then(function (container) {
|
717
|
+
if (_this.markForRelease) {
|
634
718
|
container.release();
|
635
719
|
return;
|
636
720
|
}
|
637
|
-
|
638
|
-
|
639
|
-
if (
|
640
|
-
|
641
|
-
|
642
|
-
|
643
|
-
|
644
|
-
|
645
|
-
container.onChange = (path, value)
|
646
|
-
|
647
|
-
|
721
|
+
_this.container = container;
|
722
|
+
_this.data = container.data;
|
723
|
+
if (_this.immutable)
|
724
|
+
_this.freezeRecursive(_this.data);
|
725
|
+
_this.complete = true;
|
726
|
+
_this.notfound = false;
|
727
|
+
_this.trigger('load');
|
728
|
+
_this.trigger('change', { path: [], value: _this.data });
|
729
|
+
container.onChange = function (path, value) {
|
730
|
+
_this.changes.push({ path: path, value: value });
|
731
|
+
_this.setChangesBufferTimer();
|
648
732
|
};
|
649
|
-
container.onConnectionChange = state
|
650
|
-
|
733
|
+
container.onConnectionChange = function (state) {
|
734
|
+
_this.trigger('connection', state);
|
651
735
|
};
|
652
|
-
}).catch(e
|
736
|
+
}).catch(function (e) {
|
653
737
|
if (!e || e.retry === undefined)
|
654
738
|
throw e;
|
655
|
-
if (
|
739
|
+
if (_this.markForRelease)
|
656
740
|
return;
|
657
741
|
if (!e.retry) {
|
658
|
-
|
659
|
-
|
660
|
-
|
742
|
+
_this.complete = true;
|
743
|
+
_this.notfound = true;
|
744
|
+
_this.trigger('load');
|
661
745
|
return;
|
662
746
|
}
|
663
|
-
|
664
|
-
|
665
|
-
|
666
|
-
|
747
|
+
var sleepSeconds = Math.min(Math.pow(2, retryCount), 30);
|
748
|
+
_this.retryLoadTimer = setTimeout(function () {
|
749
|
+
_this.retryLoadTimer = null;
|
750
|
+
_this.load(retryCount + 1);
|
667
751
|
}, sleepSeconds * 1000);
|
668
752
|
});
|
669
|
-
}
|
670
|
-
setChangesBufferTimer() {
|
753
|
+
};
|
754
|
+
ArSyncStore.prototype.setChangesBufferTimer = function () {
|
755
|
+
var _this = this;
|
671
756
|
if (this.changesBufferTimer)
|
672
757
|
return;
|
673
|
-
this.changesBufferTimer = setTimeout(()
|
674
|
-
|
675
|
-
|
676
|
-
|
677
|
-
|
678
|
-
|
679
|
-
changes.forEach(patch
|
758
|
+
this.changesBufferTimer = setTimeout(function () {
|
759
|
+
_this.changesBufferTimer = null;
|
760
|
+
var changes = _this.changes;
|
761
|
+
_this.changes = [];
|
762
|
+
_this.freezeMarked();
|
763
|
+
_this.data = _this.container.data;
|
764
|
+
changes.forEach(function (patch) { return _this.trigger('change', patch); });
|
680
765
|
}, 20);
|
681
|
-
}
|
682
|
-
subscribe(event, callback) {
|
683
|
-
|
766
|
+
};
|
767
|
+
ArSyncStore.prototype.subscribe = function (event, callback) {
|
768
|
+
var listeners = this.eventListeners.events[event];
|
684
769
|
if (!listeners)
|
685
770
|
this.eventListeners.events[event] = listeners = {};
|
686
|
-
|
771
|
+
var id = this.eventListeners.serial++;
|
687
772
|
listeners[id] = callback;
|
688
|
-
return { unsubscribe: ()
|
689
|
-
}
|
690
|
-
trigger(event, arg) {
|
691
|
-
|
773
|
+
return { unsubscribe: function () { delete listeners[id]; } };
|
774
|
+
};
|
775
|
+
ArSyncStore.prototype.trigger = function (event, arg) {
|
776
|
+
var listeners = this.eventListeners.events[event];
|
692
777
|
if (!listeners)
|
693
778
|
return;
|
694
|
-
for (
|
779
|
+
for (var id in listeners)
|
695
780
|
listeners[id](arg);
|
696
|
-
}
|
697
|
-
mark(object) {
|
781
|
+
};
|
782
|
+
ArSyncStore.prototype.mark = function (object) {
|
698
783
|
this.markedForFreezeObjects.push(object);
|
699
|
-
}
|
700
|
-
freezeRecursive(obj) {
|
784
|
+
};
|
785
|
+
ArSyncStore.prototype.freezeRecursive = function (obj) {
|
701
786
|
if (Object.isFrozen(obj))
|
702
787
|
return obj;
|
703
|
-
for (
|
788
|
+
for (var key in obj)
|
704
789
|
this.freezeRecursive(obj[key]);
|
705
790
|
Object.freeze(obj);
|
706
|
-
}
|
707
|
-
freezeMarked() {
|
708
|
-
|
791
|
+
};
|
792
|
+
ArSyncStore.prototype.freezeMarked = function () {
|
793
|
+
var _this = this;
|
794
|
+
this.markedForFreezeObjects.forEach(function (obj) { return _this.freezeRecursive(obj); });
|
709
795
|
this.markedForFreezeObjects = [];
|
710
|
-
}
|
711
|
-
release() {
|
796
|
+
};
|
797
|
+
ArSyncStore.prototype.release = function () {
|
712
798
|
if (this.retryLoadTimer)
|
713
799
|
clearTimeout(this.retryLoadTimer);
|
714
800
|
if (this.changesBufferTimer)
|
@@ -719,6 +805,7 @@ class ArSyncStore {
|
|
719
805
|
else {
|
720
806
|
this.markForRelease = true;
|
721
807
|
}
|
722
|
-
}
|
723
|
-
|
808
|
+
};
|
809
|
+
return ArSyncStore;
|
810
|
+
}());
|
724
811
|
exports.default = ArSyncStore;
|