@braintree/destructor
TypeScript icon, indicating that this package has built-in type declarations

1.0.0 • Public • Published

destructor

Installation

npm install @braintree/destructor

Example

A class to keep track of tasks that must be performed to clean up any side effects caused by your SDK.

import Destructor from "@braintree/destructor";

class MyClass {
  constructor() {
    this.destructor = new Destructor();

    // add iframe to page
    const iframe = document.createElement("iframe");
    iframe.src = "https://example.com";
    document.body.appendChild(iframe);

    // register the removal of the iframe with the destructor
    this.destructor.register(() => {
      if (iframe.parentNode) {
        iframe.parentNode.removeChild(iframe);
      }
    });

    // any other tasks that need to be registered to the destructor
    // go here. Think event listeners or script tags, etc.
  }

  teardown() {
    // if you need to clean up any changes MyClass made, call
    // teardown and the destructor will run any functions that
    // were registered to it
    this.destructor.teardown();
  }
}

Requirements

This module requires Promises, so if used in an environment without Promise support, Promises will need to be polyfilled.

Running tests

npm test

Versions

Current Tags

  • Version
    Downloads (Last 7 Days)
    • Tag
  • 1.0.0
    1
    • latest

Version History

  • Version
    Downloads (Last 7 Days)
    • Published
  • 1.0.0
    1

Package Sidebar

Install

npm i @braintree/destructor

Weekly Downloads

1

Version

1.0.0

License

MIT

Unpacked Size

11.2 kB

Total Files

14

Last publish

Collaborators

  • braintree