fix: support multiple subscribers per signal

This commit is contained in:
Richard Ramos 2020-05-21 16:00:15 -04:00
parent 1a92cda0d9
commit ed683fd2f0
No known key found for this signature in database
GPG Key ID: 80D4B01265FDFE8F
1 changed files with 27 additions and 9 deletions

View File

@ -4,12 +4,16 @@ import tables
import json
import types
import messages
import chronicles
logScope:
topics = "signals"
QtObject:
type SignalsController* = ref object of QObject
app: QApplication
statusSignal: string
signalSubscribers*: Table[SignalType, SignalSubscriber]
signalSubscribers*: Table[SignalType, seq[SignalSubscriber]]
variant*: QVariant
# Constructor
@ -17,7 +21,7 @@ QtObject:
new(result)
result.app = app
result.statusSignal = ""
result.signalSubscribers = initTable[SignalType, SignalSubscriber]()
result.signalSubscribers = initTable[SignalType, seq[SignalSubscriber]]()
result.setup()
result.variant = newQVariant(result)
@ -31,20 +35,34 @@ QtObject:
self.QObject.delete
proc addSubscriber*(self: SignalsController, signalType: SignalType, subscriber: SignalSubscriber) =
self.signalSubscribers[signalType] = subscriber
if not self.signalSubscribers.hasKey(signalType):
self.signalSubscribers[signalType] = @[]
self.signalSubscribers[signalType].add(subscriber)
proc processSignal(self: SignalsController) =
let jsonSignal = (self.statusSignal).parseJson
let signalType = $jsonSignal["type"].getStr
let signalString = $jsonSignal["type"].getStr
case signalType:
var signalType: SignalType
var signal: Signal
case signalString:
of "messages.new":
self.signalSubscribers[SignalType.Message].onSignal(messages.fromEvent(jsonSignal))
signalType = SignalType.Message
signal = messages.fromEvent(jsonSignal)
of "wallet":
self.signalSubscribers[SignalType.Wallet].onSignal(WalletSignal(content: $jsonSignal))
signalType = SignalType.Wallet
signal = WalletSignal(content: $jsonSignal)
else:
# TODO: log error?
discard
warn "Unhandled signal received", type = signalString
signalType = SignalType.Unknown
if not self.signalSubscribers.hasKey(signalType):
self.signalSubscribers[signalType] = @[]
for subscriber in self.signalSubscribers[signalType]:
subscriber.onSignal(signal)
proc statusSignal*(self: SignalsController): string {.slot.} =
result = self.statusSignal