-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathbase.coffee
More file actions
137 lines (115 loc) · 3.84 KB
/
base.coffee
File metadata and controls
137 lines (115 loc) · 3.84 KB
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
platform = require '../helpers/platform'
uuid = require 'uuid'
class BaseRuntime extends platform.EventEmitter
constructor: (definition) ->
super()
@definition = definition
@definition.capabilities = [] unless @definition.capabilities
@graph = null
setMain: (@graph) ->
getType: -> @definition.protocol
getAddress: -> @definition.address
canDo: (capability) ->
@definition.capabilities.indexOf(capability) isnt -1
isConnected: -> false
# Connect to the target runtime environment (iframe URL, WebSocket address)
connect: ->
disconnect: ->
reconnect: ->
do @disconnect
do @connect
# Start a NoFlo Network
start: ->
unless @graph
throw new Error 'No graph defined for execution'
@sendNetwork 'start',
graph: @graph.name or @graph.properties.id
# Stop a NoFlo network
stop: ->
unless @graph
throw new Error 'No graph defined for execution'
@sendNetwork 'stop',
graph: @graph.name or @graph.properties.id
# Set the parent element that some runtime types need
setParentElement: (parent) ->
# Get a DOM element rendered by the runtime for preview purposes
getElement: ->
recvMessage: (message) ->
@emit 'message', message
switch message.protocol
when 'runtime' then @recvRuntime message.command, message.payload
when 'graph' then @recvGraph message.command, message.payload
when 'network' then @recvNetwork message.command, message.payload
when 'component' then @recvComponent message.command, message.payload
when 'trace' then @recvTrace message.command, message.payload
recvRuntime: (command, payload) ->
if command is 'runtime'
for key, val of payload
@definition[key] = val
@emit 'capabilities', payload.capabilities or []
@emit 'runtime',
command: command
payload: payload
recvComponent: (command, payload) ->
switch command
when 'error'
@emit 'network',
command: command
payload: payload
else
@emit 'component',
command: command
payload: payload
recvGraph: (command, payload) ->
@emit 'graph',
command: command
payload: payload
recvNetwork: (command, payload) ->
switch command
when 'started'
@emit 'execution',
running: if payload? and payload.running? then payload.running else true
started: if payload? and payload.started then payload.started else true
when 'stopped'
@emit 'execution',
running: if payload? and payload.running? then payload.running else false
started: if payload? and payload.started then payload.started else false
when 'status'
@emit 'execution',
running: payload.running
started: payload.started
when 'icon'
@emit 'icon', payload
else
@emit 'network',
command: command
payload: payload
recvTrace: (command, payload) ->
@emit 'trace',
command: command
payload: payload
sendRuntime: (command, payload = {}) ->
payload.secret = @definition.secret
@send 'runtime', command, payload
sendGraph: (command, payload = {}) ->
payload.secret = @definition.secret
@send 'graph', command, payload
sendNetwork: (command, payload = {}) ->
payload.secret = @definition.secret
@send 'network', command, payload
sendComponent: (command, payload = {}) ->
payload.secret = @definition.secret
@send 'component', command, payload
sendTrace: (command, payload = {}) ->
payload.secret = @definition.secret
@send 'trace', command, payload
send: (protocol, command, payload) ->
_prepareMessage: (protocol, command, payload) ->
msg =
protocol: protocol
command: command
payload: payload
secret: @definition.secret
requestId: uuid()
return msg
module.exports = BaseRuntime