Source: synchronization/synchronization.js

/**
 * Copyright© 2016, Oracle and/or its affiliates. All rights reserved.
 */


/**
 * Class that provides caching and synchronization capabilities. Callers should use
 * MobileBackend's [synchronization]{@link MobileBackend#synchronization} property.
 * @constructor
 * @global
 */
function Synchronization(manager, backend, config, utils, platform, persistence) {

  var _this = this;
  var _config = config;

  this._persistence = persistence;

  /**
   * The [MobileBackend]{@link MobileBackend} object that this Synchronization instance belongs to.
   * @type {MobileBackend}
   * @readonly
   */
  this.backend = backend;

  // TODO: next version
  // /**
  //  * The number of cache misses during the current WebStarterApp lifetime.
  //  * @type {number}
  //  * @readonly
  //  */
  // this.cacheHitCount = 0;

  // TODO: next version
  // /**
  //  * The number of cache hits during the current WebStarterApp lifetime.
  //  * @type {number}
  //  * @readonly
  //  */
  // this.cacheMissCount = 0;

  var _endpoints = {};
  // TODO: next version
  // this._pinnedUrls = {};


  /**
   * Sets the device to offline mode, which is good for testing.
   * If the device is in real offline mode, then this setting will be ignored
   * @param isOffline whether to set the device online or offline.
   */
  this.setOfflineMode = manager._setOfflineMode;

  /**
   * Gets device network status which is currently being used by Synchronization.
   * @returns {Boolean}
     */
  this.isOnline = function(){
    return persistence.options.isOnline();
  };

  /**
   * Deletes all cached resources.
   */
  this.purge = function () {
    for(var apiName in _endpoints){
      if(_endpoints.hasOwnProperty(apiName)){
        var api = _endpoints[apiName];
        for(var path in api){
          if(api.hasOwnProperty(path)){
            api[path].purge();
          }
        }
      }
    }
  };

  /**
   * Returns a [MobileEndpoint]{@link MobileEndpoint} that provides access to an endpoint in a custom code API.
   * @param apiName The name of the custom code API
   * @param endpointPath The endpoint in the custom code API
   * @returns A MobileEndpoint object.
   */
  this.openEndpoint = function (apiName, endpointPath) {
    if (_endpoints[apiName] == null) {
      _endpoints[apiName] = {};
    }

    if (_endpoints[apiName][endpointPath] == null) {
      _endpoints[apiName][endpointPath] = new MobileEndpoint(this, apiName, endpointPath, utils, platform);
    }

    return _endpoints[apiName][endpointPath];
  };

  // TODO: next version
  // this._pin = function (resource, successCallback, errorCallback) {
  //   this._pinnedUrls[resource.url] = mcs._PinStatePinned;
  //   resource.pinState = mcs._PinStatePinned;
  //
  //   this._store.saveResource(resource, successCallback, errorCallback);
  //   this._store.savePinedResources(this._pinnedUrls);
  // };

  // TODO: next version
  // this._unpin = function (resource, successCallback, errorCallback) {
  //   delete this._pinnedUrls[resource.url];
  //   resource.pinState = mcs._PinState.unpinned;
  //
  //   // PERSISTENCE: Only delete if no collection references this item!
  //   this._store.deleteResource(resource, successCallback, errorCallback);
  //   this._store.savePinedResources(this._pinnedUrls);
  // }
}

Synchronization.prototype._run = function(callback){
  return persistence.process.run(callback);
};

Synchronization.prototype._runWithoutReadInBackground = function(callback){
  return persistence.process.runWithoutReadInBackground(callback);
};