Created
April 28, 2015 19:06
-
-
Save mrzafod/6299da46b5dfd4ddff61 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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