Skip to content

Function: copyRxSubject()

Call Signature

copyRxSubject<TCopy, TSource$, T, TCopy$, ThisArg>(source$, copy$?, valueModifier?, options?): TCopy$

Defined in: packages/rx/src/copyRxSubject.ts:102

Function

copyRxSubject

Type Parameters

TCopy

TCopy

TSource$

TSource$

T

T = UnwrapSubjectValue<TSource$>

TCopy$

TCopy$ extends SubjectLike<TCopy> = BehaviorSubject<TCopy>

ThisArg

ThisArg = unknown

Parameters

source$

TSource$

RxJS source subject(s). If Array provied, value of copy$ will also be an Array by default, unless a different type is provided by copy$ or valueModifier.

copy$?

(optional) RxJS copy/destination subject. If undefined, a new subject will be created. Value type will be inferred automatically based on copy$, valueModifier and source$. Default: new BehaviorSubject()

TCopy$ | null

valueModifier?

(optional) callback to modify the value (an thus type) before copying from source$. Accepts async functions. Function invocation errors will be gracefully ignored. PS: If the very first invokation returns IGNORE_UPDATE_SYMBOL, the value of copy$.value will be undefined. Args: newValue, previousValue, copy$

ValueModifier<T, TCopy> | null

options?

CopyRxSubjectOptions<ThisArg>

(optional) options to enable debouce/throttling copy$ value changes.

Returns

TCopy$

copy$ if provided, otherwise, a new BehaviorSubject instance

Examples

Auto-copy values from a single subject

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

const number$ = new BehaviorSubject(1)
const even$ = copyRxSubject(
  // source subject
  number$,
  // create and return a new BehaviorSubject. An existing RxJS subject can also be provided here.
  null,
  // copy and transform the value from number$
  newValue => newValue % 2 === 0,
  // debounce/throttle value changes to even$
  // {
  //   delay: 300 //
  //   throttle: false,
  // }
)
// subscribe to even$ changes
even$.subscribe(console.log)
number$.next(2) // prints: true
number$.next(3) // print: false

Auto-copy from an array of subjects & values

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

const theme$ = new BehaviorSubject('dark')
const userId$ = new BehaviorSubject('username')
const settings$ = copyRxSubject(
  [
	   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

copyRxSubject<TCopy, TSource$, T, TCopy$, ThisArg>(source$, copy$, valueModifier, options?): TCopy$

Defined in: packages/rx/src/copyRxSubject.ts:116

Function

copyRxSubject

Type Parameters

TCopy

TCopy

TSource$

TSource$

T

T = UnwrapSubjectValue<TSource$>

TCopy$

TCopy$ extends SubjectLike<TCopy> = BehaviorSubject<TCopy>

ThisArg

ThisArg = unknown

Parameters

source$

TSource$

RxJS source subject(s). If Array provied, value of copy$ will also be an Array by default, unless a different type is provided by copy$ or valueModifier.

copy$

(optional) RxJS copy/destination subject. If undefined, a new subject will be created. Value type will be inferred automatically based on copy$, valueModifier and source$. Default: new BehaviorSubject()

TCopy$ | null | undefined

valueModifier

(optional) callback to modify the value (an thus type) before copying from source$. Accepts async functions. Function invocation errors will be gracefully ignored. PS: If the very first invokation returns IGNORE_UPDATE_SYMBOL, the value of copy$.value will be undefined. Args: newValue, previousValue, copy$

ValueModifier<T, TCopy> | null

options?

CopyRxSubjectOptions<ThisArg>

(optional) options to enable debouce/throttling copy$ value changes.

Returns

TCopy$

copy$ if provided, otherwise, a new BehaviorSubject instance

Examples

Auto-copy values from a single subject

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

const number$ = new BehaviorSubject(1)
const even$ = copyRxSubject(
  // source subject
  number$,
  // create and return a new BehaviorSubject. An existing RxJS subject can also be provided here.
  null,
  // copy and transform the value from number$
  newValue => newValue % 2 === 0,
  // debounce/throttle value changes to even$
  // {
  //   delay: 300 //
  //   throttle: false,
  // }
)
// subscribe to even$ changes
even$.subscribe(console.log)
number$.next(2) // prints: true
number$.next(3) // print: false

Auto-copy from an array of subjects & values

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

const theme$ = new BehaviorSubject('dark')
const userId$ = new BehaviorSubject('username')
const settings$ = copyRxSubject(
  [
	   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 })
)