Simple data emitter and subscriber from scratch in Javascript

--

Have to ever used a reactive setup in JS?Lets try to make it from scratch

So if you have used any reactive stuff in JS, it mainly contains two methods, one for emitting the data and one for listen the changes by subscribing to it.

Lets say we have a class CustomRxm, So it should have two methods emit and subscribe

We are going to use these methods like:

Lets code these methods:-

See we are getting a callback function as an argument in subscribe method.The trick is to store this as class variable and call this in emit method with new data as argument.
(Since this is a simple example this will only work with one subscriber)

In action:-

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

--

--

No responses yet

Write a response