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.
Files changed (40) hide show
  1. checksums.yaml +4 -4
  2. data/.github/workflows/test.yml +27 -0
  3. data/Gemfile +1 -0
  4. data/Gemfile.lock +29 -31
  5. data/ar_sync.gemspec +1 -1
  6. data/core/{ActioncableAdapter.d.ts → ActionCableAdapter.d.ts} +0 -0
  7. data/core/ActionCableAdapter.js +31 -0
  8. data/core/ArSyncApi.d.ts +8 -2
  9. data/core/ArSyncApi.js +123 -49
  10. data/core/ArSyncModel.js +69 -60
  11. data/core/ArSyncStore.js +522 -381
  12. data/core/ConnectionManager.d.ts +1 -1
  13. data/core/ConnectionManager.js +45 -38
  14. data/core/DataType.d.ts +14 -9
  15. data/core/hooks.d.ts +5 -0
  16. data/core/hooks.js +64 -36
  17. data/gemfiles/Gemfile-rails-6 +9 -0
  18. data/gemfiles/Gemfile-rails-7 +9 -0
  19. data/index.js +2 -2
  20. data/lib/ar_sync/class_methods.rb +71 -36
  21. data/lib/ar_sync/collection.rb +23 -19
  22. data/lib/ar_sync/core.rb +3 -3
  23. data/lib/ar_sync/instance_methods.rb +7 -4
  24. data/lib/ar_sync/rails.rb +1 -1
  25. data/lib/ar_sync/type_script.rb +50 -14
  26. data/lib/ar_sync/version.rb +1 -1
  27. data/lib/generators/ar_sync/install/install_generator.rb +1 -1
  28. data/package-lock.json +1706 -227
  29. data/package.json +1 -1
  30. data/src/core/{ActioncableAdapter.ts → ActionCableAdapter.ts} +0 -0
  31. data/src/core/ArSyncApi.ts +20 -7
  32. data/src/core/ArSyncStore.ts +177 -125
  33. data/src/core/ConnectionManager.ts +1 -0
  34. data/src/core/DataType.ts +15 -16
  35. data/src/core/hooks.ts +31 -7
  36. data/tsconfig.json +2 -2
  37. data/vendor/assets/javascripts/{ar_sync_actioncable_adapter.js.erb → ar_sync_action_cable_adapter.js.erb} +1 -1
  38. metadata +17 -16
  39. data/core/ActioncableAdapter.js +0 -29
  40. 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
- const ArSyncApi_1 = require("./ArSyncApi");
4
- const ModelBatchRequest = {
5
- timer: null,
6
- apiRequests: {},
7
- fetch(api, query, id) {
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
- const queryJSON = JSON.stringify(query);
11
- const apiRequest = this.apiRequests[api] = this.apiRequests[api] || {};
12
- const queryRequests = apiRequest[queryJSON] = apiRequest[queryJSON] || { query, requests: {} };
13
- const request = queryRequests.requests[id] = queryRequests.requests[id] || { id, callbacks: [] };
14
- request.callbacks.push(resolve);
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
- const { apiRequests } = this;
19
- for (const api in apiRequests) {
20
- const apiRequest = apiRequests[api];
21
- for (const { query, requests } of Object.values(apiRequest)) {
22
- const ids = Object.values(requests).map(({ id }) => id);
23
- ArSyncApi_1.default.syncFetch({ api, query, params: { ids } }).then((models) => {
24
- for (const model of models)
25
- requests[model.id].model = model;
26
- for (const { model, callbacks } of Object.values(requests)) {
27
- for (const callback of callbacks)
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
- this.timer = null;
40
- this.batchFetch();
87
+ this.timer = setTimeout(function () {
88
+ _this.timer = null;
89
+ _this.batchFetch();
41
90
  }, 20);
42
- }
43
- };
44
- class ArSyncContainerBase {
45
- constructor() {
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
- this.networkSubscriber = ArSyncStore.connectionManager.subscribeNetwork((state) => {
51
- if (state) {
52
- ArSyncApi_1.default.syncFetch(request).then(data => {
53
- if (this.data) {
54
- this.replaceData(data);
55
- if (this.onConnectionChange)
56
- this.onConnectionChange(true);
57
- if (this.onChange)
58
- this.onChange([], this.data);
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
- if (this.onConnectionChange)
64
- this.onConnectionChange(false);
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 (const child of Object.values(this.children)) {
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, ...path], data);
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 (const l of this.listeners)
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
- static compactQuery(query) {
158
+ };
159
+ ArSyncContainerBase.compactQuery = function (query) {
91
160
  function compactAttributes(attributes) {
92
- const attrs = {};
93
- const keys = [];
94
- for (const key in attributes) {
95
- const c = compactQuery(attributes[key]);
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
- const needsEscape = attrs['attributes'] || attrs['params'] || attrs['as'];
179
+ var needsEscape = attrs['attributes'] || attrs['params'] || attrs['as'];
111
180
  if (keys.length === 0)
112
181
  return [attrs, needsEscape];
113
- return [[...keys, attrs], needsEscape];
182
+ return [__spreadArrays(keys, [attrs]), needsEscape];
114
183
  }
115
184
  function compactQuery(query) {
116
185
  if (!('attributes' in query))
117
186
  return true;
118
- const { as, params } = query;
119
- const [attributes, needsEscape] = compactAttributes(query.attributes);
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
- const result = {};
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
- try {
135
- const result = compactQuery(query);
136
- return result === true ? {} : result;
137
- }
138
- catch (e) {
139
- throw JSON.stringify(query) + e.stack;
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 (const arg of query) {
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 (const key in arg) {
156
- const value = arg[key];
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
- const child = this.parseQuery(value, true);
163
- for (const k in child)
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
- static _load({ api, id, params, query }, root) {
183
- const parsedQuery = ArSyncRecord.parseQuery(query);
184
- const compactQuery = ArSyncRecord.compactQuery(parsedQuery);
185
- if (id) {
186
- return ModelBatchRequest.fetch(api, compactQuery, id).then(data => new ArSyncRecord(parsedQuery, data, null, root));
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
- const request = { api, query: compactQuery, params };
190
- return ArSyncApi_1.default.syncFetch(request).then((response) => {
191
- if (response.collection && response.order) {
192
- return new ArSyncCollection(response.sync_keys, 'collection', parsedQuery, response, request, root);
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, request, root);
269
+ return new ArSyncCollection([], '', parsedQuery, response, request_1, root);
196
270
  }
197
271
  else {
198
- return new ArSyncRecord(parsedQuery, response, request, root);
272
+ return new ArSyncRecord(parsedQuery, response, request_1, root);
199
273
  }
200
274
  });
201
275
  }
202
- }
203
- static load(apiParams, root) {
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
- const resultModels = [];
208
- let countdown = apiParams.length;
209
- apiParams.forEach((param, i) => {
210
- this._load(param, root).then(model => {
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
- class ArSyncRecord extends ArSyncContainerBase {
221
- constructor(query, data, request, root) {
222
- super();
223
- this.root = root;
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
- this.initForReload(request);
226
- this.query = query;
227
- this.data = {};
228
- this.children = {};
229
- this.replaceData(data);
230
- }
231
- setSyncKeys(sync_keys) {
232
- this.sync_keys = sync_keys;
233
- if (!this.sync_keys) {
234
- this.sync_keys = [];
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 (const key in this.query.attributes) {
246
- const subQuery = this.query.attributes[key];
247
- const aliasName = subQuery.as || key;
248
- const subData = data[aliasName];
249
- const child = this.children[aliasName];
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
- const collection = new ArSyncCollection(this.sync_keys, key, subQuery, subData, null, this.root);
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
- const model = new ArSyncRecord(subQuery, subData, null, this.root);
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.query.attributes['*']) {
294
- for (const key in data) {
295
- if (!this.query.attributes[key] && this.data[key] !== data[key]) {
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
- const { action, class_name, id } = notifyData;
305
- const query = path && this.query.attributes[path];
306
- const aliasName = (query && query.as) || path;
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
- const child = this.children[aliasName];
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
- ModelBatchRequest.fetch(class_name, ArSyncRecord.compactQuery(query), id).then(data => {
320
- if (!data || !this.data)
396
+ modelBatchRequest.fetch(className, ArSyncRecord.compactQuery(query), id).then(function (data) {
397
+ if (!data || !_this.data)
321
398
  return;
322
- const model = new ArSyncRecord(query, data, null, this.root);
323
- const child = this.children[aliasName];
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
- this.children[aliasName] = model;
327
- this.mark();
328
- this.data[aliasName] = model.data;
329
- model.parentModel = this;
403
+ _this.children[aliasName] = model;
404
+ _this.mark();
405
+ _this.data[aliasName] = model.data;
406
+ model.parentModel = _this;
330
407
  model.parentKey = aliasName;
331
- this.onChange([aliasName], model.data);
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
- const { field } = notifyData;
336
- const query = field ? this.patchQuery(field) : this.reloadQuery();
337
- if (query)
338
- ModelBatchRequest.fetch(class_name, query, id).then(data => {
339
- if (this.data)
340
- this.update(data);
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
- const callback = data => this.onNotify(data);
346
- for (const key of this.sync_keys) {
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
- for (const path of this.paths) {
350
- const pathCallback = data => this.onNotify(data, path);
351
- for (const key of this.sync_keys)
352
- this.subscribe(key + path, pathCallback);
353
- }
354
- }
355
- patchQuery(key) {
356
- const val = this.query.attributes[key];
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
- let { attributes, as, params } = val;
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
- const result = {};
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
- const reloadQuery = this.reloadQueryCache = { attributes: [] };
377
- for (const key in this.query.attributes) {
468
+ var reloadQuery = this.reloadQueryCache = { attributes: [] };
469
+ for (var key in this.queryAttributes) {
378
470
  if (key === 'sync_keys')
379
471
  continue;
380
- const val = this.query.attributes[key];
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]: val });
477
+ reloadQuery.attributes.push((_a = {}, _a[key] = val, _a));
386
478
  }
387
479
  }
388
480
  return reloadQuery;
389
- }
390
- update(data) {
391
- for (const key in data) {
392
- const subQuery = this.query.attributes[key];
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 = Object.assign({}, 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
- class ArSyncCollection extends ArSyncContainerBase {
416
- constructor(sync_keys, path, query, data, request, root) {
417
- super();
418
- this.order = { limit: null, mode: 'asc', key: 'id' };
419
- this.aliasOrderKey = 'id';
420
- this.root = root;
421
- this.path = path;
422
- this.query = query;
423
- this.compactQuery = ArSyncRecord.compactQuery(query);
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
- this.initForReload(request);
426
- if (query.params && (query.params.order || query.params.limit)) {
427
- this.setOrdering(query.params.limit, query.params.order);
428
- }
429
- this.data = [];
430
- this.children = [];
431
- this.replaceData(data, sync_keys);
432
- }
433
- setOrdering(limit, order) {
434
- let mode = 'asc';
435
- let key = 'id';
436
- if (order === 'asc' || order === 'desc') {
437
- mode = order;
438
- }
439
- else if (typeof order === 'object' && order) {
440
- const keys = Object.keys(order);
441
- if (keys.length > 1)
442
- throw 'multiple order keys are not supported';
443
- if (keys.length === 1)
444
- key = keys[0];
445
- mode = order[key] === 'asc' ? 'asc' : 'desc';
446
- }
447
- const limitNumber = (typeof limit === 'number') ? limit : null;
448
- if (limitNumber !== null && key !== 'id')
449
- throw 'limit with custom order key is not supported';
450
- const subQuery = this.query.attributes[key];
451
- this.aliasOrderKey = (subQuery && subQuery.as) || key;
452
- this.order = { limit: limitNumber, mode, key };
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 => key + this.path);
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
- const existings = {};
465
- for (const child of this.children)
466
- existings[child.data.id] = child;
467
- let collection;
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
- else {
562
+ var collection;
563
+ if (Array.isArray(data)) {
473
564
  collection = data;
474
565
  }
475
- const newChildren = [];
476
- const newData = [];
477
- for (const subData of collection) {
478
- let model = null;
479
- if (typeof (subData) === 'object' && subData && 'id' in subData)
480
- model = existings[subData.id];
481
- let data = subData;
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.id) {
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
- data = model.data;
588
+ data_1 = model.data;
493
589
  }
494
- newData.push(data);
590
+ newData.push(data_1);
495
591
  }
496
592
  while (this.children.length) {
497
- const child = this.children.pop();
498
- if (!existings[child.data.id])
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 (const child of newChildren)
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
- for (const el of newData)
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
- if (this.data.findIndex(a => a.id === id) >= 0)
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 (this.order.limit === this.data.length) {
515
- if (this.order.mode === 'asc') {
516
- const last = this.data[this.data.length - 1];
517
- if (last && last.id < id)
518
- return;
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
- const last = this.data[this.data.length - 1];
522
- if (last && last.id > id)
523
- return;
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
- ModelBatchRequest.fetch(className, this.compactQuery, id).then((data) => {
527
- if (!data || !this.data)
641
+ modelBatchRequest.fetch(className, this.compactQuery, id).then(function (data) {
642
+ if (!data || !_this.data)
528
643
  return;
529
- const model = new ArSyncRecord(this.query, data, null, this.root);
530
- model.parentModel = this;
644
+ var model = new ArSyncRecord(_this.query, data, null, _this.root);
645
+ model.parentModel = _this;
531
646
  model.parentKey = id;
532
- const overflow = this.order.limit && this.order.limit === this.data.length;
533
- let rmodel;
534
- this.mark();
535
- const orderKey = this.aliasOrderKey;
536
- if (this.order.mode === 'asc') {
537
- const last = this.data[this.data.length - 1];
538
- this.children.push(model);
539
- this.data.push(model.data);
540
- if (last && last[orderKey] > data[orderKey])
541
- this.markAndSort();
542
- if (overflow) {
543
- rmodel = this.children.shift();
544
- rmodel.release();
545
- this.data.shift();
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
- const first = this.data[0];
550
- this.children.unshift(model);
551
- this.data.unshift(model.data);
552
- if (first && first[orderKey] > data[orderKey])
553
- this.markAndSort();
554
- if (overflow) {
555
- rmodel = this.children.pop();
556
- rmodel.release();
557
- this.data.pop();
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
- this.onChange([model.id], model.data);
690
+ _this.onChange([model.id], model.data);
561
691
  if (rmodel)
562
- this.onChange([rmodel.id], null);
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
- const orderKey = this.aliasOrderKey;
568
- if (this.order.mode === 'asc') {
569
- this.children.sort((a, b) => a.data[orderKey] < b.data[orderKey] ? -1 : +1);
570
- this.data.sort((a, b) => a[orderKey] < b[orderKey] ? -1 : +1);
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) => a.data[orderKey] > b.data[orderKey] ? -1 : +1);
574
- this.data.sort((a, b) => a[orderKey] > b[orderKey] ? -1 : +1);
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
- const idx = this.data.findIndex(a => a.id === id);
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
- const callback = data => this.onNotify(data);
597
- for (const key of this.sync_keys)
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
- onChange(path, data) {
601
- super.onChange(path, data);
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
- const idx = this.data.findIndex(a => a.id === id);
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 = [...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
- class ArSyncStore {
621
- constructor(request, { immutable } = {}) {
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
- ArSyncContainerBase.load(this.request, this).then((container) => {
633
- if (this.markForRelease) {
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
- this.container = container;
638
- this.data = container.data;
639
- if (this.immutable)
640
- this.freezeRecursive(this.data);
641
- this.complete = true;
642
- this.notfound = false;
643
- this.trigger('load');
644
- this.trigger('change', { path: [], value: this.data });
645
- container.onChange = (path, value) => {
646
- this.changes.push({ path, value });
647
- this.setChangesBufferTimer();
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
- this.trigger('connection', state);
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 (this.markForRelease)
793
+ if (_this.markForRelease)
656
794
  return;
657
795
  if (!e.retry) {
658
- this.complete = true;
659
- this.notfound = true;
660
- this.trigger('load');
796
+ _this.complete = true;
797
+ _this.notfound = true;
798
+ _this.trigger('load');
661
799
  return;
662
800
  }
663
- const sleepSeconds = Math.min(Math.pow(2, retryCount), 30);
664
- this.retryLoadTimer = setTimeout(() => {
665
- this.retryLoadTimer = null;
666
- this.load(retryCount + 1);
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
- this.changesBufferTimer = null;
675
- const changes = this.changes;
676
- this.changes = [];
677
- this.freezeMarked();
678
- this.data = this.container.data;
679
- changes.forEach(patch => this.trigger('change', 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
- let listeners = this.eventListeners.events[event];
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
- const id = this.eventListeners.serial++;
825
+ var id = this.eventListeners.serial++;
687
826
  listeners[id] = callback;
688
- return { unsubscribe: () => { delete listeners[id]; } };
689
- }
690
- trigger(event, arg) {
691
- const listeners = this.eventListeners.events[event];
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 (const id in listeners)
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 (const key in obj)
842
+ for (var key in obj)
704
843
  this.freezeRecursive(obj[key]);
705
844
  Object.freeze(obj);
706
- }
707
- freezeMarked() {
708
- this.markedForFreezeObjects.forEach(obj => this.freezeRecursive(obj));
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;