Subject

class stable

A Subject is a special type of Observable that allows values to be multicasted to many Observers. Subjects are like EventEmitters.

class Subject<T> extends Observable implements SubscriptionLike {
  static create: (...args: any[]) => any
  constructor()
  closed: false
  observers: Observer<T>[]
  isStopped: false
  hasError: false
  thrownError: any
  get observed
  lift<R>(operator: Operator<T, R>): Observable<R>
  next(value: T)
  error(err: any)
  complete()
  unsubscribe()
  asObservable(): Observable<T>

  // inherited from index/Observable
  static create: (...args: any[]) => any
  constructor(subscribe?: (this: Observable<T>, subscriber: Subscriber<T>) => TeardownLogic)
  source: Observable<any> | undefined
  operator: Operator<any, T> | undefined
  lift<R>(operator?: Operator<T, R>): Observable<R>
  subscribe(observerOrNext?: Partial<Observer<T>> | ((value: T) => void), error?: (error: any) => void, complete?: () => void): Subscription
  forEach(next: (value: T) => void, promiseCtor?: PromiseConstructorLike): Promise<void>
  pipe(...operations: OperatorFunction<any, any>[]): Observable<any>
  toPromise(promiseCtor?: PromiseConstructorLike): Promise<T | undefined>
}

Subclasses

  • BehaviorSubject
  • ReplaySubject
  • AsyncSubject

Description

Every Subject is an Observable and an Observer. You can subscribe to a Subject, and you can call next to feed values as well as error and complete.

Static Properties

Property Type Description
create (...args: any[]) => any

Creates a "subject" by basically gluing an observer to an observable.

Constructor

constructor()

Parameters

There are no parameters.

Properties

Property Type Description
closed false
observers Observer<T>[]
isStopped false
hasError false
thrownError any
observed Read-only.

Methods

lift<R>(operator: Operator<T, R>): Observable<R>

Deprecation Notes

Internal implementation detail, do not use directly. Will be made internal in v8.

Parameters

operator

Type: Operator.

Returns

Observable<R>

next(value: T)

Parameters

value

Type: T.

error(err: any)

Parameters

err

Type: any.

complete()

Parameters

There are no parameters.

unsubscribe()

Parameters

There are no parameters.

asObservable(): Observable<T>

Creates a new Observable with this Subject as the source. You can do this to create customize Observer-side logic of the Subject and conceal it from code that uses the Observable.

Parameters

There are no parameters.

Returns

Observable<T>: Observable that the Subject casts to

© 2015–2021 Google, Inc., Netflix, Inc., Microsoft Corp. and contributors.
Code licensed under an Apache-2.0 License. Documentation licensed under CC BY 4.0.
https://rxjs.dev/api/index/class/Subject