coffeescript-ui
Version:
Coffeescript User Interface System
115 lines (92 loc) • 2.98 kB
text/coffeescript
###
* coffeescript-ui - Coffeescript User Interface System (CUI)
* Copyright (c) 2013 - 2016 Programmfabrik GmbH
* MIT Licence
* https://github.com/programmfabrik/coffeescript-ui, http://www.coffeescript-ui.org
###
class CUI.Deferred
constructor: ->
# let's not use the coffeescript binding here,
# so that the stack traces are more readable
getUniqueId: ->
__callback: (types, args) ->
# console.error("callback:", @getUniqueId(), types, @__callbacks.length)
idx = 0
while idx < @__callbacks.length
cb = @__callbacks[idx]
if cb.type in types
# console.info @getUniqueId(), idx, cb.type, @__callbacks.length
#
cb.func.apply(@, args)
if cb.type in ["done", "always", "fail"]
# remove from callback queue
continue
idx++
# console.error("callback DONE:", @getUniqueId(), types, @__callbacks.length)
__register: (type, func) ->
# console.error("register:", @getUniqueId(), type, @__runningCallbacks, @__state)
#
CUI.util.assert(CUI.util.isFunction(func), "Deferred."+type+": Callback needs to be Function.", callback: func)
if @__state == "rejected" and type == "done"
# nothing to do
return
if @__state == "resolved" and type == "fail"
# nothing to do
return
if @__state != "pending" and not @__runningCallbacks
switch @__state
when "resolved"
when "rejected"
done: (func) ->
fail: (func) ->
always: (func) ->
progress: (func) ->
__notify: ->
CUI.util.assert(@__state == "pending", "CUI.Deferred.notify", "Cannot notify state #{@__state}.")
__resolve: ->
CUI.util.assert(@__state == "pending", "CUI.Deferred.resolve", "Cannot resolve state #{@__state}.")
# console.error "Deferred.resolve", @getUniqueId(), @__finished_args
# console.error("Deferred.resolve: calling done")
__reject: ->
CUI.util.assert(@__state == "pending", "CUI.Deferred.reject", "Cannot reject state #{@__state}.")
# console.error "Deferred.reject", @getUniqueId(), @__finished_args
state: ->
promise: ->
new CUI.Promise(@)