/
plex.js
166 lines (155 loc) · 5.62 KB
/
plex.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
//universal plexer: plex callbacks, promise returns
// plex events by config (membername + eventnames)
var hash = require('object-hash');
var asynk = require('async');
var arrayRotate = function(arr){ //2D flip
return arr[0].map((_, colIndex) => arr.map(row => row[colIndex]));
}
var arrLikeToArr = function(a){ return Array.prototype.slice.call(a) };
var GroupResponder = function(setSize){
this.responses = [];
this.isDone = function(){
return this.responses.filter(function(a){return a}).length === setSize;
}
}
GroupResponder.prototype.addResponse = function(res, index){
if(index) this.responses[index] = res;
if(this.isDone()){
if(this.callback) return callback(this.responses);
}
}
var Plex = function(opts){
this.options = opts || {};
this.plexed = [];
this.index = {};
this.emitter = this.options.emitter;
//this.highwater = 0; //only needed going bottom up... memory risk for index
return this;
}
Plex.prototype.plex = function(obj){
var emitter
var ob = this;
//process.exit();
this.plexed.push(obj);
if(this.options.bubbleup){
this.options.bubbleup.forEach(function(arr, i){
var member = arr[0];
var events = arr[1];
if(!obj[member]) throw new Error(
'could not find emitter:'+member
);
events.forEach(function(eventName){
if(!ob.index[eventName]) ob.index[eventName] = {};
obj[member].on(eventName, function(){
var args = Array.prototype.slice.call(arguments);
var argHash = hash(args);
if(!ob.index[eventName][argHash]){
args.occuranceCount = 1;
ob.index[eventName][argHash] = args;
}else{
ob.index[eventName][argHash].occuranceCount++;
}
if(ob.index[eventName][argHash].occuranceCount === ob.plexed.length){
delete ob.index[eventName][argHash];
try{
if(ob.emitter){
ob.emitter.emit.apply(
ob.emitter,
[eventName].concat(args)
);
}
}catch(ex){ console.log('ERROR', ex) }
}
});
});
});
}
}
PlexProxyWrapper = function(obj){
this.plexer = obj;
}
PlexProxyWrapper.wrap = function(obj) {
return new Proxy(
Object.getPrototypeOf(obj),
new PlexProxyWrapper(obj)
);
};
var reserved = ['plex', 'plexed', 'options', 'emitter', 'index'];
var isReserved = function(s){ return reserved.indexOf(s) !== -1 };
PlexProxyWrapper.prototype = {
get: function(rcvr, n){
var name = n;
if(name==='plex'){
var plexer = this.plexer;
return function(obj){
return plexer.plex(obj);
};
}
//if all submembers are fns, go to fn mode
var isFunctionReturn = this.plexer.plexed.filter(function(obj){
return typeof obj[name] === 'function';
}).length === this.plexer.plexed.length;
if(isReserved(name)) return this.plexer[name];
var returns = [];
var plexed = this.plexer.plexed;
var fnReturn;
if(isFunctionReturn){
fnReturn = function(){
var calledargs = arrLikeToArr(arguments);
var lastArgisFn = typeof calledargs[calledargs.length-1] === 'function';
var callback;
var callbackReturns = [];
if(lastArgisFn) callback = calledargs.pop();
var promisesFound = 0;
plexed.forEach(function(ob, index){
returns[index] = ob[name].apply(
ob,
(callback?calledargs.concat([function(){
callbackReturns[index] = arrLikeToArr(arguments);
if(
callbackReturns.filter(function(a){return a}).length ===
plexed.length
){
callback(null, callbackReturns);
}
}]):calledargs)
);
if(returns[index] && returns[index].then && returns[index].catch){
promisesFound++;
}
});
if(promisesFound === plexed.length){
return Promise.all(returns);
}else{
return returns;
}
}
}else{
plexed.forEach(function(ob, index){
returns[index] = ob[name];
});
}
return fnReturn || returns;
},
set: function(rcvr, name, val){
if(isReserved(name)) throw new Error("Can't set reserved field: "+name);
//todo: iterate
return this.plexer.plexed[0][name] = val;
/*var ob = this;
var errors = [];
this.plexer.plexed.forEach(function(obj){
if(typeof obj[name] === 'function'){
//todo: dynamic function set handler
errors.push(new Error(name+ 'is a function, cannot be set'));
}else{
obj[name] = value;
}
});
return errors.length?false:true;
*/
}
};
module.exports = function(opts){
var ob = new Plex(opts);
return PlexProxyWrapper.wrap(ob);
};