Skip to content

Commit

Permalink
Take helper functions out of plug classes
Browse files Browse the repository at this point in the history
  • Loading branch information
aaronmars committed Oct 25, 2016
1 parent 86ceb75 commit e7b43f2
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 34 deletions.
34 changes: 17 additions & 17 deletions lib/progressPlugBrowser.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,22 @@ function _doXhr({ xhr, body, progressInfo }) {
xhr.send(body);
});
}
function _readCookies(request) {
if(this._cookieManager !== null) {
return this._cookieManager.getCookieString(this.url).then((cookieString) => {
if(cookieString !== '') {
request.xhr.setRequestHeader('Cookie', cookieString);
}
}).then(() => request);
}
return Promise.resolve(request);
}
function _handleCookies(xhr) {
if(this._cookieManager !== null) {
return this._cookieManager.storeCookies(this.url, xhr.getResponseHeader('Set-Cookie')).then(() => xhr);
}
return Promise.resolve(xhr);
}
function _doRequest({ method, headers, body = null, progressInfo }) {
const xhr = new XMLHttpRequest(); // eslint-disable-line no-undef
xhr.open(method, this.url, true);
Expand All @@ -31,25 +47,9 @@ function _doRequest({ method, headers, body = null, progressInfo }) {
}
const request = { xhr: xhr, body: body, progressInfo: progressInfo };
progressInfo.callback({ loaded: 0, total: progressInfo.size });
return this._readCookies(request).then(_doXhr).then(this._handleCookies.bind(this));
return _readCookies.call(this, request).then(_doXhr).then(_handleCookies.bind(this));
}
export class ProgressPlugBrowser extends Plug {
_readCookies(request) {
if(this._cookieManager !== null) {
return this._cookieManager.getCookieString(this.url).then((cookieString) => {
if(cookieString !== '') {
request.xhr.setRequestHeader('Cookie', cookieString);
}
}).then(() => request);
}
return Promise.resolve(request);
}
_handleCookies(xhr) {
if(this._cookieManager !== null) {
return this._cookieManager.storeCookies(this.url, xhr.getResponseHeader('Set-Cookie')).then(() => xhr);
}
return Promise.resolve(xhr);
}
constructor(url, params) {
super(url, params);
}
Expand Down
34 changes: 17 additions & 17 deletions plug.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,32 +34,32 @@ function _handleHttpError(response) {
}
});
}
function _readCookies(request) {
if(this._cookieManager !== null) {
return this._cookieManager.getCookieString(request.url).then((cookieString) => {
if(cookieString !== '') {
request.headers.set('Cookie', cookieString);
}
}).then(() => request);
}
return Promise.resolve(request);
}
function _handleCookies(response) {
if(this._cookieManager !== null) {
return this._cookieManager.storeCookies(response.url, response.headers.getAll('Set-Cookie')).then(() => response);
}
return Promise.resolve(response);
}
function _doFetch({ method, headers, body = null }) {
let requestHeaders = new Headers(headers);
let requestData = { method: method, headers: requestHeaders, credentials: 'include' };
if(body !== null) {
requestData.body = body;
}
let request = new Request(this._url.toString(), requestData);
return this._readCookies(request).then(fetch).then(_handleHttpError).then(this._handleCookies.bind(this));
return _readCookies.call(this, request).then(fetch).then(_handleHttpError).then(_handleCookies.bind(this));
}
export class Plug {
_readCookies(request) {
if(this._cookieManager !== null) {
return this._cookieManager.getCookieString(request.url).then((cookieString) => {
if(cookieString !== '') {
request.headers.set('Cookie', cookieString);
}
}).then(() => request);
}
return Promise.resolve(request);
}
_handleCookies(response) {
if(this._cookieManager !== null) {
return this._cookieManager.storeCookies(response.url, response.headers.getAll('Set-Cookie')).then(() => response);
}
return Promise.resolve(response);
}
constructor(url = '/', { uriParts = {}, headers = {}, timeout = null, beforeRequest = (params) => params, cookieManager = null } = {}) {

// Initialize the url for this instance
Expand Down

0 comments on commit e7b43f2

Please sign in to comment.