sigswift
sigswift copied to clipboard
A toy, event-driven command line app inspired by the Elm Architecture
sigswift
This was actually an excuse to design a completely event-driven terminal program in Swift. It's inspired by the Elm Architecture.
How it works:
-
Sets the terminal to raw mode, to enable processing input character-by-character instead of line-by-line.
-
Configures a Grand Central Dispatch (GCD)
DispatchReadSource
to read from standard input one character at a time. -
Configures a number of GCD
DispatchSignalSource
s to process signals as events. -
With the event handlers in place, models the application as an
App
struct that receives anEvent
, optionally updates its state, and dispatches an array ofAction
. -
No side effects are implemented in
App
, but are instead implemented in top-level functionshandle()
anddispatch()
, which convertAction
s into side effects.