Coder Social home page Coder Social logo

lifenautjoe / noel Goto Github PK

View Code? Open in Web Editor NEW
164.0 3.0 2.0 1.73 MB

A universal, human-centric, replayable javascript event emitter.

Home Page: https://lifenautjoe.github.io/noel/

License: MIT License

TypeScript 77.30% HTML 5.62% JavaScript 11.99% CSS 5.09%
event event-emitter emitter replay event-replay events typescript event-dispatcher node-emitter browser-emitter

noel's Introduction

Noel logo

A universal, human-centric, replayable event emitter.

Build Status Human Friendly Coverage Status

Table of Contents

Motivation

The world just like software is full of events. Sometimes these events occur while we are busy doing other things. Wouldn't it be nice to have a way to replay all events? Noel is the way.

By being able to replay events we can design reactive systems without having to worry about timing.

For example, code like

// Check if there is a user in case we missed the userChanged event
const user = sessionService.getUser();
if(user) updateAvatarPhoto(user)

// Listen for further changes
sessionService.listenUserChanged(updateAvatarPhoto);

can become

sessionService.listenUserChanged(doSomethingWithUser).replay();

Meaning that if the userChanged event was already fired, it will be replayed with the last arguments and if it gets fired again, we'll be listening.

Features

  • Event replaying. Never miss a beat.
  • API designed for humans. No useless concepts to try to make sense of.

Installation

npm install noel

Usage

Creating a noel instance

const Noel = require('Noel');

const noel = new Noel();

Emitting and listening to events

// Listen for an event
noel.on('friday', partyAllNightLong);

// Emit an event
noel.emit('friday', arg1, arg2 ....);

Replaying events

// Replay an event once
noel.on(eventName, eventListener).replay();

// Replay an event x amount of times
noel.on(eventName, eventListener).replay(x);

Disabling replay

// When creating the noel instance
const noel = new Noel({
    replay: false
});

// At runtime
noel.disableReplay(anotherBufferSize);

Enabling replay

Please do note that replay is enabled by default, so this should only be necessary if it was disabled at runtime.

// At runtime
noel.enableReplay(anotherBufferSize);

Removing an event listener

noel.removeListener(eventName, eventListener);

or if you save a reference to the eventManager

const eventManager = noel.on('myEvent', eventListener);
// ... 
eventManager.remove();

Removing all event listeners

noel.removeAllListeners(eventName, eventListener);

Clearing an event replay buffer

If you would like to clear all saved event emissions for replay

noel.clearReplayBufferForEvent(eventName);

Clearing all events replay buffers

If you would like to clear all saved events emissions for replay

noel.clearEventsReplayBuffers();

Changing the events replay buffer size

Or in human words, changing the amount of event emissions that are saved for replays.

Default is 1.

// When creating the noel instance
const noel = new Noel({
    replayBufferSize: aNewBufferSize
});

// ...

// At runtime
noel.setReplayBufferSize(anotherBufferSize);

Disabling the no event listeners warning

When an event is emitted, no listeners have been set AND the replay was disabled so there is no way of doing anything with the emission, a warning will be logged into the console if available. To disable this behaviour:

// When creating the noel instance
const noel = new Noel({
    noEventListenersWarning: false
});

// ...

// At runtime
noel.disableNoEventListenersWarning();

Enabling the no event listeners warning

Please do note that the no event listeners warning is enabled by default, so this should only be necessary if it was disabled at runtime.

noel.enableNoEventListenersWarning();

Development

Clone the repository

git clone [email protected]:lifenautjoe/noel.git

Use npm commands

  • npm t: Run test suite
  • npm start: Runs npm run build in watch mode
  • npm run test:watch: Run test suite in interactive watch mode
  • npm run test:prod: Run linting and generate coverage
  • npm run build: Generate bundles and typings, create docs
  • npm run lint: Lints code
  • npm run commit: Commit using conventional commit style (husky will tell you to use it if you haven't ๐Ÿ˜‰)

Author Joel Hernandez

noel's People

Contributors

lifenautjoe avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

Forkers

tujoworker phukon

noel's Issues

Add EventListenerManager.removeFn to get a function that removes the listener when called

Everytime a listener is registered, and EventListenerManager is returned, this is the item that contains .replay and thus allows the following.

noel.on('myEvent', listener).replay();

This class contains another method remove() which removes the event listener.

In the case that we want to return a remover for the event in the form of a function we currently need to do something like

const eventListenerManager = noel.on('myEvent', listener);
return () => eventListenerManager.remove();

or event worse

return noel.on('myEvent',listener).remove.bind(noel);

This could be simplified by adding a new function

noel.on('myEvent',listener).removeFn()

This function will return a function that when called will remove the listener.

This also allows for the service/item having the Noel instance to control whether the event should be replayed or not, returning the deleteFn when the latter is desired.

Real World Example

Hi Joel,

thanks for making this event emitter.
Could You think of an real world example, where You really see neol shine? It has not to be much code. But some more lines or explanation than the one example in the Readme.md.

Thanks!

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.