Giter Club home page Giter Club logo

lifespan's Introduction

Lifespan

Reify the lifespan of an object, a component, whatever... Its really just like an never-failing Promise which executes its callback synchronously.

const released = {};

released.a = false;
let count = 0;
const a = new Lifespan().onRelease(() => released.a = true);
const i = setInterval(() => count = count + 1, 1000);
a.onRelease(() => clearInterval(i));
released.b = false;
const b = new Lifespan();
b.onRelease(() => released.b = true);
setTimeout(b.release, 5500);
b.onRelease(() => a.release());

released.c1 = released.c2 = released.c3 = false;
const c1 = new Lifespan().onRelease(() => released.c1 = true);
const c2 = new Lifespan().onRelease(() => released.c2 = true);
const c3 = new Lifespan().onRelease(() => released.c3 = true);

released.c4 = false;
const c4 = Lifespan.race(c1, c2, c3).onRelease(() => released.c4 = true);
c1.release();
released.c4.should.be.true;

released.d1 = released.d2 = released.d3 = false;
const d1 = new Lifespan().onRelease(() => released.d1 = true);
const d2 = new Lifespan().onRelease(() => released.d2 = true);
const d3 = Lifespan.join(d1, d2).onRelease(() => released.d3 = true);
d1.release();
released.d3.should.be.false;
d2.release();
released.d3.should.be.true;

setTimeout(() => {
  released.a.should.be.false;
  count.should.be.exactly(2);
}, 2200);

setTimeout(() => {
  released.a.should.be.true;
  count.should.be.exactly(5);
}, 6000);

// adds 'this.getLifespan()' method.
@lifespan
class MyComponent extends React.Component {
  ...
}

Why not just use Promise?

Promise, when properly implemented (like bluebird does) are great. Really. But sometimes you just want to reifiy "something to call later", and these calls to be synchronous.

Thats where Lifespan comes handy.

React.Component decorator

Lifespan comes with a ridiculously simple, yet very useful React.Component decorator. It adds the method this.getLifespan() to the component, which is released upon componentWillUnmount. It is lazily initialized and memoized so that the lifespan instance is only created if needed, and created once per component.

Use cases ?

EventEmitters, React Components, really anything when cleanup has to be performed at some point which is triggered manually.

Usage

This module is written in ES6/7. You will need babel to run it.

lifespan's People

Contributors

antca avatar elierotenberg avatar glaciuspgm avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  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.