0.0.3 • Published 7 years ago

hapi-signals v0.0.3

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

hapi-signals

Signals and events for Hapi.

Install

$ npm install hapi-signals

Usage

It decorates the server with the method addSignal. This method takes a string and adds a signals with that name. It also exposes the signals and decorates the requests with the signals object.

A signal has the following methods:

  • dispatch: fires all callbacks with the arguments provided to this method.
  • add: registers a callback to be called when this signal dispatches. The call back takes as arguments whatever is passed to the dispatch method.
  • once: same as add but runs only once

The plugin can also be passed the following options:

  • signals: string or array of strings of names of signals to add during registration

Example

const Hapi = require('hapi');

const server = new Hapi.Server(/* options */);

// some other stuff ...

server.register(require('hapi-signals'), err => {
  if (err) throw err;

  server.addSignal('addedUser');

  server.plugins.signals.addedUser.add(name => console.log(`${name} `));

  server.plugins.signals.addedUser.dispatch('Tom'); // logs to console 'Tom'

  server.route({
    method: 'post',
    path: '/signup',
    handler: (request, reply) => {
      // logs to console whatever the name is
      request.signals.addedUser(request.payload.name);

      // rest of your logic ...

    },
  });

  server.start();
});

License

MIT

0.0.3

7 years ago

0.0.2

7 years ago

0.0.1

7 years ago