all files / lib/replicate/ replication.js

100% Statements 41/41
83.33% Branches 5/6
100% Functions 10/10
100% Lines 40/40
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 56 57 58 59 60 61 62 63 64 65 66 67 68                          1023× 1023× 1023× 1023× 1023× 1023× 1023×   1023× 547×   1023× 1033×       1023×     344× 344× 344×     1131× 1131× 108×   1023×   99×   1023× 1023× 965× 965×   1023×    
'use strict';
 
Object.defineProperty(exports, "__esModule", {
  value: true
});
 
var _events = require('events');
 
var _promise = require('../deps/promise');
 
var _promise2 = _interopRequireDefault(_promise);
 
var _inherits = require('inherits');
 
var _inherits2 = _interopRequireDefault(_inherits);
 
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
 
// We create a basic promise so the caller can cancel the replication possibly
// before we have actually started listening to changes etc
(0, _inherits2.default)(Replication, _events.EventEmitter);
function Replication() {
  _events.EventEmitter.call(this);
  this.cancelled = false;
  this.state = 'pending';
  var self = this;
  var promise = new _promise2.default(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);
};
 
exports.default = Replication;
module.exports = exports['default'];