Changes

Jump to: navigation, search

WebAPI/DataStore

1,972 bytes added, 12:28, 14 September 2013
no edit summary
}
});
});
 
=== Sync ===
 
The synchronization of a DataStore with a 'private' app storage can be done using the 'sync' method. Calling this method, DataStore creates a DataStoreCursor that helps the app with the synchronization starting from scratch or for a valid revisionId. When a cursor is active, onchange events are not sent to the app. The sync operation can be terminated calling cursor.close(). If something changes in the DataStore when the cursor is synchronize the app, all the changes will be managed by the cursor as additional operation: this means that when the cursor completes its tasks, the app will be always in sync with the current revisionId of the DataStore.
 
The basic usage of the cursor is this:
 
navagiator.getDataStores('contacts').then(functions(stores) {
if (!stores.length) return;
let cursor = stores[0].sync(/* a revisionId can be used here. If it's invalid it'll be ignored */);
function cursorResolve(task) {
// task.operation describes what the app has to do in order to be in sync with the current revision of this datastore.
switch (task.operation) {
case 'done':
// No additional operation has to be done. When 'done' is received, the sync is completed and onchange events can be received again.
dump("The current revision ID is: " + task.revisionId + "\n");
return;
case 'clear':
// All the data you have are out-of-sync. Delete all of them.
break;
case 'add':
// A new object has to be inserted
dump("Adding id: " + task.id + " data: " + task.data + "\n");
break;
case 'update':
// Something has to be updated
dump("Updating id: " + task.id + " data: " + task.data + "\n");
break;
case 'remove':
dump("Record: " + task.id + " must be removed\n");
break;
}
cursor.next().then(cursorResolve);
}
// Cursor.next() always returns a promise.
cursor.next().then(cursorResolve);
});
* {name, owner, value} is a complicated key.
* UI: what to do when we have multiple access requests?
* What's happening if the central gets changes during the process of local updates?
* Should all data stores with the same name share a schema?
* Enforcing types can be a footgun. What should a data provider do if it decides some key should have a different type?
Confirm
53
edits

Navigation menu