module A simple, uni-directional data flow architecture. All global functions inside this module can be used on all statefuls.
We start by defining a model. A model is a record with an initial state and an update function.
The update function takes a state and a message and returns an Update
record.
In this example we have a simple counter model.
import apps
enum Message {
data Inc
data Dec
}
let model = apps.defineModel 0, { state, msg =>
with msg, type apps.Message {
Inc: { _ => apps.Update state + 1, Nil },
Dec: { _ => apps.Update state - 1, Nil },
}
}
let view = apps.render model, { dispatch, state =>
print strings.join ["Current state: ", state]
}
import tests
tests.test "apps", { fail =>
apps.send view, Inc
unless (apps.currentState view) == 1, fail "should be 1"
}
- data Batch
- enum Command
- enum Message
- data Model
- data Quit
- enum Stateful
- data Store
- data Update
- data View
- func currentState stateful
- func defineModel initial, update
- func render model, view
- func send stateful, cmd
- func storeFrom model
- func subscribe observer, stateful
- func update stateful, msg
data A batch of Commands
cmds
enum A command can be either a message, a batch of messages or an async message.
enum
The messages any stateful can handle.
prelude.Nil
and prelude.None
are used to indicate that no message should be send.
prelude.Any
is used to indicate that any message should be send.
data A model defines the abstract and underlaying behaviour of a stateful. It has an initial state and an update function.
initial
- The initial stateupdate state, msg
- The update function. Takes a state and a message and returns anUpdate
record.
data Quits the whole program
enum Represents all statefuls like Stores and Views.
data A stateful which holds a state and can be observed.
model
- The Modelstates
- A rx.Variable of a Stateobservers
- A rx.Variable of functions to be notified.
data The update record includes the new state after applying a message and a command to be send to the store.
state
- The new statecmd
- A command to be send to the store.
data A stateful which renders a view. The result of the view differs from use case to use case.
Whenever you have one representation of your whole state, the View
is the right choice.
store
- The underlying and observed store.view dispatch, state
- A function which renders the current state. Mightdispatch cmd
.
func currentState stateful
Returns the current state of a stateful.
func defineModel initial, update
Creates a new model.
func render model, view
Creates a new view by rendering a store on every change.
func send stateful, cmd
Sends an eventually async Command to a stateful. Always returns a Result.
func storeFrom model
Creates a new store from a model. This essentially adds dynamic behaviour to a model.
func subscribe observer, stateful
Observes changes of a stateful.
func update stateful, msg
Directly applies a message to a stateful.