File: src/lib/EventObject.js

Recommend this page to a friend!
  Classes of Dom Hastings  >  JS Webdav Client  >  src/lib/EventObject.js  >  Download  
File: src/lib/EventObject.js
Role: Class source
Content type: text/plain
Description: Class source
Class: JS Webdav Client
Access files of a Webdav server
Author: By
Last change:
Date: 8 months ago
Size: 766 bytes
 

Contents

Class file image Download
const events = {};

export default class EventObject {
  hasEvent(event) {
    return event in events;
  }

  on(event, listener) {
    if (! this.hasEvent(event)) {
      events[event] = [];
    }

    events[event].push(listener);
  }

  off(event, listener = null) {
    if (! this.hasEvent(event)) {
      return;
    }

    if (listener === null) {
      return events[event] = [];
    }

    events[event] = events.filter((eventListener) => eventListener !== listener);
  }

  trigger(event, ...data) {
    if (this.hasEvent(event)) {
      let stopped = false;

      events[event].forEach((listener) => {
        if (stopped) {
          return;
        }

        if (listener(...data) === false) {
          stopped = true;
        }
      });
    }
  }
}