From a0580893c4b314e7dea5bfdbcd8b64e83ff2c3cf Mon Sep 17 00:00:00 2001 From: Iuri Matias Date: Fri, 8 May 2020 16:18:01 -0400 Subject: [PATCH] basic application state; basic subscriber and dispatcher --- src/applicationLogic.nim | 16 +++++++++++++++- src/state.nim | 25 +++++++++++++++++++++++++ 2 files changed, 40 insertions(+), 1 deletion(-) create mode 100644 src/state.nim diff --git a/src/applicationLogic.nim b/src/applicationLogic.nim index 5e81f5ef1c..7b075e6242 100644 --- a/src/applicationLogic.nim +++ b/src/applicationLogic.nim @@ -2,7 +2,7 @@ import NimQml import status import libstatus import json - +import state var signalHandler: SignalCallback = proc(p0: cstring): void = setupForeignThreadGc() @@ -37,7 +37,21 @@ QtObject: result.accountResult = status.queryAccounts() status.subscribeToTest() + var appState = state.newAppState() + echo appState.title + appState.subscribe(proc () = + echo "1nd subscriber got a new update!" + ) + appState.addChannel("test") + + appState.subscribe(proc () = + echo "2nd subscriber got a new update!" + ) + + appState.addChannel("test2") + for channel in appState.channels: + echo channel.name # ¯\_(ツ)_/¯ dunno what is this proc setup(self: ApplicationLogic) = diff --git a/src/state.nim b/src/state.nim new file mode 100644 index 0000000000..0d28eb88e7 --- /dev/null +++ b/src/state.nim @@ -0,0 +1,25 @@ + + +type Channel = object + name*: string +type + Subscriber* = proc () + +type AppState* = ref object + title*: string + channels*: seq[Channel] + subscribers*: seq[Subscriber] + +proc newAppState*(): AppState = + result = AppState(title: "hello") + +proc subscribe*(self: AppState, subscriber: Subscriber) = + self.subscribers.add(subscriber) + +proc dispatch*(self: AppState) = + for subscriber in self.subscribers: + subscriber() + +proc addChannel*(self: AppState, name: string) = + self.channels.add(Channel(name: name)) + self.dispatch()