Skip to content

Commit

Permalink
feat(Subject): implement asObservable
Browse files Browse the repository at this point in the history
  • Loading branch information
kwonoj committed Jan 29, 2016
1 parent d026c41 commit 7119e4b
Show file tree
Hide file tree
Showing 2 changed files with 91 additions and 1 deletion.
80 changes: 79 additions & 1 deletion spec/Subject-spec.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* globals describe, it, expect */
/* globals describe, it, expect, hot, expectObservable */
var Rx = require('../dist/cjs/Rx');

var Subject = Rx.Subject;
Expand Down Expand Up @@ -508,4 +508,82 @@ describe('Subject', function () {

source.subscribe(subject);
});

describe('asObservable', function () {
it('should hide subject', function () {
var subject = new Rx.Subject();
var observable = subject.asObservable();

expect(subject).not.toEqual(observable);

expect(observable instanceof Observable).toBe(true);
expect(observable instanceof Subject).toBe(false);
});

it('should handle subject never emits', function () {
var observable = hot('-').asObservable();

expectObservable(observable).toBe([]);
});

it('should handle subject completes without emits', function () {
var observable = hot('--^--|').asObservable();
var expected = '---|';

expectObservable(observable).toBe(expected);
});

it('should handle subject throws', function () {
var observable = hot('--^--#').asObservable();
var expected = '---#';

expectObservable(observable).toBe(expected);
});

it('should handle subject emits', function () {
var observable = hot('--^--x--|').asObservable();
var expected = '---x--|';

expectObservable(observable).toBe(expected);
});

it('should work with inherited subject', function (done) {
var subject = new Rx.AsyncSubject();

subject.next(42);
subject.complete();

var observable = subject.asObservable();

var expected = [new Rx.Notification('N', 42),
new Rx.Notification('C')];

observable.materialize().subscribe(function (x) {
expect(x).toEqual(expected.shift());
}, function (err) {
done.fail(err);
}, function () {
expect(expected).toEqual([]);
done();
});
});

it('should not eager', function () {
var subscribed = false;

var subject = new Rx.Subject(new Rx.Observable(function (observer) {
subscribed = true;
var subscription = Rx.Observable.of('x').subscribe(observer);
return function () {
subscription.unsubscribe();
};
}));

var observable = subject.asObservable();
expect(subscribed).toBe(false);

observable.subscribe();
expect(subscribed).toBe(true);
});
});
});
12 changes: 12 additions & 0 deletions src/Subject.ts
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,11 @@ export class Subject<T> extends Observable<T> implements Observer<T>, Subscripti
this._complete();
}

asObservable(): Observable<T> {
const observable = new SubjectObservable(this);
return observable;
}

protected _next(value: T): void {
if (this.destination) {
this.destination.next(value);
Expand Down Expand Up @@ -203,3 +208,10 @@ export class Subject<T> extends Observable<T> implements Observer<T>, Subscripti
return new Subscriber<T>(this);
}
}

class SubjectObservable<T> extends Observable<T> {
constructor(source: Subject<T>) {
super();
this.source = source;
}
}

0 comments on commit 7119e4b

Please sign in to comment.