From 5a16a36a0a45e8acfe459ad8f947ca5c566f73ec Mon Sep 17 00:00:00 2001 From: Tylor Steinberger Date: Tue, 20 Oct 2015 12:37:38 -0400 Subject: [PATCH 1/4] Rx 5.0.0-alpha.6 --- package.json | 5 ++--- src/http-driver.js | 17 ++++++++--------- test/common.js | 19 ++++++++++--------- test/node.js | 11 +++++------ 4 files changed, 25 insertions(+), 27 deletions(-) diff --git a/package.json b/package.json index c185924..69608a6 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/src/http-driver.js b/src/http-driver.js index debbfb0..5c2fd2f 100644 --- a/src/http-driver.js +++ b/src/http-driver.js @@ -1,4 +1,4 @@ -const Rx = require(`rx`) +const Rx = require(`@reactivex/rxjs`) const superagent = require(`superagent`) function optionsToSuperagent({ @@ -73,7 +73,7 @@ function createResponse$(reqOptions) { } else if (typeof reqOptions === `object`) { request = optionsToSuperagent(reqOptions) } 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 } @@ -81,14 +81,14 @@ function createResponse$(reqOptions) { 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() { @@ -103,14 +103,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(null, 1) response$.connect() } response$.request = reqOptions return response$ }) - .replay(null, 1) - response$$.connect() + .publishReplay(null, 1).refCount() return response$$ } } diff --git a/test/common.js b/test/common.js index 3f2d94b..e900b16 100644 --- a/test/common.js +++ b/test/common.js @@ -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) { @@ -18,12 +17,13 @@ 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( function onNext() { assert.fail(); }, function onError(err) { + debugger; assert.strictEqual(err.message, 'Observable of requests given to ' + 'HTTP Driver must emit either URL strings or objects with ' + 'parameters.' @@ -36,7 +36,7 @@ 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( @@ -47,14 +47,15 @@ function run(uri) { 'options.' ); done(); - } + }, + function complete() { assert.fail(); } ); } ); 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$) { @@ -70,7 +71,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'} @@ -93,7 +94,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'} @@ -117,7 +118,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$) { diff --git a/test/node.js b/test/node.js index 943d3d3..73e6a4f 100644 --- a/test/node.js +++ b/test/node.js @@ -7,8 +7,7 @@ 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'); @@ -16,7 +15,7 @@ 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'} @@ -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'} @@ -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'}, @@ -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'} From ba1512bca82d4d24b0ff8ee5428c004c7714788c Mon Sep 17 00:00:00 2001 From: Tylor Steinberger Date: Wed, 21 Oct 2015 11:24:59 -0400 Subject: [PATCH 2/4] Fixed last failing test --- src/http-driver.js | 14 +++++++++----- test/common.js | 22 +++++++++++----------- 2 files changed, 20 insertions(+), 16 deletions(-) diff --git a/src/http-driver.js b/src/http-driver.js index 5c2fd2f..edf6f9d 100644 --- a/src/http-driver.js +++ b/src/http-driver.js @@ -71,7 +71,12 @@ 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.error(new Error(`Observable of requests given to HTTP ` + `Driver must emit either URL strings or objects with parameters.`)) @@ -103,13 +108,12 @@ function makeHTTPDriver({eager = false} = {eager: false}) { .map(reqOptions => { let response$ = createResponse$(reqOptions) if (eager || reqOptions.eager) { - response$ = response$.publishReplay(null, 1) - response$.connect() + response$ = response$.publishReplay(null, 1).connect() + return response$ } response$.request = reqOptions return response$ - }) - .publishReplay(null, 1).refCount() + }).shareReplay(null, 1) return response$$ } } diff --git a/test/common.js b/test/common.js index e900b16..ec09a37 100644 --- a/test/common.js +++ b/test/common.js @@ -39,17 +39,17 @@ function run(uri) { 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(); - }, - function complete() { assert.fail(); } - ); + response$$.mergeAll() + .subscribe( + function onNext() { assert.fail(); }, + function onError(err) { + assert.strictEqual( + err.message, 'Please provide a `url` property in the request ' + + 'options.' + ); + done(); + } + ); } ); From dd99b98f890b6219093b8103606493bbf455b516 Mon Sep 17 00:00:00 2001 From: Tylor Steinberger Date: Wed, 21 Oct 2015 12:05:46 -0400 Subject: [PATCH 3/4] fixes --- .eslintrc | 2 +- src/http-driver.js | 3 ++- test/common.js | 1 - 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/.eslintrc b/.eslintrc index 8230e1e..1eee6bd 100644 --- a/.eslintrc +++ b/.eslintrc @@ -3,5 +3,5 @@ "env": { "browser": true, "node": true - } + }, } diff --git a/src/http-driver.js b/src/http-driver.js index edf6f9d..a3d4bde 100644 --- a/src/http-driver.js +++ b/src/http-driver.js @@ -108,7 +108,8 @@ function makeHTTPDriver({eager = false} = {eager: false}) { .map(reqOptions => { let response$ = createResponse$(reqOptions) if (eager || reqOptions.eager) { - response$ = response$.publishReplay(null, 1).connect() + response$ = response$.publishReplay(null, 1) + response$.connect() return response$ } response$.request = reqOptions diff --git a/test/common.js b/test/common.js index ec09a37..52f8d70 100644 --- a/test/common.js +++ b/test/common.js @@ -23,7 +23,6 @@ function run(uri) { response$$.mergeAll().subscribe( function onNext() { assert.fail(); }, function onError(err) { - debugger; assert.strictEqual(err.message, 'Observable of requests given to ' + 'HTTP Driver must emit either URL strings or objects with ' + 'parameters.' From fec447591a06ea81bae90e014473cdd38c331559 Mon Sep 17 00:00:00 2001 From: Tylor Steinberger Date: Wed, 21 Oct 2015 12:52:48 -0400 Subject: [PATCH 4/4] Use publishReplay(1) --- .eslintrc | 2 +- src/http-driver.js | 5 +++-- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/.eslintrc b/.eslintrc index 1eee6bd..8230e1e 100644 --- a/.eslintrc +++ b/.eslintrc @@ -3,5 +3,5 @@ "env": { "browser": true, "node": true - }, + } } diff --git a/src/http-driver.js b/src/http-driver.js index a3d4bde..75f52e1 100644 --- a/src/http-driver.js +++ b/src/http-driver.js @@ -108,13 +108,14 @@ function makeHTTPDriver({eager = false} = {eager: false}) { .map(reqOptions => { let response$ = createResponse$(reqOptions) if (eager || reqOptions.eager) { - response$ = response$.publishReplay(null, 1) + response$ = response$.publishReplay(1) response$.connect() return response$ } response$.request = reqOptions return response$ - }).shareReplay(null, 1) + }).publishReplay(1) + response$$.connect() return response$$ } }