Class: PersistenceSyncManager

QuickNav

PersistenceSyncManager

The PersistenceSyncManager should be used support synchronization capabilities for requests which were made when offline. The PersistenceSyncManager instance can be obtained via the persistenceManager.getSyncManager() API. The PersistenceSyncManager supports operations such as retrieving the sync log, invoking the sync() API to replay the requests which were made while offline, inserting/removing requests from the sync log, adding event listeners for sync operations, and performing undo/redo operations for the shredded local data which were made as a result of those requests.

Constructor

new PersistenceSyncManager(isOnline, browserFetch, cache)

Parameters:
Name Type Description
isOnline function The persistenceManager.isOnline() function
browserFetch function The persistenceManager.browserFetch() function
cache function The persistenceManager.getCache() function

Methods

addEventListener(type, listener, scope)

Add an event listener. The listener should always return a Promise which should resolve to either null or an object with the action field.

For the beforeSyncRequest event, the resolved value of the Promise returned by the listener should be one of the items given below:

Resolved Value Behavior
null Continue replaying the request
{action: 'replay', request: Request obj} Replay the provided Request obj
{action: 'skip'} Skip replaying the request
{action: 'stop'} Stop the sync process
{action: 'continue'} Continue replaying the request

For the syncRequest event, the resolved value of the Promise returned by the listener should be one of the items given below:

Resolved Value Behavior
null Continue processing the sync log
{action: 'stop'} Stop the sync process
{action: 'continue'} Continue processing the sync log

Parameters:
Name Type Argument Description
type string A string representing the event type to listen for. Such as "beforeSyncRequest", "syncRequest", and "afterSyncRequest"
listener function The function that receives a notification when an event of the specified type occurs. The function should return a Promise.
scope string <optional>
optional scope of the Requests. If not specified, will trigger for all Requests.
Example
var afterRequestListener = function (event) {
  var statusCode = event.response.status;
  if (statusCode == 200) {
    return new Promise(function (resolve, reject) {
      // Handle Response Here
      resolve({action: 'continue'});
    });
  }
  return Promise.resolve({action: 'continue'});
}
PersistenceSyncManager.addEventListener('syncRequest',afterRequestListener,'/sync');

getSyncLog() → {Promise.<Array.<Request>>}

Returns a Promise which resolves to all the Requests in the Sync Log returned as an Array sorted by the created date of the Request
Returns:
Returns a Promise which resolves to all the Requests in the Sync Log returned as an Array of compound objects which have the following structure:
  • requestId An internally generated unique id for the Request.
  • request The Request object.
  • undo The undo function which returns a Promise to undo the changes made to the local shredded data store for the Request. The Promise will resolve to true if there is undo data and false if there wasn't any.
  • redo The redo function which returns a Promise to redo the changes made to the local shredded data store for the Request. The Promise will resolve to true if there is redo data and false if there wasn't any.
Type
Promise.<Array.<Request>>
Example
PersistenceSyncManger.getSyncLog().then(function(allRequests){
// Your Code Here
});

insertRequest(request, options) → {Promise.<String>}

Insert a Request into the Sync Log. The position in the Sync Log the Request will be inserted at is determined by the Request created date.
Parameters:
Name Type Argument Description
request Request Request object
options {undoRedoDataArray: Array} <optional>
Options
  • options.undoRedoDataArray optionally specify undo/redo data if this request was shredded. Should be an Array whose entries should have the structure:
    • operation The operation performed on the local store, e.g. upsert or remove
    • storeName The local store name
    • undoRedoData An Array of compounds object with the following structure containing the undo/redo data
      • key The key for the shredded data row.
      • undo The undo data for the shredded data row.
      • redo The redo data for the shredded data row.
Returns:
Returns a Promise which resolves with the Request Id when complete
Type
Promise.<String>
Examples
PersistenceSyncManager.insertRequest(request).then(function(requestID){
// Your Code Here
});
PersistenceSyncManager.insertRequest(request,{undoRedoDataArray:['upsert','localStore',[key,true,false]]}).then(function(requestID){
// Your Code Here
});

removeEventListener(type, listener, scope)

Remove the event listener.
Parameters:
Name Type Argument Description
type string A string representing the event type to listen for.
listener function The function that receives a notification when an event of the specified type occurs. The function should return a Promise.
scope string <optional>
optional scope of the Requests. If not specified, will trigger for all Requests.

removeRequest(requestId) → {Promise.<Request>}

Delete a Request from the Sync Log
Parameters:
Name Type Description
requestId string The unique id for the Request
Returns:
Returns a Promise which resolves to the removed Request when complete
Type
Promise.<Request>
Example
PersistenceSyncManager.removeRequest('uniqueid').then(function(removedRequest){
// your code here
});

sync(options) → {Promise}

Synchronize the log with the server. By default sync will first send an OPTIONS request before each request URL to determine if the server is reachable. This OPTIONS request will be timed out after 60s and the sync will fail with a HTTP response 504 error. If the OPTIONS request does not time out then sync will progress.
Parameters:
Name Type Argument Description
options Object <optional>
Options
  • options.preflightOptionsRequest - 'enabled' or 'disabled' or url regex. 'enabled' is the default. 'disabled' will disable sending an OPTIONS request for all URLs. Specifying an URL pattern will enable the OPTIONS request only for those URLs.
  • options.preflightOptionsRequestTimeout - The timeout for the OPTIONS request. Default is 60s.
Returns:
Returns a Promise which resolves when complete
Type
Promise
Examples
PersistenceSyncManager.sync().then(function(){
// Your code here
});
PersistenceSyncManager.sync({preflightOptionRequest:/employee/g,preflightOptionsRequestTimeout:100})

updateRequest(requestId, request) → {Promise.<Request>}

Update a Request from the Sync Log. This function effectivaly replaces the Request in the sync log with the provided Request.
Parameters:
Name Type Description
requestId string The unique id for the Request
request Request Request object
Returns:
Returns a Promise which resolves to the replaced Request when complete
Type
Promise.<Request>
Example
PersistenceSyncManager.updateRequest('uniqueID',request).then(function(oldRequest){
// your code here
});