Skip to content

Commit

Permalink
fix(holdSubject): Fix holdSubject to buffer without consumer
Browse files Browse the repository at this point in the history
In previous 4.x releases, holdSubject buffered events regardless of whether it had a consumer. This
was broken in a recent fix to another issue and should be fixed to be consistent with previous 4.x
releases.
  • Loading branch information
brandonpayton committed Jul 30, 2016
1 parent 476d031 commit 332827b
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 11 deletions.
7 changes: 5 additions & 2 deletions src/HoldSubjectSource.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,12 @@ export class HoldSubjectSource<T> extends BasicSubjectSource<T> {
}

next (value: T) {
if (!this.active || this.scheduler === void 0) { return; }
if (this.scheduler === void 0) { return; }
const time = this.scheduler.now();
this.buffer = dropAndAppend({time, value}, this.buffer, this.bufferSize);
this._next(time, value);

if (this.active) {
this._next(time, value);
}
}
}
29 changes: 20 additions & 9 deletions test/holdSubject.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,25 +9,37 @@ describe('holdSubject', () => {
})
})

it('should replay the last value', () => {
it('should buffer with consumer and replay last value', () => {
const stream = holdSubject()

stream.observe(() => {})

stream.next(1)
stream.next(2)

setTimeout(() => stream.complete(), 10)
setTimeout(() => stream.complete())

return stream.forEach(x => {
assert.strictEqual(x, 2)
})
return stream
.reduce((x, y) => x.concat(y), [])
.then(x => assert.deepEqual(x, [ 2 ]))
})

it('should buffer without consumer and replay last value', () => {
const stream = holdSubject()

stream.next(1)
stream.next(2)

setTimeout(() => stream.complete())

return stream
.reduce((x, y) => x.concat(y), [])
.then(x => assert.deepEqual(x, [ 2 ]))
})

it('should allow for adjusting bufferSize of stream', () => {
const stream = holdSubject(3)

// Add an observer so the stream begins buffering events
stream.observe(() => {})

stream.next(1)
stream.next(2)
stream.next(3)
Expand All @@ -40,6 +52,5 @@ describe('holdSubject', () => {
.then(x => {
assert.deepEqual(x, [2, 3, 4])
})

})
})

0 comments on commit 332827b

Please sign in to comment.