I am using a library called observable-worker to conceptually turn a webworker into an RxJS observable. The worker will perform an intensive calculation that could take anywhere from a 300ms to 10 minutes depending on user input. The observable would emit members of an object called BuildRecipe I defined. This had been going fine until I wanted to expand the functionality a bit.
I wanted to display a number that increased with each step in the calculation. But with the observable-worker package my only access to the object containing the algorithm is through the observable it returns. I decided I would create two streams: one of type BuildRecipe and one of type number, merge them together and return that stream through the observable-worker functions. The stream of type number is called counter$. The stream of type BuildRecipe is called build$. getBuildStream returns an observable of type Observable<BuildRecipe>. workUnit is a function of an interface I get from observable-worker and it is the function that will be run on the created web-worker.
workUnit(input: InputChainData): Observable<BuildRecipe | number> {
let counter$ = of(this.counter).pipe(delay(1000), repeat(100))
let build$ = this.getBuildStream(userInput)
return merge(counter$, build$)
}
this.counter is a number I increment at different points during the runtime of this.getBuilderStream(). The class implementing the observable-worker interface is located in my builder.worker file, while I display the information from the worker in my builder.component.ts file.