-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmiddlewareManager.js
More file actions
55 lines (48 loc) · 1.37 KB
/
middlewareManager.js
File metadata and controls
55 lines (48 loc) · 1.37 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
/*
* A middleware Manager based on socket.
* */
"use strict";
module.exports = class ZmqMiddlewareManager {
constructor(socket) {
this.socket = socket;
this.inboundMiddleware = []; //[1]
this.outboundMiddleware = [];
socket.on('message', message => { //[2]
this.executeMiddleware(this.inboundMiddleware, {
data: message
});
});
}
send(data) {
const message = {
data: data
};
this.executeMiddleware(this.outboundMiddleware, message,
() => {
this.socket.send(message.data);
}
);
}
use(middleware) {
if (middleware.inbound) {
this.inboundMiddleware.push(middleware.inbound);
}
if (middleware.outbound) {
this.outboundMiddleware.unshift(middleware.outbound);
}
}
executeMiddleware(middleware, arg, finish) {
function iterator(index) {
if (index === middleware.length) {
return finish && finish();
}
middleware[index].call(this, arg, err => {
if (err) {
return console.log('There was an error: ' + err.message);
}
iterator.call(this, ++index);
});
}
iterator.call(this, 0);
}
};