Browse Source

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.
v0.7.4-release
Felix Geisendörfer 15 years ago
committed by Ryan Dahl
parent
commit
f2274840a9
  1. 17
      doc/api.txt
  2. 21
      src/node.js
  3. 13
      test/mjsunit/test-promise-cancel.js
  4. 28
      test/mjsunit/test-promise-timeout.js

17
doc/api.txt

@ -286,7 +286,6 @@ emit anymore events.
| Event | Parameters | Notes | Event | Parameters | Notes
| +"success"+ | (depends) | | +"success"+ | (depends) |
| +"error"+ | (depends) | | +"error"+ | (depends) |
| +"cancel"+ | (depends) |
|========================================================= |=========================================================
+promise.addCallback(listener)+ :: +promise.addCallback(listener)+ ::
@ -295,9 +294,6 @@ Adds a listener for the +"success"+ event. Returns the same promise object.
+promise.addErrback(listener)+ :: +promise.addErrback(listener)+ ::
Adds a listener for the +"error"+ event. Returns the same promise object. 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, ...)+ :: +promise.emitSuccess(arg1, arg2, ...)+ ::
If you created the promise (by doing +new events.Promise()+) then call If you created the promise (by doing +new events.Promise()+) then call
+emitSuccess+ to emit the +"success"+ event with the given arguments. +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, ...)+ :: +promise.emitError(arg1, arg2, ...)+ ::
Emits the +"error"+ event. 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)+ :: +promise.timeout(timeout = undefined)+ ::
If the +timeout+ parameter is provided, the promise will emit an +"error"+ 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 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: To tell apart a timeout from a regular "error" event, use the following test:
+ +

21
src/node.js

@ -259,7 +259,6 @@ var eventsModule = createInternalModule('events', function (exports) {
this this
.addCallback(onComplete) .addCallback(onComplete)
.addCancelback(onComplete)
.addErrback(onComplete); .addErrback(onComplete);
var self = this; var self = this;
@ -275,21 +274,6 @@ var eventsModule = createInternalModule('events', function (exports) {
return this; 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() { exports.Promise.prototype.emitSuccess = function() {
if (this.hasFired) return; if (this.hasFired) return;
this.hasFired = true; this.hasFired = true;
@ -314,11 +298,6 @@ var eventsModule = createInternalModule('events', function (exports) {
return this; return this;
}; };
exports.Promise.prototype.addCancelback = function (listener) {
this.addListener("cancel", listener);
return this;
};
/* Poor Man's coroutines */ /* Poor Man's coroutines */
var coroutineStack = []; var coroutineStack = [];

13
test/mjsunit/test-promise-cancel.js

@ -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();

28
test/mjsunit/test-promise-timeout.js

@ -51,34 +51,6 @@ errorPromise.addErrback(function(e) {
assert.equal('intentional', e.message); 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() { process.addListener('exit', function() {
assert.equal(2, timeouts); assert.equal(2, timeouts);
}); });

Loading…
Cancel
Save