[483] | 1 | define([ |
---|
| 2 | "../_base/lang", |
---|
| 3 | "./Promise", |
---|
| 4 | "../Evented" |
---|
| 5 | ], function(lang, Promise, Evented){ |
---|
| 6 | "use strict"; |
---|
| 7 | |
---|
| 8 | // module: |
---|
| 9 | // dojo/promise/tracer |
---|
| 10 | |
---|
| 11 | /*===== |
---|
| 12 | return { |
---|
| 13 | // summary: |
---|
| 14 | // Trace promise fulfillment. |
---|
| 15 | // description: |
---|
| 16 | // Trace promise fulfillment. Calling `.trace()` or `.traceError()` on a |
---|
| 17 | // promise enables tracing. Will emit `resolved`, `rejected` or `progress` |
---|
| 18 | // events. |
---|
| 19 | |
---|
| 20 | on: function(type, listener){ |
---|
| 21 | // summary: |
---|
| 22 | // Subscribe to traces. |
---|
| 23 | // description: |
---|
| 24 | // See `dojo/Evented#on()`. |
---|
| 25 | // type: String |
---|
| 26 | // `resolved`, `rejected`, or `progress` |
---|
| 27 | // listener: Function |
---|
| 28 | // The listener is passed the traced value and any arguments |
---|
| 29 | // that were used with the `.trace()` call. |
---|
| 30 | } |
---|
| 31 | }; |
---|
| 32 | =====*/ |
---|
| 33 | |
---|
| 34 | var evented = new Evented; |
---|
| 35 | var emit = evented.emit; |
---|
| 36 | evented.emit = null; |
---|
| 37 | // Emit events asynchronously since they should not change the promise state. |
---|
| 38 | function emitAsync(args){ |
---|
| 39 | setTimeout(function(){ |
---|
| 40 | emit.apply(evented, args); |
---|
| 41 | }, 0); |
---|
| 42 | } |
---|
| 43 | |
---|
| 44 | Promise.prototype.trace = function(){ |
---|
| 45 | // summary: |
---|
| 46 | // Trace the promise. |
---|
| 47 | // description: |
---|
| 48 | // Tracing allows you to transparently log progress, |
---|
| 49 | // resolution and rejection of promises, without affecting the |
---|
| 50 | // promise itself. Any arguments passed to `trace()` are |
---|
| 51 | // emitted in trace events. See `dojo/promise/tracer` on how |
---|
| 52 | // to handle traces. |
---|
| 53 | // returns: dojo/promise/Promise |
---|
| 54 | // The promise instance `trace()` is called on. |
---|
| 55 | |
---|
| 56 | var args = lang._toArray(arguments); |
---|
| 57 | this.then( |
---|
| 58 | function(value){ emitAsync(["resolved", value].concat(args)); }, |
---|
| 59 | function(error){ emitAsync(["rejected", error].concat(args)); }, |
---|
| 60 | function(update){ emitAsync(["progress", update].concat(args)); } |
---|
| 61 | ); |
---|
| 62 | return this; |
---|
| 63 | }; |
---|
| 64 | |
---|
| 65 | Promise.prototype.traceRejected = function(){ |
---|
| 66 | // summary: |
---|
| 67 | // Trace rejection of the promise. |
---|
| 68 | // description: |
---|
| 69 | // Tracing allows you to transparently log progress, |
---|
| 70 | // resolution and rejection of promises, without affecting the |
---|
| 71 | // promise itself. Any arguments passed to `trace()` are |
---|
| 72 | // emitted in trace events. See `dojo/promise/tracer` on how |
---|
| 73 | // to handle traces. |
---|
| 74 | // returns: dojo/promise/Promise |
---|
| 75 | // The promise instance `traceRejected()` is called on. |
---|
| 76 | |
---|
| 77 | var args = lang._toArray(arguments); |
---|
| 78 | this.otherwise(function(error){ |
---|
| 79 | emitAsync(["rejected", error].concat(args)); |
---|
| 80 | }); |
---|
| 81 | return this; |
---|
| 82 | }; |
---|
| 83 | |
---|
| 84 | return evented; |
---|
| 85 | }); |
---|