Skip to content

virgilioneto/simple-mediator

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

35 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Simple Mediator

Build Status codecov

Overview

This is a simple event stream mediator module.

Install

npm install @virgilioneto/simple-mediator

Using

Simple usage

// Load the mediator module
const SimpleMediator = require('@virgilioneto/simple-mediator')
// Get mediator context (use a default context when no context name given)
const defaultContext = SimpleMediator.getContext()

// Bind event on context
defaultContext.on('my-event', data => {
  /* Handle your code here */
})

// Bind once event on context
defaultContext.once('my-event-2', data => {
  /* This event will fire only once */
})

// Emit event to context
defaultContext.emit('my-event', { /* Put your data here */ })

// Emit event to context
defaultContext.emit('my-event-2', { /* Put your data here */ })
// This event will be ignored
defaultContext.emit('my-event-2', { /* Put your data here */ })

Working with multiple contexts

// Load the mediator module
const SimpleMediator = require('@virgilioneto/simple-mediator')
// Get mediator contexts
const context1 = SimpleMediator.getContext('context1')
const context2 = SimpleMediator.getContext('context2')

// Bind event on context1
context1.on('my-event', data => {
  /* Handle your code here */
})
// Bind event on context2
context2.on('my-event', data => {
  /* Handle your code here */
})

// Only context1 will be notified
context1.emit('my-event', { /* Put your data here */ })
// Only context2 will be notified
context2.emit('my-event', { /* Put your data here */ })

Docs

Simple Mediator

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published