What is the difference between a Observable and a Subject in rxjs?

前端 未结 8 916
难免孤独
难免孤独 2020-12-01 00:05

i was going through this blog and to read about Observables and i couldnt figure out the difference between the Observable and a Subject

8条回答
  •  有刺的猬
    2020-12-01 00:35

    See rxjs document (more information and examples there): http://reactivex.io/rxjs/manual/overview.html#subject

    What is a Subject? An RxJS Subject is a special type of Observable that allows values to be multicasted to many Observers. While plain Observables are unicast (each subscribed Observer owns an independent execution of the Observable), Subjects are multicast.

    A Subject is like an Observable, but can multicast to many Observers. Subjects are like EventEmitters: they maintain a registry of many listeners.

    and code, Subject extending Observable: https://github.com/ReactiveX/rxjs/blob/master/src/internal/Subject.ts#L22

    /**
     * @class Subject
     */
    export class Subject extends Observable implements SubscriptionLike {
    //...
    }
    

提交回复
热议问题