Code coverage report for lib/replicate/replication.js

Statements: 97.14% (34 / 35)      Branches: 100% (2 / 2)      Functions: 88.89% (8 / 9)      Lines: 97.14% (34 / 35)      Ignored: none     

All files » lib/replicate/ » replication.js
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55    1 1 1       1 1 1035 1035 1035 1035 1035 1035 1035   1035 581   1035 1043       1035     1 268 268 268     1 1195 1195 160   1035   1     1035 1035 1 986 986   1035     1
'use strict';
 
var utils = require('./../utils');
var EE = require('events').EventEmitter;
var Promise = utils.Promise;
 
// We create a basic promise so the caller can cancel the replication possibly
// before we have actually started listening to changes etc
utils.inherits(Replication, EE);
function Replication() {
  EE.call(this);
  this.cancelled = false;
  this.state = 'pending';
  var self = this;
  var promise = new Promise(function (fulfill, reject) {
    self.once('complete', fulfill);
    self.once('error', reject);
  });
  self.then = function (resolve, reject) {
    return promise.then(resolve, reject);
  };
  self.catch = function (reject) {
    return promise.catch(reject);
  };
  // As we allow error handling via "error" event as well,
  // put a stub in here so that rejecting never throws UnhandledError.
  self.catch(function () {});
}
 
Replication.prototype.cancel = function () {
  this.cancelled = true;
  this.state = 'cancelled';
  this.emit('cancel');
};
 
Replication.prototype.ready = function (src, target) {
  var self = this;
  if (self._readyCalled) {
    return;
  }
  self._readyCalled = true;
 
  function onDestroy() {
    self.cancel();
  }
  src.once('destroyed', onDestroy);
  target.once('destroyed', onDestroy);
  function cleanup() {
    src.removeListener('destroyed', onDestroy);
    target.removeListener('destroyed', onDestroy);
  }
  self.once('complete', cleanup);
};
 
module.exports = Replication;