Skip to content

Commit

Permalink
Remove once and emtiness
Browse files Browse the repository at this point in the history
PR-URL: #33
  • Loading branch information
tshemsedinov committed Jan 11, 2019
1 parent 75dc50c commit 30fb025
Showing 1 changed file with 3 additions and 21 deletions.
24 changes: 3 additions & 21 deletions do.js
Original file line number Diff line number Diff line change
@@ -1,21 +1,5 @@
'use strict';

const emptiness = () => {};

// Wrap function: call once, not null
// fn <Function> (optional)
// Returns: <Function> wrapped callback
const once = fn => {
if (!fn) return emptiness;
let finished = false;
const wrap = (...args) => {
if (finished) return;
finished = true;
fn(...args);
};
return wrap;
};

function Do() {}

const chain = function(fn, ...args) {
Expand Down Expand Up @@ -125,7 +109,7 @@ Collector.prototype.finalize = function(key, err, data) {
this.timer = null;
}
this.isDone = true;
this.onDone(key, err, data);
if (this.onDone) this.onDone(key, err, data);
}
return this;
};
Expand All @@ -141,9 +125,7 @@ Collector.prototype.cancel = function(err) {
return this;
};

Collector.prototype.then = function(fulfilled, rejected) {
const fulfill = once(fulfilled);
const reject = once(rejected);
Collector.prototype.then = function(fulfill, reject) {
this.onDone = (err, result) => {
if (err) reject(err);
else fulfill(result);
Expand All @@ -162,7 +144,7 @@ const collect = expected => {
keys: new Set(),
count: 0,
timer: null,
onDone: emptiness,
onDone: null,
isDistinct: false,
isDone: false,
data: {}
Expand Down

0 comments on commit 30fb025

Please sign in to comment.