Giter Club home page Giter Club logo

koala-event's Introduction

koala-event

Manage NodeJS Events by extending EventEmitter to support Koa-like middleware.

Install

Currently not on NPM - just hosted here.

npm i cavejay/koala-event

Usage

The KoalaEvent Class enables the equivalent of koa.use(fn).

var EventEmitter = require("events").EventEmitter;
const { KoalaEvent, EventRouter } = require("../koala-event");

var emitter = new EventEmitter();
var eventHandler = new KoalaEvent();

// This function will be run on all emitted events.
eventHandler.use(async function test1(ctx, next) {
  console.log("Print the event Context", ctx);
  await next();
});

/**
 * ctx provided to functions is: 
 * {
 *  event: <Original Event String>,
 *  data: <Original Event's accompanying data>
 * }
 */

// Once the middleware/.use()s are applied then, overwrite an emitter with the an emitter that handles 
// This is similar to Koa's callback()
emitter.emit = eventHandler.newEmitter(emitter);

// Proceed as normal
emitter.emit("test", "hi");
emitter.emit("something", "else");
emitter.emit("something", "else again");

The EventRouter Class is very similar to koa-mount's mount('uri', fn). This is an effective replacement for normal events, but supports mutation of context through multiple listeners - similar to processing of a Koa request.

var emitter = new EventEmitter();
const e = new EventRouter();

e.on("test", (ctx, next) => {
  console.log(ctx);
});

e.on("test", () => {
  console.log("I won't appear") // test's first middleware doesn't continue the flow"
})

e.on("something", (ctx, next) => {
  console.log(ctx);
  ctx.secret = "hi"; // Add the 'secret' after showing the initial ctx
  next(); // to continue processing listeners to the 'something' event
});

e.on("something", (ctx, next) => {
  console.log(ctx.secret); // the secret appended during an earlier middleware is present.
});

emitter.emit = e.newEmitter(emitter);

emitter.emit("test", "hi");
emitter.emit("something", "else");
emitter.emit("something", "again");
emitter.emit("nothing", 122); // nothing has no listener. We could make a global listener using .use (just like Koa) should we want to.

koala-event's People

Contributors

cavejay avatar

Watchers

James Cloos avatar  avatar

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.