Skip to content

Instantly share code, notes, and snippets.

@mrzafod
Created April 28, 2015 19:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mrzafod/6299da46b5dfd4ddff61 to your computer and use it in GitHub Desktop.
Save mrzafod/6299da46b5dfd4ddff61 to your computer and use it in GitHub Desktop.
class ManagedSub
constructor: (name, options = {}) ->
self = @
self.sub = null
enter = ->
options.enter?()
# bind self.enter - it runs once per call
self.enter = _.once enter
# defite self.exit
self.exit = ->
options.exit?()
# define main handle function, it runs once for delay ms
self.handle = _.debounce false, (options.delay or 1000), (args...) ->
# temporary sub to prevent documents mess. Subscribe Man!
tmpSub = Meteor.subscribe name, args..., ->
# sub done, redefine self.enter for the next time
self.enter = _.once enter
# call exit
self.exit()
# swap subs
self.sub?.stop?()
self.sub = tmpSub
run: (args...) ->
# call enter
@enter()
# run handle (no matter delay, just bind last args)
@handle args...
ready: ->
@sub?.ready?()
stop: ->
@sub?.stop?()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment