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 🙏

© 2024 – Pkg Stats / Ryan Hefner

@composi/observer

v1.1.6

Published

A class to watch and dispatch custom events.

Downloads

20

Readme

@composi/observer

A class to watch and send custom events.

Install

npm i -D @composi/observer

After installing, you can import it into your project to use:

import { Observer } from '@composi/observer'

Create an Observer

Because Observer is a class, you must instantiate a new instance to use it:

import { Observer } from '@composi/observer'

const observer = new Observer()

Create a Watcher

To use an observer you need to tell it to watch for an event. You do this by passing it the event and a callback to the observer's watch method. By default the callback with get passed a parameter holding any data that was passed when the event is sent.

import { Observer } from '@composi/observer'

const observer = new Observer()
observer.watch('some-event', data => {
  console.log(`Received this data: ${data}`)
})

Event with Multiple Watchers

You can create multiple watchers for the same event, but with different callbacks. In that case, when the event is send, all the callbacks will fire. You might do this when you want multiple components to respond to the same event.

import { Observer } from '@composi/observer'

const observer = new Observer()
// First watcher for 'some-event':
observer.watch('some-event', data => {
  console.log(`First watcher received this data: ${data}`)
})

// Second watcher for 'some-event':
observer.watch('some-event', data => {
  console.log(`Second watcher received this data: ${data}`)
})

Ignore Event Data

You do not have to use data passed in a event. In fact, you can set up a watcher that just reacts to the event:

import { Observer } from '@composi/observer'

const observer = new Observer()
observer.watch('some-event', () => {
  console.log(`The event did fire!`)
})

Note: events are custom, so you can namespace them if you need to.

Send an Event

After setting up a watcher for an event, you need to send the event to make the watcher react. You do this with the observer's watch method. This takes two arguments: the event to send and any optional data you want to send with the event.

import { Observer } from '@composi/observer'

const observer = new Observer()
observer.watch('some-event', data => {
  console.log(`Received this data: ${data}`)
})

// Sometime later:
observer.send('some-event', 'We are firing "some-event" now!')
// Console will log:
// Received this data: We are firing "some-event" now!

Send Event without Data

As we mentioned earlier, you do not have to pass data with a sent event. Here we take the example of a watcher without data and send to it:

import { Observer } from '@composi/observer'

const observer = new Observer()
observer.watch('some-event' () => {
  console.log(`The event did fire!`)
})

// Sometime later send event without data:
observer.send('some-event')
// Console will log:
// The event did fire!

Unwatch an Event

After registering an observer and watching an event, you may at some point want to turn it off. You do this by using the observer's unwatch method. This takes one argument: the event to unwatch.

In this example we set up an observer and later unwatch the event:

import { Observer } from '@composi/observer'

const observer = new Observer()
observer.watch('some-event', data => {
  console.log(`Received this data: ${data}`)
})

// Sometime later:
observer.send('some-event', 'We are firing "some-event" now!')
// Console will log:
// Received this data: We are firing "some-event" now!

// Sometime later we unwatch the event:
observer.unwatch('some-event')
// Now sending to that even will do nothing:
observer.send('some-event', 'Sending to unwatched event.')

Note: if you have multiple watchers for an event and you unwatch it, this disables all watchers for that event.