Skip to content

Commit efe34f5

Browse files
committed
Only allow a promise to fire once, remove promise.cancel()
promise.cancel() is due to return at some point.
1 parent fc937aa commit efe34f5

File tree

5 files changed

+9
-66
lines changed

5 files changed

+9
-66
lines changed

doc/api.txt

Lines changed: 1 addition & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -215,7 +215,6 @@ emit anymore events.
215215
| Event | Parameters | Notes
216216
| +"success"+ | (depends) |
217217
| +"error"+ | (depends) |
218-
| +"cancel"+ | (depends) |
219218
|=========================================================
220219

221220
+promise.addCallback(listener)+ ::
@@ -224,9 +223,6 @@ Adds a listener for the +"success"+ event. Returns the same promise object.
224223
+promise.addErrback(listener)+ ::
225224
Adds a listener for the +"error"+ event. Returns the same promise object.
226225

227-
+promise.addCancelback(listener)+ ::
228-
Adds a listener for the +"cancel"+ event. Returns the same promise object.
229-
230226
+promise.emitSuccess(arg1, arg2, ...)+ ::
231227
If you created the promise (by doing +new node.Promise()+) then call
232228
+emitSuccess+ to emit the +"success"+ event with the given arguments.
@@ -237,20 +233,10 @@ the moment due to a bug; use +emitSuccess+ instead.)
237233
+promise.emitError(arg1, arg2, ...)+ ::
238234
Emits the +"error"+ event.
239235

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-
250236
+promise.timeout(timeout = undefined)+ ::
251237
If the +timeout+ parameter is provided, the promise will emit an +"error"+
252238
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.
254240
+
255241
To tell apart a timeout from a regular "error" event, use the following test:
256242
+

src/events.js

Lines changed: 0 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -20,24 +20,6 @@ node.EventEmitter.prototype.listeners = function (type) {
2020
return this._events[type];
2121
};
2222

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-
4123
node.Promise.prototype.timeout = function(timeout) {
4224
if (timeout === undefined) {
4325
return this._timeoutDuration;
@@ -51,7 +33,6 @@ node.Promise.prototype.timeout = function(timeout) {
5133
var self = this
5234
this._timer = setTimeout(function() {
5335
self.emitError(new Error('timeout'));
54-
self.cancel();
5536
}, this._timeoutDuration);
5637

5738
return this;
@@ -67,11 +48,6 @@ node.Promise.prototype.addErrback = function (listener) {
6748
return this;
6849
};
6950

70-
node.Promise.prototype.addCancelback = function (listener) {
71-
this.addListener("cancel", listener);
72-
return this;
73-
};
74-
7551
node.Promise.prototype.wait = function () {
7652
var ret;
7753
var had_error = false;

src/node_events.cc

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,16 +205,22 @@ void Promise::Detach(void) {
205205
}
206206

207207
bool Promise::EmitSuccess(int argc, v8::Handle<v8::Value> argv[]) {
208+
if (has_fired_) return false;
209+
208210
bool r = Emit("success", argc, argv);
209211

212+
has_fired_ = true;
210213
Detach();
211214

212215
return r;
213216
}
214217

215218
bool Promise::EmitError(int argc, v8::Handle<v8::Value> argv[]) {
219+
if (has_fired_) return false;
220+
216221
bool r = Emit("error", argc, argv);
217222

223+
has_fired_ = true;
218224
Detach();
219225

220226
return r;

src/node_events.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,12 +43,14 @@ class Promise : public EventEmitter {
4343

4444
virtual void Detach(void);
4545

46+
bool has_fired_;
4647
bool blocking_;
4748
Promise *prev_; /* for the prev in the Poor Man's coroutine stack */
4849

4950
void Destack();
5051

5152
Promise() : EventEmitter() {
53+
has_fired_ = false;
5254
blocking_ = false;
5355
prev_ = NULL;
5456
}

test/mjsunit/test-promise-cancel.js

Lines changed: 0 additions & 27 deletions
This file was deleted.

0 commit comments

Comments
 (0)