noflo
Version:
Flow-Based Programming environment for JavaScript
157 lines (127 loc) • 4.27 kB
text/coffeescript
# NoFlo - Flow-Based Programming for JavaScript
# (c) 2014-2016 TheGrid (Rituwall Inc.)
# NoFlo may be freely distributed under the MIT license
#
# Input Port (inport) implementation for NoFlo components
BasePort = require './BasePort'
IP = require './IP'
class InPort extends BasePort
constructor: (options, process) ->
= null
if not process and typeof options is 'function'
process = options
options = {}
options ?= {}
options.buffered ?= false
options.control ?= false
options.triggering ?= true
if not process and options and options.process
process = options.process
delete options.process
if process
unless typeof process is 'function'
throw new Error 'process must be a function'
= process
if options.handle
unless typeof options.handle is 'function'
throw new Error 'handle must be a function'
= options.handle
delete options.handle
super options
attachSocket: (socket, localId = null) ->
# Assign a delegate for retrieving data should this inPort
# have a default value.
if
if
socket.setDataDelegate => new IP 'data', .default
else
socket.setDataDelegate => .default
socket.on 'connect', =>
'connect', socket, localId
socket.on 'begingroup', (group) =>
'begingroup', group, localId
socket.on 'data', (data) =>
data
'data', data, localId
socket.on 'endgroup', (group) =>
'endgroup', group, localId
socket.on 'disconnect', =>
'disconnect', socket, localId
socket.on 'ip', (ip) =>
ip, localId
handleIP: (ip, id) ->
return if
return if .control and ip.type isnt 'data'
ip.owner =
ip.index = id
if ip.scope
[ip.scope] = [] unless ip.scope of
buf = [ip.scope]
else
buf =
buf.push ip
buf.shift() if .control and buf.length > 1
if
ip,
'ip', ip, id
handleSocketEvent: (event, payload, id) ->
# Handle buffering the old way
if
.push
event: event
payload: payload
id: id
# Notify receiver
if
event, id, if
event, id
else
event, if
event
return
if
if
event, payload, id,
else
event, payload,
# Emit port event
return event, payload, id if
event, payload
hasDefault: ->
return .default isnt undefined
prepareBuffer: ->
= []
= {}
validateData: (data) ->
return unless .values
if .values.indexOf(data) is -1
throw new Error "Invalid data='#{data}' received, not in [#{@options.values}]"
# Returns the next packet in the (legacy) buffer
receive: ->
unless
throw new Error 'Receive is only possible on buffered ports'
.shift()
# Returns the number of data packets in a (legacy) buffered inport
contains: ->
unless
throw new Error 'Contains query is only possible on buffered ports'
.filter((packet) -> return true if packet.event is 'data').length
# Fetches a packet from the port
get: (scope) ->
if scope
return undefined unless scope of
buf = [scope]
else
buf =
return if .control then buf[buf.length - 1] else buf.shift()
# Returns the number of data packets in an inport
length: (scope) ->
if scope
return 0 unless scope of
return [scope].length
return .length
# Tells if buffer has packets or not
ready: (scope) ->
return > 0
module.exports = InPort