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

eventuate-core

v4.0.2

Published

Handle events without emitters or frills

Downloads

9

Readme

eventuate-core

NPM version Build Status Coverage Status

Handle events without emitters or frills.

This implements the core eventuate objects, with a limited feature set. You may also want to look at eventuate which aggregates this, and several other modules to provide a reactive api for handling data over time.

example

var eventuate = require('eventuate-core')

// lets create a server object
var server = {}

// this server will produce request events
server.request = eventuate()

// lets consume them!
server.request(function onRequest (req) {
    console.log('we got a request for ' + req.url)
})

// lets produce some of these requests
server.request.produce({ url: '/hello.js' })
server.request.produce({ url: '/world.js' })
server.request.produce({ url: '/bye.js' })

api

var eventuate = require('eventuate-core') 

var event = eventuate()

Create an object, event, that represents a consumable event type.

Valid options are:

  • requireConsumption (default: false) - throw an error if a produced event is not consumed, useful for error producers
  • destroyResidual (default: false) - call the destroy function when the last consumer is removed via removeConsumer or removeAllConsumers (after at least one consumer was added)

event(consumer)

Convenient shortcut for calling event.consume(consumer).

event.consume(consumer)

Consume events with the consumer function, which should have the signature function (data) {}. When an event is produced, it will be passed to the consumer function as the first and only argument.

Returns event.

event.produce(data)

Produce an event. All event consumer functions will be called with data. If the requireConsumption option was provided, and nothing consumes the data, an error will be thrown. In this case, if the data being produced is an instanceof Error, it will be thrown directly, otherwise an UnconsumedEventError (see below) will be thrown, and the data that was produced will be attached to the error as a data property.

event.removeConsumer(consumer)

Remove the formerly added consumer, so that it will not be called with future produced events.

If the consumer function has a property of removed, and that property is a function, it will be executed (with no arguments) after it is removed.

event.removeAllConsumers()

Remove all consumers from the eventuate event.

event.hasConsumer()

Returns true is the eventuate has any consumers, otherwise false.

event.getConsumers()

Returns a shallow copy of the array of all consuming functions.

event.destroy()

Remove all consumers, prevent further consumers from being added, and throw an EventuateDestroyedError if further produce calls are attempted. This function is called automatically when the last consumer is removed if the destroyResidual option was set to true.

event.error(consumer)

A basic eventuate (see "basic eventuates" below) representing Error objects produced by the eventuate. By assigning a handler to the event.error, any Error objects produced will no longer be supplied to event consumers; only event.error consumers will receive them.

event.consumerAdded(consumer)

A basic eventuate representing additions of consumers. Any consumers of consumerAdded will be invoked and passed the consumer function that was added to the eventuate.

Example:

var event = eventuate() 
event.consumerAdded(function (eventConsumer) {
    console.log('a consumer was added to event: ' + eventConsumer.name) 
}) 

event.consumerRemoved(consumer)

A basic eventuate representing removal of consumers. Any consumers of consumerRemoved will be invoked with the consumer function removed from the eventuate.

Example:

var event = eventuate() 
event.consumerRemoved(function (eventConsumer) {
    console.log('a consumer was removed from event: ' + eventConsumer.name) 
}) 

event.destroyed(consumer)

A basic eventuate representing destruction of the event. This eventuate occurs only one time, after destroy is called. This eventuate will not occur if destroyResidual was set to false at eventuate creation.

event.factory

Exposes the factory function used to create the eventuate. Example:

var eventuate = require('eventuate'), 
    assert    = require('assert')

var event = eventuate() 
assert(event.factory === eventuate) 

eventuate error types

var errors = require('eventuate/errors')

errors.EventuateUnconsumedError

Constructor of error potentially thrown from eventuate.produce when requireConsumption is true.

errors.EventuateDestroyedError

Constructor of error thrown from eventuate.produce when the eventuate is already destroyed.

basic eventuates

var basicEventuate = require('eventuate-core/basic') 

Basic eventuates offer minimal functionality: production, consumption, and limited consumer management. They offer: produce, consume, hasConsumer, and removeConsumer. That also have a factory property that points to the basic eventuate factory (eventuate-core/basic).

Basic eventuates are used internally to implement the "sub-eventuates" error, consumerRemoved, consumerAdded, and destroyed.

supporting modules

The following modules support and extend the functionality of eventuate and are included in the eventuate module (without the -core).

  • eventuate-once - act once (via callback or promise) upon the next occurrence of an eventuate
  • eventuate-filter - create filtered eventuate, acting as subset of broader eventuate
  • eventuate-map - create mapped eventuates, producing events transformed from the source eventuate
  • eventuate-reduce - create an eventuate that reduces events produced from a source eventuate

install

With npm do:

npm install --save eventuate-core

testing

npm test

Or to run tests in phantom: npm run phantom

coverage

npm run view-cover

This will output a textual coverage report.

npm run open-cover

This will open an HTML coverage report in the default browser.