Sign Up for Free

RunKit +

Try any Node.js package right in your browser

This is a playground to test code. It runs a full Node.js environment and already has all of npm’s 400,000 packages pre-installed, including arctic-events with all npm packages installed. Try it out:

var arcticEvents = require("arctic-events")

This service is provided by RunKit and is not affiliated with npm, Inc or the package authors.

arctic-events v0.1.0

Arctic Events is a simple event handler and manager for events in javascript.

Arctic Events

Build Status npm version Maintenance Made With Love

The actual version is 0.1.0, these badges are always out of date for me or take for ever to update.

The core concept here is to register an event or multiple events and to trigger said events.

I wanted to build this library as a learning exercise more then anything.

Installing:

yarn add arctic-events

// In your JS:

const EventHandler = require('artic-events');

Getting Started

We need to create an EventHandler which handles the events that you then register, to do this we can extend the class EventHandler or we can use it as is. For the examples below, we will extend the class:

class MyEventHandler extends EventHandler {};

const myEventHandler = new myEventHandler();

myEventHandler.register('event', () => {
  console.log('hello world');
});

myEventHandler.trigger('event'); // => hello world.

Here we see we register an event, trigger it and see the results.

Parameters

What if your events takes in parameters?

class MyEventHandler extends EventHandler {};

const myEventHandler = new myEventHandler();

myEventHandler.register('event', (a, b, eventHandler) => {
  console.log(a, b, eventHandler);
});

myEventHandler.trigger('event', 'a', 'b');

// Shows the following:

a,
b,
MyEventHandler { events: [ { name: 'event', cb: [Function] } ] }

We have an additional parameter that we pass in. this which gives you back the EventHandler class object (in this case the subclass: MyEventHandler). You see this with the third param eventHandler.

RunKit is a free, in-browser JavaScript dev environment for prototyping Node.js code, with every npm package installed. Sign up to share your code.
Sign Up for Free