Skip to content

Commit

Permalink
Use superagent for _changes feed request
Browse files Browse the repository at this point in the history
This is an interim solution until fetch polyfill supports abort.

#34
  • Loading branch information
pasin committed May 25, 2016
1 parent 4fa765c commit 7b840b5
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 3 deletions.
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@
"react": "^15.0.2",
"react-bootstrap": "^0.29.4",
"react-dom": "^15.0.2",
"react-router": "^2.4.0"
"react-router": "^2.4.0",
"superagent": "^2.0.0-alpha.3"
},
"devDependencies": {
"babel-core": "^6.8.0",
Expand Down
42 changes: 42 additions & 0 deletions src/js/api/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import fetch from 'isomorphic-fetch'
import request from 'superagent'
import { serverApi } from '../app';
import { makePath } from '../utils';

Expand Down Expand Up @@ -34,6 +35,39 @@ function _fetch(...args) {
};
}

function _request(req) {
if (!req)
return null;

let rej;
let isCanceled = false;
const promise = new Promise((resolve, reject) => {
if (isCanceled)
reject({ isCanceled });
else {
rej = reject;
req.end((err, res) => {
if (err)
reject({ message: err.message, status: err.status });
else
resolve({ data: res.body, status: res.status });
});
}
});

return {
promise,
cancel() {
if (!isCanceled) {
isCanceled = true;
req.abort();
if (rej)
rej({ isCanceled });
}
}
};
}

export function fetchAllDatabases() {
const path = makePath('_all_dbs');
return _fetch(serverApi(path));
Expand Down Expand Up @@ -110,6 +144,14 @@ export function deleteDoc(db, docId, revId) {
}

export function fetchChangesFeed(db, params) {
const path = makePath(db, '_changes');
const req = request.post(serverApi(path))
.set('Content-Type', 'application/json')
.send(params);
return _request(req);
}

export function _fetchChangesFeed(db, params) {
const path = makePath(db, '_changes');
return _fetch(serverApi(path), {
method: 'POST',
Expand Down
2 changes: 0 additions & 2 deletions src/js/stores/AppStore.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,6 @@ class AppStore extends Store {
}

setAlert(alert) {
if (alert)
debugger;
this.setData(data => {
return Object.assign({ }, data, { alert });
});
Expand Down

0 comments on commit 7b840b5

Please sign in to comment.