feat: add new server log page
This commit is contained in:
parent
fc1951c4b2
commit
703c5d7c91
38 changed files with 2844 additions and 29 deletions
29
lib/event-bus.js
Normal file
29
lib/event-bus.js
Normal file
|
|
@ -0,0 +1,29 @@
|
|||
// 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