Skip to content

Commit

Permalink
Merge pull request #21 from TylorS/master
Browse files Browse the repository at this point in the history
Update to RxJS 5.0.0-alpha.6
  • Loading branch information
staltz committed Oct 21, 2015
2 parents ccb9499 + fec4475 commit 80e5f69
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 37 deletions.
5 changes: 2 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,10 @@
"superagent": "1.4.0"
},
"peerDependencies": {
"@cycle/core": "5.x.x"
"@reactivex/rxjs": "5.0.0-alpha.6"
},
"devDependencies": {
"@cycle/core": "5.0.x",
"rx": "4.0.6",
"@reactivex/rxjs": "5.0.0-alpha.6",
"babel": "5.8.23",
"babelify": "6.3.0",
"body-parser": "1.14.1",
Expand Down
25 changes: 15 additions & 10 deletions src/http-driver.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const Rx = require(`rx`)
const Rx = require(`@reactivex/rxjs`)
const superagent = require(`superagent`)

function optionsToSuperagent({
Expand Down Expand Up @@ -71,24 +71,29 @@ function createResponse$(reqOptions) {
if (typeof reqOptions === `string`) {
request = urlToSuperagent(reqOptions)
} else if (typeof reqOptions === `object`) {
request = optionsToSuperagent(reqOptions)
try {
request = optionsToSuperagent(reqOptions)
} catch (err) {
observer.error(err)
return () => {} // noop
}
} else {
observer.onError(new Error(`Observable of requests given to HTTP ` +
observer.error(new Error(`Observable of requests given to HTTP ` +
`Driver must emit either URL strings or objects with parameters.`))
return () => {} // noop
}

try {
request.end((err, res) => {
if (err) {
observer.onError(err)
observer.error(err)
} else {
observer.onNext(res)
observer.onCompleted()
observer.next(res)
observer.complete()
}
})
} catch (err) {
observer.onError(err)
observer.error(err)
}

return function onDispose() {
Expand All @@ -103,13 +108,13 @@ function makeHTTPDriver({eager = false} = {eager: false}) {
.map(reqOptions => {
let response$ = createResponse$(reqOptions)
if (eager || reqOptions.eager) {
response$ = response$.replay(null, 1)
response$ = response$.publishReplay(1)
response$.connect()
return response$
}
response$.request = reqOptions
return response$
})
.replay(null, 1)
}).publishReplay(1)
response$$.connect()
return response$$
}
Expand Down
36 changes: 18 additions & 18 deletions test/common.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@
/* global describe, it */
var assert = require('assert');
var src = require('../lib/index');
var Cycle = require('@cycle/core');
var Rx = require('rx');
var Rx = require('@reactivex/rxjs');
var makeHTTPDriver = src.makeHTTPDriver;

function run(uri) {
Expand All @@ -18,7 +17,7 @@ function run(uri) {
describe('HTTP Driver', function () {
it('should throw when request stream emits neither string nor object',
function(done) {
var request$ = Rx.Observable.just(123);
var request$ = Rx.Observable.of(123);
var httpDriver = makeHTTPDriver();
var response$$ = httpDriver(request$);
response$$.mergeAll().subscribe(
Expand All @@ -36,25 +35,26 @@ function run(uri) {

it('should throw when given options object without url string',
function(done) {
var request$ = Rx.Observable.just({method: 'post'});
var request$ = Rx.Observable.of({method: 'post'});
var httpDriver = makeHTTPDriver();
var response$$ = httpDriver(request$);
response$$.mergeAll().subscribe(
function onNext() { assert.fail(); },
function onError(err) {
assert.strictEqual(
err.message, 'Please provide a `url` property in the request ' +
'options.'
);
done();
}
);
response$$.mergeAll()
.subscribe(
function onNext() { assert.fail(); },
function onError(err) {
assert.strictEqual(
err.message, 'Please provide a `url` property in the request ' +
'options.'
);
done();
}
);
}
);

it('should return response metastream when given a simple URL string',
function(done) {
var request$ = Rx.Observable.just(uri + '/hello');
var request$ = Rx.Observable.of(uri + '/hello');
var httpDriver = makeHTTPDriver();
var response$$ = httpDriver(request$);
response$$.subscribe(function(response$) {
Expand All @@ -70,7 +70,7 @@ function run(uri) {

it('should return response metastream when given simple options obj',
function(done) {
var request$ = Rx.Observable.just({
var request$ = Rx.Observable.of({
url: uri + '/pet',
method: 'POST',
send: {name: 'Woof', species: 'Dog'}
Expand All @@ -93,7 +93,7 @@ function run(uri) {

it('should return response metastream when given another options obj',
function(done) {
var request$ = Rx.Observable.just({
var request$ = Rx.Observable.of({
url: uri + '/querystring',
method: 'GET',
query: {foo: 102030, bar: 'Pub'}
Expand All @@ -117,7 +117,7 @@ function run(uri) {

it('should send 500 server errors to response$ onError',
function(done) {
var request$ = Rx.Observable.just(uri + '/error');
var request$ = Rx.Observable.of(uri + '/error');
var httpDriver = makeHTTPDriver();
var response$$ = httpDriver(request$);
response$$.subscribe(function(response$) {
Expand Down
11 changes: 5 additions & 6 deletions test/node.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,15 @@ require('./common')(uri);

// Node.js specific ============================================================
var assert = require('assert');
var Cycle = require('@cycle/core');
var Rx = require('rx');
var Rx = require('@reactivex/rxjs');
var src = require('../lib/index');
var makeHTTPDriver = src.makeHTTPDriver;
var globalSandbox = require('./support/global');

describe('HTTP Driver in Node.js', function () {
it('should auto-execute HTTP request when factory gets eager = true',
function(done) {
var request$ = Rx.Observable.just({
var request$ = Rx.Observable.of({
url: uri + '/pet',
method: 'POST',
send: {name: 'Woof', species: 'Dog'}
Expand All @@ -35,7 +34,7 @@ describe('HTTP Driver in Node.js', function () {

it('should not auto-execute HTTP request by default',
function(done) {
var request$ = Rx.Observable.just({
var request$ = Rx.Observable.of({
url: uri + '/pet',
method: 'POST',
send: {name: 'Woof', species: 'Dog'}
Expand All @@ -52,7 +51,7 @@ describe('HTTP Driver in Node.js', function () {

it('should auto-execute HTTP request if the request has eager = true',
function(done) {
var request$ = Rx.Observable.just({
var request$ = Rx.Observable.of({
url: uri + '/pet',
method: 'POST',
send: {name: 'Woof', species: 'Dog'},
Expand All @@ -72,7 +71,7 @@ describe('HTTP Driver in Node.js', function () {

it('should not auto-execute HTTP request when factory gets eager = false',
function(done) {
var request$ = Rx.Observable.just({
var request$ = Rx.Observable.of({
url: uri + '/pet',
method: 'POST',
send: {name: 'Woof', species: 'Dog'}
Expand Down

0 comments on commit 80e5f69

Please sign in to comment.