Open steel opened 7 years ago
@steel How do you propose the API would work?
Currently we have (currently - pseudo ish code):
let myConsumer = new FirehoseConnection(channel, options)
myConsumer.on("data", doStuff) //similar for old callback style
myConsumer.on("failed", showError) //similar for old callback style
// ... later
myConsumer.disconnect()
How are you thinking of managing:
mostRecentMessage
and re-send that to the new client(s)multiplex
(I don't want different consumers, I'd like this to be an option passed in when you connect) and longPollFirst
(I hope to change the default to websocket))
multiplex
maybe some kind of Map
with keys "channel:multiplex" and "channel:singleplex" - or we just follow the first requestors option?longPollFirst
maybe we just follow the first requestors option?We should disable the option of even not using multi-plexed connections. WebSocket connections are always multi-plexed.
I imagining this simple reference tracker and garbage collecting system
class Channel
constructor: (name, @wsConnection) ->
# event handlers string: [handler functions]
@handlers = {}
@name = name
# connect to channel
@wsConnection.send "subscribe", name
@wsConnection.on "message", @handleMessage
remove: ->
@wsConnection.send "unsubscribe from channel"
on: (event, handler) ->
@handlers[event].push handler
@trigger "addListener"
off: (event, handler) ->
# remove from table
@handlers[event][handler] = null
@trigger "removeListener"
handleMessage: (channel, msg) ->
if channel == @name
# iterate through handlers and call them with the message
@handlers["message"].forEach (fn) -> fn(msg)
class Firehose
constructor: ->
# channels is a map of string to Channel objects
@channels = {}
bind: (channel) ->
# look up if channel is exists or not
channel = new Channel(name)
channel.on "removeListener", @pruneChannels
# if channel doesn't exist, create one and connect it
# then put it in the lookup table
pruneChannels: ->
# iterate through channels and remove channels that have no listeners
# In a view
# get access to this channel
channel = Firehose.subscribe "my/channel"
channel.on "message", handleMessage
# when removing the view
channel.off "message", handleMessage