noflo
Version:
Flow-Based Programming environment for JavaScript
117 lines (96 loc) • 2.81 kB
text/coffeescript
# NoFlo - Flow-Based Programming for JavaScript
# (c) 2014-2017 Flowhub UG
# NoFlo may be freely distributed under the MIT license
#
# Base port type used for options normalization
{EventEmitter} = require 'events'
validTypes = [
'all'
'string'
'number'
'int'
'object'
'array'
'boolean'
'color'
'date'
'bang'
'function'
'buffer'
'stream'
]
class BasePort extends EventEmitter
constructor: (options) ->
options
= []
= null
= null
handleOptions: (options) ->
options = {} unless options
options.datatype = 'all' unless options.datatype
options.required = false if options.required is undefined
options.datatype = 'int' if options.datatype is 'integer'
if validTypes.indexOf(options.datatype) is -1
throw new Error "Invalid port datatype '#{options.datatype}' specified, valid are #{validTypes.join(', ')}"
if options.type and options.type.indexOf('/') is -1
throw new Error "Invalid port type '#{options.type}' specified. Should be URL or MIME type"
= options
getId: ->
unless and
return 'Port'
"#{@node} #{@name.toUpperCase()}"
getDataType: -> .datatype
getDescription: -> .description
attach: (socket, index = null) ->
if not or index is null
index = .length
[index] = socket
socket, index
if
'attach', socket, index
return
'attach', socket
attachSocket: ->
detach: (socket) ->
index = .indexOf socket
if index is -1
return
[index] = undefined
if
'detach', socket, index
return
'detach', socket
isAddressable: ->
return true if .addressable
false
isBuffered: ->
return true if .buffered
false
isRequired: ->
return true if .required
false
isAttached: (socketId = null) ->
if and socketId isnt null
return true if [socketId]
return false
return true if .length
false
listAttached: ->
attached = []
for socket, idx in
continue unless socket
attached.push idx
attached
isConnected: (socketId = null) ->
if
throw new Error "#{@getId()}: Socket ID required" if socketId is null
throw new Error "#{@getId()}: Socket #{socketId} not available" unless [socketId]
return [socketId].isConnected()
connected = false
.forEach (socket) ->
return unless socket
if socket.isConnected()
connected = true
return connected
canAttach: -> true
module.exports = BasePort