@synet/patterns
Version:
Robust, battle-tested collection of stable patterns used in Synet packages
38 lines (37 loc) • 903 B
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.Subject = void 0;
/**
* Subject that maintains a list of observers and notifies them of changes
*/
class Subject {
constructor() {
this.observers = [];
}
/**
* Add an observer to be notified of changes
*/
subscribe(observer) {
if (!this.observers.includes(observer)) {
this.observers.push(observer);
}
}
/**
* Remove an observer from the notification list
*/
unsubscribe(observer) {
const index = this.observers.indexOf(observer);
if (index !== -1) {
this.observers.splice(index, 1);
}
}
/**
* Notify all observers of a change
*/
notify(data) {
for (const observer of this.observers) {
observer.update(data);
}
}
}
exports.Subject = Subject;