From 7d233e588f0eeaf3c987ed61eab31c98759a5970 Mon Sep 17 00:00:00 2001 From: tompng Date: Wed, 3 Jul 2024 02:08:31 +0900 Subject: [PATCH 1/2] Store data.sync_keys and data.id into data._sync --- lib/ar_sync/class_methods.rb | 6 +- lib/ar_sync/core.rb | 8 +-- lib/ar_sync/instance_methods.rb | 8 +++ src/core/ArSyncStore.ts | 100 ++++++++++++++++---------------- test/sync_test.rb | 3 +- test/type_test.ts | 2 +- 6 files changed, 65 insertions(+), 62 deletions(-) diff --git a/lib/ar_sync/class_methods.rb b/lib/ar_sync/class_methods.rb index 852b5d9..29d3eef 100644 --- a/lib/ar_sync/class_methods.rb +++ b/lib/ar_sync/class_methods.rb @@ -173,12 +173,8 @@ def _initialize_sync_callbacks _sync_define :id - _sync_define :sync_keys, type: [:string] do |current_user| - ArSync.sync_keys self, current_user - end - serializer_defaults namespace: :sync do |current_user| - { id: id, sync_keys: ArSync.sync_keys(self, current_user) } + { _sync: _sync_field(current_user) } end after_initialize do diff --git a/lib/ar_sync/core.rb b/lib/ar_sync/core.rb index cf17aca..edceda1 100644 --- a/lib/ar_sync/core.rb +++ b/lib/ar_sync/core.rb @@ -41,7 +41,7 @@ def self.sync_send(to:, action:, model:, path: nil, field: nil, to_user: nil) e = { action: action } e[:field] = field if field if model - e[:class_name] = model.class.base_class.name + e[:class] = model.class.base_class.name e[:id] = model.id end event = ["#{key}#{path}", e] @@ -53,10 +53,6 @@ def self.sync_send(to:, action:, model:, path: nil, field: nil, to_user: nil) end end - def self.sync_keys(model, user) - [sync_key(model), sync_key(model, user)] - end - def self.sync_key(model, to_user = nil, signature: true) if model.is_a? ArSync::Collection key = [to_user&.id, model.klass.name, model.name].join '/' @@ -91,7 +87,7 @@ def self.sync_serialize(target, user, query) serialized = ArSerializer.serialize target, query, context: user, use: :sync return serialized if target.is_a? ArSync::ModelBase { - sync_keys: ArSync.sync_keys(target, user), + _sync: { keys: [ArSync.sync_key(target), ArSync.sync_key(target, user)] }, ordering: target.ordering, collection: serialized } diff --git a/lib/ar_sync/instance_methods.rb b/lib/ar_sync/instance_methods.rb index 5b42409..da995f7 100644 --- a/lib/ar_sync/instance_methods.rb +++ b/lib/ar_sync/instance_methods.rb @@ -1,4 +1,12 @@ module ArSync::ModelBase::InstanceMethods + def sync_keys(current_user) + [ArSync.sync_key(self), ArSync.sync_key(self, current_user)] + end + + def _sync_field(current_user) + { id:, keys: sync_keys(current_user) } + end + def _sync_notify(action) _sync_notify_parent action if self.class._sync_self? diff --git a/src/core/ArSyncStore.ts b/src/core/ArSyncStore.ts index ccb97f8..2e0639a 100644 --- a/src/core/ArSyncStore.ts +++ b/src/core/ArSyncStore.ts @@ -1,5 +1,6 @@ import ArSyncApi from './ArSyncApi' export type Request = { api: string; query: any; params?: any; id?: any } + class ModelBatchRequest { timer: number | null = null apiRequests = new Map { for (const model of models) { - const req = requests.get(model.id) + const req = requests.get(model._sync.id) if (req) req.model = model } requests.forEach(({ model, callbacks }) => { @@ -67,6 +68,7 @@ type ParsedQuery = { params: any } | {} +type SyncField = { id: number; keys: string[] } type Unsubscribable = { unsubscribe: () => void } class ArSyncContainerBase { @@ -76,9 +78,8 @@ class ArSyncContainerBase { parentModel: ArSyncRecord | ArSyncCollection | null = null parentKey children: ArSyncContainerBase[] | { [key: string]: ArSyncContainerBase | null } - sync_keys: string[] onConnectionChange?: (status: boolean) => void - replaceData(_data, _sync_keys?) {} + replaceData(_data, _parentSyncKeys?) {} initForReload(request) { this.networkSubscriber = ArSyncStore.connectionManager.subscribeNetwork((state) => { if (!state) { @@ -211,8 +212,8 @@ class ArSyncContainerBase { return ArSyncApi.syncFetch(request).then((response: any) => { if (!response) { throw { retry: false } - } else if (response.collection && response.order) { - return new ArSyncCollection(response.sync_keys, 'collection', parsedQuery, response, request, root, null, null) + } else if (response.collection && response.order && response._sync) { + return new ArSyncCollection(response._sync.keys, 'collection', parsedQuery, response, request, root, null, null) } else if (response instanceof Array) { return new ArSyncCollection([], '', parsedQuery, response, request, root, null, null) } else { @@ -225,7 +226,7 @@ class ArSyncContainerBase { type NotifyData = { action: 'add' | 'remove' | 'update' - class_name: string + class: string id: number field?: string } @@ -236,6 +237,7 @@ class ArSyncRecord extends ArSyncContainerBase { query queryAttributes data + syncKeys: string[] children: { [key: string]: ArSyncContainerBase | null } paths: string[] reloadQueryCache @@ -250,39 +252,35 @@ class ArSyncRecord extends ArSyncContainerBase { this.data = {} this.children = {} this.rootRecord = !parentModel + this.id = data._sync.id + this.syncKeys = data._sync.keys this.replaceData(data) this.parentModel = parentModel this.parentKey = parentKey } - setSyncKeys(sync_keys: string[] | undefined) { - this.sync_keys = sync_keys ?? [] - } - replaceData(data) { - this.setSyncKeys(data.sync_keys) + replaceData(data: { _sync: SyncField }) { + this.id = data._sync.id + this.syncKeys = data._sync.keys this.unsubscribeAll() - if (this.data.id !== data.id) { - this.mark() - this.data.id = data.id - } this.paths = [] for (const key in this.queryAttributes) { const subQuery = this.queryAttributes[key] const aliasName = subQuery.as || key const subData = data[aliasName] const child = this.children[aliasName] - if (key === 'sync_keys') continue - if (subData instanceof Array || (subData && subData.collection && subData.order)) { + if (key === '_sync') continue + if (subData instanceof Array || (subData && subData.collection && subData.order && subData._sync)) { if (child) { - child.replaceData(subData, this.sync_keys) + child.replaceData(subData, this.syncKeys) } else { - const collection = new ArSyncCollection(this.sync_keys, key, subQuery, subData, null, this.root, this, aliasName) + const collection = new ArSyncCollection(this.syncKeys, key, subQuery, subData, null, this.root, this, aliasName) this.mark() this.children[aliasName] = collection this.data[aliasName] = collection.data } } else { if (subQuery.attributes && Object.keys(subQuery.attributes).length > 0) this.paths.push(key) - if (subData && subData.sync_keys) { + if (subData && subData._sync) { if (child) { child.replaceData(subData) } else { @@ -305,6 +303,7 @@ class ArSyncRecord extends ArSyncContainerBase { } if (this.queryAttributes['*']) { for (const key in data) { + if (key === '_sync') continue if (!this.queryAttributes[key] && this.data[key] !== data[key]) { this.mark() this.data[key] = data[key] @@ -314,7 +313,7 @@ class ArSyncRecord extends ArSyncContainerBase { this.subscribeAll() } onNotify(notifyData: NotifyData, path?: string) { - const { action, class_name: className, id } = notifyData + const { action, class: className, id } = notifyData const query = path && this.queryAttributes[path] const aliasName = (query && query.as) || path; if (action === 'remove') { @@ -326,7 +325,8 @@ class ArSyncRecord extends ArSyncContainerBase { this.data[aliasName] = null this.onChange([aliasName], null) } else if (action === 'add') { - if (this.data[aliasName] && this.data[aliasName].id === id) return + const child = this.children[aliasName] + if (child instanceof ArSyncRecord && child.id === id) return const fetchKey = `${aliasName}:${id}` this.fetching.add(fetchKey) modelBatchRequest.fetch(className, ArSyncRecord.compactQueryAttributes(query), id).then(data => { @@ -358,16 +358,16 @@ class ArSyncRecord extends ArSyncContainerBase { } subscribeAll() { const callback = data => this.onNotify(data) - for (const key of this.sync_keys) { + for (const key of this.syncKeys) { this.subscribe(key, callback) } for (const path of this.paths) { const pathCallback = data => this.onNotify(data, path) - for (const key of this.sync_keys) this.subscribe(key + path, pathCallback) + for (const key of this.syncKeys) this.subscribe(key + path, pathCallback) } if (this.rootRecord) { - const destroyCallback = () => this.root.handleDestroy() - for (const key of this.sync_keys) this.subscribe(key + '_destroy', destroyCallback) + const key = this.syncKeys[0] + if (key) this.subscribe(key + '_destroy', () => this.root.handleDestroy()) } } patchQuery(key: string) { @@ -379,7 +379,7 @@ class ArSyncRecord extends ArSyncContainerBase { let arrayQuery = [] as string[] | null const hashQuery = {} for (const key in this.queryAttributes) { - if (key === 'sync_keys') continue + if (key === '_sync') continue const val = this.queryAttributes[key] if (!val || !val.attributes) { arrayQuery?.push(key) @@ -393,6 +393,7 @@ class ArSyncRecord extends ArSyncContainerBase { } update(data) { for (const key in data) { + if (key === '_sync') continue const subQuery = this.queryAttributes[key] if (subQuery && subQuery.attributes && Object.keys(subQuery.attributes).length > 0) continue if (this.data[key] === data[key]) continue @@ -421,11 +422,12 @@ class ArSyncCollection extends ArSyncContainerBase { query queryAttributes compactQueryAttributes + syncKeys: string[] data: any[] children: ArSyncRecord[] aliasOrderKey = 'id' fetching = new Set() - constructor(sync_keys: string[], path: string, query, data: any[], request, root: ArSyncStore, parentModel: ArSyncRecord | null, parentKey: string | null){ + constructor(parentSyncKeys: string[], path: string, query, data: any[], request, root: ArSyncStore, parentModel: ArSyncRecord | null, parentKey: string | null){ super() this.root = root this.path = path @@ -438,7 +440,7 @@ class ArSyncCollection extends ArSyncContainerBase { } this.data = [] this.children = [] - this.replaceData(data, sync_keys) + this.replaceData(data, parentSyncKeys) this.parentModel = parentModel this.parentKey = parentKey } @@ -455,17 +457,17 @@ class ArSyncCollection extends ArSyncContainerBase { this.aliasOrderKey = (subQuery && subQuery.as) || orderBy this.ordering = { first, last, direction, orderBy } } - setSyncKeys(sync_keys: string[]) { - if (sync_keys) { - this.sync_keys = sync_keys.map(key => key + this.path) + setSyncKeys(parentSyncKeys: string[] | undefined) { + if (parentSyncKeys) { + this.syncKeys = parentSyncKeys.map(key => key + this.path) } else { - this.sync_keys = [] + this.syncKeys = [] } } - replaceData(data: any[] | { collection: any[]; ordering: Ordering }, sync_keys: string[]) { - this.setSyncKeys(sync_keys) + replaceData(data: any[] | { collection: any[]; ordering: Ordering }, parentSyncKeys: string[]) { + this.setSyncKeys(parentSyncKeys) const existings = new Map() - for (const child of this.children) existings.set(child.data.id, child) + for (const child of this.children) existings.set(child.id, child) let collection: any[] if (Array.isArray(data)) { collection = data @@ -477,12 +479,12 @@ class ArSyncCollection extends ArSyncContainerBase { const newData: any[] = [] for (const subData of collection) { let model: ArSyncRecord | undefined = undefined - if (typeof(subData) === 'object' && subData && 'sync_keys' in subData) model = existings.get(subData.id) + if (typeof(subData) === 'object' && subData && '_sync' in subData) model = existings.get(subData._sync.id) let data = subData if (model) { model.replaceData(subData) - } else if (subData.sync_keys) { - model = new ArSyncRecord(this.query, subData, null, this.root, this, subData.id) + } else if (subData._sync) { + model = new ArSyncRecord(this.query, subData, null, this.root, this, subData._sync.id) } if (model) { newChildren.push(model) @@ -492,7 +494,7 @@ class ArSyncCollection extends ArSyncContainerBase { } while (this.children.length) { const child = this.children.pop()! - if (!existings.has(child.data.id)) child.release() + if (!existings.has(child.id)) child.release() } if (this.data.length || newChildren.length) this.mark() while (this.data.length) this.data.pop() @@ -503,10 +505,10 @@ class ArSyncCollection extends ArSyncContainerBase { consumeAdd(className: string, id: number) { const { first, last, direction } = this.ordering const limit = first || last - if (this.data.findIndex(a => a.id === id) >= 0) return - if (limit && limit <= this.data.length) { - const lastItem = this.data[this.data.length - 1] - const firstItem = this.data[0] + if (this.children.find(a => a.id === id)) return + if (limit && limit <= this.children.length) { + const lastItem = this.children[this.children.length - 1] + const firstItem = this.children[0] if (direction === 'asc') { if (first) { if (lastItem && lastItem.id < id) return @@ -584,7 +586,7 @@ class ArSyncCollection extends ArSyncContainerBase { } } consumeRemove(id: number) { - const idx = this.data.findIndex(a => a.id === id) + const idx = this.children.findIndex(a => a.id === id) this.fetching.delete(id) // To cancel consumeAdd if (idx < 0) return this.mark() @@ -593,16 +595,16 @@ class ArSyncCollection extends ArSyncContainerBase { this.data.splice(idx, 1) this.onChange([id], null) } - onNotify(notifyData) { + onNotify(notifyData: NotifyData) { if (notifyData.action === 'add') { - this.consumeAdd(notifyData.class_name, notifyData.id) + this.consumeAdd(notifyData.class, notifyData.id) } else if (notifyData.action === 'remove') { this.consumeRemove(notifyData.id) } } subscribeAll() { const callback = data => this.onNotify(data) - for (const key of this.sync_keys) this.subscribe(key, callback) + for (const key of this.syncKeys) this.subscribe(key, callback) } onChange(path: (string | number)[], data) { super.onChange(path, data) @@ -610,7 +612,7 @@ class ArSyncCollection extends ArSyncContainerBase { } markAndSet(id: number, data) { this.mark() - const idx = this.data.findIndex(a => a.id === id) + const idx = this.children.findIndex(a => a.id === id) if (idx >= 0) this.data[idx] = data } mark() { diff --git a/test/sync_test.rb b/test/sync_test.rb index 7f9e05d..dc5e06d 100644 --- a/test/sync_test.rb +++ b/test/sync_test.rb @@ -40,6 +40,7 @@ class Schema title: true, myComments: { as: 'myCmnts', attributes: ['id', 'starCount'] }, comments: { + id: true, starCount: { as: '星' }, user: ['id', 'name'], myStar: { as: 'myReaction', attributes: 'id' } @@ -252,7 +253,7 @@ class Schema runner.eval_script <<~JAVASCRIPT global.noSubqueryTestModel = new ArSyncModel({ api: 'currentUser', - query: ['id', 'posts'] + query: ['id', { posts: 'id' }] }) JAVASCRIPT runner.assert_script 'noSubqueryTestModel.data' diff --git a/test/type_test.ts b/test/type_test.ts index 3f56b7e..e166533 100644 --- a/test/type_test.ts +++ b/test/type_test.ts @@ -25,7 +25,7 @@ isOK>() const data2 = new ArSyncModel({ api: 'currentUser', query: ['id', 'name'] }).data! isOK>() const data3 = new ArSyncModel({ api: 'currentUser', query: '*' }).data! -isOK>() +isOK>() const data4 = new ArSyncModel({ api: 'currentUser', query: { posts: 'id' } }).data! isOK>() const data5 = new ArSyncModel({ api: 'currentUser', query: { posts: '*' } }).data! From 0163ecf5d5026cf0e54339845d9caa2102e02d4f Mon Sep 17 00:00:00 2001 From: tompng Date: Thu, 4 Jul 2024 00:51:57 +0900 Subject: [PATCH 2/2] build ts --- core/ArSyncStore.js | 97 ++++++++++++++++++++++----------------------- 1 file changed, 48 insertions(+), 49 deletions(-) diff --git a/core/ArSyncStore.js b/core/ArSyncStore.js index f0da1ed..e1bf249 100644 --- a/core/ArSyncStore.js +++ b/core/ArSyncStore.js @@ -63,7 +63,7 @@ var ModelBatchRequest = /** @class */ (function () { ArSyncApi_1.default.syncFetch({ api: api, query: query, params: { ids: ids } }).then(function (models) { for (var _i = 0, models_1 = models; _i < models_1.length; _i++) { var model = models_1[_i]; - var req = requests.get(model.id); + var req = requests.get(model._sync.id); if (req) req.model = model; } @@ -98,7 +98,7 @@ var ArSyncContainerBase = /** @class */ (function () { this.listeners = []; this.parentModel = null; } - ArSyncContainerBase.prototype.replaceData = function (_data, _sync_keys) { }; + ArSyncContainerBase.prototype.replaceData = function (_data, _parentSyncKeys) { }; ArSyncContainerBase.prototype.initForReload = function (request) { var _this = this; this.networkSubscriber = ArSyncStore.connectionManager.subscribeNetwork(function (state) { @@ -266,8 +266,8 @@ var ArSyncContainerBase = /** @class */ (function () { if (!response) { throw { retry: false }; } - else if (response.collection && response.order) { - return new ArSyncCollection(response.sync_keys, 'collection', parsedQuery, response, request_1, root, null, null); + else if (response.collection && response.order && response._sync) { + return new ArSyncCollection(response._sync.keys, 'collection', parsedQuery, response, request_1, root, null, null); } else if (response instanceof Array) { return new ArSyncCollection([], '', parsedQuery, response, request_1, root, null, null); @@ -293,35 +293,31 @@ var ArSyncRecord = /** @class */ (function (_super) { _this.data = {}; _this.children = {}; _this.rootRecord = !parentModel; + _this.id = data._sync.id; + _this.syncKeys = data._sync.keys; _this.replaceData(data); _this.parentModel = parentModel; _this.parentKey = parentKey; return _this; } - ArSyncRecord.prototype.setSyncKeys = function (sync_keys) { - this.sync_keys = sync_keys !== null && sync_keys !== void 0 ? sync_keys : []; - }; ArSyncRecord.prototype.replaceData = function (data) { - this.setSyncKeys(data.sync_keys); + this.id = data._sync.id; + this.syncKeys = data._sync.keys; this.unsubscribeAll(); - if (this.data.id !== data.id) { - this.mark(); - this.data.id = data.id; - } this.paths = []; for (var key in this.queryAttributes) { var subQuery = this.queryAttributes[key]; var aliasName = subQuery.as || key; var subData = data[aliasName]; var child = this.children[aliasName]; - if (key === 'sync_keys') + if (key === '_sync') continue; - if (subData instanceof Array || (subData && subData.collection && subData.order)) { + if (subData instanceof Array || (subData && subData.collection && subData.order && subData._sync)) { if (child) { - child.replaceData(subData, this.sync_keys); + child.replaceData(subData, this.syncKeys); } else { - var collection = new ArSyncCollection(this.sync_keys, key, subQuery, subData, null, this.root, this, aliasName); + var collection = new ArSyncCollection(this.syncKeys, key, subQuery, subData, null, this.root, this, aliasName); this.mark(); this.children[aliasName] = collection; this.data[aliasName] = collection.data; @@ -330,7 +326,7 @@ var ArSyncRecord = /** @class */ (function (_super) { else { if (subQuery.attributes && Object.keys(subQuery.attributes).length > 0) this.paths.push(key); - if (subData && subData.sync_keys) { + if (subData && subData._sync) { if (child) { child.replaceData(subData); } @@ -355,6 +351,8 @@ var ArSyncRecord = /** @class */ (function (_super) { } if (this.queryAttributes['*']) { for (var key in data) { + if (key === '_sync') + continue; if (!this.queryAttributes[key] && this.data[key] !== data[key]) { this.mark(); this.data[key] = data[key]; @@ -365,7 +363,7 @@ var ArSyncRecord = /** @class */ (function (_super) { }; ArSyncRecord.prototype.onNotify = function (notifyData, path) { var _this = this; - var action = notifyData.action, className = notifyData.class_name, id = notifyData.id; + var action = notifyData.action, className = notifyData.class, id = notifyData.id; var query = path && this.queryAttributes[path]; var aliasName = (query && query.as) || path; if (action === 'remove') { @@ -379,7 +377,8 @@ var ArSyncRecord = /** @class */ (function (_super) { this.onChange([aliasName], null); } else if (action === 'add') { - if (this.data[aliasName] && this.data[aliasName].id === id) + var child = this.children[aliasName]; + if (child instanceof ArSyncRecord && child.id === id) return; var fetchKey_1 = aliasName + ":" + id; this.fetching.add(fetchKey_1); @@ -418,13 +417,13 @@ var ArSyncRecord = /** @class */ (function (_super) { ArSyncRecord.prototype.subscribeAll = function () { var _this = this; var callback = function (data) { return _this.onNotify(data); }; - for (var _i = 0, _a = this.sync_keys; _i < _a.length; _i++) { + for (var _i = 0, _a = this.syncKeys; _i < _a.length; _i++) { var key = _a[_i]; this.subscribe(key, callback); } var _loop_1 = function (path) { var pathCallback = function (data) { return _this.onNotify(data, path); }; - for (var _i = 0, _a = this_1.sync_keys; _i < _a.length; _i++) { + for (var _i = 0, _a = this_1.syncKeys; _i < _a.length; _i++) { var key = _a[_i]; this_1.subscribe(key + path, pathCallback); } @@ -435,11 +434,9 @@ var ArSyncRecord = /** @class */ (function (_super) { _loop_1(path); } if (this.rootRecord) { - var destroyCallback = function () { return _this.root.handleDestroy(); }; - for (var _d = 0, _e = this.sync_keys; _d < _e.length; _d++) { - var key = _e[_d]; - this.subscribe(key + '_destroy', destroyCallback); - } + var key = this.syncKeys[0]; + if (key) + this.subscribe(key + '_destroy', function () { return _this.root.handleDestroy(); }); } }; ArSyncRecord.prototype.patchQuery = function (key) { @@ -454,7 +451,7 @@ var ArSyncRecord = /** @class */ (function (_super) { var arrayQuery = []; var hashQuery = {}; for (var key in this.queryAttributes) { - if (key === 'sync_keys') + if (key === '_sync') continue; var val = this.queryAttributes[key]; if (!val || !val.attributes) { @@ -470,6 +467,8 @@ var ArSyncRecord = /** @class */ (function (_super) { }; ArSyncRecord.prototype.update = function (data) { for (var key in data) { + if (key === '_sync') + continue; var subQuery = this.queryAttributes[key]; if (subQuery && subQuery.attributes && Object.keys(subQuery.attributes).length > 0) continue; @@ -496,7 +495,7 @@ var ArSyncRecord = /** @class */ (function (_super) { }(ArSyncContainerBase)); var ArSyncCollection = /** @class */ (function (_super) { __extends(ArSyncCollection, _super); - function ArSyncCollection(sync_keys, path, query, data, request, root, parentModel, parentKey) { + function ArSyncCollection(parentSyncKeys, path, query, data, request, root, parentModel, parentKey) { var _this = _super.call(this) || this; _this.ordering = { orderBy: 'id', direction: 'asc' }; _this.aliasOrderKey = 'id'; @@ -513,7 +512,7 @@ var ArSyncCollection = /** @class */ (function (_super) { } _this.data = []; _this.children = []; - _this.replaceData(data, sync_keys); + _this.replaceData(data, parentSyncKeys); _this.parentModel = parentModel; _this.parentKey = parentKey; return _this; @@ -535,21 +534,21 @@ var ArSyncCollection = /** @class */ (function (_super) { this.aliasOrderKey = (subQuery && subQuery.as) || orderBy; this.ordering = { first: first, last: last, direction: direction, orderBy: orderBy }; }; - ArSyncCollection.prototype.setSyncKeys = function (sync_keys) { + ArSyncCollection.prototype.setSyncKeys = function (parentSyncKeys) { var _this = this; - if (sync_keys) { - this.sync_keys = sync_keys.map(function (key) { return key + _this.path; }); + if (parentSyncKeys) { + this.syncKeys = parentSyncKeys.map(function (key) { return key + _this.path; }); } else { - this.sync_keys = []; + this.syncKeys = []; } }; - ArSyncCollection.prototype.replaceData = function (data, sync_keys) { - this.setSyncKeys(sync_keys); + ArSyncCollection.prototype.replaceData = function (data, parentSyncKeys) { + this.setSyncKeys(parentSyncKeys); var existings = new Map(); for (var _i = 0, _a = this.children; _i < _a.length; _i++) { var child = _a[_i]; - existings.set(child.data.id, child); + existings.set(child.id, child); } var collection; if (Array.isArray(data)) { @@ -564,14 +563,14 @@ var ArSyncCollection = /** @class */ (function (_super) { for (var _b = 0, collection_1 = collection; _b < collection_1.length; _b++) { var subData = collection_1[_b]; var model = undefined; - if (typeof (subData) === 'object' && subData && 'sync_keys' in subData) - model = existings.get(subData.id); + if (typeof (subData) === 'object' && subData && '_sync' in subData) + model = existings.get(subData._sync.id); var data_1 = subData; if (model) { model.replaceData(subData); } - else if (subData.sync_keys) { - model = new ArSyncRecord(this.query, subData, null, this.root, this, subData.id); + else if (subData._sync) { + model = new ArSyncRecord(this.query, subData, null, this.root, this, subData._sync.id); } if (model) { newChildren.push(model); @@ -581,7 +580,7 @@ var ArSyncCollection = /** @class */ (function (_super) { } while (this.children.length) { var child = this.children.pop(); - if (!existings.has(child.data.id)) + if (!existings.has(child.id)) child.release(); } if (this.data.length || newChildren.length) @@ -602,11 +601,11 @@ var ArSyncCollection = /** @class */ (function (_super) { var _this = this; var _a = this.ordering, first = _a.first, last = _a.last, direction = _a.direction; var limit = first || last; - if (this.data.findIndex(function (a) { return a.id === id; }) >= 0) + if (this.children.find(function (a) { return a.id === id; })) return; - if (limit && limit <= this.data.length) { - var lastItem = this.data[this.data.length - 1]; - var firstItem = this.data[0]; + if (limit && limit <= this.children.length) { + var lastItem = this.children[this.children.length - 1]; + var firstItem = this.children[0]; if (direction === 'asc') { if (first) { if (lastItem && lastItem.id < id) @@ -700,7 +699,7 @@ var ArSyncCollection = /** @class */ (function (_super) { } }; ArSyncCollection.prototype.consumeRemove = function (id) { - var idx = this.data.findIndex(function (a) { return a.id === id; }); + var idx = this.children.findIndex(function (a) { return a.id === id; }); this.fetching.delete(id); // To cancel consumeAdd if (idx < 0) return; @@ -712,7 +711,7 @@ var ArSyncCollection = /** @class */ (function (_super) { }; ArSyncCollection.prototype.onNotify = function (notifyData) { if (notifyData.action === 'add') { - this.consumeAdd(notifyData.class_name, notifyData.id); + this.consumeAdd(notifyData.class, notifyData.id); } else if (notifyData.action === 'remove') { this.consumeRemove(notifyData.id); @@ -721,7 +720,7 @@ var ArSyncCollection = /** @class */ (function (_super) { ArSyncCollection.prototype.subscribeAll = function () { var _this = this; var callback = function (data) { return _this.onNotify(data); }; - for (var _i = 0, _a = this.sync_keys; _i < _a.length; _i++) { + for (var _i = 0, _a = this.syncKeys; _i < _a.length; _i++) { var key = _a[_i]; this.subscribe(key, callback); } @@ -733,7 +732,7 @@ var ArSyncCollection = /** @class */ (function (_super) { }; ArSyncCollection.prototype.markAndSet = function (id, data) { this.mark(); - var idx = this.data.findIndex(function (a) { return a.id === id; }); + var idx = this.children.findIndex(function (a) { return a.id === id; }); if (idx >= 0) this.data[idx] = data; };