Skip to content

Instantly share code, notes, and snippets.

@togakangaroo
Last active December 25, 2015 03:09

Revisions

  1. togakangaroo renamed this gist Oct 9, 2013. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  2. togakangaroo revised this gist Oct 9, 2013. 1 changed file with 1 addition and 7 deletions.
    8 changes: 1 addition & 7 deletions gistfile1.js
    Original file line number Diff line number Diff line change
    @@ -27,14 +27,8 @@ defr.defaultConversion = (fn, ctx) -> (args...) ->
    $.Deferred (d) ->
    fn.apply ctx, (_.union args, [d.resolve, d.reject] )

    w.logResults = (text, continueWith) -> ->
    logResults = (text, continueWith) -> ->
    w.logResults.last = ctx: @, args: (_.toArray arguments), text: text
    (w.logResults.stack = w.logResults.stack || []).push w.logResults.last
    console.log text||"", this, arguments
    continueWith?.apply @, arguments

    w.ok = logResults "ok"
    w.err = logResults "error"

    w.storageInfo = w.storageInfo || w.webkitStorageInfo
    w.requestFileSystem = w.requestFileSystem || w.webkitRequestFileSystem
  3. togakangaroo created this gist Oct 9, 2013.
    40 changes: 40 additions & 0 deletions gistfile1.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,40 @@
    w = window

    w.defr = (fnOrCtx, fnOrUndef) ->
    ctx = if fnOrUndef then fnOrCtx else window
    fn = if fnOrUndef then fnOrUndef else fnOrCtx
    submittedArgs = null
    getDeferredGenerator = (_.find(defr.conversions, (c)->c.canAdapt fn, ctx)?.adapt || defr.defaultConversion)
    getDeferred = _.wrap getDeferredGenerator(fn, ctx), (fn, args...) ->
    submittedArgs = _.toArray args
    fn.apply @, args
    _.compose ((d)-> d.fail (logResults "error", -> console.log this, "fn", fn, "ctx", ctx, "args", submittedArgs)), getDeferred
    defr.conversions = [
    {
    canAdapt: (fn, ctx) -> fn instanceof FileReader
    adapt: (reader) -> (startRead) ->
    $.Deferred (d) ->
    reader.onerror = reader.onabort = d.reject
    reader.onloadend = (e, args...) -> d.resolve.apply @, (_.union [e.target.result], [e], args)
    startRead(reader)
    }
    {
    canAdapt: (fn) -> (_.isArray fn) and _.all fn, (x) -> x.done && x.pipe && x.fail && x.then;
    adapt: (deferreds) -> -> $.when.apply $, deferreds
    }
    ]
    defr.defaultConversion = (fn, ctx) -> (args...) ->
    $.Deferred (d) ->
    fn.apply ctx, (_.union args, [d.resolve, d.reject] )

    w.logResults = (text, continueWith) -> ->
    w.logResults.last = ctx: @, args: (_.toArray arguments), text: text
    (w.logResults.stack = w.logResults.stack || []).push w.logResults.last
    console.log text||"", this, arguments
    continueWith?.apply @, arguments

    w.ok = logResults "ok"
    w.err = logResults "error"

    w.storageInfo = w.storageInfo || w.webkitStorageInfo
    w.requestFileSystem = w.requestFileSystem || w.webkitRequestFileSystem