-
-
Notifications
You must be signed in to change notification settings - Fork 600
LocalDatastore can update from server #734
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 4 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -12,6 +12,7 @@ | |
| import CoreManager from './CoreManager'; | ||
|
|
||
| import type ParseObject from './ParseObject'; | ||
| import ParseQuery from './ParseQuery'; | ||
|
|
||
| const DEFAULT_PIN = '_default'; | ||
| const PIN_PREFIX = 'parsePin_'; | ||
|
|
@@ -259,6 +260,54 @@ const LocalDatastore = { | |
| } | ||
| }, | ||
|
|
||
| async _updateFromServer() { | ||
| if (!this.isEnabled || this.isSyncing) { | ||
| return; | ||
| } | ||
| const localDatastore = await this._getAllContents(); | ||
| const keys = []; | ||
| for (const key in localDatastore) { | ||
| if (key !== DEFAULT_PIN && !key.startsWith(PIN_PREFIX)) { | ||
| keys.push(key); | ||
| } | ||
| } | ||
| if (keys.length === 0) { | ||
| return; | ||
| } | ||
| this.isSyncing = true; | ||
| const pointersHash = {}; | ||
| for (const key of keys) { | ||
| const [className, objectId] = key.split('_'); | ||
| pointersHash[className] = pointersHash[className] || new Set(); | ||
| pointersHash[className].add(objectId); | ||
| } | ||
| const queryPromises = Object.keys(pointersHash).map(className => { | ||
| const objectIds = Array.from(pointersHash[className]); | ||
| const query = new ParseQuery(className); | ||
| query.limit(objectIds.length); | ||
| if (objectIds.length === 1) { | ||
| query.equalTo('objectId', objectIds[0]); | ||
| } else { | ||
| query.containedIn('objectId', objectIds); | ||
| } | ||
| return query.find(); | ||
| }); | ||
| try { | ||
| const responses = await Promise.all(queryPromises); | ||
| const objects = [].concat.apply([], responses); | ||
| const pinPromises = objects.map((object) => { | ||
| const objectKey = this.getKeyForObject(object); | ||
| return this.pinWithName(objectKey, object._toFullJSON()); | ||
| }); | ||
| await Promise.all(pinPromises); | ||
| this.isSyncing = false; | ||
| } catch(error) { | ||
| console.log('Error syncing LocalDatastore'); // eslint-disable-line | ||
dplewis marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| console.log(error); // eslint-disable-line | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. any reason not to use parse's logger so this stuff shows up in aggregated logs? see middlewares, MogoTransform, PromiseRouter, etc. for examples
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. something like
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Any reason to not follow the exact pattern we have already? i.e. https://github.com/parse-community/parse-server/blob/master/src/PromiseRouter.js#L10 and remove the console.log and the eslint exception comment?
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thats the parse-server repo, there isn't a logger in this repo that I know of.
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ha. riiiight. yeah, lgtm. ;) . I don't use the client js sdk is the only lame excuse i have for that one. |
||
| this.isSyncing = false; | ||
| } | ||
| }, | ||
|
|
||
| getKeyForObject(object: any) { | ||
| const objectId = object.objectId || object._getId(); | ||
| return `${object.className}_${objectId}`; | ||
|
|
@@ -282,6 +331,7 @@ const LocalDatastore = { | |
| LocalDatastore.DEFAULT_PIN = DEFAULT_PIN; | ||
| LocalDatastore.PIN_PREFIX = PIN_PREFIX; | ||
| LocalDatastore.isEnabled = false; | ||
| LocalDatastore.isSyncing = false; | ||
|
|
||
| module.exports = LocalDatastore; | ||
|
|
||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.