Coder Social home page Coder Social logo

rlugojr / phosphor-disposable Goto Github PK

View Code? Open in Web Editor NEW

This project forked from blink1073/phosphor-disposable

0.0 2.0 0.0 153 KB

A module for expressing the disposable object pattern.

License: BSD 3-Clause "New" or "Revised" License

Shell 6.24% TypeScript 93.76%

phosphor-disposable's Introduction

phosphor-disposable

Build Status Coverage Status

A module for expressing the disposable object pattern.

Package Install

Prerequisites

npm install --save phosphor-disposable

Source Build

Prerequisites

git clone https://github.com/phosphorjs/phosphor-disposable.git
cd phosphor-disposable
npm install

Rebuild

npm run clean
npm run build

Run Tests

Follow the source build instructions first.

npm test

Build Docs

Follow the source build instructions first.

npm run docs

Navigate to docs/index.html.

Supported Runtimes

The runtime versions which are currently known to work are listed below. Earlier versions may also work, but come with no guarantees.

  • Node 0.12.7+
  • IE 11+
  • Firefox 32+
  • Chrome 38+

Bundle for the Browser

Follow the package install instructions first.

Any bundler that understands how to require() files with .js extensions can be used with this package.

Usage Examples

Note: This module is fully compatible with Node/Babel/ES6/ES5. Simply omit the type declarations when using a language other than TypeScript.

To test the phosphor-disposable module in a node interactive shell after the installation, open a terminal in your current working directory and run:

node

Then import the module into Node with the following command:

> disposable = require('phosphor-disposable');

Converting a function into a disposable object is straightforward using the provided DisposableDelegate class. Once the delegate is created, dispose() disposes of the delegate and invokes the callback. The dispose() method will only execute the function the first time, subsequent calls will do nothing.

> let delegate = new disposable.DisposableDelegate(() => {
... console.log('disposed');
... });

> delegate.dispose();
disposed

> delegate.dispose();

Collections of disposables are created with the provided DisposableSet() class. An array of disposable objects can be passed as argument to the class constructor. Individual objects can also be added to the disposable set by means of the add() method. The dispose() method will dispose all of the objects added to the set.

> let d1 = new disposable.DisposableDelegate(() => {
... console.log('one');
... });

> let d2 = new disposable.DisposableDelegate(() => {
... console.log('two');
... });

> let d3 = new disposable.DisposableDelegate(() => {
... console.log('three');
... });

> let set = new disposable.DisposableSet([d1, d2]);

> set.add(d3);

> set.dispose();
one
two
three

> set.dispose();

Custom disposable objects can be created by implementing IDisposable:

import {
  IDisposable
} from 'phosphor-disposable';

class MyDisposable implements IDisposable {

  constructor(id: string) {
    this._id = id;
  }

  get isDisposed(): boolean {
    return this._disposed;
  }

  dispose(): void {
    if (!this._disposed) {
      this._disposed = true;
      console.log(this._id);
    }
  }

  private _id: string;
  private _disposed = false;
}

let foo = new MyDisposable('foo');
let bar = new MyDisposable('bar');
let baz = new MyDisposable('baz');

let set = new DisposableSet();
set.add(foo);
set.add(bar);
set.add(baz);

set.dispose();  // logs: 'foo', 'bar', 'baz'
set.dispose();  // no-op

API

API Docs

phosphor-disposable's People

Contributors

blink1073 avatar gochoam avatar rlugojr avatar sccolbert avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.