Getting started

Install listener, create a listener library, and use it 🌅

Install listener

$ npm install @listener-js/listener

Use the development dependency flag (--save-dev) if you're creating a listener library and only using the listener function in tests.

Create a listener library

Listener libraries are basically normal class instances, but with one exception — functions that receive a listener identifier (lid) as the first argument become listener functions.

Listener functions are special because they can be bound to and extended even if they have asynchronous outputs.

Here we export a class instance with a listener function and save it as logger.ts:

logger.ts
export class Logger {
  public log(lid: string[], msg: string) {
    console.log(lid, msg)
  }
}

export default new Logger()

Arguments after the first listener id argument (lid) can be whatever you like.

Using a listener library

Here we show how to load a library with the listener.load function.

Load the listener library, call the listener function, and save it as main.ts:

main.ts
import listener from "@listener-js/listener"
import logger from "./logger"

listener.load([], { logger })
logger.log([], "hi")

Execute main.ts and view the output:

$ ts-node main.ts
[ 'logger.log' ] 'hi'

From the output, we see that calling a listener function automatically prepends its name to the identifier.

What's next?

Welcome to the world of dynamic libraries! 🌐

pageIdentifierspageCallbackspageBindings

Last updated