Giter VIP home page Giter VIP logo

base-event-object's Introduction

base-event-object

Installation

npm

$ npm install base-event-object --save

yarn

$ yarn add base-event-object

Usage

import BaseEventObject from 'base-event-object';

// Pass in an array of event names for initializing the supported events
const eventObject = new BaseEventObject(['change']);

const handle = (e, ...args) => {};

let listener = eventObject.on('change', handle); // Add 'change' listener

listener.remove(); // Remove 'change' listener
// or
eventObject.off('change', handle); // Remove 'change' listener

eventObject.off('change'); // Remove all 'change' listener
eventObject.off(); // Remove all listener

eventObject.emit('change', ...args); // Emit 'change' event

More usage

const eventObject = new BaseEventObject({
    // Every time the emit will trigger the event
    events: ['change'],
  
    // Emit only once, and the event will be executed immediately after each registration
    onceEvents: ['ready']
});

eventObject.once('change', e => {}); // The handler will only execute once

eventObject.on('ready', e => {}); // Wait for the emit to execute
eventObject.emit('ready');
eventObject.on('ready', e => {}); // It will be implemented immediately

// Register multiple event listeners simultaneously
eventObject.on('ready change', e => {});

Inherit

ES6

class EventObject extends BaseEventObject {
    constructor(events) {
        super(events);
    }
}
const eventObject = new EventObject(['change']);

ES5

function EventObject (events) {
    BaseEventObject.call(this, events);
}
EventObject.prototype = Object.create(BaseEventObject.prototype);
const eventObject = new EventObject(['change']);

Extend

const customObject = new CustomObject();
const eventObject = new BaseEventObject(['change']);
Object.assign(customObject, eventObject);

Params

BaseEventObject( events )

events {String|Array|Object} Single event name, or event name array, or event option.

If it is an event option, it contains two optional attributes.

Each property is an array containing supported event names.

  • events : Every time the emit will trigger the event.
  • onceEvents : Emit only once, and the event will be executed immediately after each registration.
new BaseEventObject('ready change');
// or
new BaseEventObject(['ready', 'change']);
// Is equivalent to
new BaseEventObject({ events: ['ready', 'change'] });

License

MIT

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.