
Company News
Socket Named Top Sales Organization by RepVue
Socket won two 2026 Reppy Awards from RepVue, ranking in the top 5% of all sales orgs. AE Alexandra Lister shares what it's like to grow a sales career here.
@med1802/scoped-observer
Advanced tools
A lightweight, type-safe, and **zero-dependency** event system for frontend applications. Built on native `EventTarget` API with support for **hierarchical scopes**, **last event replay**.
A lightweight, type-safe, and zero-dependency event system for frontend applications. Built on native EventTarget API with support for hierarchical scopes, last event replay.
dispatch and subscribe, nothing morenpm i @med1802/scoped-observer
Define your scope hierarchy once at initialization:
import { createScopedObserver } from "@med1802/scoped-observer";
const observer = createScopedObserver([
{
scope: "app",
subScopes: [
{
scope: "dashboard",
subScopes: [{ scope: "widgets" }, { scope: "settings" }],
},
{
scope: "profile",
},
],
},
]);
// Dispatch to a specific scope
observer.dispatch({
scope: "app:dashboard:widgets",
eventName: "widgetUpdated",
payload: { id: 1, name: "Chart" },
});
// Dispatch to root scope (scope is optional)
observer.dispatch({
eventName: "appInitialized",
payload: { version: "1.0.0" },
});
// Subscribe to a scoped event
const unsubscribe = observer.subscribe({
scope: "app:dashboard:widgets",
eventName: "widgetUpdated",
callback: ({ payload, eventName, scope }) => {
console.log("Widget updated:", payload);
console.log("Event:", eventName);
console.log("Scope:", scope);
},
});
// Unsubscribe when done
unsubscribe();
If you subscribe after an event has been dispatched, the callback will be invoked immediately with the last payload:
// 1. Dispatch first
observer.dispatch({
scope: "app:profile",
eventName: "userLoaded",
payload: { id: 1, name: "John" },
});
// 2. Subscribe later
observer.subscribe({
scope: "app:profile",
eventName: "userLoaded",
callback: ({ payload }) => {
// This will fire immediately with { id: 1, name: 'John' }
console.log("User:", payload);
},
});
This is perfect for state synchronization where components mount after data has loaded.
Scopes are separated by : and form a hierarchical structure:
app → root level scopeapp:dashboard → nested scopeapp:dashboard:widgets → deeply nested scopeIf scope is omitted or empty, the root scope is used.
createScopedObserver(scopes?)Creates a new scoped observer instance.
Parameters:
scopes (optional): Array of ScopeNode objects defining the hierarchyReturns: Observer instance with dispatch and subscribe methods
observer.dispatch(options)Dispatches an event to a specific scope.
Options:
scope? (string): Target scope path (e.g., "app:dashboard")eventName (string): Name of the eventpayload? (any): Data to send with the eventobserver.subscribe(options)Subscribes to events on a specific scope.
Options:
scope? (string): Target scope patheventName (string): Name of the event to listen forcallback (function): Handler receiving { payload, eventName, scope }Returns: Unsubscribe function
EventTarget instanceMIT
FAQs
A lightweight, type-safe, and **zero-dependency** event system for frontend applications. Built on native `EventTarget` API with support for **hierarchical scopes**, **last event replay**.
We found that @med1802/scoped-observer demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Company News
Socket won two 2026 Reppy Awards from RepVue, ranking in the top 5% of all sales orgs. AE Alexandra Lister shares what it's like to grow a sales career here.

Security News
NIST will stop enriching most CVEs under a new risk-based model, narrowing the NVD's scope as vulnerability submissions continue to surge.

Company News
/Security News
Socket is an initial recipient of OpenAI's Cybersecurity Grant Program, which commits $10M in API credits to defenders securing open source software.