better tests

This commit is contained in:
2017-11-01 17:01:30 +01:00
parent eb2d3e1b25
commit 4d2ed1260d
4 changed files with 154 additions and 4 deletions

View File

@ -17,6 +17,9 @@ export class ObservableIntake<T> {
// nothing
}
}
private generator = null
private buffered = false
private payloadBuffer = []
constructor () {
this.observable = Observable.create((observerArg) => {
@ -37,14 +40,66 @@ export class ObservableIntake<T> {
}
push (payloadArg: T) {
this.observableFunctions.next(payloadArg)
if (this.buffered) {
this.payloadBuffer.push(payloadArg)
} else {
this.internalPush(payloadArg)
}
}
/**
* pushes many payloads as array
* @param payloadArgArray
*/
pushMany (payloadArgArray: T[]) {
for (let item of payloadArgArray) {
this.push(item)
}
}
/**
* sets a generator to query the next pushed value
* @param generatorArg
*/
setGenerator (generatorArg) {
this.generator = generatorArg
}
makeBuffered() {
this.buffered = true
}
subscribe (...args) {
return this.observable.subscribe(...args)
}
/**
* request the next values in the quantity specified
* @param howManyArg if a generator is set, of a buffer exists, this allows retrieving values
*/
request (howManyArg: number) {
if (howManyArg === 0) {
return
} else {
for (let i = 0; i !== howManyArg; i++) {
if (this.payloadBuffer.length > 0) {
this.internalPush(this.payloadBuffer.shift())
} else {
const nextPayload = this.generator()
this.internalPush(nextPayload)
}
}
}
}
/**
* signals the completion of this observable
*/
signalComplete () {
this.observableFunctions.complete()
}
private internalPush (payloadArg) {
this.observableFunctions.next(payloadArg)
}
}