braintree-web
Version: 
A suite of tools for integrating Braintree in the browser
40 lines (29 loc) • 788 B
JavaScript
;
var batchExecuteFunctions = require("./batch-execute-functions");
function Destructor() {
  this._teardownRegistry = [];
  this._isTearingDown = false;
}
Destructor.prototype.registerFunctionForTeardown = function (fn) {
  if (typeof fn === "function") {
    this._teardownRegistry.push(fn);
  }
};
Destructor.prototype.teardown = function (callback) {
  if (this._isTearingDown) {
    callback(new Error("Destructor is already tearing down"));
    return;
  }
  this._isTearingDown = true;
  batchExecuteFunctions(
    this._teardownRegistry,
    function (err) {
      this._teardownRegistry = [];
      this._isTearingDown = false;
      if (typeof callback === "function") {
        callback(err);
      }
    }.bind(this)
  );
};
module.exports = Destructor;