-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathaemission.js
More file actions
78 lines (66 loc) · 1.89 KB
/
aemission.js
File metadata and controls
78 lines (66 loc) · 1.89 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
"use strict";
const uuid = require("uuid");
const emitter = new (require("events")).EventEmitter();
const deepClone = require("./deepClone.js");
emitter.on("error", function(err) {
console.log("err in mod emitter");
console.error(err);
});
function asyncEmit(emitid, err, data) {
return function() {
emitter.emit(emitid, err, data);
};
}
function cbEmit(emitid) {
return function(err, data) {
return emitter.emit(emitid, err, data);
};
}
function asyncNext(initArgs, arrFn, errCB) {
const aeID = "aeID.next " + uuid.v1();
const l = arrFn.length;
let cbID = cbEmit(aeID);
let dataStore = deepClone(initArgs);
let k = 0;
emitter.on(aeID, function(err, data) {
if (err) {
console.log('aemis.next err', err);
emitter.removeAllListeners(aeID);
errCB(data);
console.log('aemis.next err stopped', aeID);
} else if (++k < l) {
Object.assign(dataStore, data);
setImmediate(function() { arrFn[k](cbID, dataStore)});
} else {
emitter.removeAllListeners(aeID);
console.log('aemis.next Done: ', aeID);
}
});
process.nextTick(function() {
arrFn[k](cbID, dataStore);
});
}
function asyncAll(arrFn, errCB, optD) {
let k = 0;
const aeID = "aeID.all " + uuid.v1();
let cbID = cbEmit(aeID);
const l = arrFn.length;
emitter.on(aeID, function(err, data) {
if (err) {
emitter.removeAllListeners(aeID);
errCB(data);
} else if (++k === l) {
emitter.removeAllListeners(aeID);
console.log('aemis.all Done: ', aeID);
}
});
process.nextTick(function() {
for (let i = 0; i < l; i++) arrFn[i](cbID, optD);
})
}
console.log("aemission Module");
module.exports = {
next: asyncNext,
emit: asyncEmit,
emitter: emitter
};