From c35c256c9eb70080f92344d4cc1b57ae294a6c47 Mon Sep 17 00:00:00 2001 From: fb Date: Wed, 9 Nov 2016 00:05:44 +0100 Subject: [PATCH] better observable dependency handling and fix baqend distribution --- Gruntfile.js | 8 ++++---- README.md | 10 ++++++++-- lib/index.js | 2 ++ package.json | 1 - spec/guide.spec.js | 2 +- streaming/index.js | 1 + streaming/observable.js | 34 ++++++++++++++++++++++++++++++++++ streaming/query/Stream.js | 14 ++------------ 8 files changed, 52 insertions(+), 20 deletions(-) create mode 100644 streaming/observable.js diff --git a/Gruntfile.js b/Gruntfile.js index 1a4bd16f..d01efc07 100644 --- a/Gruntfile.js +++ b/Gruntfile.js @@ -26,7 +26,7 @@ module.exports = function (grunt) { browserifyOptions: browserifyOptions, banner: longBanner, plugin: ['./scripts/babel-helper', 'bundle-collapser/plugin', 'browserify-derequire'], - exclude: ['rxjs/Observable'], + exclude: ['rxjs/Observable'], //never bundle rxjs transform: [ ['babelify', { "plugins": ["external-helpers"] @@ -50,14 +50,14 @@ module.exports = function (grunt) { test: { files: { - 'build/baqend.js': ['polyfills/index.js', 'streaming/index.js'] + 'build/baqend.js': ['streaming/index.js'] } }, dist: { files: { - 'dist/baqend.js': ['polyfills/index.js', 'lib/index.js'], - 'dist/baqend-streaming.js': ['polyfills/index.js', 'streaming/index.js'] + 'dist/baqend.js': ['lib/index.js'], + 'dist/baqend-streaming.js': ['streaming/index.js'] } } }, diff --git a/README.md b/README.md index 12e665eb..bba374f9 100644 --- a/README.md +++ b/README.md @@ -65,8 +65,8 @@ Baqend Streaming SDK If you want to use the realtime streaming queries, you have to either use `baqend-streaming.js` or `baqend-streaming.min.js` for production. -In Addition, you must include [Rx.js](https://github.com/ReactiveX/rxjs) v5 into your project. -As minimal setup, the rxjs Observable is required. +In Addition, you can include [Rx.js](https://github.com/ReactiveX/rxjs) v5 into your project, for many advanced +Observable features. You can use the unpkg CDN: ```html @@ -75,6 +75,12 @@ You can use the unpkg CDN: ``` +The SDK is shipped with the core-js Observable shim per default. +If you include Rx.js globally, it will be detected and used by the SDK automatically. +You can also set the Observable implementation which the SDK will use, +by setting the `require('baqend/streaming').Observable = Observable` afterwards. + + Usage in Node.js ---------------- diff --git a/lib/index.js b/lib/index.js index a5e55456..fe556628 100644 --- a/lib/index.js +++ b/lib/index.js @@ -1,2 +1,4 @@ +require('../polyfills'); + module.exports = require('./baqend'); diff --git a/package.json b/package.json index 81eaa576..b1bf8c9f 100644 --- a/package.json +++ b/package.json @@ -63,7 +63,6 @@ "grunt-mocha-test": "^0.12.7", "grunt-run": "^0.6.0", "grunt-template": "^1.0.0", - "grunt-umd": "^2.3.6", "jquery": "^3.1.0", "jsdoc": "^3.4.0", "karma": "^1.2.0", diff --git a/spec/guide.spec.js b/spec/guide.spec.js index 9e27dee9..5b331c3d 100644 --- a/spec/guide.spec.js +++ b/spec/guide.spec.js @@ -299,7 +299,7 @@ describe("Guide Examples", function() { console.log("checking event attributes"); // expect(events[0].index).to.be.equal(0); - expect(events[0].matchType).to.be.equal('match'); + expect(events[0].matchType).to.be.equal('add'); expect(events[0].operation).to.be.equal('none'); expect(!events[0].initial); expect(events[1].index).to.be.equal(1); diff --git a/streaming/index.js b/streaming/index.js index 2f1055a4..4621fba6 100644 --- a/streaming/index.js +++ b/streaming/index.js @@ -2,6 +2,7 @@ var db = require('../lib'); module.exports = exports = db; +exports.Observable = require('./observable'); exports.connector.WebSocketConnector = require('./connector/WebSocketConnector'); exports.query.Node = require('./query/Node'); exports.query.Stream = require('./query/Stream'); diff --git a/streaming/observable.js b/streaming/observable.js new file mode 100644 index 00000000..2f3ead08 --- /dev/null +++ b/streaming/observable.js @@ -0,0 +1,34 @@ +/* + * loads the observalbe from the global context, the global Rx variable or try to load Rx.js, fallback to core-js shim + * The Observable can be overwritten by setting the require('baqend/streaming').Observable = Observable afterwards + */ + +if (typeof Observable != 'undefined') { + module.exports = Observable; +} else if (typeof Rx !== 'undefined') { + module.exports = Rx.Observable; +} else { + try { + module.exports = require('rxjs/Observable').Observable; + } catch (e) { + require('core-js/modules/es7.symbol.observable'); + require('core-js/modules/es7.observable'); + var sub = Observable.prototype.subscribe; + + //patch subscribe until core-js implements the new proposal + //https://github.com/zloirock/core-js/issues/257 + Observable.prototype.subscribe = function(onNext, onError, onComplete) { + if (onNext instanceof Function) { + return sub.call(this, { + next: onNext, + error: onError, + complete: onComplete + }); + } else { + return sub.call(this, onNext); + } + }; + + module.exports = Observable; + } +} \ No newline at end of file diff --git a/streaming/query/Stream.js b/streaming/query/Stream.js index 6cdc7a98..8adcce8e 100644 --- a/streaming/query/Stream.js +++ b/streaming/query/Stream.js @@ -1,17 +1,7 @@ "use strict"; var Metadata = require('../../lib/util/Metadata'); var WebSocketConnector = require('../connector/WebSocketConnector'); -var Observable; - -try { - Observable = require('rxjs/Observable').Observable; -} catch (e) { - if (typeof Rx !== 'undefined') { - Observable = Rx.Observable; - } else { - throw e; - } -} +var lib = require('../../lib'); /** * @alias query.Stream @@ -24,7 +14,7 @@ class Stream { * @returns {Observable} an RxJS observable */ observable() { - return Observable.create(observer => { + return new lib.Observable(observer => { var callback = (e) => { if (e.matchType === 'error') { observer.error(e);