-
Notifications
You must be signed in to change notification settings - Fork 60
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: move SubscriptionManager under waku_core (#2025)
* chore: cherry-pick from Filter V2 RestApi PR: move FilterPushHandler and SubscriptionManager from Filter V1 to under waku_core
- Loading branch information
1 parent
ebe715e
commit 563b2b2
Showing
5 changed files
with
72 additions
and
45 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
import | ||
./subscription/subscription_manager, | ||
./subscription/push_handler | ||
|
||
export | ||
subscription_manager, | ||
push_handler |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
when (NimMajor, NimMinor) < (1, 4): | ||
{.push raises: [Defect].} | ||
else: | ||
{.push raises: [].} | ||
|
||
import | ||
chronos | ||
|
||
import | ||
../topics, | ||
../message | ||
|
||
type FilterPushHandler* = proc(pubsubTopic: PubsubTopic, message: WakuMessage) {.async, gcsafe, closure.} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
when (NimMajor, NimMinor) < (1, 4): | ||
{.push raises: [Defect].} | ||
else: | ||
{.push raises: [].} | ||
|
||
import | ||
std/tables, | ||
stew/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() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters