Skip to content

Commit e189bf1

Browse files
committed
Use metautil instead of metarhia@common
1 parent e55e530 commit e189bf1

File tree

8 files changed

+639
-313
lines changed

8 files changed

+639
-313
lines changed

lib/collector.class.js

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,15 @@
11
'use strict';
22

3-
const common = require('@metarhia/common');
3+
const emptiness = () => {};
4+
const once = (callback) => {
5+
let called = false;
6+
return (...args) => {
7+
if (!called) {
8+
called = true;
9+
callback(...args);
10+
}
11+
};
12+
};
413

514
const UNEXPECTED_KEY = 'Metasync: unexpected key: ';
615
const COLLECT_TIMEOUT = 'Metasync: Collector timed out';
@@ -15,7 +24,7 @@ class Collector {
1524
this.keys = new Set();
1625
this.count = 0;
1726
this.timer = null;
18-
this.onDone = common.emptiness;
27+
this.onDone = emptiness;
1928
this.isDistinct = false;
2029
this.isDone = false;
2130
this.data = {};
@@ -105,8 +114,8 @@ class Collector {
105114
}
106115

107116
then(fulfilled, rejected) {
108-
const fulfill = common.once(fulfilled);
109-
const reject = common.once(rejected);
117+
const fulfill = once(fulfilled);
118+
const reject = once(rejected);
110119
this.onDone = (err, result) => {
111120
if (err) reject(err);
112121
else fulfill(result);

lib/collector.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
'use strict';
22

3-
const common = require('@metarhia/common');
3+
const emptiness = () => {};
44

55
const UNEXPECTED_KEY = 'Metasync: unexpected key: ';
66
const COLLECT_TIMEOUT = 'Metasync: Collector timed out';
@@ -14,7 +14,7 @@ function Collector(expected) {
1414
this.keys = new Set();
1515
this.count = 0;
1616
this.timer = null;
17-
this.onDone = common.emptiness;
17+
this.onDone = emptiness;
1818
this.isDistinct = false;
1919
this.isDone = false;
2020
this.data = {};
@@ -138,10 +138,10 @@ Collector.prototype.cancel = function (err) {
138138
};
139139

140140
Collector.prototype.then = function (fulfill, reject) {
141-
if (!fulfill) fulfill = common.emptiness;
142-
if (!reject) reject = common.emptiness;
141+
if (!fulfill) fulfill = emptiness;
142+
if (!reject) reject = emptiness;
143143
this.onDone = (err, result) => {
144-
this.onDone = common.emptiness;
144+
this.onDone = emptiness;
145145
if (err) reject(err);
146146
else fulfill(result);
147147
};

lib/collector.prototype.js

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
'use strict';
22

3-
const common = require('@metarhia/common');
3+
const inherits = (child, parent) => {
4+
child.prototype = Object.create(parent.prototype);
5+
child.prototype.constructor = child;
6+
};
47

58
function Collector() {}
69

@@ -54,7 +57,7 @@ const DataCollector = function (expected, timeout) {
5457
}
5558
};
5659

57-
common.inherits(DataCollector, Collector);
60+
inherits(DataCollector, Collector);
5861

5962
// Push data to collector
6063
// key - <string>, key in result data
@@ -104,7 +107,7 @@ const KeyCollector = function (keys, timeout) {
104107
}
105108
};
106109

107-
common.inherits(KeyCollector, Collector);
110+
inherits(KeyCollector, Collector);
108111
// Collect keys and data
109112
// key - <string>
110113
// data - <scalar> | <Object> | <Error>, value or error

lib/control.js

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,23 @@
11
'use strict';
22

3-
const common = require('@metarhia/common');
3+
const once = (callback) => {
4+
let called = false;
5+
return (...args) => {
6+
if (!called) {
7+
called = true;
8+
callback(...args);
9+
}
10+
};
11+
};
12+
const last = (arr) => arr[arr.length - 1];
413

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

716
// Executes all asynchronous functions and pass first result to callback
817
// fns - <Function[]>, callback-last / err-first
918
// callback - <Function>, on done, err-first
1019
const firstOf = (fns, callback) => {
11-
const done = common.once(callback);
20+
const done = once(callback);
1221
each(fns, (f, iterCb) =>
1322
f((...args) => {
1423
done(...args);
@@ -30,7 +39,7 @@ const parallel = (fns, context, callback) => {
3039
callback = context;
3140
context = {};
3241
}
33-
const done = common.once(callback);
42+
const done = once(callback);
3443
const isArray = Array.isArray(context);
3544
const len = fns.length;
3645
if (len === 0) return void done(null, context);
@@ -66,7 +75,7 @@ const sequential = (fns, context, callback) => {
6675
callback = context;
6776
context = {};
6877
}
69-
const done = common.once(callback);
78+
const done = once(callback);
7079
const isArray = Array.isArray(context);
7180
const len = fns.length;
7281
if (len === 0) return void done(null, context);
@@ -108,7 +117,7 @@ const runIf = (condition, defaultVal, asyncFn, ...args) => {
108117
if (condition) {
109118
asyncFn(...args);
110119
} else {
111-
const callback = common.last(args);
120+
const callback = last(args);
112121
process.nextTick(callback, null, defaultVal);
113122
}
114123
};

metasync.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
'use strict';
22

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

66
const submodules = [
77
'composition', // Unified abstraction

0 commit comments

Comments
 (0)