life-cycle-hooks

0.1.1 • Public • Published

life-cycle-hooks

A clean way to bind functions to events in custom code

Install

npm i --save life-cycle-hooks

Usage

Start by initializing a lifecycle object:

const LifeCycle = require('life-cycle-hooks');
const lifecycle = new LifeCycle();

Then, to attach events:

lifecycle.on('some_event', (args, sent, by, event) => {
  console.log("some event happened")
})

To trigger all events bound to 'some_event':

lifecycle.trigger('some_event', "args", "to", "the", "bound", "function")

Example use case

Suppose you have a websocket server, and you want to respond differently for mobile and desktop:

ws_server.on('connection', async function connection(ws, req) {
  if(isMobile(req)) {
    lifecycle.trigger('mobile_connection', ws);
  } else {
    lifecycle.trigger('desktop_connection', ws);
  }
}

You can then return lifecycle, and in another part of your code, do the following:

lifecycle.on('mobile_connection', (ws) => {
  ws.send('hello mobile user');
});
 
lifecycle.on('desktop_connection', (ws) => {
  ws.send('hello desktop user');
});

Readme

Keywords

Package Sidebar

Install

npm i life-cycle-hooks

Weekly Downloads

3

Version

0.1.1

License

MIT

Unpacked Size

3.48 kB

Total Files

4

Last publish

Collaborators

  • lutraman