DEV Community

Cover image for Make Professional Components with Pub/Sub and Custom Events
Joe Honton
Joe Honton

Posted on

Make Professional Components with Pub/Sub and Custom Events

Publish/subscribe is a classic software design pattern that allows pieces of an application to be decoupled. It uses messages instead of functions, closures, or callbacks. It promotes an architectural style that has flexibility through independence.

I recently applied the pub/sub pattern to the state management of a DOM component. Its implementation was at times confusing, but the final result was a satisfying UI/UX. The extra effort was worth it.

The full story is on Medium at https://medium.com/javascript-fanboi/2021-033-pub-sub-and-custom-events-a9ff6f93152

Bottom line: The pub/sub pattern helps remove dependencies between the source of truth and the user interface.

Unfortunately pub/sub can become a tangled mess — because the code for publishing and subscribing are almost always in separate modules. The key to success is staying organized.

Discussion (0)