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

dot-event

Package Overview
Dependencies
Maintainers
1
Versions
85
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

dot-event

Powerful event emitter

  • 2.12.11
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
91
increased by4450%
Maintainers
1
Weekly downloads
 
Created
Source

dot-event

Powerful event emitter.

dot event

Install

npm install --save dot-event

Start simple

Part of the beauty of the dot-event API is that it can shrink down to incredibly simple functionality.

Here we have the simplest possible subscriber and emitter:

import dotEvent from "dot-event"

const events = dotEvent()

events.on(() => {
  /* do something */
})

events.emit()

Subscription listeners can be asynchronous:

events.on(async () => {})

await events.emit()

The emitter returns a promise that waits for all listeners to resolve concurrently.

Dot-props

Use dot-props to maintain distinct subscriptions:

events.on("emit.hello.world", () => {})

events.emit("hello.world") // emits
events.emit() // doesn't emit

Dot-props come in handy with the onAny subscriber, which subscribes to a dot-prop and its child props:

events.onAny("emit.hello", () => {})

events.emit("hello") // emits
events.emit("hello.world") // emits
events.emit() // doesn't emit

Operations

You might be confused why this subscription prop is emit.hello:

events.on("emit.hello", () => {})

events.emit("hello") // emits

This is because emit is an "operation", and you can have more than one.

First, define your custom operation:

events.setOp("create")

Then use it:

events.on("create", () => {})

events.create() // emits the create operation

Subscription listener argument

Subscription listeners receive a single object argument. To add to that object, pass an object to the emitter:

events.on(({ hello }) => {
  /* hello === "world" */
})

events.emit({ hello: "world" })

The listener argument also contains an event property with extra information, such as the emitter arguments:

events.on(({ event }) => {
  /* event.args === [true] */
})

events.emit(true)

Prepositions (before or after)

Subscribe to before or after the main subscription listener:

events.on("before", () => {
  /* 1 */
})
events.on(() => {
  /* 2 */
})
events.on("after", () => {
  /* 3 */
})

events.emit()

More subscribers

On any

Subscribe to any emit:

events.onAny(() => {})

events.emit() // emits
events.emit("hello") // emits
events.emit("hello.world") // emits

When used with a dot-prop, it subscribes to any child prop emit:

events.onAny("emit.hello", () => {})

events.emit("hello") // emits
events.emit("hello.world") // emits
events.emit() // doesn't emit

On emitted

Like on, but emit immediately if a previous emit occurred:

events.emit()

events.onEmitted(() => {}) // emits immediately

events.emit() // emits

On any emitted

Like onAny, but emit immediately if a previous emit occurred:

events.emit("hello.world")

events.onAnyEmitted("emit.hello", () => {}) // emits immediately

events.emit("hello.world") // emits
events.emit() // doesn't emit

Once

events.once(() => {})

events.emit() // emits
events.emit() // doesn't emit

Once emitted

Like once, but emit immediately if a previous emit occurred:

events.emit()

events.onceEmitted(() => {}) // emits immediately

events.emit() // doesn't emit

Once any

A combination of once and onAny:

events.onceAny("emit.hello", () => {})

events.emit("hello.world") // emits
events.emit("hello.world") // doesn't emit

Once any emitted

A combination of once, onAny, and onEmitted:

events.emit("hello.world")

events.onceAnyEmitted("emit.hello", () => {}) // emits immediately

events.emit("hello.world") // doesn't emit

Subscriber shorthand

Build lots of dot-prop subscriptions at once:

events.on({
  "emit.hello": () => {},
  "emit.hello.world": () => {},
})

Test matrix

FunctionFeaturesEmptyPropsWildcardProp variableWildcard op
afterSubscribes to after emit
beforeSubscribes to before emit
emitEmits
onSubscribes
onAnySubscribes to child props
onAnyEmittedSubscribes to child prop
Emit immediately if emitted
onEmittedEmit immediately if emitted
onceSubscribes once
Returns promise
onceAnySubscribes to child props once
Returns promise
onceAnyEmittedSubscribes to child props once
Emit immediately if emitted
Returns promise
onceEmittedSubscribes to child props once
Emit immediately if emitted
Returns promise
withOptionsAdds options to emit
Adds options to subscribe

Keywords

FAQs

Package last updated on 02 Jan 2019

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