fork
Creates a new stream by forking from the input Stream or Observable. The new stream subscribes to the input stream and emits the same values.
Type
typescript
type fork: <T>(arg$: Stream<T> | Observable<T>, autoUnsubscribe?: boolean) => Stream<T>;
Parameters
arg$
: Input Stream or Observable instanceautoUnsubscribe
: Optional, defaults totrue
. Controls whether the forked stream automatically unsubscribes when the input stream unsubscribestrue
: When the input stream unsubscribes, the forked stream also unsubscribes automaticallyfalse
: When the input stream unsubscribes, the forked stream does not automatically unsubscribe
Details
- The fork operator creates a new stream that subscribes to the input stream
- The new stream emits exactly the same values as the input stream (including resolved and rejected values)
- When
autoUnsubscribe
istrue
, after the input stream unsubscribes, the new stream will also asynchronously unsubscribe - When
autoUnsubscribe
istrue
, after the input stream completes, the new stream will also complete
Example
typescript
import { $, fork } from 'fluth'
const source$ = $('initial value')
const forked$ = fork(source$)
forked$.then((value) => {
console.log('Forked value:', value)
})
console.log(forked$.value) // Output: initial value
source$.next('new value')
// Output: Forked value: new value
forked$.next('new forked value')
// Output: Forked value: new forked value