npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

@mirawision/observer

v1.0.1

Published

A lightweight and flexible observer pattern implementation for TypeScript, providing a simple way to implement event-driven architecture with type safety.

Readme

@mirawision/observer

A lightweight and flexible observer pattern implementation for TypeScript, providing a simple way to implement event-driven architecture with type safety. This library offers a clean and intuitive API for implementing the observer pattern in your applications.

Features

  • Type Safety: Full TypeScript support with generic types for type-safe event data
  • Simple API: Clean and intuitive interface for subscribing, unsubscribing, and notifying observers
  • Memory Efficient: Uses Set for efficient observer management and automatic cleanup
  • Flexible: Generic implementation allows for any data type to be passed to observers
  • Lightweight: Minimal footprint with no external dependencies
  • Unsubscribe Support: Individual and bulk unsubscribe methods for proper cleanup

Installation

npm install @mirawision/observer

or

yarn add @mirawision/observer

Usage

Basic Example

import { Observer } from '@mirawision/observer';

// Create an observer instance for string events
const observer = new Observer<string>();

// Subscribe to events
const listener1 = (data: string) => {
  console.log('Listener 1 received:', data);
};

const listener2 = (data: string) => {
  console.log('Listener 2 received:', data);
};

observer.subscribe(listener1);
observer.subscribe(listener2);

// Notify all observers
observer.notify('Hello, World!');
// Output:
// Listener 1 received: Hello, World!
// Listener 2 received: Hello, World!

Working with Custom Types

import { Observer } from '@mirawision/observer';

// Define a custom type for your events
interface UserEvent {
  id: number;
  name: string;
  action: 'login' | 'logout';
}

// Create an observer for your custom type
const userObserver = new Observer<UserEvent>();

// Subscribe with type-safe listeners
userObserver.subscribe((event: UserEvent) => {
  console.log(`User ${event.name} (ID: ${event.id}) performed ${event.action}`);
});

// Notify with type-safe data
userObserver.notify({
  id: 123,
  name: 'John Doe',
  action: 'login'
});
// Output: User John Doe (ID: 123) performed login

Managing Subscriptions

import { Observer } from '@mirawision/observer';

const observer = new Observer<number>();

const listener = (data: number) => {
  console.log('Received:', data);
};

// Subscribe
observer.subscribe(listener);

// Notify
observer.notify(42);
// Output: Received: 42

// Unsubscribe specific listener
observer.unsubscribe(listener);

// Notify again (no output since listener was removed)
observer.notify(100);

// Unsubscribe all listeners
observer.unsubscribeAll();

Real-world Example: Event System

import { Observer } from '@mirawision/observer';

// Define event types
interface AppEvent {
  type: 'user-login' | 'user-logout' | 'data-update';
  payload: any;
  timestamp: Date;
}

// Create a global event bus
class EventBus {
  private observer = new Observer<AppEvent>();

  subscribe(listener: (event: AppEvent) => void) {
    this.observer.subscribe(listener);
  }

  unsubscribe(listener: (event: AppEvent) => void) {
    this.observer.unsubscribe(listener);
  }

  emit(event: AppEvent) {
    this.observer.notify(event);
  }
}

// Usage
const eventBus = new EventBus();

// Subscribe to events
eventBus.subscribe((event) => {
  console.log(`[${event.timestamp.toISOString()}] ${event.type}:`, event.payload);
});

// Emit events
eventBus.emit({
  type: 'user-login',
  payload: { userId: 123, username: 'john' },
  timestamp: new Date()
});

eventBus.emit({
  type: 'data-update',
  payload: { items: ['item1', 'item2'] },
  timestamp: new Date()
});

React Integration Example

import React, { useEffect, useState } from 'react';
import { Observer } from '@mirawision/observer';

// Create a global observer for app state
const appStateObserver = new Observer<{ theme: 'light' | 'dark' }>();

function ThemeToggle() {
  const [theme, setTheme] = useState<'light' | 'dark'>('light');

  useEffect(() => {
    const handleThemeChange = (data: { theme: 'light' | 'dark' }) => {
      setTheme(data.theme);
    };

    appStateObserver.subscribe(handleThemeChange);

    // Cleanup on unmount
    return () => {
      appStateObserver.unsubscribe(handleThemeChange);
    };
  }, []);

  const toggleTheme = () => {
    const newTheme = theme === 'light' ? 'dark' : 'light';
    appStateObserver.notify({ theme: newTheme });
  };

  return (
    <button onClick={toggleTheme}>
      Current theme: {theme}
    </button>
  );
}

API Reference

Observer

A generic observer class that manages a collection of listeners for a specific data type.

Methods

  • subscribe(observer: ObserverListener<T>): void

    • Adds a listener to the observer
    • The listener will be called whenever notify() is called
  • unsubscribe(observer: ObserverListener<T>): void

    • Removes a specific listener from the observer
    • Useful for cleanup to prevent memory leaks
  • unsubscribeAll(): void

    • Removes all listeners from the observer
    • Clears the entire observer collection
  • notify(data: T): void

    • Notifies all subscribed listeners with the provided data
    • Calls each listener function with the data parameter

Type Definitions

type ObserverListener<T> = (data: T) => void;

class Observer<T> {
  // ... methods
}

Contributing

Contributions are always welcome! Feel free to open issues or submit pull requests.

License

This project is licensed under the MIT License.