Class: PersistenceSyncManager

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
Source:

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 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 scope of the Requests. If not specified, will trigger for all Requests.
Source:

##getSyncLog() → {Promise}

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
Source:
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

##insertRequest(request, options) → {Promise}

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 Description
request Request Request object
options Object 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.
Source:
Returns:
Returns a Promise which resolves with the Request Id when complete
Type
Promise

##removeEventListener(type, listener, scope)

Remove the event listener.
Parameters:
Name Type 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 scope of the Requests. If not specified, will trigger for all Requests.
Source:

##removeRequest(requestId) → {Promise}

Delete a Request from the Sync Log
Parameters:
Name Type Description
requestId string The unique id for the Request
Source:
Returns:
Returns a Promise which resolves to the removed Request when complete
Type
Promise

##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 Description
options Object 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.
Source:
Returns:
Returns a Promise which resolves when complete
Type
Promise

##updateRequest(requestId, request) → {Promise}

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
Source:
Returns:
Returns a Promise which resolves to the replaced Request when complete
Type
Promise