Browse Source

Use @joepie91/consumable

master
Sven Slootweg 3 months ago
parent
commit
feb7f4b8e2
  1. 1
      package.json
  2. 12
      promise-listener.js

1
package.json

@ -6,6 +6,7 @@
"author": "Sven Slootweg <admin@cryto.net>",
"license": "WTFPL OR CC0-1.0",
"dependencies": {
"@joepie91/consumable": "^1.0.1",
"@promistream/is-aborted": "^0.1.1",
"@promistream/is-end-of-stream": "^0.1.1",
"@promistream/pipe": "^0.1.4",

12
promise-listener.js

@ -1,19 +1,19 @@
"use strict";
const consumable = require("@joepie91/consumable");
module.exports = function createPromiseListener() {
let listeners = [];
let listeners = consumable([]);
function consumeListeners() {
/* To ensure that a resolve/reject is atomic, and can never apply to the same listener queue twice. */
let consumedListeners = listeners;
listeners = [];
return consumedListeners;
return listeners.replace([]);
}
return {
listen: function () {
return new Promise((resolve, reject) => {
listeners.push({ resolve, reject });
listeners.peek().push({ resolve, reject });
});
},
resolve: function (value) {
@ -27,4 +27,4 @@ module.exports = function createPromiseListener() {
});
},
}
};
};
Loading…
Cancel
Save