'use strict'; const emptiness = () => {}; const UNEXPECTED_KEY = 'Metasync: unexpected key: '; const COLLECT_TIMEOUT = 'Metasync: Collector timed out'; const COLLECT_CANCELED = 'Metasync: Collector cancelled'; // Data collector // expected - | , count or keys function Collector(expected) { const isKeys = Array.isArray(expected); this.expectKeys = isKeys ? new Set(expected) : null; this.expected = isKeys ? expected.length : expected; this.keys = new Set(); this.count = 0; this.timer = null; this.onDone = emptiness; this.isDistinct = false; this.isDone = false; this.data = {}; } // Pick or fail key // key - // err - // value - // // Returns: Collector.prototype.collect = function (key, err, value) { if (this.isDone) return this; if (err) { this.finalize(err, this.data); return this; } const unexpected = this.expectKeys && !this.expectKeys.has(key); if (unexpected) { if (this.isDistinct) { const unexpectedErr = new Error(UNEXPECTED_KEY + key); this.finalize(unexpectedErr, this.data); return this; } } else if (!this.keys.has(key)) { this.count++; } this.data[key] = value; this.keys.add(key); if (this.expected === this.count) { this.finalize(null, this.data); } return this; }; // Pick key // key - // value - // // Returns: Collector.prototype.pick = function (key, value) { this.collect(key, null, value); return this; }; // Fail key // key - // err - // // Returns: Collector.prototype.fail = function (key, err) { this.collect(key, err); return this; }; // Take method result // key - // fn - // args - , rest arguments, to be passed in fn // // Returns: Collector.prototype.take = function (key, fn, ...args) { fn(...args, (err, data) => { this.collect(key, err, data); }); return this; }; // Set timeout // msec - // // Returns: Collector.prototype.timeout = function (msec) { if (this.timer) { clearTimeout(this.timer); this.timer = null; } if (msec > 0) { this.timer = setTimeout(() => { const err = new Error(COLLECT_TIMEOUT); this.finalize(err, this.data); }, msec); } return this; }; // Set on done listener // callback - // err - // data - // // Returns: Collector.prototype.done = function (callback) { this.onDone = callback; return this; }; Collector.prototype.finalize = function (key, err, data) { if (this.isDone) return this; if (this.timer) { clearTimeout(this.timer); this.timer = null; } this.isDone = true; this.onDone(key, err, data); return this; }; // Deny or allow unlisted keys // value - // // Returns: Collector.prototype.distinct = function (value = true) { this.isDistinct = value; return this; }; Collector.prototype.cancel = function (err) { const cancelErr = err || new Error(COLLECT_CANCELED); this.finalize(cancelErr, this.data); return this; }; Collector.prototype.then = function (fulfill, reject) { if (!fulfill) fulfill = emptiness; if (!reject) reject = emptiness; this.onDone = (err, result) => { this.onDone = emptiness; if (err) reject(err); else fulfill(result); }; return this; }; // Create Collector instance // expected - | // // Returns: const collect = (expected) => new Collector(expected); module.exports = { collect, Collector };