Skip to content

Instantly share code, notes, and snippets.

@maksimr
Last active June 6, 2024 08:24
Show Gist options
  • Save maksimr/8c3e415208234b41bbb2e9350a03827c to your computer and use it in GitHub Desktop.
Save maksimr/8c3e415208234b41bbb2e9350a03827c to your computer and use it in GitHub Desktop.
rxjs-1
// @ts-check
import { Observable, of, from, reduce, mergeMap } from 'rxjs'
type IObjectNode = { value: number };
const arr1 = new Observable<IObjectNode[]>(subscriber => {
setInterval(() => {
subscriber.next([
createObjectNode(),
createObjectNode()
]);
}, 1000);
});
const arr2 = arr1.pipe(mergeMap((obj) => {
if (whatif(obj)) {
return of(obj);
}
return f(obj).pipe(reduce((acc, it) => {
acc.push(it);
return acc;
}, [] as IObjectNode[]));
}));
arr2.subscribe(console.log);
function createObjectNode(): IObjectNode {
const uuid = Math.random();
return { value: uuid };
}
function f(x: IObjectNode[]): Observable<IObjectNode> {
return from(x);
}
function whatif(ob: IObjectNode[]) {
// @ts-ignore
whatif.i ??= 0;
// @ts-ignore
whatif.i++;
// @ts-ignore
return whatif.i % 2;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment