I am trying to use Rxjs with a small Node program that will do some I/O operations. Void operations (such as writeFile) have been done as Observables without any emitted values. It can be represented in a marble diagram as:
writeFile (success) ---------|
writeFile (error) ---------X
I am trying to do something like the following:
removeDir -------|
writeFiles -------|
transformFiles -------|
To accomplish these operations in the correct order, I have code that looks something like this:
// helpers.ts
import { Observable } from '@reactivex/rxjs';
export function removeDir(dir: string): Observable<void> { ... }
export function writeFiles(fromDir: string, toDir: string): Observable<void> { ... }
export function transformFiles(dir: string): Observable<void> { ... }
// main.ts
import { Observable } from '@reactivex/rxjs';
import { removeDir, writeFiles, transformFiles } from './helpers';
const filesReady = removeDir('output')
.concat(Observable.defer(() => writeFiles('input', 'output')))
.concat(Observable.defer(() => transformFiles('output')));
filesReady.subscribe({
error(err) {
// Something went wrong!
}
complete() {
// Do something when files are ready
}
})
This example is a bit simplified, but I run into issues in places where I want to compose Observables together. Many Rxjs operators does not work well with only complete events.
Is there a better way to compose Rxjs with I/O events? I find some concepts around Rxjs confusing, so any help is greatly appreciated.
via Csvn
No comments:
Post a Comment