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

event-courier

v0.6.5

Published

A tiny wrapper over the DOM CustomEvent with some additional niceties.

Downloads

4

Readme

npm npm bundle size NPM GitHub top language

Courier

A tiny wrapper over the DOM CustomEvent with some additional niceties. It facilitates communication among independent or loosely coupled components. The only dependency is a shared DOM.

Setup

event-courier can be installed with npm or using a content delivery network URL to embed it on your HTML page

npm i event-courier

or

<script src="https://unpkg.com/event-courier"></script>

Usage

If installed via npm

import {
    createCourier,
    on,
    once,
    subscribe,
    emit,
    emitWithResponse,
    emitAndStore,
    emitAndStoreWithResponse
} from 'event-courier';

If loaded as a script

const {
    createCourier,
    on,
    once,
    subscribe,
    emit,
    emitWithResponse,
    emitAndStore,
    emitAndStoreWithResponse
} = Courier;

The library expose the createCourier factory function bundled with methods of the pre-made instance. That instance uses window as an event target.

import { on, emit } from 'event-courier';

or

import { createCourier } from 'event-courier';
const element = document.getElementById('elementID');
const courier = createCourier(element);
const { on, emit } = courier;

then

import { on, once, subscribe, emit } from 'event-courier';

function eventAHandler(data) {
    console.log(data); // 42 then 43
}

function oneTimeEventHandler(data) {
    console.log(data); // 42
}

function otherEventAHandler(data) {
    console.log(data); // 42 then 43
}

function eventBHandler(data) {
    console.log(data); // 44
}

once('EventA', oneTimeEventHandler);
on('EventA', eventAHandler);
subscribe({
    EventA: otherEventAHandler,
    EventB: eventBHandler
});
emit('EventA', 42);
emit('EventA', 43);
emit('EventB', 44);

Unsubscription

import { on, subscribe, emit } from 'event-courier';

function eventAHandler(data) {
    console.log(data); // 42
}

function otherEventAHandler(data) {
    console.log(data); // 42
}

function eventBHandler(data) {
    // never called
}

const unsubscribeA = on('EventA', eventAHandler);
const unsubscribe = subscribe({
    EventA: otherEventAHandler,
    EventB: eventBHandler
});
emit('EventA', 42);
unsubscribeA();
unsubscribe.EventA();
unsubscribe.EventB();
emit('EventA', 43);
emit('EventB', 44);

Saving event

import { on, emitAndStore } from 'event-courier';

function eventAHandler(data) {
    console.log(data); // 42
}
emitAndStore('EventA', 42);
setTimeout(() => {
    on('EventA', eventAHandler);
}, 1000);

The callback will be immediately called upon subscription and then on all subsequent events.

All events with the same name emitted this way are stored independently. It means that upon subscrition the callback will be called for each stored event.

import { on, emitAndStore } from 'event-courier';

function eventAHandler(data) {
    console.log(data); // 42 and 'foo'
}
emitAndStore('EventA', 42);
emitAndStore('EventA', 'foo');
setTimeout(() => {
    on('EventA', eventAHandler);
}, 1000);

Event with response

import { on, emitWithResponse } from 'event-courier';

function onEventAResponse(data) {
    console.log(data); // 43
}

function eventAHandler(data, sendResponse) {
    // no matter how the event was fired,
    // it's safe to assume that the 'senResponse'
    // is a function and always there as a second argument
    console.log(data); // 42
    sendResponse(data + 1)
}

on('EventA', eventAHandler);
emitWithResponse('EventA', 42, onEventAResponse);

Saved event with response

The combination of the two options above:

import { on, emitAndStoreWithResponse } from 'event-courier';

function onEventAResponse(data) {
    console.log(data); // 43
}

function eventAHandler(data, sendResponse) {
    // no matter how the event was fired,
    // it's safe to assume that the 'senResponse'
    // is a function and always there as a second argument
    console.log(data); // 42
    sendResponse(data + 1)
}

emitAndStoreWithResponse('EventA', 42, onEventAResponse);
setTimeout(() => {
    on('EventA', eventAHandler);
}, 1000)

Currying

All the non-unary methods of a Courier instance will be curried if you provide the second truthy argument:

const element = document.getElementById('elementID');
const { on, emit } = createCourier(element, true);

In this case you can do thing like the following:

const onEventA = on('EventA'); // will not fire an event, just returns a function
const fireEventB = emit('EventB'); // will not set a listener, just returns a function
// and then later
onEventA(data => console.log(data));
fireEventB(42);

Documentation

Please find the full docs here

License

Copyright © 2020, Sergey Chernykh. Released under the MIT License.