1.0.1 • Published 7 years ago

@bre!zh/emitter v1.0.1

Weekly downloads
1
License
MIT
Repository
github
Last release
7 years ago

Emitter

The main goal of this library is to provide a very simple event emitter system, that is well tested and easily extensible.

// ES5
const { EventEmitter } = require('@bre!zh/emitter');
// ES6 / TypeScript
import { EventEmitter } from '@bre!zh/emitter';


function eventInitSuccess() {
    console.log('If you see this message, the event emitter works correctly :)');
}

function printTemperature(temperature) {
    console.log(`Tomorrow it will be ${temperature} degrees Celsius`);
}

function analyzeTemperature(temperature) {
    if (temperature > 24) {
        console.log(`It's going to be hot...`);
    } else {
        console.log(`Don't forget your scarf!`);
    }
}

// Create a new `EventEmitter` instance.
const emitter = new EventEmitter();

// Register some listeners.
emitter.once('weather:report', eventInitSuccess)
    .on('weather:report', printTemperature)
    .on('weather:report', analyzeTemperature);

// Emit the value `30` for 'weather:report'.
emitter.emit('weather:report', 30);
// Prints:
// If you see this message, the event emitter works correctly :)
// Tomorrow it will be 30 degrees Celsius
// It's going to be hot...

// Unregister the `printTemperature` listener from the `weather:report` event.
emitter.off('weather:report', printTemperature);

// Emit the value `-10` for 'weather:report'.
emitter.emit('weather:report', -10);
// Prints:
// Don't forget your scarf!

// Removes all listeners from the `weather:report` event.
emitter.clear('weather:report');
1.0.1

7 years ago

1.0.0

7 years ago