Web Dev Solutions

Catalin Mititiuc

const observers = {}; export const Observable = Object.freeze({ notify: (event, ...data) => { (observers[event] || []).forEach((observer) => observer(...data)) }, subscribe: (event, func) => { (observers[event] ??= []).push(func); }, unsubscribe: (event, func) => { [...observers[event] || []].forEach((observer, index) => { if (observer === func) { observers[event].splice(index, 1); } }); }, });