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

@dannyfranca/radarjs

v0.3.0

Published

Modern and Robust Event Emitter, with tagging, emitting and broadcasting. Internally uses Promises and RxJS Subjects.

Downloads

8

Readme

Install

Module

Download

npm i radarjs

Import

import { Radar } from '@dannyfranca/radarjs'

const radar = new Radar()

CDN

<script src="unpkg.com/radarjs"></script>

<!-- Specific Version -->
<script src="unpkg.com/[email protected]/lib/radar.umd.js"></script>

Usage

Listen for Events

const state = {
  count: 0,
  lastNotificationType: ''
}

radar.on('notify', () => state.count++)

// receive any number off values as arguments
radar.on('notify', ({ type }, ...data) => {
  state.lastNotificationType = type)
  console.log(data)
}

// can use namespaces
radar.on('notify.namespace1.namespace2', (...data) => {/*...*/})

Unsubscribe from Events

// by event name
radar.off('notify')

// by namespace
radar.off('.namespace1')

// by Subscription
const subscribeThenUnsubscribe = async () => {
  const subscription = await radar.on('event', (...data) => {/*...*/})
  subscription.unsubscribe()
}

subscribeThenUnsubscribe()

// by Subscription (sync)
const subscription = await radar.onSync('event', (...data) => {/*...*/})
subscription.unsubscribe()

Trigger Events

// pass any data to an event trigger
radar.trigger('notify', {
  type: 'info',
  message: 'Just an ordinary notification'
})

// pass any number of data
radar.trigger('notify', notification, ...data)

MultiLevel Events

// set child event
radar.link('grandparent', 'parent')
radar.link('parent', 'child')

// destroy link
radar.unlink('parent', 'child')

// broadcast events down to the whole tree just like trigger
// will trigger grandparent, parent and child
radar.broadcast('grandparent', ...data)

// will trigger parent and child
radar.broadcast('parent', ...data)

// emit events up to the whole tree just like trigger
// will trigger child, parent and grandparent
radar.emit('child', ...data)

// will trigger only grandparent
radar.emit('grandparent', ...data)

Sync Variants

Every public methods returns Promises, but each one has a sync variant, with same syntax.

radar.on('event', ...data) // returns Promise<Subscription>
radar.onSync('event', ...data) // returns Subscription
radar.trigger('event', ...data) // returns Promise<void>
radar.triggerSync('event', ...data) // returns void
// and so on...

MultiTriggering

To trigger/emit/broadcast many events with same data, you can use arrays of strings or a single string with event names separated by dots. Each event name is resolved by a glob pattern (powered by micromatch)!

// will trigger "foo" and "bar", sending same datas
radar.trigger('foo.bar', ...data)
// same as
radar.trigger(['foo', 'bar'], ...data)


// with micromatch
radar.on('foo', () => {/*...*/})
radar.on('bar', () => {/*...*/})
radar.on('baz', () => {/*...*/})
radar.on('boom', () => {/*...*/})

// will trigger "bar" and "baz"
radar.trigger('ba*', ...data) 
// will trigger "boom"
radar.trigger('b*m', ...data)

// many micromatches allowed!
// will trigger "bar", "baz" and  "boom"
radar.triggerMany('ba*.bo*', ...data)
radar.triggerMany(['ba*', 'bo*'], ...data)

See all glob possibilities in micromatch

Auto Link

You can autolink with ":" character

radar.on('event:frag1:frag2', () => {/*...*/})

// will fire "event", "event:frag1" and "event:frag1:frag2" events
radar.broadcast('event', ...data)

// will fire "event:frag1" and "event" events
radar.emit('event:frag1', ...data)

// ATTENTION: will fire just frag1.
// frag1 and frag2 is not supposed to be events,
// just words appended to eventNames
radar.broadcast('frag1', ...data)

Native Events

Native events has reserved names starting with $. Until now, the only native event available is $error.

$error event

// listening to $error
radar.on('$error', (error: Error) => {/*...*/})

Radarjs default error handler is:

(error: Error) => { throw error }

You can set yout own:

// set your error handler
radar.setErrorHandler((error: Error) => {/*...*/})

License

MIT

Copyright (c) Danny França mailto:[email protected]