-
Notifications
You must be signed in to change notification settings - Fork 97
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add overlapping option to flatMap #320
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,12 +5,13 @@ import {concat, forEach, findByPred, find, remove, cloneArray} from '../utils/co | |
|
||
const id = x => x | ||
|
||
function AbstractPool({queueLim = 0, concurLim = -1, drop = 'new'} = {}) { | ||
function AbstractPool({queueLim = 0, concurLim = -1, drop = 'new', overlapping = false} = {}) { | ||
Stream.call(this) | ||
|
||
this._queueLim = queueLim < 0 ? -1 : queueLim | ||
this._concurLim = concurLim < 0 ? -1 : concurLim | ||
this._drop = drop | ||
this._overlapping = overlapping | ||
this._queue = [] | ||
this._curSources = [] | ||
this._$handleSubAny = event => this._handleSubAny(event) | ||
|
@@ -25,16 +26,21 @@ function AbstractPool({queueLim = 0, concurLim = -1, drop = 'new'} = {}) { | |
inherit(AbstractPool, Stream, { | ||
_name: 'abstractPool', | ||
|
||
_add(obj, toObs /* Function | falsey */) { | ||
_add(obj, toObs /* Function | falsey */, allowOverflow) { | ||
toObs = toObs || id | ||
if (this._concurLim === -1 || this._curSources.length < this._concurLim) { | ||
this._addToCur(toObs(obj)) | ||
} else { | ||
if (this._queueLim === -1 || this._queue.length < this._queueLim) { | ||
if (this._queueLim === -1 || this._queue.length < this._queueLim || allowOverflow) { | ||
this._addToQueue(toObs(obj)) | ||
} else if (this._drop === 'old') { | ||
this._removeOldest() | ||
this._add(obj, toObs) | ||
if (this._overlapping) { | ||
this._add(obj, toObs, true) | ||
this._removeOldest(true) | ||
} else { | ||
this._removeOldest() | ||
this._add(obj, toObs) | ||
} | ||
} | ||
} | ||
}, | ||
|
@@ -148,8 +154,8 @@ inherit(AbstractPool, Stream, { | |
return index | ||
}, | ||
|
||
_removeCur(obs) { | ||
if (this._active) { | ||
_removeCur(obs, after) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't like passing a boolean, not sure about performance impact, but wouldn't it be better to have two methods?
|
||
if (!after && this._active) { | ||
this._unsubscribe(obs) | ||
} | ||
let index = find(this._curSources, obs) | ||
|
@@ -161,11 +167,14 @@ inherit(AbstractPool, Stream, { | |
this._onEmpty() | ||
} | ||
} | ||
if (after && this._active) { | ||
this._unsubscribe(obs) | ||
} | ||
return index | ||
}, | ||
|
||
_removeOldest() { | ||
this._removeCur(this._curSources[0]) | ||
_removeOldest(after) { | ||
this._removeCur(this._curSources[0], after) | ||
}, | ||
|
||
_pullQueue() { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
const sinon = require('sinon') | ||
const {stream, prop, send, value, error, end, activate, deactivate, Kefir, expect} = require('../test-helpers') | ||
|
||
describe('flatMap', () => { | ||
|
@@ -239,4 +240,90 @@ describe('flatMap', () => { | |
expect(result).to.flowErrors(c) | ||
}) | ||
}) | ||
|
||
describe('overlapping with a concurrency limit that has maxed out', () => { | ||
describe('and with a queue limit', () => { | ||
it('not maxed out, should add to the queue', () => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. All these "should add to the queue" tests are kinda silly because I can't really confirm that the queue has changed... not sure how to improve these tests. |
||
onDeactivate = sinon.spy() | ||
a = stream() | ||
b = Kefir.stream(() => onDeactivate) | ||
map = a.flatMap({concurLim: 1, drop: 'old', overlapping: true, queueLim: 1}) | ||
activate(map) | ||
send(a, [value(b)]) // added to current | ||
send(a, [value(b)]) // added to queue | ||
deactivate(map) | ||
expect(onDeactivate.callCount).to.equal(1) | ||
}) | ||
|
||
it('maxed out, should add the next stream before removing the previous', () => { | ||
onDeactivate = sinon.spy() | ||
a = stream() | ||
b = Kefir.stream(() => onDeactivate) | ||
map = a.flatMap({concurLim: 1, drop: 'old', overlapping: true, queueLim: 1}) | ||
activate(map) | ||
send(a, [value(b)]) // added to current | ||
send(a, [value(b)]) // added to queue | ||
send(a, [value(b)]) // added to queue (overflow) | ||
deactivate(map) | ||
expect(onDeactivate.callCount).to.equal(1) | ||
}) | ||
}) | ||
|
||
describe('and without a queue limit', () => { | ||
it('should add to the queue', () => { | ||
onDeactivate = sinon.spy() | ||
a = stream() | ||
b = Kefir.stream(() => onDeactivate) | ||
map = a.flatMap({concurLim: 1, drop: 'old', overlapping: true, queueLim: -1}) | ||
activate(map) | ||
send(a, [value(b)]) // added to current | ||
send(a, [value(b)]) // replaced current | ||
deactivate(map) | ||
expect(onDeactivate.callCount).to.equal(1) | ||
}) | ||
}) | ||
}) | ||
|
||
describe('non-overlapping with a concurrency limit that has maxed out', () => { | ||
describe('and with a queue limit', () => { | ||
it('not maxed out, should add to the queue', () => { | ||
onDeactivate = sinon.spy() | ||
a = stream() | ||
b = Kefir.stream(() => onDeactivate) | ||
map = a.flatMap({concurLim: 1, drop: 'old', overlapping: false, queueLim: 1}) | ||
activate(map) | ||
send(a, [value(b)]) // added to current | ||
send(a, [value(b)]) // added to queue | ||
deactivate(map) | ||
expect(onDeactivate.callCount).to.equal(1) | ||
}) | ||
|
||
it('maxed out, should remove the previous stream before adding the next', () => { | ||
onDeactivate = sinon.spy() | ||
a = stream() | ||
b = Kefir.stream(() => onDeactivate) | ||
map = a.flatMap({concurLim: 1, drop: 'old', overlapping: false, queueLim: 1}) | ||
activate(map) | ||
send(a, [value(b)]) // added to current | ||
send(a, [value(b)]) // added to queue | ||
send(a, [value(b)]) // added to queue (overflow) | ||
deactivate(map) | ||
expect(onDeactivate.callCount).to.equal(2) | ||
}) | ||
}) | ||
|
||
describe('and without a queue limit', () => { | ||
it('should add to the queue', () => { | ||
onDeactivate = sinon.spy() | ||
a = stream() | ||
b = Kefir.stream(() => onDeactivate) | ||
map = a.flatMap({concurLim: 1, drop: 'old', overlapping: false, queueLim: -1}) | ||
activate(map) | ||
send(a, [value(b)]) // added to current | ||
send(a, [value(b)]) // replaced current | ||
deactivate(map) | ||
expect(onDeactivate.callCount).to.equal(1) | ||
}) | ||
}) | ||
}) | ||
}) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If only we had object rest spread... 😬