basic application state; basic subscriber and dispatcher

This commit is contained in:
Iuri Matias 2020-05-08 16:18:01 -04:00
parent f5eed607bd
commit a0580893c4
2 changed files with 40 additions and 1 deletions

View File

@ -2,7 +2,7 @@ import NimQml
import status import status
import libstatus import libstatus
import json import json
import state
var signalHandler: SignalCallback = proc(p0: cstring): void = var signalHandler: SignalCallback = proc(p0: cstring): void =
setupForeignThreadGc() setupForeignThreadGc()
@ -37,7 +37,21 @@ QtObject:
result.accountResult = status.queryAccounts() result.accountResult = status.queryAccounts()
status.subscribeToTest() 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 # ¯\_(ツ)_/¯ dunno what is this
proc setup(self: ApplicationLogic) = proc setup(self: ApplicationLogic) =

25
src/state.nim Normal file
View File

@ -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()