onceupon.js
TypeScript icon, indicating that this package has built-in type declarations

1.6.5 • Public • Published

onceupon.js v1.6.5

Custom event system for JavaScript exported as Node.js module.

npm npm npm

let onceupon = require('onceupon.js')();

onceupon.on('event', (data) => {
    console.log(data);
    // data
});

onceupon.fire('event', 'data');

Installation

Install using NPM (or yarn):

$ npm i -g npm
$ npm i --save onceupon.js

In Node.js:

// Require onceupon & create a new instance
let onceupon = require('onceupon.js')();

API

onceupon(object)

Require onceupon & create a new instance.

let onceupon = require('onceupon.js')();

Assign onceupon object to an existing one.

let data = {};

let onceupon = require('onceupon.js')(data);
// Merge onceupon & data object

.create(event)

Optionally, an event with any name can be created.

onceupon.create('name');

.on(event, callback, options)

The callback is executed each time the event is fired. There is a possible argument for the data from the fire function. If there are several arguments at the event call, data is an array.

onceupon.on('name', (data) => {
    // Event is fired, callback executed
    // Use transmitted data
    console.log(data);
});

If the argument isIgnoringPrevious in options is set to true, the listener does not execute event calls from before the initialization of the listener.

// Fire event before the initialization of .on()
// This event call is not executed
onceupon.fire('event', 'before');

setTimeout(() => {
    onceupon.on('event', data => {
        console.log(data);
        // OUTPUT: after
        // OUTPUT: another call
    }, true);

    onceupon.fire('event', 'after');
    onceupon.fire('event', 'another call');
}, 1000);

Multiple events

It is also possible to use a callback for several events. For this purpose, the events names can be separated by a |.

// Listen to the events 'first' and 'second'
onceupon.on('first|second', () => {
    // Event 'first' or 'second' is fired, callback executed
});

.once(event, callback, ignorePreviousCalls)

The callback is only executed once when the event is called first. There is a possible argument for the data from the fire function. If there are several arguments at the event call, data is an array.

onceupon.once('event', (data) => {
    // Event is fired, callback executed
    // Use transmitted data
    console.log(data);
    // one
});

onceupon.fire('event', 'one');
onceupon.fire('event', 'two');

If the argument isIgnoringPrevious in options is set to true, the listener does not execute event calls from before the initialization of the listener.

// Fire event before the initialization of .once()
// This event call is not executed
onceupon.fire('event', 'before');

// Set a timeout
setTimeout(() => {
    onceupon.once('event', data => {
        console.log(data);
        // OUTPUT: after
    }, true);

    onceupon.fire('event', 'after');
}, 1000);

Multiple events

It is also possible to use a callback for several events. For this purpose, the events names can be separated by a |.

// Listen to the events 'first' and 'second'
onceupon.once('first|second', () => {
    // Event 'first' or 'second' is fired, callback executed
});

.only(event, callback, ignorePreviousCalls)

The callback is only executed if this callback is the first and only one to be called. There is a possible argument for the data from the fire function. If there are several arguments at the event call, data is an array.

onceupon.only('name', (data) => {
    // Event is fired, callback executed
    // Use transmitted data
    console.log(data);
});
onceupon.on('event', () => {});

onceupon.only('event', (data) => {
    // Event is fired, callback is not executed
    // because it's not the only event listener
    console.log(data);
});

onceupon.fire('event', 'data');

If the argument isIgnoringPrevious in options is set to true, the listener does not execute event calls from before the initialization of the listener.

// Fire event before the initialization of .only()
// This event call is not executed
onceupon.fire('event', 'before');

// Set a timeout
setTimeout(() => {
    onceupon.only('event', data => {
        console.log(data);
        // OUTPUT: after
    }, true);

    onceupon.fire('event', 'after');
}, 1000);

Multiple events

It is also possible to use a callback for several events. For this purpose, the events names can be separated by a |.

// Listen to the events 'first' and 'second'
onceupon.only('first|second', () => {
    // Event 'first' or 'second' is fired, callback executed
});

.fire(event, ...data)

Events can be fired using the function fire. The first required argument is the name of the event, the second, optional one, is data that can be transmitted.

onceupon.fire('name', 'data');

Multiple events

It is also possible to fire multiple events with one call. For this purpose, the events names can be separated by a |.

// Fire the events 'first' and 'second' with the same call
onceupon.fire('first|second', 'data');

Multiple arguments

If there are more than two arguments, data is an array.

// Listen to the events 'first' and 'second'
onceupon.on('event', data => {
    console.log(data);
    // OUTPUT: ['some', 'data']
});

onceupon.fire('event', 'some', 'data');

.isFired(event)

Returns: <Boolean>

Returns if a given event was fired.

// Fire an event
onceupon.fire('event');

// Check if event was fired
onceupon.isFired('event')
// true

License

MIT License

Copyright (c) 2022 Luca Joos

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Readme

Keywords

Package Sidebar

Install

npm i onceupon.js

Weekly Downloads

0

Version

1.6.5

License

MIT

Unpacked Size

27.4 kB

Total Files

16

Last publish

Collaborators

  • 1a85ra7z