-
Notifications
You must be signed in to change notification settings - Fork 40
/
Copy pathflow.js
319 lines (261 loc) · 7.51 KB
/
flow.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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
// THIS IS A FLOW TEST INTERFACE
// EACH COMPONENT USES THIS SCRIPT FOR TESTING (DON'T RUN THIS SCRIPT DIRECTLY)
// MIT License
// Copyright 2017 Peter Širka <[email protected]>
const Fs = require('fs');
const Util = require('util');
const Exec = require('child_process').exec;
const EventEmitter = require('events');
const ERR = '------- ERROR: ';
process.on('exit', function() {
console.log('');
});
global.assert = require('assert');
var DEBUG = true;
global.FLOW = { component: null, current: null, inmemory: {}, triggers: {}, variables: {} };
function log(name, data, index) {
if (data instanceof FlowData)
data = data.data;
DEBUG && console.log(new Date().format('HH:mm:ss') + (' ' + name + (index === undefined ? '' : ' (' + index + ')')).padRight(22, ' '), '|', data instanceof Buffer ? data : JSON.stringify(data));
}
function Component(options) {
U.extend(this, options);
}
Util.inherits(Component, EventEmitter);
Component.prototype.error = function(e, parent) {
console.log(e);
return this;
};
Component.prototype.set = function(key, value) {
FLOW.set('$' + this.id + key, value);
return this;
};
Component.prototype.get = function(key) {
return FLOW.get('$' + this.id + key);
};
Component.prototype.rem = function(key) {
return FLOW.rem('$' + this.id + key);
};
Component.prototype.click = function() {
return this.emit('click');
};
Component.prototype.signal = function(index, data) {
log('instance.signal', data, index);
EMIT('flow.signal', index, data);
return this;
};
Component.prototype.make = function(data, index) {
return new FlowData(data, false, +index);
};
Component.prototype.send = Component.prototype.send2 = function(index, message) {
if (message === undefined) {
message = index;
index = undefined;
}
log('instance.send', message, index == null ? 'A' : index);
if (!(message instanceof FlowData))
message = new FlowData(message);
var self = this;
message.tracking.push(self);
message.parent = self;
EMIT('flow.send', index, message);
return this;
};
Component.prototype.status = function(text, color) {
this.state = { text: text || '', color: color || 'gray' };
log('instance.status', this.state);
EMIT('flow.status', text, color);
return this;
};
Component.prototype.debug = function(data, style) {
log('instance.debug', data);
EMIT('flow.debug', data, style || 'info');
return this;
};
Component.prototype.throw = function(data) {
log('instance.throw', data);
EMIT('flow.throw', data);
return this;
};
FLOW.register = function(name, options, fn) {
if (typeof(options) === 'function') {
fn = options;
options = EMPTYOBJECT;
}
var id = name.slug().replace(/-/g, '');
console.log('============================================================');
console.log('Total.js Flow Test Interface');
console.log('Component: "{0}"'.format(name));
console.log('Author: {0}'.format(options.author || 'unknown'));
console.log('Version: {0}'.format(options.version || '0.0.0'));
console.log('Date: ' + new Date().format('yyyy-MM-dd HH:mm:ss'));
console.log('------------------------------------------------------------');
console.log('');
FLOW.component = {
id: id,
component: name,
name: options.title || name,
author: options.author || 'Unknown',
color: options.color,
input: options.input == null ? false : options.input,
output: options.output == null ? 0 : options.output,
click: options.click ? true : false,
group: options.group || 'Common',
options: options.options,
uninstall: options.uninstall,
fn: fn,
readme: options.readme || '',
html: options.html || ''
};
var exec = function() {
EMIT('flow.register', FLOW.component);
FLOW.current = new Component(FLOW.component);
FLOW.current.custom = {};
FLOW.current.options = U.extend(U.extend({}, FLOW.component.options || EMPTYOBJECT, true), FLOW.component.options || EMPTYOBJECT, true);
FLOW.current.name = FLOW.component.name;
FLOW.current.fn.call(FLOW.current, FLOW.current, FLOW.component);
setTimeout(function() {
global.FLOWINSTANCE = FLOW.current;
EMIT('flow.ready');
}, 50);
};
if (options.dependencies && options.dependencies.length)
FLOW.npm(options.dependencies, exec);
else
exec();
return FLOW;
};
FLOW.emit = function() {
FLOW.current.emit.apply(FLOW.current, arguments);
return FLOW;
};
FLOW.set = function(key, value) {
FLOW.inmemory[key] = value;
return FLOW;
};
FLOW.get = function(key) {
var val = FLOW.inmemory[key];
return val;
};
FLOW.trigger = function(name, fn) {
FLOW.triggers[name] = fn;
return FLOW;
};
FLOW.rem = function(key) {
delete FLOW.inmemory[key];
return FLOW;
};
FLOW.debug = function(data, style) {
log('flow.debug', data);
return FLOW;
};
global.FLOWINIT = function(obj) {
var id = obj.id;
var install = obj.install;
delete obj.id;
delete obj.install;
FLOW.register(id, obj, install);
};
FLOW.npm = function(dependencies, callback) {
if (!dependencies || !dependencies.length)
return callback();
var path = F.path.root('node_modules/');
F.path.exists(path, function(e) {
!e && Fs.mkdirSync(path);
dependencies.wait(function(item, next) {
var index = item.indexOf('@');
var filename = path + (index === -1 ? item : item.substring(0, index));
F.path.exists(filename, function(e) {
if (e)
return next();
Exec('npm install ' + item, { cwd: path }, function(err) {
err && console.error('NPM INSTALL: ' + item, err);
next();
});
});
}, callback);
});
};
function FlowData(data) {
this.begin = new Date();
this.repository = {};
this.data = data;
this.completed = false;
this.tracking = [];
}
FlowData.prototype.free = function() {
this.tracking = undefined;
this.data = undefined;
this.repository = undefined;
return this;
};
FlowData.prototype.set = function(key, value) {
this.repository[key] = value;
return this;
};
FlowData.prototype.get = function(key) {
return this.repository[key];
};
FlowData.prototype.rem = function(key) {
this.repository[key] = undefined;
return this;
};
global.FLOWSIGNAL = function(data) {
FLOW.current.emit('signal', data, EMPTYOBJECT);
};
global.FLOWCLICK = function() {
FLOW.current.emit('click');
};
global.FLOWEMIT = function(name, data) {
FLOW.current.emit(name, data);
};
global.FLOWOPTIONS = function(options) {
var instance = FLOW.current;
var old_options = instance.options;
instance.options = options;
instance.name = instance.options.name;
instance.reference = instance.options.reference;
instance.emit('options', instance.options, old_options);
log('instance.options', instance.options);
EMIT('flow.options', instance.options, old_options);
};
global.FLOWUNINSTALL = function() {
FLOW.component.uninstall();
};
global.FLOWTRIGGER = function(name, data) {
var fn = FLOW.triggers[name];
if (fn)
fn(function(response) {
log('flow.trigger ({0}) -->'.format(name), JSON.stringify(response));
}, data);
else
console.error(ERR + 'trigger ({0}) --> not found'.format(name));
};
global.FLOWDATA = function(data, input) {
data = new FlowData(data);
if (typeof(input) === 'number' && input > -1) {
data.index = input;
FLOW.current.emit('' + input, data);
}
FLOW.current.emit('data', data);
EMIT('flow.data', data);
};
global.FLOWDEBUG = function(enable) {
DEBUG = enable;
};
global.FLOWCLOSE = function(callback) {
var instance = FLOW.current;
EMIT('flow.close', instance);
instance.$closed = true;
// FLOW.debug('Closing ' + instance.name);
instance.emit('close');
if (instance.close) {
instance.close(function() {
instance.removeAllListeners();
callback && callback();
});
} else {
instance.removeAllListeners();
callback && callback();
}
};