-
Notifications
You must be signed in to change notification settings - Fork 0
/
shell.js
176 lines (150 loc) · 5.02 KB
/
shell.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
171
172
173
174
175
176
var SimpleEvents = require('more-events').Emitter;//require('./simple_events');
function Shell(){
var shells = Array.prototype.slice.call(arguments),
cb = shells.shift(),
alive = true;
if(cb === undefined)
cb = function * (obj){ return obj; };
var events = new SimpleEvents();
function callshell(pre){
return new Promise(function(resolve, reject){
var gen, obj = {};
function fn(next){
if(next.done){
if(next.value.then){
return next.value.then(function(value){
resolve(value);
});
}else{
return resolve(next.value);
}
}
if(next.value.then){
next.value.then(function(value){
fn(gen.next(value));
}, function(e){
fn(gen.throw(e));
});
}else{
gen.next(next.value);
}
}
prepareInput();
function prepareInput(){
var result;
if(typeof pre === 'function'){
//Use co-routines, or async-await for this!
//Get a model with async control.
pre().then(start, function(e){
return Promise.reject(e);
});
}else if(typeof pre.then === 'function'){
//The input is a thenable.
//Propably an async function.
pre.then(start, function(e){
return Promise.reject(e);
});
}else{
start(pre);
}
}
function start(obj2){
var type = Object.prototype.toString.call(obj2), list;
if(type === '[object Object]'){
for(var n in obj2){
obj[n] = obj2[n];
}
}else if(type === '[object Array]'){
for(var i=0; i<obj2.length; i++){
obj[i] = obj2[i];
}
}else{
obj = obj2;
}
if(shells && shells.length){
//This is called composition.
//All members bubble destruction.
list = new Array(shells.length);
for(var i=0; i<shells.length; i++){
if(typeof shells[i].exec !== 'function'){
throw new Error('exec method was not found on '+shells[i]);
}
list[i] = shells[i].exec(obj);
}
Promise.all(list).then(function(list){
if(obj !== undefined){
list.unshift(obj);
}
//Add the object to the arguments.
//list.unshift(obj);
run(list);
});
}else{
list = [];
if(obj !== undefined){
list = [obj];
}
run(list);
}
}
function run(args){
gen = cb.apply(null, args);
if(typeof gen.next === 'function'){
fn(gen.next());
}else if(typeof gen.then === 'function'){
//Skip recursive fn call.
//cb is probably an async function.
gen.then(function(value){
resolve(value);
}, function(e){
resolve(Promise.reject(e));
}).catch(function(e){
resolve(Promise.reject(e));
});
}
}
});
}
function exec(obj){
if(!alive)
return Promise.resolve(undefined);
return callshell.call(this, obj).then(function(value){
events.emit('exec', obj, value);
return value;
});
}
function include(){
for(var i=0; i<arguments.length; i++){
shells.push(arguments[i]);
}
return this;
}
function erase(){
shells = [];
alive = false;
return this;
}
function on(event, cb){
events.on(event, cb);
return this;
}
function destroy(){
if(!alive)
return;
alive = false;
for(var i=0; i<shells.length; i++)
shells[i].destroy();
events.emit('destroy');
events = null;
shells = null;
cb = null;
}
return Object.create({
exec: exec,
include: include,
erase: erase,
on: on,
destroy: destroy
});
}
module.exports = Shell;