Comparing version 0.0.12 to 0.0.13
88
index.js
@@ -1,8 +0,11 @@ | ||
module.exports = function makeDefer() { | ||
let resolve = undefined | ||
let reject = undefined | ||
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.makeBroadcastStream = void 0; | ||
function makeDefer() { | ||
let resolve = undefined; | ||
let reject = undefined; | ||
const promise = new Promise((rs, rj) => { | ||
resolve = rs | ||
reject = rj | ||
}) | ||
resolve = rs; | ||
reject = rj; | ||
}); | ||
return { | ||
@@ -12,9 +15,9 @@ resolve, | ||
promise | ||
} | ||
}; | ||
} | ||
module.exports.makeBroadcastStream = function makeBroadcastStream() { | ||
const listeners = [] | ||
let done = false | ||
let defer = makeDefer() | ||
exports.default = makeDefer; | ||
function makeBroadcastStream() { | ||
const listeners = []; | ||
let done = false; | ||
let defer = makeDefer(); | ||
return { | ||
@@ -24,56 +27,59 @@ [Symbol.asyncIterator]() { | ||
next() { | ||
return defer.promise | ||
const { promise } = defer; | ||
defer = makeDefer(); | ||
return promise; | ||
}, | ||
async return(value) { | ||
return {value: undefined, done: true} | ||
return { value: undefined, done: true }; | ||
}, | ||
async throw(e) { | ||
return {value: undefined, done: true} | ||
return { value: undefined, done: true }; | ||
} | ||
} | ||
}; | ||
}, | ||
listen(onNext, {onError, onDone} = {}) { | ||
listen(onNext, { onError, onDone } = {}) { | ||
if (done) | ||
throw new Error('Cannot listen after done') | ||
const listener = {onNext, onError, onDone} | ||
listeners.push(listener) | ||
throw new Error('Cannot listen after done'); | ||
const listener = { onNext, onError, onDone }; | ||
listeners.push(listener); | ||
return function removeListener() { | ||
const idx = listeners.lastIndexOf(listener) | ||
const idx = listeners.lastIndexOf(listener); | ||
if (idx >= 0) | ||
listeners.splice(idx, 1) | ||
} | ||
listeners.splice(idx, 1); | ||
}; | ||
}, | ||
next(value) { | ||
if (done) | ||
throw new Error('Cannot next after done') | ||
defer.resolve({value, done: false}) | ||
for (const {onNext} of listeners) | ||
throw new Error('Cannot next after done'); | ||
defer.resolve({ value, done: false }); | ||
for (const { onNext } of listeners) | ||
try { | ||
onNext(value) | ||
} catch { | ||
onNext(value); | ||
} | ||
catch { } | ||
}, | ||
throw(error) { | ||
if (done) | ||
throw new Error('Cannot throw after done') | ||
done = true | ||
defer.reject(error) | ||
for (const {onError} of listeners) | ||
throw new Error('Cannot throw after done'); | ||
done = true; | ||
defer.reject(error); | ||
for (const { onError } of listeners) | ||
try { | ||
onError?.(error) | ||
} catch { | ||
onError?.(error); | ||
} | ||
catch { } | ||
}, | ||
done() { | ||
if (done) | ||
throw new Error('Cannot done after done') | ||
done = true | ||
defer.resolve({value: undefined, done: true}) | ||
for (const {onDone} of listeners) | ||
throw new Error('Cannot done after done'); | ||
done = true; | ||
defer.resolve({ value: undefined, done: true }); | ||
for (const { onDone } of listeners) | ||
try { | ||
onDone?.() | ||
} catch { | ||
onDone?.(); | ||
} | ||
catch { } | ||
}, | ||
} | ||
}; | ||
} | ||
exports.makeBroadcastStream = makeBroadcastStream; |
{ | ||
"name": "jdefer", | ||
"version": "0.0.12", | ||
"version": "0.0.13", | ||
"main": "index.js", | ||
@@ -5,0 +5,0 @@ "repository": "https://github.com/tranvansang/jdefer", |
Sorry, the diff of this file is not supported yet
5779
177