| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -215,7 +215,6 @@ emit anymore events. | |||
| 215 | 215 | | Event | Parameters | Notes | |
| 216 | 216 | | +"success"+ | (depends) | | |
| 217 | 217 | | +"error"+ | (depends) | | |
| 218 | - | +"cancel"+ | (depends) | | ||
| 219 | 218 | |========================================================= | |
| 220 | 219 | ||
| 221 | 220 | +promise.addCallback(listener)+ :: | |
@@ -224,9 +223,6 @@ Adds a listener for the +"success"+ event. Returns the same promise object. | |||
| 224 | 223 | +promise.addErrback(listener)+ :: | |
| 225 | 224 | Adds a listener for the +"error"+ event. Returns the same promise object. | |
| 226 | 225 | ||
| 227 | - +promise.addCancelback(listener)+ :: | ||
| 228 | - Adds a listener for the +"cancel"+ event. Returns the same promise object. | ||
| 229 | - | ||
| 230 | 226 | +promise.emitSuccess(arg1, arg2, ...)+ :: | |
| 231 | 227 | If you created the promise (by doing +new node.Promise()+) then call | |
| 232 | 228 | +emitSuccess+ to emit the +"success"+ event with the given arguments. | |
@@ -237,20 +233,10 @@ the moment due to a bug; use +emitSuccess+ instead.) | |||
| 237 | 233 | +promise.emitError(arg1, arg2, ...)+ :: | |
| 238 | 234 | Emits the +"error"+ event. | |
| 239 | 235 | ||
| 240 | - +promise.emitCancel(arg1, arg2, ...)+ :: | ||
| 241 | - Emits the +"cancel"+ event. You may still get a +"success"+ or +"error"+ | ||
| 242 | - callback if the promise giver does not handle the cancel event. Use | ||
| 243 | - +promise.cancel()+ to ignore any later events. | ||
| 244 | - | ||
| 245 | - +promise.cancel()+ :: | ||
| 246 | - Clears all +"success"+ and +"error"+ event listeners from the promise, then | ||
| 247 | - emits the +"cancel"+ event. Whether or not the promise is actually canceled | ||
| 248 | - or not depends on the promise giver. | ||
| 249 | - | ||
| 250 | 236 | +promise.timeout(timeout = undefined)+ :: | |
| 251 | 237 | If the +timeout+ parameter is provided, the promise will emit an +"error"+ | |
| 252 | 238 | event after the given amount of millseconds. The timeout is canceled by any | |
| 253 | - +"success"+, +"error"+ or +"cancel"+ event being emitted by the Promise. | ||
| 239 | + +"success"+ or +"error"+ event being emitted by the Promise. | ||
| 254 | 240 | + | |
| 255 | 241 | To tell apart a timeout from a regular "error" event, use the following test: | |
| 256 | 242 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -20,24 +20,6 @@ node.EventEmitter.prototype.listeners = function (type) { | |||
| 20 | 20 | return this._events[type]; | |
| 21 | 21 | }; | |
| 22 | 22 | ||
| 23 | - // node.Promise is defined in src/events.cc | ||
| 24 | - node.Promise.prototype.cancel = function() { | ||
| 25 | - this._events['success'] = []; | ||
| 26 | - this._events['error'] = []; | ||
| 27 | - | ||
| 28 | - this.emitSuccess = function() {}; | ||
| 29 | - this.emitError = function() {}; | ||
| 30 | - | ||
| 31 | - this.emitCancel(); | ||
| 32 | - }; | ||
| 33 | - | ||
| 34 | - node.Promise.prototype.emitCancel = function() { | ||
| 35 | - var args = Array.prototype.slice.call(arguments); | ||
| 36 | - args.unshift('cancel'); | ||
| 37 | - | ||
| 38 | - this.emit.apply(this, args); | ||
| 39 | - }; | ||
| 40 | - | ||
| 41 | 23 | node.Promise.prototype.timeout = function(timeout) { | |
| 42 | 24 | if (timeout === undefined) { | |
| 43 | 25 | return this._timeoutDuration; | |
@@ -51,7 +33,6 @@ node.Promise.prototype.timeout = function(timeout) { | |||
| 51 | 33 | var self = this | |
| 52 | 34 | this._timer = setTimeout(function() { | |
| 53 | 35 | self.emitError(new Error('timeout')); | |
| 54 | - self.cancel(); | ||
| 55 | 36 | }, this._timeoutDuration); | |
| 56 | 37 | ||
| 57 | 38 | return this; | |
@@ -67,11 +48,6 @@ node.Promise.prototype.addErrback = function (listener) { | |||
| 67 | 48 | return this; | |
| 68 | 49 | }; | |
| 69 | 50 | ||
| 70 | - node.Promise.prototype.addCancelback = function (listener) { | ||
| 71 | - this.addListener("cancel", listener); | ||
| 72 | - return this; | ||
| 73 | - }; | ||
| 74 | - | ||
| 75 | 51 | node.Promise.prototype.wait = function () { | |
| 76 | 52 | var ret; | |
| 77 | 53 | var had_error = false; | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -205,16 +205,22 @@ void Promise::Detach(void) { | |||
| 205 | 205 | } | |
| 206 | 206 | ||
| 207 | 207 | bool Promise::EmitSuccess(int argc, v8::Handle<v8::Value> argv[]) { | |
| 208 | + if (has_fired_) return false; | ||
| 209 | + | ||
| 208 | 210 | bool r = Emit("success", argc, argv); | |
| 209 | 211 | ||
| 212 | + has_fired_ = true; | ||
| 210 | 213 | Detach(); | |
| 211 | 214 | ||
| 212 | 215 | return r; | |
| 213 | 216 | } | |
| 214 | 217 | ||
| 215 | 218 | bool Promise::EmitError(int argc, v8::Handle<v8::Value> argv[]) { | |
| 219 | + if (has_fired_) return false; | ||
| 220 | + | ||
| 216 | 221 | bool r = Emit("error", argc, argv); | |
| 217 | 222 | ||
| 223 | + has_fired_ = true; | ||
| 218 | 224 | Detach(); | |
| 219 | 225 | ||
| 220 | 226 | return r; | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -43,12 +43,14 @@ class Promise : public EventEmitter { | |||
| 43 | 43 | ||
| 44 | 44 | virtual void Detach(void); | |
| 45 | 45 | ||
| 46 | + bool has_fired_; | ||
| 46 | 47 | bool blocking_; | |
| 47 | 48 | Promise *prev_; /* for the prev in the Poor Man's coroutine stack */ | |
| 48 | 49 | ||
| 49 | 50 | void Destack(); | |
| 50 | 51 | ||
| 51 | 52 | Promise() : EventEmitter() { | |
| 53 | + has_fired_ = false; | ||
| 52 | 54 | blocking_ = false; | |
| 53 | 55 | prev_ = NULL; | |
| 54 | 56 | } | |
| Back | FazBrowse Home | New Git URL |
0 commit comments