Last active
December 14, 2015 14:08
-
-
Save edom18/5098375 to your computer and use it in GitHub Desktop.
Simple Deferred with CoffeeScript.
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 Disposal | |
constructor: -> | |
dispose: -> | |
@el?.parentNode?.removeChild @el | |
@el = null | |
@off?() | |
@disposeInternal.apply @, arguments | |
disposeInternal: -> | |
# ---------------------------------------------- | |
class Deferred extends Disposal | |
constructor: -> | |
resolve: (val) -> | |
done?.call @, val for done in @_doneHandlers if @_doneHandlers | |
return @ | |
done: (func) -> | |
(@_doneHandlers or (@_doneHandlers = [])).push func if func | |
return @ | |
fail: (func) -> | |
(@_failHandlers or (@_failHandlers = [])).push func if func | |
return @ | |
disposeInternal: -> | |
@_doneHandlers = null | |
class When extends Disposal | |
constructor: (defList...) -> | |
@def = new Deferred | |
len = defList.length | |
_done = => | |
--len or @def.resolve() | |
def.done _done for def in defList | |
done: (func) -> | |
@def.done func | |
return @ | |
fail: (func) -> | |
@def.fail func | |
return @ | |
disposeInternal: -> | |
@def.dispose() | |
@def = null |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment