Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 13 additions & 4 deletions lib/collector.class.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,15 @@
'use strict';

const common = require('@metarhia/common');
const emptiness = () => {};
const once = (callback) => {
let called = false;
return (...args) => {
if (!called) {
called = true;
callback(...args);
}
};
};

const UNEXPECTED_KEY = 'Metasync: unexpected key: ';
const COLLECT_TIMEOUT = 'Metasync: Collector timed out';
Expand All @@ -15,7 +24,7 @@ class Collector {
this.keys = new Set();
this.count = 0;
this.timer = null;
this.onDone = common.emptiness;
this.onDone = emptiness;
this.isDistinct = false;
this.isDone = false;
this.data = {};
Expand Down Expand Up @@ -105,8 +114,8 @@ class Collector {
}

then(fulfilled, rejected) {
const fulfill = common.once(fulfilled);
const reject = common.once(rejected);
const fulfill = once(fulfilled);
const reject = once(rejected);
this.onDone = (err, result) => {
if (err) reject(err);
else fulfill(result);
Expand Down
10 changes: 5 additions & 5 deletions lib/collector.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict';

const common = require('@metarhia/common');
const emptiness = () => {};

const UNEXPECTED_KEY = 'Metasync: unexpected key: ';
const COLLECT_TIMEOUT = 'Metasync: Collector timed out';
Expand All @@ -14,7 +14,7 @@ function Collector(expected) {
this.keys = new Set();
this.count = 0;
this.timer = null;
this.onDone = common.emptiness;
this.onDone = emptiness;
this.isDistinct = false;
this.isDone = false;
this.data = {};
Expand Down Expand Up @@ -138,10 +138,10 @@ Collector.prototype.cancel = function (err) {
};

Collector.prototype.then = function (fulfill, reject) {
if (!fulfill) fulfill = common.emptiness;
if (!reject) reject = common.emptiness;
if (!fulfill) fulfill = emptiness;
if (!reject) reject = emptiness;
this.onDone = (err, result) => {
this.onDone = common.emptiness;
this.onDone = emptiness;
if (err) reject(err);
else fulfill(result);
};
Expand Down
9 changes: 6 additions & 3 deletions lib/collector.prototype.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
'use strict';

const common = require('@metarhia/common');
const inherits = (child, parent) => {
child.prototype = Object.create(parent.prototype);
child.prototype.constructor = child;
};

function Collector() {}

Expand Down Expand Up @@ -54,7 +57,7 @@ const DataCollector = function (expected, timeout) {
}
};

common.inherits(DataCollector, Collector);
inherits(DataCollector, Collector);

// Push data to collector
// key - <string>, key in result data
Expand Down Expand Up @@ -104,7 +107,7 @@ const KeyCollector = function (keys, timeout) {
}
};

common.inherits(KeyCollector, Collector);
inherits(KeyCollector, Collector);
// Collect keys and data
// key - <string>
// data - <scalar> | <Object> | <Error>, value or error
Expand Down
19 changes: 14 additions & 5 deletions lib/control.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,23 @@
'use strict';

const common = require('@metarhia/common');
const once = (callback) => {
let called = false;
return (...args) => {
if (!called) {
called = true;
callback(...args);
}
};
};
const last = (arr) => arr[arr.length - 1];

const { each } = require('./array');

// Executes all asynchronous functions and pass first result to callback
// fns - <Function[]>, callback-last / err-first
// callback - <Function>, on done, err-first
const firstOf = (fns, callback) => {
const done = common.once(callback);
const done = once(callback);
each(fns, (f, iterCb) =>
f((...args) => {
done(...args);
Expand All @@ -30,7 +39,7 @@ const parallel = (fns, context, callback) => {
callback = context;
context = {};
}
const done = common.once(callback);
const done = once(callback);
const isArray = Array.isArray(context);
const len = fns.length;
if (len === 0) return void done(null, context);
Expand Down Expand Up @@ -66,7 +75,7 @@ const sequential = (fns, context, callback) => {
callback = context;
context = {};
}
const done = common.once(callback);
const done = once(callback);
const isArray = Array.isArray(context);
const len = fns.length;
if (len === 0) return void done(null, context);
Expand Down Expand Up @@ -108,7 +117,7 @@ const runIf = (condition, defaultVal, asyncFn, ...args) => {
if (condition) {
asyncFn(...args);
} else {
const callback = common.last(args);
const callback = last(args);
process.nextTick(callback, null, defaultVal);
}
};
Expand Down
4 changes: 2 additions & 2 deletions metasync.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
'use strict';

const common = require('@metarhia/common');
const nodeVerion = common.between(process.version, 'v', '.');
const { between } = require('metautil');
const nodeVerion = between(process.version, 'v', '.');

const submodules = [
'composition', // Unified abstraction
Expand Down
Loading
Loading