Last active
June 6, 2024 08:24
-
-
Save maksimr/8c3e415208234b41bbb2e9350a03827c to your computer and use it in GitHub Desktop.
rxjs-1
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// @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