1.1.33 • Published 4 years ago

alcumus-local-events v1.1.33

Weekly downloads
2
License
ISC
Repository
-
Last release
4 years ago

Local Events & Hookable Event Emitter

The local events module provides a project wide method of creating loosely coupled events that have the ability to provide hooks to override default behaviour or to apply additional processing before or after standard events.

The library provides a standard local-events emitter and an upgraded version of eventemitter2 that can be used to create other hookable events.

Hookable Events

All of the events fired have multiple steps that can be used to override or augment default behaviour.

Events are fired with additional elements prepended to allow for a variety of hooks. The elements are divided by the standard delimiter, which is '.' as standard. The order of events for 'EVENT' is early.0.EVENT - early.9.EVENT, before.EVENT, pre.0.EVENT - pre.9.EVENT, EVENT, post.0.EVENT - post.9.EVENT, after.EVENT, late.0.EVENT - late.9.EVENT

Each event processing function is passed a first parameter to indicate context, this has a cancel property and a preventDefault() method which causes the chain of events to be aborted.

    let counter = 0
    events.on('test', function() {
        throw new Error("Should not be here")
    })
    events.on('pre.4.test', function() {
        counter++
    })
    events.on('before.test', function(context) {
        context.preventDefault()
        counter++
    })
    events.emit('test')
    expect(counter).to.equal(2)

You may also use the context object to pass other data between event invocations.

Wildcard Events

The eventemitter2 implementation allows multipart events with wildcard support. For example:

    events.on('registerUser.*.start', function(context, param1, param2) {
        console.log("Generic user start")
    })
    
    events.on('before.registerUser.tfl.start', async function(context, param1, param2) {
        console.log("Hook tfl before Generic user start")
        if(param1 === 'admin') {
            if(!await callSomething(param2)) {
                context.preventDefault()
            }
        }
    })

    await events.emitAsync(`registerUser.${clientName}.start`, role, authToken)

Asynchronous Events

eventemitter2 fully supports asynchronous events using emitAsync.

    async function test() {
        let counter = 0
        events.on('early.1.test', function () {
            expect(counter++).to.equal(0)
        })
        events.on('before.test', function () {
            expect(counter++).to.equal(1)
        })
        events.on('pre.1.test', function () {
            expect(counter++).to.equal(2)
        })
        events.on('test', function () {
            expect(counter++).to.equal(3)
        })
        events.on('post.1.test', function () {
            expect(counter++).to.equal(4)
        })
        events.on('after.test', function () {
            expect(counter++).to.equal(5)
        })
        events.on('late.1.test', function () {
            expect(counter++).to.equal(6)
        })
        await events.emitAsync('test')
        expect(counter).to.equal(7)
    }

Usage

const HookedEvents = require('local-events/hooked-events')
const myEmitter = new HookedEvents({wildcard: true, delimiter: '::'})

Local Events

The local-events object is a configured hooked eventemitter2 instance with wildcards supported and the delimiter set to be '.' (it also supports up to 200 listeners per event before warning).

local-events can therefore be used across an entire project to react to and raise events.

Usage

const events = require('local-events')

1.1.29

4 years ago

1.1.28

4 years ago

1.1.30

4 years ago

1.1.33

4 years ago

1.1.32

4 years ago

1.1.31

4 years ago

1.2.29

4 years ago

1.0.27

5 years ago

1.0.26

5 years ago

1.0.25

5 years ago

1.0.24

5 years ago

1.0.23

5 years ago

1.0.22

5 years ago

1.0.21

5 years ago

1.0.20

5 years ago

1.0.18

5 years ago

1.0.17

5 years ago

1.0.16

5 years ago

1.0.15

5 years ago

1.0.14

5 years ago

1.0.13

5 years ago

1.0.12

5 years ago

1.0.11

5 years ago

1.0.10

5 years ago

1.0.9

5 years ago

1.0.8

5 years ago

1.0.7

5 years ago

1.0.6

5 years ago

1.0.5

5 years ago

1.0.4

6 years ago

1.0.3

6 years ago

1.0.2

6 years ago

1.0.1

6 years ago