
Observer Pattern
- 506 installs
- 235 repo stars
- Updated April 15, 2026
- patternsdev/skills
observer-pattern is a Claude Code skill that teaches the observer design pattern for decoupled publish/subscribe event communication in JavaScript and TypeScript codebases.
About
observer-pattern is a Claude Code skill from patternsdev/skills version 1.1 that teaches event-driven publish/subscribe architecture for JavaScript and TypeScript projects. The skill explains when observers and subjects fit—multiple components reacting to state changes without tight coupling—and when simpler callbacks or state managers are enough. It applies to **/*.js and **/*.ts files under an MIT license from patterns.dev, with related skills module-pattern and singleton-pattern for complementary structural patterns. Developers reach for observer-pattern while refactoring event buses, DOM listeners, store subscriptions, or cross-module notifications where one observable object must notify many dependents. The guidance covers subscription lifecycle, unsubscribe hygiene, and avoiding memory leaks from orphaned listeners. Use it during feature design or code review when event fan-out is growing beyond direct function calls.
- observer-pattern
Observer Pattern by the numbers
- 506 all-time installs (skills.sh)
- +14 installs in the week ending Aug 4, 2026 (Skillselion tracking)
- Ranked #808 of 4,347 Backend & APIs skills by installs in the Skillselion catalog
- Data as of Aug 4, 2026 (Skillselion catalog sync)
npx skills add https://github.com/patternsdev/skills --skill observer-patternAdd your badge
Show developers this skill is listed on Skillselion. Paste this into your README.
| Installs | 506 |
|---|---|
| repo stars | ★ 235 |
| Last updated | April 15, 2026 |
| Repository | patternsdev/skills ↗ |
How do you implement the observer pattern in TypeScript?
Use observer-pattern for development tasks
Who is it for?
JavaScript and TypeScript developers adding decoupled event notification when several components must react to the same state changes.
Skip if: Codebases that only need a single callback, a trivial one-to-one listener, or a full state library like Redux without custom observer wiring.
When should I use this skill?
User needs publish/subscribe behavior, event buses, or decoupled listeners across multiple JS or TS modules reacting to state changes.
What you get
Observer and subject classes, subscription API, unsubscribe handling, and decoupled event notification wiring in JS or TS modules.
- Observer and subject implementation
- Subscription API design
- Event notification wiring
By the numbers
- Skill version 1.1 from patterns.dev
- Targets **/*.js and **/*.ts file globs
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
- Create an Observable class with
subscribe,unsubscribe, andnotifymethods - Keep observers loosely coupled to the observable for better separation of concerns
- Be mindful of performance when notifying many subscribers with complex logic
- Consider using libraries like RxJS for more advanced reactive programming needs
Details
An observable object usually contains 3 important parts:
observers: an array of observers that will get notified whenever a specific event occurssubscribe(): a method in order to add observers to the observers listunsubscribe(): a method in order to remove observers from the observers listnotify(): a method to notify all observers whenever a specific event occurs
Let's create an observable using an ES6 class:
class Observable {
constructor() {
this.observers = [];
}
subscribe(func) {
this.observers.push(func);
}
unsubscribe(func) {
this.observers = this.observers.filter((observer) => observer !== func);
}
notify(data) {
this.observers.forEach((observer) => observer(data));
}
}We can now add observers to the list of observers with the subscribe method, remove the observers with the unsubscribe method, and notify all subscribers with the notify method.
Let's build something with this observable. We have a very basic app that only consists of two components: a Button, and a Switch.
export default function App() {
return (
<div className="App">
<Button>Click me!</Button>
<FormControlLabel control={<Switch />} />
</div>
);
}We want to keep track of the user interaction with the application. Whenever a user either clicks the button or toggles the switch, we want to log this event with the timestamp. Besides logging it, we also want to create a toast notification that shows up whenever an event occurs!
Whenever the user invokes the handleClick or handleToggle function, the functions invoke the notify method on the observer. The notify method notifies all subscribers with the data that was passed by the handleClick or handleToggle function!
First, let's create the logger and toastify functions. These functions will eventually receive data from the notify method.
import { ToastContainer, toast } from "react-toastify";
function logger(data) {
console.log(`${Date.now()} ${data}`);
}
function toastify(data) {
toast(data);
}
export default function App() {
return (
<div className="App">
<Button>Click me!</Button>
<FormControlLabel control={<Switch />} />
<ToastContainer />
</div>
);
}Currently, the logger and toastify functions are unaware of observable: the observable can't notify them yet! In order to make them observers, we'd have to _subscribe_ them, using the subscribe method on the observable!
import { ToastContainer, toast } from "react-toastify";
function logger(data) {
console.log(`${Date.now()} ${data}`);
}
function toastify(data) {
toast(data);
}
observable.subscribe(logger);
observable.subscribe(toastify);
export default function App() {
return (
<div className="App">
<Button>Click me!</Button>
<FormControlLabel control={<Switch />} />
<ToastContainer />
</div>
);
}Whenever an event occurs, the logger and toastify functions will get notified. Now we just need to implement the functions that actually notify the observable: the handleClick and handleToggle functions! These functions should invoke the notify method on the observable, and pass the data that the observers should receive.
import { ToastContainer, toast } from "react-toastify";
function logger(data) {
console.log(`${Date.now()} ${data}`);
}
function toastify(data) {
toast(data);
}
observable.subscribe(logger);
observable.subscribe(toastify);
export default function App() {
function handleClick() {
observable.notify("User clicked button!");
}
function handleToggle() {
observable.notify("User toggled switch!");
}
return (
<div className="App">
<Button>Click me!</Button>
<FormControlLabel control={<Switch />} />
<ToastContainer />
</div>
);
}We just finished the entire flow: handleClick and handleToggle invoke the notify method on the observer with the data, after which the observer notifies the subscribers: the logger and toastify functions in this case.
Whenever a user interacts with either of the components, both the logger and the toastify functions will get notified with the data that we passed to the notify method!
Although we can use the observer pattern in many ways, it can be very useful when working with asynchronous, event-based data. Maybe you want certain components to get notified whenever certain data has finished downloading, or whenever users sent new messages to a message board and all other members should get notified.
Case study
A popular library that uses the observable pattern is RxJS. RxJS has tons of built-in features and examples that work with the observable pattern.
Pros
Using the observer pattern is a great way to enforce separation of concerns and the single-responsibility principle. The observer objects aren't tightly coupled to the observable object, and can be (de)coupled at any time. The observable object is responsible for monitoring the events, while the observers simply handle the received data.
Cons
If an observer becomes too complex, it may cause performance issues when notifying all subscribers.
Source
References
Related skills
How it compares
Pick observer-pattern over generic refactoring skills when you specifically need publish/subscribe structure and subscription lifecycle guidance in JS or TS.
FAQ
What does observer-pattern teach?
observer-pattern teaches the observer design pattern for decoupled publish/subscribe communication in JavaScript and TypeScript, where observers subscribe to a subject and react when shared state or events change.
Which file types does observer-pattern target?
observer-pattern targets **/*.js and **/*.ts files in patternsdev/skills version 1.1, making it suitable for frontend and Node.js modules that need multi-listener event notification.
When should developers avoid the observer pattern?
observer-pattern guidance recommends skipping observers when a single callback suffices, coupling is already low, or a dedicated state management library handles subscriptions more safely than a custom subject implementation.