noflo
Version:
Flow-Based Programming environment for JavaScript
104 lines (88 loc) • 2.93 kB
text/coffeescript
# NoFlo - Flow-Based Programming for JavaScript
# (c) 2014-2017 Flowhub UG
# NoFlo may be freely distributed under the MIT license
#
# Output Port (outport) implementation for NoFlo components
BasePort = require './BasePort'
IP = require './IP'
class OutPort extends BasePort
constructor: (options) ->
= {}
super options
attach: (socket, index = null) ->
super socket, index
if and [index]?
[index], index
connect: (socketId = null) ->
sockets = socketId
sockets
for socket in sockets
continue unless socket
socket.connect()
beginGroup: (group, socketId = null) ->
sockets = socketId
sockets
sockets.forEach (socket) ->
return unless socket
return socket.beginGroup group
send: (data, socketId = null) ->
sockets = socketId
sockets
if and data isnt [socketId]
[socketId] = data
sockets.forEach (socket) ->
return unless socket
return socket.send data
endGroup: (socketId = null) ->
sockets = socketId
sockets
for socket in sockets
continue unless socket
socket.endGroup()
disconnect: (socketId = null) ->
sockets = socketId
sockets
for socket in sockets
continue unless socket
socket.disconnect()
sendIP: (type, data, options, socketId, autoConnect = true) ->
if IP.isIP type
ip = type
socketId = ip.index
else
ip = new IP type, data, options
sockets = socketId
sockets
if and data isnt [socketId]?.data
[socketId] = ip
pristine = true
for socket in sockets
continue unless socket
if pristine
socket.post ip, autoConnect
pristine = false
else
ip = ip.clone() if ip.clonable
socket.post ip, autoConnect
@
openBracket: (data = null, options = {}, socketId = null) ->
'openBracket', data, options, socketId
data: (data, options = {}, socketId = null) ->
'data', data, options, socketId
closeBracket: (data = null, options = {}, socketId = null) ->
'closeBracket', data, options, socketId
checkRequired: (sockets) ->
if sockets.length is 0 and
throw new Error "#{@getId()}: No connections available"
getSockets: (socketId) ->
# Addressable sockets affect only one connection at time
if
throw new Error "#{@getId()} Socket ID required" if socketId is null
return [] unless [socketId]
return [[socketId]]
# Regular sockets affect all outbound connections
isCaching: ->
return true if .caching
false
module.exports = OutPort