1 | noflo = require 'noflo'
|
2 |
|
3 | class MakeFunction extends noflo.Component
|
4 | description: 'Evaluates a function each time data hits the "in" port
|
5 | and sends the return value to "out". Within the function "x" will
|
6 | be the variable from the in port. For example, to make a ^2 function
|
7 | input "return x*x;" to the function port.'
|
8 | icon: 'code'
|
9 |
|
10 | constructor: ->
|
11 | @f = null
|
12 |
|
13 |
|
14 |
|
15 | @inPorts = new noflo.InPorts
|
16 | in:
|
17 | datatype: 'all'
|
18 | description: 'Packet to be processed'
|
19 | function:
|
20 | datatype: 'string'
|
21 | description: 'Function to evaluate'
|
22 |
|
23 | @outPorts = new noflo.OutPorts
|
24 | out:
|
25 | datatype: 'all'
|
26 | function:
|
27 | datatype: 'function'
|
28 | error:
|
29 | datatype: 'object'
|
30 |
|
31 |
|
32 | @inPorts.function.on 'data', (data) =>
|
33 | if typeof data is "function"
|
34 | @f = data
|
35 | else
|
36 | try
|
37 | @f = Function("x", data)
|
38 | catch error
|
39 | @error 'Error creating function: ' + data
|
40 | if @f and @outPorts.function.isAttached()
|
41 | @outPorts.function.send @f
|
42 |
|
43 |
|
44 |
|
45 | @inPorts.in.on 'data', (data) =>
|
46 | unless @f
|
47 | @error 'No function defined'
|
48 | return
|
49 | try
|
50 | @outPorts.out.send @f data
|
51 | catch error
|
52 | @error 'Error evaluating function.'
|
53 |
|
54 | error: (msg) ->
|
55 | if @outPorts.error.isAttached()
|
56 | @outPorts.error.send new Error msg
|
57 | @outPorts.error.disconnect()
|
58 | return
|
59 | throw new Error msg
|
60 |
|
61 | exports.getComponent = -> new MakeFunction
|