-
Notifications
You must be signed in to change notification settings - Fork 2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Consider building a singleton for connection management #16
Milestone
Comments
@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:
|
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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
The text was updated successfully, but these errors were encountered: