-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathrhei.js
321 lines (269 loc) · 8.78 KB
/
rhei.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
320
321
/**
* rhei.js
* David Brooks
* MIT License
*/
FBP = {}
FBP._components = []
FBP._networks = []
FBP._processes = []
/**
* Set the debug bool
* @param {boolean} bool - To debug or not to debug
*/
FBP.debug = function (bool) {
FBP._debug = bool
}
/**
* Set the maximum ignore count
* @param {int} maxignore - How much ignoring can we stand?
*/
FBP.maxIgnore = function (maxignore) {
FBP._maxIgnore = maxignore
}
/**
* Log (if debugging)
*/
FBP.log = function log (msg) {
if (FBP._debug) {
console.log(msg)
}
}
/**
* Register a component with the runtime
* @param {obj} c - A component object
*
* {
* name: 'component name',
* inPorts: ['in1', ... 'inN'],
* outPorts: ['out', ... 'outN'],
* body: component's function
* }
*/
FBP.registerComponent = function (component) {
FBP._components.push(component)
}
/**
* Takes a component and turns it into a running process
* @param {string} pname - A process name
* @param {string} cname - A component name
*/
FBP._instantiateProcess = function (pname, cname) {
const component = FBP._components.find(c => c.name === cname)
if (!component) throw new Error(`whoops no component: ${cname}`)
// giving inports data
const newInports = component.inPorts.map((p) => {
return {
name: p,
thisProcessName: pname,
data: []
}
})
// giving outports data
const newOutports = component.outPorts.map((p) => {
return {
name: p,
connectedTo: null,
data: []
}
})
const process = {
processName: pname,
name: component.name,
inPorts: newInports,
outPorts: newOutports,
body: component.body
}
FBP.log(`Registering process: ${pname}`)
return process
}
/**
* Create an Information Packet
* @param {anything} data - A data packet's data
*
* Can be strings, numbers, objects anything really.
*/
FBP.createIP = function (data) {
return {
id: Math.random().toString(16).slice(2),
data: data
}
}
/**
* Drop an Information Packet
* @param {IP} ip - An Information Packet
*/
FBP.dropIP = function (ip) {
ip = null
}
/**
* Register a network with the runtime
* TODO: let's make this where there is no need to "kickstart" the network...
* rather the network should just start running, and "kickstart" itself when
* the left-hand side processes have all their inputs. then the network runs..
* .. until it hits a place where not all inputs are filled (from left to right)
* .. and waits there until it has all inputs, then continues.
* ..... in practice this means the networks are always "running", and when
* an input from the user occurs (keypress, mouseclick, REST call, etc) then
* the network runs as you'd expect.
*
* @param {obj} network - A network config object
* {
* name: 'network name',
* processes: [
* {name: 'process name', component: 'component name'}
* ],
* connections: {
* 'process.output connection name': 'process.input connection name',
* ...
* '[last connection out]': '[last connection in]'
* }
* }
*/
FBP.registerNetwork = function (network) {
FBP.log('Registering network: "' + network.name + '"')
network._processes = []
// give the network the processes as defined
for (let key in network.processes) {
const pname = network.processes[key].name
const cname = network.processes[key].component
if (network._processes.find(p => p.processName === pname)) {
throw new Error(`whoops you have two processes with the same name: ${pname} in network: ${network.name}`)
}
const p = FBP._instantiateProcess(pname, cname)
// NOTE: network._processes not network.processes
network._processes.push(p)
}
// wire up the processes for each defined connection
for (let key in network.connections) {
// find the process
const process = network._processes.find(p => p.processName === key.split('.')[0])
if (!process) {
throw new Error(`whoops no process found: ${key.split('.')[0]} in network: ${network.name}`)
}
// find the port
const port = process.outPorts.find(p => p.name === key.split('.')[1])
if (!port) throw new Error(`whoops no port found: ${key}`)
// find the second process
const connectedProcess = network._processes.find(p=> p.processName === network.connections[key].split('.')[0])
const cannotConnectPort = key + '.' + network.connections[key].split('.')[1]
if (!connectedProcess) throw new Error(`whoops cannot connect: ${cannotConnectPort} to missing: ${network.connections[key]}`)
// find the second process's port to connect to
const connectedPort = connectedProcess.inPorts.find(p => p.name === network.connections[key].split('.')[1])
if (!connectedPort) throw new Error(`whoops missing ${network.connections[key]} port for: ${process.name}`)
// connect the two processes
port.connectedTo = connectedPort
}
FBP._networks.push(network)
return network
}
/**
* Activate all processes with full inputs
* @param {network} network - A network to process
*/
FBP.step = function (network) {
// every network process
for (let x = 0; x < network._processes.length; x++) {
const process = network._processes[x]
let ignore = false
// every inport must have data
for (let y = 0; y < process.inPorts.length; y++) {
if (process.inPorts[y].data.length === 0) {
ignore = true;
break
}
}
// "halt" this process if not all inputs have data
if (ignore) {
FBP._ignoreCount++ // count the number of times any process
// has been ignored (for debugging)
continue
}
// otherwise collect the args for the component
const args = []
// NOTE: the collecting of input ports and output ports must be
// done in this order. By convention, input ports are listed
// first in the component function, and output ports are listed last
// collect input ports
for (let i = 0; i < process.inPorts.length; i++) {
const ip = process.inPorts[i].data.shift()
// get the next IP
args.push(ip)
}
// collect output ports
for (let j = 0; j < process.outPorts.length; j++) {
const connection = process.outPorts[j].connectedTo
// pushing the callback for the output
const output = FBP._makeOutput(process, connection)
args.push(output)
}
// run the process's component
process.body.apply(process, args)
}
}
/**
* Create an output callback
* @param {process} currentProcess - A process with an output
* @param {port} connection - An input port on a process
*/
FBP._makeOutput = function (currentProcess, connection) {
return function (output) {
if (!connection) {
FBP.log(`an output from: ${currentProcess.name} is missing a destination`)
} else {
FBP.log(`pushing ip: ${JSON.stringify(output)} from: ${currentProcess.name} onto ${connection.thisProcessName}.${connection.name}`)
// we need to take this output IP and put it into
// the the connected process's input port
connection.data.push(output)
}
}
}
/**
* Loop the stepping function
* @param {network} network - A network to operate
*/
FBP.loop = function (network) {
var id = setTimeout(function () {
if (!network.running || FBP._ignoreCount > FBP._maxIgnore) {
clearTimeout(id)
FBP.log(`Network "${network.name}" stopped. FBP._ignoreCount: ${FBP._ignoreCount}`)
FBP._ignoreCount = 0
return
} else {
FBP.step(network)
FBP.loop(network)
}
}, network.delay || 0) // optional delay
}
/**
* Run a network. expects initial input ports and their initial values
* @param {string} networkName - The name of the network
* @param {obj} init - The initial input values if needed
* @param {boolean} debug - To debug or not to debug
*/
FBP.go = function (networkName, init, debug) {
if (debug) FBP.debug(debug)
FBP.log(`\n\nrunning network: "${networkName}"`)
const network = FBP._networks.find(n => n.name === networkName)
if (!network) throw new Error(`whoops... cannot find network: "${networkName}"`)
FBP._currentNetwork = network
// push values into initial input ports
for (let key in init) {
const processName = key.split('.')[0]
const portName = key.split('.')[1]
if (!processName) throw new Error(`whoops... no process name for key: ${key}`)
const process = network._processes.find(p => p.processName === processName)
// give the ports
if (!process) throw new Error(`whoops.. no process: ${processName}`)
const port = process.inPorts.find(p => p.name === portName)
if (!port) throw new Error(`whoops.. no port: ${portName}`)
const idx = process.inPorts.indexOf(port)
const iip = FBP.createIP(init[key])
process.inPorts[idx].data.push(iip)
}
// now kick start the network
network.running = true
FBP.loop(network)
return network
}
if (typeof module !== 'undefined') module.exports = FBP