v12.0.0 - initial commit
This commit is contained in:
commit
e2c49ea43c
1145 changed files with 97211 additions and 0 deletions
32
packages/server/lib/event-bus.js
Normal file
32
packages/server/lib/event-bus.js
Normal file
|
|
@ -0,0 +1,32 @@
|
|||
// Adapted from https://medium.com/@soffritti.pierfrancesco/create-a-simple-event-bus-in-javascript-8aa0370b3969
|
||||
|
||||
const uuid = require('uuid')
|
||||
const _ = require('lodash/fp')
|
||||
|
||||
const subscriptions = {}
|
||||
|
||||
function subscribe(eventType, callback) {
|
||||
const id = uuid.v1()
|
||||
|
||||
if (!subscriptions[eventType]) subscriptions[eventType] = {}
|
||||
|
||||
subscriptions[eventType][id] = callback
|
||||
|
||||
return {
|
||||
unsubscribe: () => {
|
||||
delete subscriptions[eventType][id]
|
||||
if (_.keys(subscriptions[eventType]).length === 0)
|
||||
delete subscriptions[eventType]
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
function publish(eventType, arg) {
|
||||
if (!subscriptions[eventType]) return
|
||||
|
||||
_.keys(subscriptions[eventType]).forEach(key =>
|
||||
subscriptions[eventType][key](arg),
|
||||
)
|
||||
}
|
||||
|
||||
module.exports = { subscribe, publish }
|
||||
Loading…
Add table
Add a link
Reference in a new issue