Skip to content

Commit

Permalink
Remove hard dependency on when.js
Browse files Browse the repository at this point in the history
rest.js now relies on an ES6 Promise implementation rather than when.js.
For modern browsers (http://caniuse.com/#feat=promises) this means one
less dependency. Older browsers can pick their polyfill of choise.

Of course, I still recomend when.js as an amazing Promise implementation
and utility library.

Using when.js as a Promise polyfill;
https://github.com/cujojs/when/blob/master/docs/es6-promise-shim.md
  • Loading branch information
scothis committed Jun 16, 2016
1 parent 1102517 commit 300b7ab
Show file tree
Hide file tree
Showing 45 changed files with 427 additions and 340 deletions.
6 changes: 2 additions & 4 deletions bower.json
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
{
"name": "rest",
"version": "1.3.2",
"version": "1.4.0-pre",
"main": "./browser.js",
"moduleType": ["amd", "node"],
"dependencies": {
"when": "~3"
},
"dependencies": {},
"ignore": [
"docs",
"test"
Expand Down
5 changes: 2 additions & 3 deletions client/jsonp.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2012-2014 the original author or authors
* Copyright 2012-2015 the original author or authors
* @license MIT, see LICENSE.txt for details
*
* @author Scott Andrews
Expand All @@ -12,9 +12,8 @@

define(function (require) {

var when, UrlBuilder, responsePromise, client;
var UrlBuilder, responsePromise, client;

when = require('when');
UrlBuilder = require('../UrlBuilder');
responsePromise = require('../util/responsePromise');
client = require('../client');
Expand Down
5 changes: 2 additions & 3 deletions client/node.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2012-2014 the original author or authors
* Copyright 2012-2015 the original author or authors
* @license MIT, see LICENSE.txt for details
*
* @author Jeremy Grelle
Expand All @@ -11,12 +11,11 @@

define(function (require) {

var parser, http, https, when, UrlBuilder, mixin, normalizeHeaderName, responsePromise, client, httpsExp;
var parser, http, https, UrlBuilder, mixin, normalizeHeaderName, responsePromise, client, httpsExp;

parser = envRequire('url');
http = envRequire('http');
https = envRequire('https');
when = require('when');
UrlBuilder = require('../UrlBuilder');
mixin = require('../util/mixin');
normalizeHeaderName = require('../util/normalizeHeaderName');
Expand Down
5 changes: 2 additions & 3 deletions client/xdr.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2013-2014 the original author or authors
* Copyright 2013-2015 the original author or authors
* @license MIT, see LICENSE.txt for details
*
* @author Scott Andrews
Expand All @@ -10,9 +10,8 @@

define(function (require) {

var when, UrlBuilder, responsePromise, client;
var UrlBuilder, responsePromise, client;

when = require('when');
UrlBuilder = require('../UrlBuilder');
responsePromise = require('../util/responsePromise');
client = require('../client');
Expand Down
5 changes: 2 additions & 3 deletions client/xhr.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2012-2014 the original author or authors
* Copyright 2012-2015 the original author or authors
* @license MIT, see LICENSE.txt for details
*
* @author Scott Andrews
Expand All @@ -10,9 +10,8 @@

define(function (require) {

var when, UrlBuilder, normalizeHeaderName, responsePromise, client, headerSplitRE;
var UrlBuilder, normalizeHeaderName, responsePromise, client, headerSplitRE;

when = require('when');
UrlBuilder = require('../UrlBuilder');
normalizeHeaderName = require('../util/normalizeHeaderName');
responsePromise = require('../util/responsePromise');
Expand Down
25 changes: 8 additions & 17 deletions interceptor.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,13 @@

define(function (require) {

var defaultClient, mixin, responsePromise, client, when;
var defaultClient, mixin, responsePromise, client, Promise;

defaultClient = require('./client/default');
mixin = require('./util/mixin');
responsePromise = require('./util/responsePromise');
client = require('./client');
when = require('when');
Promise = require('./util/Promise');

/**
* Interceptors have the ability to intercept the request and/org response
Expand Down Expand Up @@ -49,15 +49,6 @@
return response;
}

function race(promisesOrValues) {
// this function is different than when.any as the first to reject also wins
return when.promise(function (resolve, reject) {
promisesOrValues.forEach(function (promiseOrValue) {
when(promiseOrValue, resolve, reject);
});
});
}

/**
* Alternate return type for the request handler that allows for more complex interactions.
*
Expand Down Expand Up @@ -97,7 +88,8 @@
successResponseHandler = handlers.success || handlers.response || defaultResponseHandler;
errorResponseHandler = handlers.error || function () {
// Propagate the rejection, with the result of the handler
return when((handlers.response || defaultResponseHandler).apply(this, arguments), when.reject, when.reject);
return Promise.resolve((handlers.response || defaultResponseHandler).apply(this, arguments))
.then(Promise.reject.bind(Promise));
};

return function (target, config) {
Expand Down Expand Up @@ -130,9 +122,8 @@
// normalize request, must be last
request = request.request;
}
response = response || when(request, function (request) {
return when(
next(request),
response = response || Promise.resolve(request).then(function (request) {
return Promise.resolve(next(request)).then(
function (response) {
return successResponseHandler.call(context, response, config, meta);
},
Expand All @@ -141,10 +132,10 @@
}
);
});
return abort ? race([response, abort]) : response;
return abort ? Promise.race([response, abort]) : response;
},
function (error) {
return when.reject({ request: request, error: error });
return Promise.reject({ request: request, error: error });
}
);
}
Expand Down
8 changes: 4 additions & 4 deletions interceptor/errorCode.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2012-2013 the original author or authors
* Copyright 2012-2015 the original author or authors
* @license MIT, see LICENSE.txt for details
*
* @author Scott Andrews
Expand All @@ -10,10 +10,10 @@

define(function (require) {

var interceptor, when;
var interceptor, Promise;

interceptor = require('../interceptor');
when = require('when');
Promise = require('../util/Promise');

/**
* Rejects the response promise based on the status code.
Expand All @@ -33,7 +33,7 @@
},
response: function (response, config) {
if (response.status && response.status.code >= config.code) {
return when.reject(response);
return Promise.reject(response);
}
return response;
}
Expand Down
31 changes: 20 additions & 11 deletions interceptor/mime.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2012-2014 the original author or authors
* Copyright 2012-2015 the original author or authors
* @license MIT, see LICENSE.txt for details
*
* @author Scott Andrews
Expand All @@ -8,20 +8,27 @@
(function (define) {
'use strict';

var undef;

define(function (require) {

var interceptor, mime, registry, noopConverter, when;
var interceptor, mime, registry, noopConverter, missingConverter, attempt;

interceptor = require('../interceptor');
mime = require('../mime');
registry = require('../mime/registry');
when = require('when');
attempt = require('../util/attempt');

noopConverter = {
read: function (obj) { return obj; },
write: function (obj) { return obj; }
};

missingConverter = {
read: function () { throw 'No read method found on converter'; },
write: function () { throw 'No write method found on converter'; }
};

/**
* MIME type support for request and response entities. Entities are
* (de)serialized using the converter for the MIME type.
Expand Down Expand Up @@ -59,17 +66,18 @@
return request;
}

return config.registry.lookup(type).otherwise(function () {
return config.registry.lookup(type)['catch'](function () {
// failed to resolve converter
if (config.permissive) {
return noopConverter;
}
throw 'mime-unknown';
}).then(function (converter) {
var client = config.client || request.originator;
var client = config.client || request.originator,
write = converter.write || missingConverter.write;

return when.attempt(converter.write, request.entity, { client: client, request: request, mime: type, registry: config.registry })
.otherwise(function() {
return attempt(write.bind(undef, request.entity, { client: client, request: request, mime: type, registry: config.registry }))
['catch'](function() {
throw 'mime-serialization';
})
.then(function(entity) {
Expand All @@ -85,11 +93,12 @@

var type = mime.parse(response.headers['Content-Type']);

return config.registry.lookup(type).otherwise(function () { return noopConverter; }).then(function (converter) {
var client = config.client || response.request && response.request.originator;
return config.registry.lookup(type)['catch'](function () { return noopConverter; }).then(function (converter) {
var client = config.client || response.request && response.request.originator,
read = converter.read || missingConverter.read;

return when.attempt(converter.read, response.entity, { client: client, response: response, mime: type, registry: config.registry })
.otherwise(function (e) {
return attempt(read.bind(undef, response.entity, { client: client, response: response, mime: type, registry: config.registry }))
['catch'](function (e) {
response.error = 'mime-deserialization';
response.cause = e;
throw response;
Expand Down
10 changes: 5 additions & 5 deletions interceptor/oAuth.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2012-2013 the original author or authors
* Copyright 2012-2015 the original author or authors
* @license MIT, see LICENSE.txt for details
*
* @author Scott Andrews
Expand All @@ -10,12 +10,12 @@

define(function (require) {

var interceptor, UrlBuilder, pubsub, when;
var interceptor, UrlBuilder, pubsub, Promise;

interceptor = require('../interceptor');
UrlBuilder = require('../UrlBuilder');
pubsub = require('../util/pubsub');
when = require('when');
Promise = require('../util/Promise');

function defaultOAuthCallback(hash) {
var params, queryString, regex, m;
Expand Down Expand Up @@ -44,7 +44,7 @@
function authorize(config) {
var state, url, dismissWindow;

return when.promise(function (resolve) {
return new Promise(function (resolve) {

state = Math.random() * new Date().getTime();
url = new UrlBuilder(config.authorizationUrlBase).build({
Expand Down Expand Up @@ -131,7 +131,7 @@
});
}
else if (response.status.code === 403) {
return when.reject(response);
return Promise.reject(response);
}

return response;
Expand Down
13 changes: 7 additions & 6 deletions interceptor/retry.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2012-2014 the original author or authors
* Copyright 2012-2015 the original author or authors
* @license MIT, see LICENSE.txt for details
*
* @author Jeremy Grelle
Expand All @@ -11,10 +11,11 @@

define(function (require) {

var interceptor, when;
var interceptor, delay, Promise;

interceptor = require('../interceptor');
when = require('when');
delay = require('../util/delay');
Promise = require('../util/Promise');

/**
* Retries a rejected request using an exponential backoff.
Expand All @@ -41,10 +42,10 @@
request = response.request;
request.retry = request.retry || config.initial;

return when(request).delay(request.retry).then(function (request) {
return delay(request.retry, request).then(function (request) {
if (request.canceled) {
// cancel here in case client doesn't check canceled flag
return when.reject({ request: request, error: 'precanceled' });
return Promise.reject({ request: request, error: 'precanceled' });
}
request.retry = Math.min(request.retry * config.multiplier, config.max);
return meta.client(request);
Expand All @@ -56,4 +57,4 @@
}(
typeof define === 'function' && define.amd ? define : function (factory) { module.exports = factory(require); }
// Boilerplate for AMD and Node
));
));
14 changes: 8 additions & 6 deletions interceptor/timeout.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@

define(function (require) {

var interceptor, when;
var interceptor, Promise;

interceptor = require('../interceptor');
when = require('when');
Promise = require('../util/Promise');

/**
* Cancels a request if it takes longer then the timeout value.
Expand All @@ -32,15 +32,17 @@
return config;
},
request: function (request, config) {
var timeout, abortTrigger, transient;
var timeout, abort, triggerAbort, transient;
timeout = 'timeout' in request ? request.timeout : config.timeout;
transient = 'transient' in request ? request.transient : config.transient;
if (timeout <= 0) {
return request;
}
abortTrigger = when.defer();
abort = new Promise(function (resolve, reject) {
triggerAbort = reject;
});
this.timeout = setTimeout(function () {
abortTrigger.reject({ request: request, error: 'timeout' });
triggerAbort({ request: request, error: 'timeout' });
if (request.cancel) {
request.cancel();
if (transient) {
Expand All @@ -52,7 +54,7 @@
request.canceled = true;
}
}, timeout);
return new interceptor.ComplexRequest({ request: request, abort: abortTrigger.promise });
return new interceptor.ComplexRequest({ request: request, abort: abort });
},
response: function (response) {
if (this.timeout) {
Expand Down
Loading

0 comments on commit 300b7ab

Please sign in to comment.