1 | noflo = require 'noflo'
|
2 |
|
3 | exports.getComponent = ->
|
4 | c = new noflo.Component
|
5 | c.description = 'Evaluates a function each time data hits the "in" port
|
6 | and sends the return value to "out". Within the function "x" will
|
7 | be the variable from the in port. For example, to make a ^2 function
|
8 | input "return x*x;" to the function port.'
|
9 | c.icon = 'code'
|
10 |
|
11 | c.inPorts.add 'in',
|
12 | datatype: 'all'
|
13 | description: 'Packet to be processed'
|
14 | c.inPorts.add 'function',
|
15 | datatype: 'string'
|
16 | description: 'Function to evaluate'
|
17 | control: true
|
18 | c.outPorts.add 'out',
|
19 | datatype: 'all'
|
20 | c.outPorts.add 'function',
|
21 | datatype: 'function'
|
22 | c.outPorts.add 'error',
|
23 | datatype: 'object'
|
24 |
|
25 | prepareFunction = (func, callback) ->
|
26 | if typeof func is 'function'
|
27 | callback null, func
|
28 | return
|
29 | try
|
30 | newFunc = Function 'x', func
|
31 | catch e
|
32 | callback e
|
33 | return
|
34 | callback null, newFunc
|
35 |
|
36 | c.process (input, output) ->
|
37 | return if input.attached('in').length and not input.hasData 'in'
|
38 | if input.hasData 'function', 'in'
|
39 |
|
40 | prepareFunction input.getData('function'), (err, func) ->
|
41 | if err
|
42 | output.done e
|
43 | return
|
44 | data = input.getData 'in'
|
45 | try
|
46 | result = func data
|
47 | catch e
|
48 | output.done e
|
49 | return
|
50 | output.sendDone
|
51 | function: func
|
52 | out: result
|
53 | return
|
54 | return
|
55 | return unless input.hasData 'function'
|
56 | prepareFunction input.getData('function'), (err, func) ->
|
57 | if err
|
58 | output.done e
|
59 | return
|
60 | output.sendDone
|
61 | function: func
|
62 | return
|
63 | return
|