-
Notifications
You must be signed in to change notification settings - Fork 396
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Initial prototype for AbortablePromise #415
Open
briancavalier
wants to merge
1
commit into
master
Choose a base branch
from
add-abortable-promise
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
/** @license MIT License (c) copyright 2010-2014 original author or authors */ | ||
/** @author Brian Cavalier */ | ||
/** @author John Hann */ | ||
|
||
(function(define) { 'use strict'; | ||
define(function() { | ||
|
||
return function makeAbortablePromise(Promise) { | ||
|
||
function AbortablePromise(resolver) { | ||
Promise.call(this, makeAbortableResolver(resolver, this)); | ||
} | ||
|
||
AbortablePromise.prototype = Object.create(Promise.prototype); | ||
|
||
// Set constructor so that Promise.prototype.then will work | ||
AbortablePromise.prototype.constructor = AbortablePromise; | ||
|
||
AbortablePromise.prototype.then = function(f, r, p) { | ||
var child = Promise.prototype.then.call(this, f, r, p); | ||
|
||
// Propagate abort to child via handler | ||
child._handler.abort = this._handler.abort; | ||
|
||
return child; | ||
}; | ||
|
||
AbortablePromise.prototype.abort = function() { | ||
var handler = this._handler; | ||
|
||
if (typeof handler.abort !== 'function') { | ||
return; | ||
} | ||
|
||
// Detach abort so it can only be called once | ||
var fn = handler.abort; | ||
handler.abort = null; | ||
|
||
try { | ||
// aborter may return a promise to control the outcome of | ||
// the abort process. | ||
var abortResult = fn.apply(void 0, arguments); | ||
handler.resolve(abortResult); | ||
|
||
// Ensure return value is always a promise, specifically a | ||
// non-abortable promise | ||
return Promise.resolve(abortResult); | ||
} catch (e) { | ||
console.error(e.stack); | ||
handler.reject(e); | ||
} | ||
}; | ||
|
||
function makeAbortableResolver(task, abortablePromise) { | ||
return function() { | ||
runAbortableTask(task, abortablePromise._handler); | ||
}; | ||
} | ||
|
||
function runAbortableTask(task, handler) { | ||
// Make rejecting be the default abort behavior. | ||
handler.abort = defaultAbort; | ||
|
||
// Resolver may return an aborter function | ||
var result = task(abortableResolve, abortableReject); | ||
|
||
// If it did, stash it on the handler | ||
if(typeof result === 'function') { | ||
handler.abort = result; | ||
} | ||
|
||
function abortableResolve(x) { | ||
// If x is also an abortable promise, propagate its abort function | ||
// to handler. | ||
handler.abort = getAbort(x); | ||
handler.resolve(x); | ||
} | ||
|
||
function abortableReject(e) { | ||
// Always disable abort upon reject | ||
handler.abort = null; | ||
handler.reject(e); | ||
} | ||
|
||
function defaultAbort(x) { | ||
handler.reject(x); | ||
} | ||
} | ||
|
||
return AbortablePromise; | ||
}; | ||
|
||
function getAbort (x) { | ||
// Make a reasonable attempt to recognize abortable thenables | ||
return maybeThenable(x) ? x.abort : null; | ||
} | ||
|
||
function maybeThenable(x) { | ||
return (typeof x === 'object' || typeof x === 'function') && x !== null; | ||
} | ||
|
||
}); | ||
}(typeof define === 'function' && define.amd ? define : function(factory) { module.exports = factory(); })); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Instead of returning
undefined
here, should we return a promise for the previousabortResult
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah, good catch. Either that, or maybe
return Promise.resolve(this)
, ie a non-abortable view of the AbortablePromise instance?