0.0.8 • Published 7 years ago

fn-eventify v0.0.8

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

fn-eventify

fn-eventify attach EventEmitter interface to Function.

Installation

npm install fn-eventify

Usage

Initilize

The first, You should initialize fn-eventify like a creating an instace of EventEmitter.

import * as Eventify from 'fn-eventify'

const Eventor = Eventify.create();

Eventor has eventify(), subscribe() and subscribeAll()

Creating an eventify function

const fn = () => 'hello world';
const greet = Eventor.eventify('GREET', fn);

Callback function can return Promise.

Eventor.eventify('ASYNC_SOMETHING', () => Promise.resolve());

Subscribing / Unsubscribing an event

const unsubscribe = greet.subscribe((event) => {...});
unsubscribe();
const unsubscribe = Eventor.subscribe('GREET', (event) => {...})
unsubscribe();

You can listen all event.

const unsubscribe = Eventor.subscribeAll((event) => {...})
unsubscribe();

Publishing an event

To publish, You just call eventified function simply.

const message = greet() // return result and pubslish event

assert.equal(message, 'hello world')

Event Structure

{
	name: string; // event name
	payload: any; // something that returned by function
}

Extending an Event Structure

You can use inject((event: EventifyEvent) => EventifyEvent | {[prop: string]: any}).

const greet = Eventor.eventify('GREET', (message) => `${message}`)
	.inject((event) => Object.assign(event, { greetor: 'cotto' })) // inject by callback

or

const greet = Eventor.eventify('GREET', (message) => `${message}`)
	.inject({ greetor: 'cotto' }) // inject by Object

then

greet.subscribe((event) => {
	assert.deepEqual(event, {
		name: 'GREET',
		payload: 'hello world',
		greetor: 'cotto' // injected extra props
	})
})

greet('hello world'); //-> hello world
0.0.8

7 years ago

0.0.7

7 years ago

0.0.6

7 years ago

0.0.5

7 years ago

0.0.4

7 years ago

0.0.3

7 years ago

0.0.2

8 years ago

0.0.1

8 years ago