diff --git a/lib/collector.class.js b/lib/collector.class.js index e9ee3d13..d13466f8 100644 --- a/lib/collector.class.js +++ b/lib/collector.class.js @@ -10,9 +10,9 @@ class Collector { // Collector constructor // expected - | , count or keys constructor(expected) { - this.expectKeys = Array.isArray(expected) ? new Set(expected) : null; + this.expectKeys = Array.isArray(expected) ? expected : null; this.expected = this.expectKeys ? expected.length : expected; - this.keys = new Set(); + this.keys = []; this.count = 0; this.timer = null; this.onDone = common.emptiness; @@ -27,17 +27,17 @@ class Collector { this.finalize(err, this.data); return this; } - if (this.expectKeys && !this.expectKeys.has(key)) { + if (this.expectKeys && !this.expectKeys.includes(key)) { if (this.isDistinct) { const err = new Error(UNEXPECTED_KEY + key); this.finalize(err, this.data); return this; } - } else if (!this.keys.has(key)) { + } else if (!this.keys.includes(key)) { this.count++; } this.data[key] = value; - this.keys.add(key); + this.keys.push(key); if (this.expected === this.count) { this.finalize(null, this.data); } diff --git a/lib/collector.functor.js b/lib/collector.functor.js index 99330c83..eb4fbc29 100644 --- a/lib/collector.functor.js +++ b/lib/collector.functor.js @@ -13,7 +13,7 @@ const collect = expected => { if (!(isCount || isKeys)) throw new TypeError(TYPE_ERROR); let keys = null; if (isKeys) { - keys = new Set(expected); + keys = expected; expected = expected.length; } let count = 0; @@ -26,7 +26,7 @@ const collect = expected => { const collector = (key, err, value) => { if (isDone) return collector; if (!isDistinct || !(key in data)) { - if (!isCount && !keys.has(key)) return collector; + if (!isCount && !keys.includes(key)) return collector; count++; } if (err) { diff --git a/lib/collector.js b/lib/collector.js index 81034dcc..2c9e3ee7 100644 --- a/lib/collector.js +++ b/lib/collector.js @@ -9,9 +9,9 @@ const COLLECT_CANCELED = 'Metasync: Collector cancelled'; // Data collector // expected - | , count or keys function Collector(expected) { - this.expectKeys = Array.isArray(expected) ? new Set(expected) : null; + this.expectKeys = Array.isArray(expected) ? expected : null; this.expected = this.expectKeys ? expected.length : expected; - this.keys = new Set(); + this.keys = []; this.count = 0; this.timer = null; this.onDone = common.emptiness; @@ -32,17 +32,17 @@ Collector.prototype.collect = function(key, err, value) { this.finalize(err, this.data); return this; } - if (this.expectKeys && !this.expectKeys.has(key)) { + if (this.expectKeys && !this.expectKeys.includes(key)) { if (this.isDistinct) { const err = new Error(UNEXPECTED_KEY + key); this.finalize(err, this.data); return this; } - } else if (!this.keys.has(key)) { + } else if (!this.keys.includes(key)) { this.count++; } this.data[key] = value; - this.keys.add(key); + this.keys.push(key); if (this.expected === this.count) { this.finalize(null, this.data); } diff --git a/lib/collector.prototype.js b/lib/collector.prototype.js deleted file mode 100644 index 1a4617ff..00000000 --- a/lib/collector.prototype.js +++ /dev/null @@ -1,134 +0,0 @@ -'use strict'; - -const common = require('@metarhia/common'); - -function Collector() {} - -const COLLECT_TIMEOUT = 'Metasync: Collector timed out'; - -// Add event listener -// eventName - -// listener - , handler -// -// Example: -// const collector = new Collector(); -// collector.on('error', (err, key) => { ... }); -// collector.on('timeout', (err, data) => { ... }); -// collector.on('done', (errs, data) => { ... }) -Collector.prototype.on = function(eventName, listener) { - if (eventName in this.events) { - this.events[eventName] = listener; - } -}; - -// Emit Collector events -// eventName - -// err - | -Collector.prototype.emit = function(eventName, err, data) { - const event = this.events[eventName]; - if (event) event(err, data); -}; - -// Create new DataCollector -// Signature: expected[, timeout] -// expected - , count of `collect()` calls expected -// timeout - , collect timeout, optional -// -// Returns: -const DataCollector = function(expected, timeout) { - this.expected = expected; - this.timeout = timeout; - this.count = 0; - this.data = {}; - this.errs = []; - this.events = { - error: null, - timeout: null, - done: null, - }; - if (this.timeout) { - this.timer = setTimeout(() => { - const err = new Error(COLLECT_TIMEOUT); - this.emit('timeout', err, this.data); - }, timeout); - } -}; - -common.inherits(DataCollector, Collector); - -// Push data to collector -// key - , key in result data -// data - | , value or error -DataCollector.prototype.collect = function(key, data) { - this.count++; - if (data instanceof Error) { - this.errs[key] = data; - this.emit('error', data, key); - } else { - this.data[key] = data; - } - if (this.expected === this.count) { - if (this.timer) clearTimeout(this.timer); - const errs = this.errs.length ? this.errs : null; - this.emit('done', errs, this.data); - } -}; - -// Key Collector -// Signature: keys[, timeout] -// keys - -// timeout - , collect timeout, optional -// -// Returns: -// -// Example: new KeyCollector(['config', 'users', 'cities']) -const KeyCollector = function(keys, timeout) { - this.isDone = false; - this.keys = keys; - this.expected = keys.length; - this.count = 0; - this.timeout = timeout; - this.data = {}; - this.errs = []; - this.events = { - error: null, - timeout: null, - done: null, - }; - const collector = this; - if (this.timeout) { - this.timer = setTimeout(() => { - const err = new Error(COLLECT_TIMEOUT); - collector.emit('timeout', err, collector.data); - }, timeout); - } -}; - -common.inherits(KeyCollector, Collector); -// Collect keys and data -// key - -// data - | | , value or error -KeyCollector.prototype.collect = function(key, data) { - if (this.keys.includes(key)) { - this.count++; - if (data instanceof Error) { - this.errs[key] = data; - this.emit('error', data, key); - } else { - this.data[key] = data; - } - if (this.expected === this.count) { - if (this.timer) clearTimeout(this.timer); - const errs = this.errs.length ? this.errs : null; - this.emit('done', errs, this.data); - } - } -}; - -KeyCollector.prototype.stop = function() {}; - -KeyCollector.prototype.pause = function() {}; - -KeyCollector.prototype.resume = function() {}; - -module.exports = { DataCollector, KeyCollector }; diff --git a/tests/load/collect.js b/tests/load/collect.js index 16ff5b99..441bd408 100644 --- a/tests/load/collect.js +++ b/tests/load/collect.js @@ -5,7 +5,7 @@ const COUNT = 1000000; const benchmark = require('./benchmark.js'); const metasync = require('../../lib/collector.js'); -const CollectPrototype = done => { +const Collect = done => { const dc = metasync.collect(6); dc.done(done); let i = 0; @@ -17,4 +17,4 @@ const CollectPrototype = done => { setImmediate(() => dc.pick('6th', 'key' + ++i * 2)); }; -benchmark.do(COUNT, [CollectPrototype]); +benchmark.do(COUNT, [Collect]); diff --git a/tests/load/collect.prototype.js b/tests/load/collect.prototype.js deleted file mode 100644 index a87a3f0a..00000000 --- a/tests/load/collect.prototype.js +++ /dev/null @@ -1,20 +0,0 @@ -'use strict'; - -const COUNT = 1000000; - -const benchmark = require('./benchmark.js'); -const metasync = require('../../lib/collector.prototype.js'); - -const CollectOldPrototype = done => { - const dc = new metasync.DataCollector(6); - dc.on('done', done); - let i = 0; - setImmediate(() => dc.collect('uno', ++i * 2)); - setImmediate(() => dc.collect('due', ++i * 3)); - setImmediate(() => dc.collect('tre', ++i * 5)); - setImmediate(() => dc.collect('4th', 'key' + ++i)); - setImmediate(() => dc.collect('5th', ++i === 5)); - setImmediate(() => dc.collect('6th', 'key' + ++i * 2)); -}; - -benchmark.do(COUNT, [CollectOldPrototype]); diff --git a/tests/load/run.sh b/tests/load/run.sh index 5099a681..1d51f0fb 100755 --- a/tests/load/run.sh +++ b/tests/load/run.sh @@ -15,5 +15,4 @@ echo echo Collector: 1mnl node tests/load/collect.js node tests/load/collect.class.js -node tests/load/collect.prototype.js node tests/load/collect.functor.js