fix: support multiple subscribers per signal
This commit is contained in:
parent
1a92cda0d9
commit
ed683fd2f0
|
@ -4,12 +4,16 @@ import tables
|
||||||
import json
|
import json
|
||||||
import types
|
import types
|
||||||
import messages
|
import messages
|
||||||
|
import chronicles
|
||||||
|
|
||||||
|
logScope:
|
||||||
|
topics = "signals"
|
||||||
|
|
||||||
QtObject:
|
QtObject:
|
||||||
type SignalsController* = ref object of QObject
|
type SignalsController* = ref object of QObject
|
||||||
app: QApplication
|
app: QApplication
|
||||||
statusSignal: string
|
statusSignal: string
|
||||||
signalSubscribers*: Table[SignalType, SignalSubscriber]
|
signalSubscribers*: Table[SignalType, seq[SignalSubscriber]]
|
||||||
variant*: QVariant
|
variant*: QVariant
|
||||||
|
|
||||||
# Constructor
|
# Constructor
|
||||||
|
@ -17,7 +21,7 @@ QtObject:
|
||||||
new(result)
|
new(result)
|
||||||
result.app = app
|
result.app = app
|
||||||
result.statusSignal = ""
|
result.statusSignal = ""
|
||||||
result.signalSubscribers = initTable[SignalType, SignalSubscriber]()
|
result.signalSubscribers = initTable[SignalType, seq[SignalSubscriber]]()
|
||||||
result.setup()
|
result.setup()
|
||||||
result.variant = newQVariant(result)
|
result.variant = newQVariant(result)
|
||||||
|
|
||||||
|
@ -31,20 +35,34 @@ QtObject:
|
||||||
self.QObject.delete
|
self.QObject.delete
|
||||||
|
|
||||||
proc addSubscriber*(self: SignalsController, signalType: SignalType, subscriber: SignalSubscriber) =
|
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) =
|
proc processSignal(self: SignalsController) =
|
||||||
let jsonSignal = (self.statusSignal).parseJson
|
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":
|
of "messages.new":
|
||||||
self.signalSubscribers[SignalType.Message].onSignal(messages.fromEvent(jsonSignal))
|
signalType = SignalType.Message
|
||||||
|
signal = messages.fromEvent(jsonSignal)
|
||||||
of "wallet":
|
of "wallet":
|
||||||
self.signalSubscribers[SignalType.Wallet].onSignal(WalletSignal(content: $jsonSignal))
|
signalType = SignalType.Wallet
|
||||||
|
signal = WalletSignal(content: $jsonSignal)
|
||||||
else:
|
else:
|
||||||
# TODO: log error?
|
warn "Unhandled signal received", type = signalString
|
||||||
discard
|
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.} =
|
proc statusSignal*(self: SignalsController): string {.slot.} =
|
||||||
result = self.statusSignal
|
result = self.statusSignal
|
||||||
|
|
Loading…
Reference in New Issue