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

atomic-emitter

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

atomic-emitter

An atomic event emitter that can only emit one type of event, optionally with private write access.

  • 3.1.0
  • latest
  • Source
  • npm
  • Socket score

Version published
Maintainers
1
Created
Source

atomic-emitter

Similar to Raynos' geval, with the following changes:

  • will proxy the whole arguments to listeners

Example

atomic-emitter has an interface similar to geval's SingleEvent. You can pass it around, add listeners and emit events.

var Emitter = require("atomic-emitter")

var clicksEmitter = Emitter()

var removeListener = clicksEmitter(function(ev, time) {
    console.log('click happened', ev)
})

document.addEventListener("click", function (ev) {
    clicksEmitter.emit(ev, +new Date())
})

// ...

removeListener() // and you will stop listening to events

You can also restrict write access to the creator of the emitter, if you want:

var Emitter = require("atomic-emitter")

var clicksEmitter = Emitter(function (emit) {
    document.addEventListener("click", function (ev) {
        emit(ev)
    })
})

var removeListener = clicksEmitter(function listener(ev) {
    console.log('click happened', ev)
})

clicksEmitter.emit({test: true}) // Will throw an exception

// ...

removeListener() // and you will stop listening to events

The purpose of this is that you no longer have an EventEmitter-inherited object with obscure events, but a normal everyday-object with some of its properties populated with atomic emitters as values -- events are real values now. For example:

var stream = {
  ondata: Emitter()
, onclose: Emitter()
}

API

Emitter(fn:Function(emit:Function)): Emitter

Create an atomic emitter with an emit function passed to the creating code.

Emitter(): Emitter

Create an atomic emitter with a public emit function.

emitter(listener:Function)

Add an event listener.

emitter.emit(args...)

Emit the event.

(c) 2015 by Marcel Klehr
MIT License

Keywords

FAQs

Package last updated on 29 Nov 2015

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