From ecec64066eb910139a797a29e7ae579a6ea7e12c Mon Sep 17 00:00:00 2001 From: Ben Lesh Date: Mon, 3 Oct 2016 15:50:25 -0700 Subject: [PATCH 1/2] fix(bufferTime): no errors with take after bufferTime with maxBufferSize Ensures that a buffer context is not created if the subscriber is closed fixes #1944 --- spec/operators/bufferTime-spec.ts | 19 +++++++++++++++++++ src/operator/bufferTime.ts | 2 +- 2 files changed, 20 insertions(+), 1 deletion(-) diff --git a/spec/operators/bufferTime-spec.ts b/spec/operators/bufferTime-spec.ts index 4d637f4ad92..defed5bd0c3 100644 --- a/spec/operators/bufferTime-spec.ts +++ b/spec/operators/bufferTime-spec.ts @@ -300,4 +300,23 @@ describe('Observable.prototype.bufferTime', () => { expectObservable(result).toBe(expected, values); expectSubscriptions(e1.subscriptions).toBe(subs); }); + + it('should not have errors when take follows and maxBufferSize is provided', () => { + const tick = 10; + const bufferTime = 50; + const expected = '-----a----b----c----d----(e|)'; + const values = { + a: [0, 1, 2, 3], + b: [4, 5, 6, 7, 8], + c: [9, 10, 11, 12, 13], + d: [14, 15, 16, 17, 18], + e: [19, 20, 21, 22, 23] + }; + + const source = Rx.Observable.interval(tick, rxTestScheduler) + .bufferTime(bufferTime, null, 10, rxTestScheduler) + .take(5); + + expectObservable(source).toBe(expected, values); + }); }); \ No newline at end of file diff --git a/src/operator/bufferTime.ts b/src/operator/bufferTime.ts index 911ba5576bf..9eb06d84fda 100644 --- a/src/operator/bufferTime.ts +++ b/src/operator/bufferTime.ts @@ -174,7 +174,7 @@ class BufferTimeSubscriber extends Subscriber { closeAction.unsubscribe(); this.remove(closeAction); - if (this.timespanOnly) { + if (!this.closed && this.timespanOnly) { context = this.openContext(); const bufferTimeSpan = this.bufferTimeSpan; const timeSpanOnlyState = { subscriber: this, context, bufferTimeSpan }; From eec927eebed58aa34c52e87f110b16a32a247266 Mon Sep 17 00:00:00 2001 From: OJ Kwon Date: Mon, 3 Oct 2016 20:45:34 -0700 Subject: [PATCH 2/2] refactor(TimeoutError): timeout throws typed TimeoutError closes #1997 --- spec/operators/timeout-spec.ts | 2 +- src/Rx.ts | 1 + src/operator/timeout.ts | 11 ++++++----- src/util/TimeoutError.ts | 15 +++++++++++++++ 4 files changed, 23 insertions(+), 6 deletions(-) create mode 100644 src/util/TimeoutError.ts diff --git a/spec/operators/timeout-spec.ts b/spec/operators/timeout-spec.ts index 82b81d81df8..95e66bde9b9 100644 --- a/spec/operators/timeout-spec.ts +++ b/spec/operators/timeout-spec.ts @@ -6,7 +6,7 @@ const Observable = Rx.Observable; /** @test {timeout} */ describe('Observable.prototype.timeout', () => { - const defaultTimeoutError = new Error('timeout'); + const defaultTimeoutError = new Rx.TimeoutError(); asDiagram('timeout(50)')('should timeout after a specified timeout period', () => { const e1 = cold('-------a--b--|'); diff --git a/src/Rx.ts b/src/Rx.ts index 39d64953fb0..cc6322359fe 100644 --- a/src/Rx.ts +++ b/src/Rx.ts @@ -155,6 +155,7 @@ export {Notification} from './Notification'; export {EmptyError} from './util/EmptyError'; export {ArgumentOutOfRangeError} from './util/ArgumentOutOfRangeError'; export {ObjectUnsubscribedError} from './util/ObjectUnsubscribedError'; +export {TimeoutError} from './util/TimeoutError'; export {UnsubscriptionError} from './util/UnsubscriptionError'; export {TimeInterval} from './operator/timeInterval'; export {Timestamp} from './operator/timestamp'; diff --git a/src/operator/timeout.ts b/src/operator/timeout.ts index 94c64597dcf..d7d52aa5ed3 100644 --- a/src/operator/timeout.ts +++ b/src/operator/timeout.ts @@ -5,6 +5,7 @@ import { Subscriber } from '../Subscriber'; import { Scheduler } from '../Scheduler'; import { Observable } from '../Observable'; import { TeardownLogic } from '../Subscription'; +import { TimeoutError } from '../util/TimeoutError'; /** * @param due @@ -80,7 +81,7 @@ class TimeoutSubscriber extends Subscriber { this._previousIndex = currentIndex; } - protected _next(value: T) { + protected _next(value: T): void { this.destination.next(value); if (!this.absoluteTimeout) { @@ -88,17 +89,17 @@ class TimeoutSubscriber extends Subscriber { } } - protected _error(err: any) { + protected _error(err: any): void { this.destination.error(err); this._hasCompleted = true; } - protected _complete() { + protected _complete(): void { this.destination.complete(); this._hasCompleted = true; } - notifyTimeout() { - this.error(this.errorToSend || new Error('timeout')); + notifyTimeout(): void { + this.error(this.errorToSend || new TimeoutError()); } } diff --git a/src/util/TimeoutError.ts b/src/util/TimeoutError.ts new file mode 100644 index 00000000000..c68adffac46 --- /dev/null +++ b/src/util/TimeoutError.ts @@ -0,0 +1,15 @@ +/** + * An error thrown when duetime elapses. + * + * @see {@link timeout} + * + * @class TimeoutError + */ +export class TimeoutError extends Error { + constructor() { + const err: any = super('Timeout has occurred'); + ( this).name = err.name = 'TimeoutError'; + ( this).stack = err.stack; + ( this).message = err.message; + } +}