patternsdev avatar

Observer Pattern

This walks you through implementing the classic observer pattern for event-driven pub/sub communication in JavaScript and TypeScript. You get a straightforward Observable class with subscribe, unsubscribe, and notify methods, plus a practical React example showing how to decouple event handlers from multiple listeners like loggers and toast notifications. The guidance from patterns.dev is solid on when to use this (multiple components reacting to state changes) and when to skip it (high subscriber counts or when simple callbacks would do). It mentions RxJS as the production-grade option if you need more power, but the vanilla implementation here is enough for understanding the fundamentals and handling basic decoupled communication.

skills.sh installs
653
GitHub stars
258
last updated
Aug 21, 2026

Install

$ npx -y skills add patternsdev/skills --skill observer-pattern --agent claude-code

Installs into .claude/skills of the current project.

Files

Observer Pattern

Table of Contents

With the observer pattern, we can subscribe certain objects, the observers, to another object, called the observable. Whenever an event occurs, the observable notifies all its observers!

When to Use

  • Use this when you need to notify multiple parts of an application about state changes or events
  • This is helpful for implementing event-driven, asynchronous communication between components

When NOT to Use

  • When the subscriber count is very high and notification performance becomes critical
  • When simpler callbacks or direct function calls suffice for one-to-one communication
  • When debugging difficulty from implicit event chains outweighs the decoupling benefit

Instructions

Sponsors

Details

Repository
github.com/patternsdev/skills
Categories
Frontend Development, Release Management
First seen
Jun 3, 2026

More from patternsdev/skills

All 47
1wangyendt avatarsend-emailwangyendt/wayne-skillsSend emails via SMTP with support for HTML formatting, file attachments, and email templates. Use when users ask to: (1) Send an email, (2) Email someone, (3) S652skills.sh installs
2hairyf avatarundocshairyf/skillsMinimal Documentation Theme and CLI for shared usage across UnJS projects. Use when creating documentation sites with Nuxt, Nuxt Content, and Nuxt UI.632skills.sh installs
3dash0hq avatarotel-ottldash0hq/agent-skillsOpenTelemetry Transformation Language (OTTL) expert. Use when writing or debugging OTTL expressions for any OpenTelemetry Collector component that supports OTTL631skills.sh installs
4sanky369 avatarsocial-graphicssanky369/vibe-building-skillsProduce platform-correct social media graphics: pick the right dimensions and layout per platform, write the headline/body/CTA copy, build the generation prompt629skills.sh installs
5patternsdev avatarmediator-patternpatternsdev/skillsTeaches the mediator pattern for centralized component communication. Use when multiple components need to communicate and direct coupling between them creates 622skills.sh installs
6alirezarezvani avatarstripe-integration-expertalirezarezvani/claude-skillsProduction-grade Stripe integrations: subscriptions with trials and proration, one-time payments, usage-based billing, checkout sessions, idempotent webhook han621skills.sh installs