1.3.1 • Published 3 years ago

alliage-events-listener-loader v1.3.1

Weekly downloads
4
License
GNU
Repository
github
Last release
3 years ago

Alliage Events Listener Loader

alliage-events-listener-loader

Provides a way to create events listener in an Alliage application without having to create a module

Dependencies

Installation

yarn add alliage-events-listener-loader

With npm

npm install alliage-events-listener-loader

Registration

If you have already installed alliage-module-installer you just have to run the following command:

$(npm bin)/alliage-scripts install alliage-events-listener-manager

Otherwise, update your alliage-modules.json file to add this at the bottom:

{
  // ... other modules
  "alliage-events-listener-loader": {
    "module": "alliage-events-listener-loader",
    "deps": [
      "alliage-di",
      "alliage-lifecycle",
      "alliage-service-loader"
    ],
    "envs": [],
  }
}

Usage

All we need to do to create an events listener is to create class extending the AbstractEventsListener and to register it as a service as we can see below:

import { AbstractEventsListener } from 'alliage-events-listener-loader/events-listener';
import { RUN_EVENTS } from 'alliage-lifecycle/events';
import { Service } from 'alliage-service-loader/decorators';

class MyEventsListener extends AbstractEventsListener {
  getEventHandlers() {
    return {
      [RUN_EVENTS.PRE_RUN]: this.handlePreRun,
      [RUN_EVENTS.POST_RUN]: this.handlePostRun,
    };
  }

  handlePreRun() {
    process.stdout.write('Test pre run\n');
  }

  handlePostRun() {
    process.stdout.write('Test post run\n');
  }
}

export default Service('my_events_listener')(MyEventsListener);

As we can see in the previous example, all we need to implement the getEventHandlers method which must return an object whose key are the events we want to handle and the value are the functions called when an event occurs.

Restrictions

As the load of the events listeners happens during the initialization phase it's not possible to listen to any event happening duting the initialization phase.

1.3.1-beta.0

3 years ago

1.3.0-beta.2

3 years ago

1.3.0-beta.5

3 years ago

1.3.1

3 years ago

1.3.0

3 years ago

1.2.0-pre1

5 years ago

1.1.1

5 years ago

1.1.0

5 years ago

1.0.0

5 years ago