mirror of
https://github.com/nodejs/node-v0.x-archive.git
synced 2026-04-28 03:01:10 -04:00
Removed Promise.cancel()
The current implementation was bad and nobody is using it. Has a chance of getting re-implemented by somebody who has an actual need for it.
This commit is contained in:
committed by
Ryan Dahl
parent
4e16e386fe
commit
f2274840a9
17
doc/api.txt
17
doc/api.txt
@@ -286,7 +286,6 @@ emit anymore events.
|
||||
| Event | Parameters | Notes
|
||||
| +"success"+ | (depends) |
|
||||
| +"error"+ | (depends) |
|
||||
| +"cancel"+ | (depends) |
|
||||
|=========================================================
|
||||
|
||||
+promise.addCallback(listener)+ ::
|
||||
@@ -295,9 +294,6 @@ Adds a listener for the +"success"+ event. Returns the same promise object.
|
||||
+promise.addErrback(listener)+ ::
|
||||
Adds a listener for the +"error"+ event. Returns the same promise object.
|
||||
|
||||
+promise.addCancelback(listener)+ ::
|
||||
Adds a listener for the +"cancel"+ event. Returns the same promise object.
|
||||
|
||||
+promise.emitSuccess(arg1, arg2, ...)+ ::
|
||||
If you created the promise (by doing +new events.Promise()+) then call
|
||||
+emitSuccess+ to emit the +"success"+ event with the given arguments.
|
||||
@@ -308,21 +304,10 @@ the moment due to a bug; use +emitSuccess+ instead.)
|
||||
+promise.emitError(arg1, arg2, ...)+ ::
|
||||
Emits the +"error"+ event.
|
||||
|
||||
+promise.emitCancel(arg1, arg2, ...)+ ::
|
||||
Emits the +"cancel"+ event. You may still get a +"success"+ or +"error"+
|
||||
callback if the promise giver does not handle the cancel event. Use
|
||||
+promise.cancel()+ to ignore any later events.
|
||||
|
||||
+promise.cancel()+ ::
|
||||
Clears all +"success"+ and +"error"+ event listeners from the promise, then
|
||||
emits the +"cancel"+ event. Whether or not the promise is actually canceled
|
||||
or not depends on the promise giver. This also clears Promise.timeout() if one
|
||||
was set.
|
||||
|
||||
+promise.timeout(timeout = undefined)+ ::
|
||||
If the +timeout+ parameter is provided, the promise will emit an +"error"+
|
||||
event after the given amount of millseconds. The timeout is canceled by any
|
||||
+"success"+, +"error"+ or +"cancel"+ event being emitted by the Promise.
|
||||
+"success"+ or +"error"+ event being emitted by the Promise.
|
||||
+
|
||||
To tell apart a timeout from a regular "error" event, use the following test:
|
||||
+
|
||||
|
||||
21
src/node.js
21
src/node.js
@@ -259,7 +259,6 @@ var eventsModule = createInternalModule('events', function (exports) {
|
||||
|
||||
this
|
||||
.addCallback(onComplete)
|
||||
.addCancelback(onComplete)
|
||||
.addErrback(onComplete);
|
||||
|
||||
var self = this;
|
||||
@@ -275,21 +274,6 @@ var eventsModule = createInternalModule('events', function (exports) {
|
||||
return this;
|
||||
};
|
||||
|
||||
exports.Promise.prototype.cancel = function() {
|
||||
if(this._cancelled) return;
|
||||
this._cancelled = true;
|
||||
|
||||
this._events['success'] = [];
|
||||
this._events['error'] = [];
|
||||
|
||||
this.emitCancel();
|
||||
};
|
||||
|
||||
exports.Promise.prototype.emitCancel = function() {
|
||||
Array.prototype.unshift.call(arguments, 'cancel')
|
||||
this.emit.apply(this, arguments);
|
||||
};
|
||||
|
||||
exports.Promise.prototype.emitSuccess = function() {
|
||||
if (this.hasFired) return;
|
||||
this.hasFired = true;
|
||||
@@ -314,11 +298,6 @@ var eventsModule = createInternalModule('events', function (exports) {
|
||||
return this;
|
||||
};
|
||||
|
||||
exports.Promise.prototype.addCancelback = function (listener) {
|
||||
this.addListener("cancel", listener);
|
||||
return this;
|
||||
};
|
||||
|
||||
/* Poor Man's coroutines */
|
||||
var coroutineStack = [];
|
||||
|
||||
|
||||
@@ -1,13 +0,0 @@
|
||||
process.mixin(require('./common'));
|
||||
events = require('events');
|
||||
|
||||
var promise = new events.Promise();
|
||||
var cancelled = false;
|
||||
promise.addCancelback(function(){
|
||||
if(cancelled){
|
||||
assert.ok(false, "promise should not cancel more than once");
|
||||
}
|
||||
cancelled = true;
|
||||
});
|
||||
promise.cancel();
|
||||
promise.cancel();
|
||||
@@ -51,34 +51,6 @@ errorPromise.addErrback(function(e) {
|
||||
assert.equal('intentional', e.message);
|
||||
});
|
||||
|
||||
var cancelPromise = new events.Promise();
|
||||
cancelPromise.timeout(500);
|
||||
setTimeout(function() {
|
||||
cancelPromise.cancel();
|
||||
}, 250);
|
||||
|
||||
setTimeout(function() {
|
||||
cancelPromise.emitSuccess('should be ignored');
|
||||
}, 400);
|
||||
|
||||
cancelPromise.addCallback(function(e) {
|
||||
assert.ok(false, 'addCallback should not fire if the promise is canceled');
|
||||
});
|
||||
|
||||
cancelPromise.addErrback(function(e) {
|
||||
assert.ok(false, 'addErrback should not fire if the promise is canceled');
|
||||
});
|
||||
|
||||
var cancelTimeoutPromise = new events.Promise();
|
||||
cancelTimeoutPromise.timeout(500);
|
||||
setTimeout(function() {
|
||||
cancelPromise.emitCancel();
|
||||
}, 250);
|
||||
|
||||
cancelPromise.addErrback(function(e) {
|
||||
assert.ok(false, 'addErrback should not fire after a cancel event');
|
||||
});
|
||||
|
||||
process.addListener('exit', function() {
|
||||
assert.equal(2, timeouts);
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user