Skip to content

Function: copyRx()

Call Signature

copyRx<TOut, Source$, TIn, Copy$, ThisArg>(source$, transform?, options?): Copy$

Defined in: packages/rx/src/copyRx.ts:141

Returns a subject that automatically copies the value(s) of the source subject(s).

Established a unidirectional data flow from source(s) to a destination subject. Changes to the destination subject are NOT applied back to the source.

Type Parameters

TOut

TOut

Source$

Source$ extends unknown[] | Observable<any> = unknown[] | Observable<any>

TIn

TIn = UnwrapSourceValueStrict<Source$>

Copy$

Copy$ extends BehaviorSubject<TOut> | Subject<TOut> = BehaviorSubject<TOut>

ThisArg

ThisArg = unknown

Parameters

source$

Source$

RxJS input observable(s) or static value(s). If an array is provided, the output subject will emit an array of values by default.

transform?

CopyRx_Transform<TIn, TOut, ThisArg> | null

(optional) A function to map or filter values before they are emitted by the output subject. Supports async functions. If it throws, the update is ignored.

options?

CopyRx_Options<TOut, ThisArg>

(optional) Configuration for timing (delay), initial state, and error handling.

Returns

Copy$

The destination subject (either the one provided in options.output or a new BehaviorSubject).

Examples

Auto-copy values from a single subject

typescript
import { BehaviorSubject, copyRx } from '@superutils/rx'

const number$ = new BehaviorSubject(1)
const even$ = copyRx(
  number$, // input observable
  newValue => newValue % 2 === 0 ? newValue : copyRx.IGNORE,
)
// subscribe to even$ changes
even$.subscribe(console.log) // prints: 2
number$.next(2)
number$.next(3) // (ignored)

Auto-copy from an array of subjects & values

javascript
import { BehaviorSubject, copyRx } from '@superutils/rx'

const theme$ = new BehaviorSubject('dark')
const userId$ = new BehaviorSubject('username')
const settings$ = copyRx(
  [
	   theme$,
	   userId$,
	   'my-fancy-app' // fixed/unobserved value
	 ]
)
// subscribe to the subject with reduced array values
settings$.subscribe(([theme, user, appName]) =>
	 console.log({ theme, user, appName })
)

Call Signature

copyRx<TOut, Source$, TIn, Copy$, ThisArg>(source$, transform, options?): Copy$

Defined in: packages/rx/src/copyRx.ts:155

Returns a subject that automatically copies the value(s) of the source subject(s).

Established a unidirectional data flow from source(s) to a destination subject. Changes to the destination subject are NOT applied back to the source.

Type Parameters

TOut

TOut

Source$

Source$ extends unknown[] | Observable<any> = unknown[] | Observable<any>

TIn

TIn = UnwrapSourceValueStrict<Source$>

Copy$

Copy$ extends BehaviorSubject<TOut> | Subject<TOut> = BehaviorSubject<TOut>

ThisArg

ThisArg = unknown

Parameters

source$

Source$

RxJS input observable(s) or static value(s). If an array is provided, the output subject will emit an array of values by default.

transform

CopyRx_Transform<TIn, TOut, ThisArg>

(optional) A function to map or filter values before they are emitted by the output subject. Supports async functions. If it throws, the update is ignored.

options?

CopyRx_Options<TOut, ThisArg>

(optional) Configuration for timing (delay), initial state, and error handling.

Returns

Copy$

The destination subject (either the one provided in options.output or a new BehaviorSubject).

Examples

Auto-copy values from a single subject

typescript
import { BehaviorSubject, copyRx } from '@superutils/rx'

const number$ = new BehaviorSubject(1)
const even$ = copyRx(
  number$, // input observable
  newValue => newValue % 2 === 0 ? newValue : copyRx.IGNORE,
)
// subscribe to even$ changes
even$.subscribe(console.log) // prints: 2
number$.next(2)
number$.next(3) // (ignored)

Auto-copy from an array of subjects & values

javascript
import { BehaviorSubject, copyRx } from '@superutils/rx'

const theme$ = new BehaviorSubject('dark')
const userId$ = new BehaviorSubject('username')
const settings$ = copyRx(
  [
	   theme$,
	   userId$,
	   'my-fancy-app' // fixed/unobserved value
	 ]
)
// subscribe to the subject with reduced array values
settings$.subscribe(([theme, user, appName]) =>
	 console.log({ theme, user, appName })
)