Skip to content

Commit

Permalink
better observable dependency handling and fix baqend distribution
Browse files Browse the repository at this point in the history
  • Loading branch information
fbuecklers committed Nov 8, 2016
1 parent fcde813 commit c35c256
Show file tree
Hide file tree
Showing 8 changed files with 52 additions and 20 deletions.
8 changes: 4 additions & 4 deletions Gruntfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
Expand All @@ -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']
}
}
},
Expand Down
10 changes: 8 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -75,6 +75,12 @@ You can use the unpkg CDN:
<script type="text/javascript" src="//www.baqend.com/js-sdk/latest/baqend-streaming.js"></script>
```

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
----------------

Expand Down
2 changes: 2 additions & 0 deletions lib/index.js
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
require('../polyfills');

module.exports = require('./baqend');

1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
2 changes: 1 addition & 1 deletion spec/guide.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
1 change: 1 addition & 0 deletions streaming/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
34 changes: 34 additions & 0 deletions streaming/observable.js
Original file line number Diff line number Diff line change
@@ -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;
}
}
14 changes: 2 additions & 12 deletions streaming/query/Stream.js
Original file line number Diff line number Diff line change
@@ -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<T>
Expand All @@ -24,7 +14,7 @@ class Stream {
* @returns {Observable<T>} an RxJS observable
*/
observable() {
return Observable.create(observer => {
return new lib.Observable(observer => {
var callback = (e) => {
if (e.matchType === 'error') {
observer.error(e);
Expand Down

0 comments on commit c35c256

Please sign in to comment.