traverson
Version:
Hypermedia API/HATEOAS client for Node.js and the browser
40 lines (34 loc) • 1.08 kB
JavaScript
;
var minilog = require('minilog')
, errorModule = require('./errors')
, errors = errorModule.errors
, createError = errorModule.createError
, log = minilog('traverson');
exports.abortTraversal = function abortTraversal() {
log.debug('aborting link traversal');
this.aborted = true;
if (this.currentRequest) {
log.debug('request in progress. trying to abort it, too.');
this.currentRequest.abort();
}
};
exports.registerAbortListener = function registerAbortListener(t, callback) {
if (t.currentRequest) {
t.currentRequest.on('abort', function() {
exports.callCallbackOnAbort(t);
});
}
};
exports.callCallbackOnAbort = function callCallbackOnAbort(t) {
log.debug('link traversal aborted');
if (!t.callbackHasBeenCalledAfterAbort) {
t.callbackHasBeenCalledAfterAbort = true;
t.callback(exports.abortError(), t);
}
};
exports.abortError = function abortError() {
var error = createError('Link traversal process has been aborted.',
errors.TraversalAbortedError);
error.aborted = true;
return error;
};