Class: PersistenceManager

QuickNav

PersistenceManager

PersistenceManager is used for offline support by acting as the interface between the application, webservice, and the the Offline Persistence Toolkit. The PersistenceManger is where the endpoints are registered/unregistered for Offline Persistence Toolkit. In order to use PersistenceManager, a default story factory must be registered using registerDefaultStoreFactory before initializing PersistenceManager. PersistenceManager is not needed when Offline Persistence Toolkit is operating in ServiceWorker Mode. Please see Concept section for more infomation.

PersistenceManager should be initialized prior to using any of the other methods.

Methods

browserFetch(request) → {Promise.<Response>}

Call fetch API without going through the persistence framework. This is the unproxied browser provided fetch API.
Parameters:
Name Type Description
request String | Request A USVString containing the direct URL of the resource you want to fetch or a Request object.
See:
Returns:
Resolves to the Response when complete
Type
Promise.<Response>
Example
PersistenceManager.browserFetch('https://www.oracle.com/').then(function(response){
// Your Code Here
});

forceOffline(offline)

Force the PersistenceManager to go offline regardless of the browsers actual network status. This function should be used if the user wants the application to function in offline mode. When PersistenceManager.isOnline() is called, it will return false is forceOffline is set to true.
Parameters:
Name Type Description
offline boolean If true, sets the PersistenceManager offline
Examples

Toggle Force Offline to true

PersistenceManager.forceOffline(true)

Toggle Force Offline to false

PersistenceManager.forceOffline(false)

getCache() → {OfflineCache}

PersistenceManager.getCache returns the Offline Persistence Toolkit implementation of the standard Cache API. In additional to functionalities provided by the standard Cache API, this OfflineCache also interacts with shredding methods for more fine-grain caching and allows for a clear() function to remove the clear the cache.
See:
Returns:
returns the cache store for the Persistence Framework. Implements the Cache API.
Type
OfflineCache
Examples
var PersisteneCache = PersistenceManager.getCache()
PersistenceCache.[someMethod]
PersistenceManager.getCache().[someMethod]

getRegistration(url) → {Promise.<(PersistenceRegistration|undefined)>}

Return the PersistenceRegistration object for the given URL.
Parameters:
Name Type Description
url string Url
Returns:
Returns a Promise which resolves to the PersistenceRegistration object for the URL or undefined if nothing is found.
Type
Promise.<(PersistenceRegistration|undefined)>
Examples

Suppose a registration was registered for '/location'

PersistenceManager.getRegistration('https://www.oracle.com/location/stores').then(function(registration){
// registration will be the registration object for '/location'
});

No PersistenceRegistration is registered

PersistenceManager.getRegistration('https://www.oracle.com/doesNotExist').then(function(registration){
// registration will be undefined
});

getRegistrations() → {Promise.<Array.<PersistenceRegistration>>}

Return an array of PersistenceRegistration objects. Similar to PersistenceManager.register() when no options are passed in.
Returns:
Returns a Promise which resolves to an array of PersistenceRegistration objects.
Type
Promise.<Array.<PersistenceRegistration>>
Example
PersistenceManager.getRegistrations().then(function(registrationArray){
// Your Code Here
});

getSyncManager() → {PersistenceSyncManager}

getSyncManager returns the Sync Manager which is used to support synchronization capabilities for requests which were made when offline. 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.
Returns:
Returns the Sync Manager
Type
PersistenceSyncManager
Examples
var syncManager = PersistenceManager.getSyncManager()
syncManager.[someMethod]
PersistenceManager.getSyncManager().[someMethod]

init() → {Promise}

Initializes the PersistenceManager and will return a promise once the PersistenceManager is ready. A default story factory must be registered using registerDefaultStoreFactory before initializing PersistenceManager. This method is normally called when the applciation is first started or when a PersistenceRegistration needs to be deregistered.
Returns:
returns a Promise when resolved that this PersistenceManager is fully initialized.
Type
Promise
Example
PersistenceManager.init().then(function(){
 // Insert Your Code Here
 // sample code
 PersistenceManager.register()
});

isOnline() → {boolean}

Checks if the browser is online. PersistenceManager can be forced into offline mode by setting forceOffline to true.
  • Returns true if the browser AND PersistenceManager is online.
  • Returns false if the browser OR PersistenceManager is offline.
Note: To determine if the browser is online, the function will use navigator.onLine whose behavior is browser specific. If being used in a hybrid mobile application, please install the Cordova Network Information Plugin. Installing the plugin will enable this function to return accurate browser online status.
Returns:
Returns true if online, false if not.
Type
boolean

register(options) → {Promise.<(PersistenceRegistration|Array.<PersistenceRegistration>|null)>}

Registers a URL endpoint for persistence storage. The scope value can also be a regular expression. If not options are passed in, PersistenceManager.register acts similarly to getRegistrations and will return a array of all registrations objects or null if none exist.
Parameters:
Name Type Argument Description
options {scope: string} <optional>
Options to control registration
  • options.scope The URI which should be persisted.
  • options.scope Can also be a regular expression.
Returns:
Returns a Promise which resolves to a PersistenceRegistration object for the specified options. If options is null, returns an array of all current PersistenceRegistration objects or null if there are none.
Type
Promise.<(PersistenceRegistration|Array.<PersistenceRegistration>|null)>
Examples

String Scope

PersistenceManager.register({scope:'/location'}).then(function(registration){
// add event listeners for scope '/location'
});
PersistenceManager.register({scope:'oracle.com'}).then(function(registration){
// add event listeners for scope 'oracle.com'
});

Regular Expression Scope

PersistenceManager.register({scope:'/emp/g'}).then(function(registration){
// add event listeners for scope '/emp/'
});

No Options

PersistenceManager.register().then(function(registrationArray){
// Returns an array of registered objects
});
PersistenceManager.register().then(function(registration){
// registration is null if there are no registration objects
});