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