DEV Community

Cover image for Observer Design Pattern in JavaScript

Observer Design Pattern in JavaScript

Shubham Khatri on August 09, 2021

While working with any language, we tend to use several reusable design solutions to commonly occurring problems. In JavaScript, too, we have a mix...
Collapse
 
shiznit013 profile image
shiznit013

Maybe I'm reading this wrong, but I think there's a bug in your unsubscribe function. If the predicate in the filter is true, then the item won't be filtered. I think you want !==.

Collapse
 
ashiquedesai profile image
Ashique

Thank you for pointing this out...it drove me crazy for an hour at least :)

Collapse
 
shubhamreacts profile image
Shubham Khatri

You are correct, thanks for pointing it out. I have updated it in the post.

Collapse
 
ashiquedesai profile image
Ashique • Edited

This is a really great article on the Observer pattern. To the point and very visual as well.
The bug mentioned by dev.to/shiznit013 "( .filter( !== instead of === )" was fixed in the first example but it still remains in the final example in Code-pen. This really drove me crazy for a few minutes till I read Shiznit013's comment.
But, other than that little bug, this is a beautiful piece. Thanks so much, for taking the time and effort to write this.

Collapse
 
maniator profile image
Naftali Lubin

You might want to check out my servable observable :-)

servable.serveside.dev

Created it a couple years ago while reading the rx docs

It does need some polish

Collapse
 
shubhamreacts profile image
Shubham Khatri

It does look really good 🙂

Collapse
 
maniator profile image
Naftali Lubin

Thanks :-)

Collapse
 
dimitrisauvage profile image
Dimitri Sauvage

No word about RXJS which impléments this pattern for a long time…
Angular uses rxjs in his core :)

Collapse
 
shubhamreacts profile image
Shubham Khatri

I would have loved to mention it but I haven't used RxJS or Angular.
I will definitely research and update the post in near future

Collapse
 
fridaycandours profile image
Friday candour

Great content, I find it very 7

Collapse
 
shubhamreacts profile image
Shubham Khatri

Thanks a lot. It feels good to hear this 😃