initial commit
This commit is contained in:
commit
b5f4836961
|
|
@ -0,0 +1,11 @@
|
|||
{
|
||||
"name": "promise-stream",
|
||||
"version": "0.1.0",
|
||||
"description": "a stream of updates connected with promises",
|
||||
"main": "promise-stream.js",
|
||||
"scripts": {
|
||||
"test": "echo \"Error: no test specified\" && exit 1"
|
||||
},
|
||||
"author": "Jesse McDonald <nybble41@gmail.com>",
|
||||
"license": "CC-PDDC"
|
||||
}
|
||||
|
|
@ -0,0 +1,72 @@
|
|||
(function(factory) {
|
||||
if (typeof define !== 'undefined' && define.amd) {
|
||||
define([], factory);
|
||||
} else if (typeof define !== 'undefined' && define.petal) {
|
||||
define(['promise-stream'], [], factory);
|
||||
} else if (typeof module !== 'undefined' && module.exports) {
|
||||
module.exports = factory();
|
||||
} else {
|
||||
window.PromiseStream = factory();
|
||||
}
|
||||
})(function() {
|
||||
class PromiseStream {
|
||||
constructor() {
|
||||
this._current = new Promise((resolve) => {
|
||||
this._resolve = resolve;
|
||||
});
|
||||
this._next = this._current;
|
||||
this._value = undefined;
|
||||
}
|
||||
|
||||
get promise() {
|
||||
/* waits for the _first_ update, or completes immediately */
|
||||
return this._current;
|
||||
}
|
||||
|
||||
get next_promise() {
|
||||
/* waits for the _next_ update (which may be the first) */
|
||||
return this._next;
|
||||
}
|
||||
|
||||
get value() {
|
||||
return this._value;
|
||||
}
|
||||
|
||||
update(value) {
|
||||
if (value !== undefined && value !== this._value) {
|
||||
const resolve = this._resolve;
|
||||
const current = this._next;
|
||||
const next = new Promise((resolve) => {
|
||||
this._resolve = resolve;
|
||||
});
|
||||
|
||||
this._current = current;
|
||||
this._next = next;
|
||||
this._value = value;
|
||||
|
||||
resolve({ value, next });
|
||||
}
|
||||
}
|
||||
|
||||
once(cb) {
|
||||
this.promise.then((v) => { cb(v.value); });
|
||||
}
|
||||
|
||||
listen(cb) {
|
||||
let cancelled = false;
|
||||
|
||||
function renew(v) {
|
||||
if (!cancelled) {
|
||||
cb(v.value);
|
||||
v.next.then(renew);
|
||||
}
|
||||
}
|
||||
|
||||
this.promise.then(renew);
|
||||
|
||||
return () => { cancelled = true; };
|
||||
}
|
||||
}
|
||||
|
||||
return PromiseStream;
|
||||
});
|
||||
Loading…
Reference in New Issue