-
Notifications
You must be signed in to change notification settings - Fork 0
/
octopus.js
170 lines (136 loc) · 3.98 KB
/
octopus.js
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
module.exports = function (debug) {
const Namespace = require('./components/namespace.js');
const rpcEndpoint = require('./components/rpcEndpoint.js');
const debugr = require('debug-pest')(debug);
var appLogger = new debugr('octopus');
/* ----------------------------------------------------------- */
var rpcs = {};
var rpc = function (name, options) {
options = options || {};
if (options.create === true) {
this.name = name;
this.commands = {};
this.logger = appLogger.child(this.name);
this.incoming = new rpcEndpoint(name, 'i', { logger: this.logger });
this.outgoing = new rpcEndpoint(name, 'o', { logger: this.logger });
this.logger.enabled && this.logger.log('Created new Octopus RPC as ', this.name);
return this;
}
if (!rpcs[name]) {
options.create = true;
rpcs[name] = new rpc(name, options);
}
return rpcs[name];
};
rpc.prototype.displayTransports = function () {
// console.log('\n\n\n------ Transports for [%s] are:\n', this.name);
// console.log('incoming:\n');
// this.incoming.displayTransports();
// console.log('\n\noutgoing:\n');
// this.outgoing.displayTransports();
// console.log('\n--------------------------\n\n');
var logString = `
-------------------- TRANSPORTS for [${this.name}] are : ---------------------------
Incoming (provides):
`;
logString += this.incoming.displayString();
logString += `\n\n-------------\nOutgoing (calls):\n\n`;
logString += this.outgoing.displayString();
logString += '\n\n------------------------------------------------------------------------------------\n\n';
console.log(logString);
};
rpc.prototype.over = function (socket, type) {
var tasks = [];
tasks.push(
this.incoming.over(socket, type)
.as(this.name)
.initPromise
);
tasks.push(
this.outgoing.over(socket, type)
.asRemote()
.initPromise
);
return Promise.all(tasks);
};
rpc.prototype.remove = function (socket) {
this.incoming.remove(socket);
this.outgoing.remove(socket);
};
rpc.prototype.pluginTransports = function (tObj) {
this.incoming.pluginTransports(tObj);
this.outgoing.pluginTransports(tObj);
};
rpc.prototype.command = function (name) {
var _self = this;
if (!_self.commands[name]) {
var iC = _self.incoming.command(name);
var oC = _self.outgoing.command(name);
_self.MESSAGETYPES = iC.MESSAGETYPES || oC.MESSAGETYPES;
_self.commands[name] = {
provide: function (fn) {
iC.provide(fn);
return _self.commands[name];
},
unProvide: function (fn) {
iC.unProvide(fn);
return _self.commands[name];
},
onProvide: function (fn) {
iC.onProvide(fn);
return _self.commands[name];
},
call: function (filter, data) {
// _self.displayTransports();
return oC.call(filter, data);
}
};
}
return this.commands[name];
};
rpc.prototype.renameTo = function (newName) {
this.incoming.rename(this.name)
.as(newName);
this.name = newName;
// this.outgoing.label = newName;
return this;
};
/* ---------------- Result parsing & Resolving to Promise resolve/reject -----------*/
rpc.prototype.parseByStatus = function (res) {
var valids = [],
invalids = [],
check;
for (var i = 0; i < res.length; i++) {
check = res[i];
if (check.sent === true && check.status === true)
valids.push(check);
else
invalids.push(check);
}
return {
valids: valids,
invalids: invalids
};
};
rpc.prototype.resolve = function (p) {
if (p.sent === true && p.status === true)
return Promise.resolve(p.response);
else
return Promise.reject(p.response);
};
rpc.prototype.resolveAll = function (p) {
for (var i = 0; i < p.length; i++) {
if (p[i].sent === false || p[i].status === false)
return Promise.reject(p);
}
return Promise.resolve(p);
};
rpc.prototype.resolveAtLeastOne = function (p) {
for (var i = 0; i < p.length; i++) {
if (p[i].sent === true && p[i].status === true)
return Promise.resolve(p);
}
return Promise.reject(p);
};
return rpc;
};