71. implement Observable Subject
Share
medium - accepted / - tried
This is a follow-up on 57. create an Observable.
Plain Observables are unicast, meaning every subscription is independent. To create multicast, you need to use Subject.
Following code is easier to understand.
// default behavior with plain Observableconst observable = from([1,2,3])observable.subscribe(console.log)observable.subscribe(console.log)// 1// 2// 3// 1// 2// 3
You can see that two subscriptions are independent so the logs are grouped by subscription.
with Subject, it works like Event Listeners in DOM world.
const subject = new Subject()subject.subscribe(console.log)subject.subscribe(console.log) const observable = from([1, 2, 3])observable.subscribe(subject)// 1// 1// 2// 2// 3// 3
Now the logs are different! That is because Subject first works as a observer, get the values, then works as an Observable and dispatch the value to different observers.
Cool right? Ok, you are asked to implement a simple Subject Class
.
Observable
is given for you, you can just use it.- you can use
new Observer({next,error,complete})
ornew Observer(function)
to create an observer.