New Research: Supply Chain Attack on Axios Pulls Malicious Dependency from npm.Details →
Socket
Book a DemoSign in
Socket

ts-observer-lib

Package Overview
Dependencies
Maintainers
1
Versions
1
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

ts-observer-lib

A TypeScript implementation of the Observer pattern

latest
Source
npmnpm
Version
1.0.0
Version published
Weekly downloads
0
-100%
Maintainers
1
Weekly downloads
 
Created
Source

TypeScript Observer Library

A lightweight and type-safe implementation of the Observer pattern in TypeScript.

Overview

This library provides a robust implementation of the Observer design pattern, allowing for type-safe event handling and state management in TypeScript applications. It includes interfaces and concrete implementations for both Subject and Observer components.

Installation

npm install ts-observer-lib

Features

  • 🔒 Type-safe implementation
  • 💡 Simple and intuitive API
  • ⚡ Lightweight with zero dependencies
  • 📦 Easy integration
  • 🛡️ Error handling for invalid states

Usage

Basic Example

import { Observable, ConcreteObserver } from 'ts-observer-lib';

// Create a subject that will handle number type
const subject = new Observable<number>();

// Create an observer with a callback
const observer = new ConcreteObserver<number>((data) => {
    console.log(`Received update: ${data}`);
});

// Add observer to subject
subject.addObserver(observer);

// Notify all observers
subject.notifyObservers(42);
// Output: "Received update: 42"

Custom Observer Implementation

import { Observer, Observable } from 'ts-observer-lib';

class CustomObserver implements Observer<string> {
    update(data: string): void {
        console.log(`Custom observer received: ${data}`);
    }
}

const subject = new Observable<string>();
const customObserver = new CustomObserver();

subject.addObserver(customObserver);
subject.notifyObservers("Hello, World!");
// Output: "Custom observer received: Hello, World!"

API Reference

Interfaces

Observer

  • update(data: T): void - Called when the subject's state changes

Subject

  • addObserver(observer: Observer<T>): void - Adds an observer
  • removeObserver(observer: Observer<T>): void - Removes an observer
  • notifyObservers(state: T): void - Notifies all observers with new state

Classes

Observable

A concrete implementation of the Subject interface that maintains a list of observers and notifies them of state changes.

ConcreteObserver

A concrete implementation of the Observer interface that accepts a callback function to handle updates.

Error Handling

The library includes built-in error handling:

  • Throws an error if attempting to notify observers with undefined or null state
  • Safely handles observer removal and notification

License

MIT

Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

Keywords

typescript

FAQs

Package last updated on 01 Apr 2025

Did you know?

Socket

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.

Install

Related posts