Skip to content

Commit

Permalink
Allow Kefir observables to be consumed by ES7 observable implementations
Browse files Browse the repository at this point in the history
  • Loading branch information
lautis committed Sep 26, 2015
1 parent 525be25 commit 068bf15
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 2 deletions.
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
},
"license": "MIT",
"devDependencies": {
"@reactivex/rxjs": "^5.0.0-alpha.1",
"babel-core": "5.8.23",
"babel-loader": "5.3.2",
"baconjs": "0.7.71",
Expand Down Expand Up @@ -57,6 +58,7 @@
"transducers-js": "0.4.174",
"transducers.js": "0.3.2",
"webpack": "1.12.1",
"webpack-dev-server": "1.10.1"
"webpack-dev-server": "1.10.1",
"zen-observable": "^0.1.3"
}
}
17 changes: 17 additions & 0 deletions src/observable.js
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,22 @@ extend(Observable.prototype, {
return this._on(ANY, fn);
},

subscribe(observer) {
let stream = this.takeErrors(1);
let fn = function(event) {
if (event.type === "value" && observer.next) {
observer.next(event.value);
} else if (event.type == "error" && observer.error) {
observer.error(event.value);
} else if (event.type === "end" && observer.complete) {
observer.complete(event.value);
}
}

stream.onAny(fn);
return () => stream.offAny(fn);
},

offValue(fn) {
return this._off(VALUE, fn);
},
Expand Down Expand Up @@ -166,6 +182,7 @@ extend(Observable.prototype, {

});

Observable.prototype[require('./utils/symbol')('observable')] = function() { return this; }

// extend() can't handle `toString` in IE8
Observable.prototype.toString = function() {
Expand Down
9 changes: 9 additions & 0 deletions src/utils/symbol.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
module.exports = function(key) {
if (typeof Symbol !== 'undefined' && Symbol[key]) {
return Symbol[key];
} else if (typeof Symbol !== 'undefined' && typeof Symbol.for === 'function') {
return Symbol.for(key);
} else {
return '@@' + key;
}
}
24 changes: 24 additions & 0 deletions test/specs/es-observable.coffee
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
Observable = require('zen-observable')
{stream, prop, send, Kefir} = require('../test-helpers.coffee')

describe '[Symbol.observable]', ->
it 'outputs a compatible Observable', (done) ->
a = stream()
values = []
observable = Observable.from(a)
observable.subscribe
next: (x) ->
values.push(x)
complete: (x) ->
expect(values).toEqual([1, 2, 3])
done()
send(a, [1, 2, 3, '<end>'])

it 'unsubscribes stream after an error', ->
a = stream()
values = []
observable = a[Symbol.observable]()
observable.subscribe(next: (x) -> values.push(x))

send(a, [1, {error: 2}, 3])
expect(values).toEqual([1])
2 changes: 1 addition & 1 deletion test/test-helpers.coffee
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
Observable = require('zen-observable') # Activate Symbol polyfill
Kefir = require("../dist/kefir")
sinon = require('sinon')

Expand Down Expand Up @@ -255,4 +256,3 @@ beforeEach ->
return !@isNot

}

0 comments on commit 068bf15

Please sign in to comment.