From 7b840b5705f4ab680e289cebd96f0922a9f3def7 Mon Sep 17 00:00:00 2001 From: Pasin Suriyentrakorn Date: Tue, 24 May 2016 18:10:02 -0700 Subject: [PATCH] Use superagent for _changes feed request This is an interim solution until fetch polyfill supports abort. #34 --- package.json | 3 ++- src/js/api/index.js | 42 +++++++++++++++++++++++++++++++++++++++ src/js/stores/AppStore.js | 2 -- 3 files changed, 44 insertions(+), 3 deletions(-) diff --git a/package.json b/package.json index dc483b5..c4708ef 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/src/js/api/index.js b/src/js/api/index.js index b786e2f..52845ac 100644 --- a/src/js/api/index.js +++ b/src/js/api/index.js @@ -1,4 +1,5 @@ import fetch from 'isomorphic-fetch' +import request from 'superagent' import { serverApi } from '../app'; import { makePath } from '../utils'; @@ -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)); @@ -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', diff --git a/src/js/stores/AppStore.js b/src/js/stores/AppStore.js index 462a977..116fe0f 100644 --- a/src/js/stores/AppStore.js +++ b/src/js/stores/AppStore.js @@ -67,8 +67,6 @@ class AppStore extends Store { } setAlert(alert) { - if (alert) - debugger; this.setData(data => { return Object.assign({ }, data, { alert }); });