mirror of
https://github.com/logos-messaging/logos-messaging-nim.git
synced 2026-01-09 01:13:08 +00:00
* bump_dependencies.md: add nim-results dependency * change imports stew/results to results * switching to Nim 2.0.8 * waku.nimble: reflect the requirement nim 1.6.0 to 2.0.8 Adding --mm:refc as nim 2.0 enables a new garbage collector that we're not yet ready to support * adapt waku code to Nim 2.0 * gcsafe adaptations because Nim 2.0 is more strict
53 lines
1.4 KiB
Nim
53 lines
1.4 KiB
Nim
{.push raises: [].}
|
|
|
|
import std/tables, results, chronicles, chronos
|
|
|
|
import ./push_handler, ../topics, ../message
|
|
|
|
## Subscription manager
|
|
type SubscriptionManager* = object
|
|
subscriptions: TableRef[(string, ContentTopic), FilterPushHandler]
|
|
|
|
proc init*(T: type SubscriptionManager): T =
|
|
SubscriptionManager(
|
|
subscriptions: newTable[(string, ContentTopic), FilterPushHandler]()
|
|
)
|
|
|
|
proc clear*(m: var SubscriptionManager) =
|
|
m.subscriptions.clear()
|
|
|
|
proc registerSubscription*(
|
|
m: SubscriptionManager,
|
|
pubsubTopic: PubsubTopic,
|
|
contentTopic: ContentTopic,
|
|
handler: FilterPushHandler,
|
|
) =
|
|
try:
|
|
# TODO: Handle over subscription surprises
|
|
m.subscriptions[(pubsubTopic, contentTopic)] = handler
|
|
except CatchableError:
|
|
error "failed to register filter subscription", error = getCurrentExceptionMsg()
|
|
|
|
proc removeSubscription*(
|
|
m: SubscriptionManager, pubsubTopic: PubsubTopic, contentTopic: ContentTopic
|
|
) =
|
|
m.subscriptions.del((pubsubTopic, contentTopic))
|
|
|
|
proc notifySubscriptionHandler*(
|
|
m: SubscriptionManager,
|
|
pubsubTopic: PubsubTopic,
|
|
contentTopic: ContentTopic,
|
|
message: WakuMessage,
|
|
) =
|
|
if not m.subscriptions.hasKey((pubsubTopic, contentTopic)):
|
|
return
|
|
|
|
try:
|
|
let handler = m.subscriptions[(pubsubTopic, contentTopic)]
|
|
asyncSpawn handler(pubsubTopic, message)
|
|
except CatchableError:
|
|
discard
|
|
|
|
proc getSubscriptionsCount*(m: SubscriptionManager): int =
|
|
m.subscriptions.len()
|