Skip to content

Library that provides an ability to use observer pattern

License

Notifications You must be signed in to change notification settings

stein197/js-observer

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

47 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Observer pattern implementation for TypeScript

This tiny package implements the observer pattern.

Installation

Run:

npm i @stein197/observer

Usage

import Event from "@stein197/observer/Event";
class JoinEvent extends Event {}
class LeaveEvent extends Event {}
const dispatcher = new EventDispatcher<[JoinEvent, LeaveEvent]>();
dispatcher.addEventListener(JoinEvent, event => {}); // Adding listener on JoinEvent event
dispatcher.addEventListener(LeaveEvent, event => {}); // Adding listener on LeaveEvent event
dispatcher.dispatch(JoinEvent, new JoinEvent()); // Firing all listeners subscribed on "AfterJoin" event
dispatcher.dispatch(LeaveEvent, new LeaveEvent()); // Firing all listeners subscribed on "AfterUnjoin" event

Every class that emits event must implement EventEmitter interface:

import type {EventEmitter} from "@stein197/observer/EventEmitter";

class User implements EventEmitter<[]> {
	public addEventListener(/* ... */) {/* ... */}
	public removeEventListener(/* ... */) {/* ... */}
}

NPM scripts

  • clean cleans working directory
  • build compiles source code
  • test runs unit tests

About

Library that provides an ability to use observer pattern

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published