A simple publish/subscribe pattern
- Plain old vanilla JS
- Just 1.2kb gzipped
npm install core-events
import Event from 'core-events'
const event = new Event()
event.subscribe('createWorld', () => {
console.log('hello world')
})
event.publish('createWorld')
A slightly more complex example:
import Event from 'core-events'
const event = new Event()
const token = event.subscribe('personBorn', ({ name }) => {
console.log(`hello ${name}`)
})
event.publish('personBorn', {
'name': 'Colin'
})
event.unsubscribe(token)
Core Events also provides some handy functions to publish events when a global event occurs.
import Event, { publishClick } from 'core-events'
const event = new Event()
publishClick({ event })
event.subscribe('global.click', ({ target }) => {
console.log('Somebody clicked on', target)
})
import Event, { publishEscape } from 'core-events'
const event = new Event()
publishEscape({ event })
event.subscribe('global.escape', () => {
console.log('Somebody hit escape')
})
import Event, { publishResize } from 'core-events'
const event = new Event()
publishResize({ event })
event.subscribe('global.resize', () => {
console.log(`Somebody resizes the browser`)
})
import Event, { publishScroll } from 'core-events'
const event = new Event()
publishScroll({ event })
event.subscribe('global.scroll', ({ direction, speed }) => {
console.log(`Somebody scrolled ${direction} at ${speed} px per ms`)
})
import Event, { publishSwipe } from 'core-events'
const event = new Event()
publishSwipe({ event, sensitivity: { x: 10, y: 50 } })
event.subscribe('global.swipe', ({ direction, target }) => {
console.log(`Somebody swiped ${direction} on`, target)
})
Core Events is packaged with Babel, and
makes use of Array.from
.
If you want Core Events to work on browsers that don't support
this method (e.g. IE11), then you will need to
polyfill Array.from
before calling Event
.