2021-03-12 10:23:54 +00:00
|
|
|
--[[
|
|
|
|
dead-simple publish-subscribe message bus
|
|
|
|
]]
|
|
|
|
|
|
|
|
local path = (...):gsub("pubsub", "")
|
|
|
|
local class = require(path .. "class")
|
2021-11-22 23:45:09 +00:00
|
|
|
local set = require(path .. "set")
|
2022-11-03 05:19:18 +00:00
|
|
|
local tablex = require(path .. "tablex")
|
2021-11-22 23:45:09 +00:00
|
|
|
|
2021-07-15 06:09:08 +00:00
|
|
|
local pubsub = class({
|
|
|
|
name = "pubsub",
|
|
|
|
})
|
2021-03-12 10:23:54 +00:00
|
|
|
|
|
|
|
--create a new pubsub bus
|
|
|
|
function pubsub:new()
|
2021-07-15 06:09:08 +00:00
|
|
|
self.subscriptions = {}
|
2022-11-03 05:19:18 +00:00
|
|
|
self._defer = {}
|
|
|
|
self._defer_stack = 0
|
|
|
|
end
|
|
|
|
|
|
|
|
--(internal; deferred area check)
|
|
|
|
function pubsub:_deferred()
|
|
|
|
return self._defer_stack > 0
|
|
|
|
end
|
|
|
|
|
|
|
|
--(internal; enter deferred area)
|
2022-11-07 06:13:25 +00:00
|
|
|
function pubsub:_push_defer(event)
|
2022-11-03 05:19:18 +00:00
|
|
|
self._defer_stack = self._defer_stack + 1
|
|
|
|
if self._defer_stack > 255 then
|
2022-11-07 06:13:25 +00:00
|
|
|
error("pubsub defer stack overflow; event infinite loop with event: "..tostring(event))
|
2022-11-03 05:19:18 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
--(internal; enter deferred area)
|
|
|
|
function pubsub:_defer_call(defer_f, event, callback)
|
|
|
|
if not self:_deferred() then
|
|
|
|
error("attempt to defer pubsub call when not required")
|
|
|
|
end
|
|
|
|
table.insert(self._defer, defer_f)
|
|
|
|
table.insert(self._defer, event)
|
|
|
|
table.insert(self._defer, callback)
|
|
|
|
end
|
|
|
|
|
|
|
|
--(internal; unwind deferred sub/unsub)
|
2022-11-07 06:13:25 +00:00
|
|
|
function pubsub:_pop_defer(event)
|
2022-11-03 05:19:18 +00:00
|
|
|
self._defer_stack = self._defer_stack - 1
|
|
|
|
if self._defer_stack < 0 then
|
2022-11-07 06:13:25 +00:00
|
|
|
error("pubsub defer stack underflow; don't call the defer methods directly - event reported: "..tostring(event))
|
2022-11-03 05:19:18 +00:00
|
|
|
end
|
|
|
|
if self._defer_stack == 0 then
|
|
|
|
local defer_len = #self._defer
|
|
|
|
if defer_len then
|
|
|
|
for i = 1, defer_len, 3 do
|
|
|
|
local defer_f = self._defer[i]
|
|
|
|
local defer_event = self._defer[i+1]
|
|
|
|
local defer_cb = self._defer[i+2]
|
|
|
|
self[defer_f](self, defer_event, defer_cb)
|
|
|
|
end
|
|
|
|
tablex.clear(self._defer)
|
|
|
|
end
|
|
|
|
end
|
2021-03-12 10:23:54 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
--(internal; notify a callback set of an event)
|
2022-11-07 06:13:25 +00:00
|
|
|
function pubsub:_notify(event, callbacks, ...)
|
2021-03-12 10:23:54 +00:00
|
|
|
if callbacks then
|
2022-11-07 06:13:25 +00:00
|
|
|
self:_push_defer(event)
|
2022-11-03 05:19:18 +00:00
|
|
|
for _, f in ipairs(callbacks:values()) do
|
2021-03-12 10:23:54 +00:00
|
|
|
f(...)
|
|
|
|
end
|
2022-11-07 06:13:25 +00:00
|
|
|
self:_pop_defer(event)
|
2021-03-12 10:23:54 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
--publish an event, with optional arguments
|
|
|
|
--notifies both the direct subscribers, and those subscribed to "everything"
|
|
|
|
function pubsub:publish(event, ...)
|
2022-11-07 06:13:25 +00:00
|
|
|
self:_notify(event, self.subscriptions[event], ...)
|
|
|
|
self:_notify(event, self.subscriptions.everything, event, ...)
|
2021-03-12 10:23:54 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
--subscribe to an event
|
|
|
|
--can be a specifically named event, or "everything" to get notified for any event
|
2021-07-18 01:30:22 +00:00
|
|
|
--for "everything", the callback will receive the event name as the first argument
|
2021-03-12 10:23:54 +00:00
|
|
|
function pubsub:subscribe(event, callback)
|
2022-11-03 05:19:18 +00:00
|
|
|
if self:_deferred() then
|
|
|
|
self:_defer_call("subscribe", event, callback)
|
|
|
|
return
|
|
|
|
end
|
2021-03-12 10:23:54 +00:00
|
|
|
local callbacks = self.subscriptions[event]
|
|
|
|
if not callbacks then
|
|
|
|
callbacks = set()
|
|
|
|
self.subscriptions[event] = callbacks
|
|
|
|
end
|
|
|
|
callbacks:add(callback)
|
|
|
|
end
|
|
|
|
|
2022-11-03 05:19:18 +00:00
|
|
|
--subscribe to an event, automatically unsubscribe once called
|
|
|
|
function pubsub:subscribe_once(event, callback)
|
2022-11-07 22:23:45 +00:00
|
|
|
local f
|
2022-11-07 06:13:25 +00:00
|
|
|
local called = false
|
2022-11-07 22:23:45 +00:00
|
|
|
f = function(...)
|
2022-11-07 06:13:25 +00:00
|
|
|
if not called then
|
|
|
|
callback(...)
|
|
|
|
self:unsubscribe(event, f)
|
|
|
|
called = true
|
|
|
|
end
|
2022-11-03 05:19:18 +00:00
|
|
|
end
|
|
|
|
self:subscribe(event, f)
|
|
|
|
end
|
|
|
|
|
2021-03-12 10:23:54 +00:00
|
|
|
--unsubscribe from an event
|
|
|
|
function pubsub:unsubscribe(event, callback)
|
2022-11-03 05:19:18 +00:00
|
|
|
if self:_deferred() then
|
|
|
|
self:_defer_call("unsubscribe", event, callback)
|
|
|
|
return
|
|
|
|
end
|
2021-03-12 10:23:54 +00:00
|
|
|
local callbacks = self.subscriptions[event]
|
|
|
|
if callbacks then
|
|
|
|
callbacks:remove(callback)
|
|
|
|
if callbacks:size() == 0 then
|
|
|
|
self.subscriptions[event] = nil
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
--check if there is a subscriber for a given event
|
2023-12-30 06:22:17 +00:00
|
|
|
function pubsub:has_subscriber(event)
|
2021-03-12 10:23:54 +00:00
|
|
|
return self.subscriptions[event] ~= nil
|
|
|
|
end
|
|
|
|
|
|
|
|
return pubsub
|