diff --git a/spec/subjects/behavior-subject-spec.js b/spec/subjects/behavior-subject-spec.js index 4ba9da772f..6ebb4b9990 100644 --- a/spec/subjects/behavior-subject-spec.js +++ b/spec/subjects/behavior-subject-spec.js @@ -12,6 +12,29 @@ describe('BehaviorSubject', function () { done(); }); + it('should have a getValue() method to retrieve the current value', function () { + var subject = new BehaviorSubject('staltz'); + expect(subject.getValue()).toBe('staltz'); + + subject.next('oj'); + + expect(subject.getValue()).toBe('oj'); + }); + + it('should not allow you to set `value` directly', function () { + var subject = new BehaviorSubject('flibberty'); + subject.value = 'jibbets'; + expect(subject.getValue()).toBe('flibberty'); + expect(subject.value).toBe('flibberty'); + }); + + it('should still allow you to retrieve the value from the value property', function () { + var subject = new BehaviorSubject('fuzzy'); + expect(subject.value).toBe('fuzzy'); + subject.next('bunny'); + expect(subject.value).toBe('bunny'); + }); + it('should start with an initialization value', function (done) { var subject = new BehaviorSubject('foo'); var expected = ['foo', 'bar']; diff --git a/src/subject/BehaviorSubject.ts b/src/subject/BehaviorSubject.ts index b4134b3b85..a6f905d902 100644 --- a/src/subject/BehaviorSubject.ts +++ b/src/subject/BehaviorSubject.ts @@ -3,21 +3,29 @@ import {Subscriber} from '../Subscriber'; import {Subscription} from '../Subscription'; export class BehaviorSubject extends Subject { - constructor(private value: any) { + constructor(private _value: T) { super(); } + getValue(): T { + return this._value; + } + + get value(): T { + return this._value; + } + _subscribe(subscriber: Subscriber): Subscription { const subscription = super._subscribe(subscriber); if (!subscription) { return; } else if (!subscription.isUnsubscribed) { - subscriber.next(this.value); + subscriber.next(this._value); } return subscription; } _next(value: T): void { - super._next(this.value = value); + super._next(this._value = value); } } \ No newline at end of file