DataStore.sync()

This API is available on Firefox OS for internal applications only.

The sync() method of the DataStore interface opens a cursor that allows you to step through any changes that have taken place in the data store going back to a particular revision ID, and run code in response to different types of change.

Note: The Data Store API is available in Web Workers, from Firefox 32 onwards (Firefox OS 2.0; see bug 949325.)

Syntax

var cursor = stores[0].sync(3);

Returns

A DataStoreCursor object.

Parameters

revisionId Optional
You can optionally pass sync() a revision ID that specifies the place in the revision history to go back to when running through the tasks. If you don't specify this, it runs through all tasks going back to the beginning of the history.

Example

In the following example, we create a cursor using sync(), then invoke the runNextTask() function to run the next task in the cursor. In this function, the DataStoreCursor.next method is used to retrieve the next task (a DataStoreTask object), then the manageTask() function checks what type of task it is, and takes action accordingly. In this case, we are either done running through the tasks, in which case we return, or the task is an addition, in which case we run a function to display that addition in the app UI then move on to the next task by running runNextTask() again.

var cursor = stores[0].sync();
runNextTask(cursor);
function runNextTask(cursor) {
 cursor.next().then(function(task) {
   manageTask(cursor, task);
 });
}
function manageTask(cursor, task) {
  if (task.operation == 'done') {
    // Finished adding contacts!
    return;
  }
  if (task.operation == 'add') {
    // Add the contacts that are different to how it was before
    displayExisting(task.id, task.data);
  }
  runNextTask(cursor);
}

Note: This code is taken from our Data Store Contacts Editor demo. Look through the code to help you understand better what is going on, and how the full code works.

Specifications

Specification Status Comment
Data Store API
The definition of 'sync()' in that specification.
Draft  

The discussion concerning this API's creation happened in various Mozilla mailing lists and other places. A summary of the discussion and further pointers can be found on the Mozilla Wiki. For further feedback and questions, send mail to the dev-webapi mailing list.

Browser compatibility

Feature Chrome Firefox (Gecko) Internet Explorer Opera Safari (WebKit)
Basic support No support No support No support No support No support
Feature Android Chrome Firefox Mobile (Gecko) Firefox OS IE Phone Opera Mobile Safari Mobile
Basic support No support No support No support 1.0.1 No support No support No support
Available in web workers No support No support No support 2.0 No support No support No support

See also

Document Tags and Contributors

 Contributors to this page: chrisdavidmills, fscholz
 Last updated by: chrisdavidmills,