Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

ts-observer-pattern

Package Overview
Dependencies
Maintainers
1
Versions
6
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

ts-observer-pattern

Interfaces to help working with the Observer Pattern

  • 3.1.1
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
1
Maintainers
1
Weekly downloads
 
Created
Source

ts-observer-pattern

A simple observer pattern implementation in TypeScript.

npm install ts-observer-pattern

Example

In the example that follows, I'm implementing a relationship bettween a magazine and it subscribers

// Magazine.ts
import { Subject } from 'ts-observer-pattern'

class Magazine extends Subject
{
    private Observers: Observers[]

    public publish()
    {
        //Some code here
        this.Observer.notify("New edition available") //Here I passed a string,
        //but you can pass any data you want
    }
}
// Subscriber.ts
import { Observer } from 'ts-observer-pattern'

class Subscriber implements Observer
{
    public update(data): this
    {
        //Do whatever you want here with the information passed by the Magazine object
    }
}
// main.ts
import { Magazine } from './Magazine'
import { Subscriber } from './Subscriber'

let Coders = new Magazine

let Matt = new Subscriber
let Ana = new Subscriber

Coders.attach(Matt)
Coders.attach(Ana)

Keywords

FAQs

Package last updated on 10 Jul 2018

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

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc