From f2274840a9737df45135fffaaaa9da266dbfd66a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Felix=20Geisend=C3=B6rfer?= Date: Tue, 19 Jan 2010 21:02:48 +0100 Subject: [PATCH] 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. --- doc/api.txt | 17 +---------------- src/node.js | 21 --------------------- test/mjsunit/test-promise-cancel.js | 13 ------------- test/mjsunit/test-promise-timeout.js | 28 ---------------------------- 4 files changed, 1 insertion(+), 78 deletions(-) delete mode 100644 test/mjsunit/test-promise-cancel.js diff --git a/doc/api.txt b/doc/api.txt index 48e51f4a58..5d09d9ac95 100644 --- a/doc/api.txt +++ b/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: + diff --git a/src/node.js b/src/node.js index 7dfcc279ba..2bad55e9ad 100644 --- a/src/node.js +++ b/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 = []; diff --git a/test/mjsunit/test-promise-cancel.js b/test/mjsunit/test-promise-cancel.js deleted file mode 100644 index 84ae9a600c..0000000000 --- a/test/mjsunit/test-promise-cancel.js +++ /dev/null @@ -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(); diff --git a/test/mjsunit/test-promise-timeout.js b/test/mjsunit/test-promise-timeout.js index 1a571a148d..b0b2727048 100644 --- a/test/mjsunit/test-promise-timeout.js +++ b/test/mjsunit/test-promise-timeout.js @@ -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); });