@oframe/emitter

npm i @oframe/emitter

Usage no npm install needed!

<script type="module">
  import oframeEmitter from 'https://cdn.skypack.dev/@oframe/emitter';
</script>

README

@oframe/emitter

Install

npm i @oframe/emitter
yarn add @oframe/emitter
import { Emitter } from './Emitter.mjs';

Documentation

@oframe/emitter is a singleton used to connect event listeners and emitters.

It creates a global emitter, exported under Emitter, however users may instantiate multuple emitters from the exported EmitterClass class using the new keyword.


Emitter.on()

Emitter.on() adds a new callback to the desired name string. Emissions of the name string will trigger the callbacks in the same order that they're added. The callback will be stored until the Emitter.off() method is called using the same parameters.

Syntax

void Emitter.on(name, callback)

Params

name

A String event name used as reference to callbacks from the Emitter.emit() method.

callback

A Function callback to be triggered when the Emitter.emit() function is called with a matching name.

Return value

None


Emitter.off()

Emitter.off() removes a callback from the desired name string. Further emissions of the name string will no longer trigger the callback.

Syntax

void Emitter.off(name, callback)

Params

name

A String event name used as reference to callbacks from the Emitter.emit() method.

callback

A Function callback to be triggered when the Emitter.emit() function is called with a matching name.

Return value

None


Emitter.emit()

Emitter.emit()

Syntax

void Emitter.emit(name, [data])

Params

name

A String event name used as reference to callbacks added with the Emitter.on() method.

data optional

An Object to be passed to the callback functions.

Return value

None