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 = {}; releaseda = false;let count = 0;const a = ;const i = ;a;releasedb = false;const b = ;b;;b; releasedc1 = releasedc2 = releasedc3 = false;const c1 = ;const c2 = ;const c3 = ; releasedc4 = false;const c4 = Lifespan;c1;releasedc4shouldbetrue; releasedd1 = releasedd2 = releasedd3 = false;const d1 = ;const d2 = ;const d3 = Lifespan;d1;releasedd3shouldbefalse;d2;releasedd3shouldbetrue; ; ; // adds 'this.getLifespan()' method.@lifespanComponent ...
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.