2.7.1 Gall Walkthrough: Egg Timer
The Arvo operating system is divided up into modules called vanes. Gall is the vane responsible for providing an API for building stateful applications in Urbit. Gall is a complex system that takes a lot of getting used to, so let's dip our toes in with a simple egg-timer app.
|% +$ move (pair bone card) +$ card [%wait path @da] -- |_ [bowl:gall ~] ++ poke-noun |= t=@dr ^+ [*(list move) +>.$] :_ +>.$ :_ ~ [ost %wait /egg-timer (add now t)] ++ wake |= [=wire error=(unit tang)] ^+ [*(list card) +>.$] ~& "Timer went off!" [~ +>.$] --
The first thing to notice is that we are creating a
|%) and a
|_). This is a typical style of Gall programming where your types are defined in the first
core and your application is defined in the following
|% +$ card (pair bone move) +$ move [%wait path @da] --
core here defines two types:
card is a
bone is a Gall-only type that identifies app event chains by mapping atoms to them. We define
move to be a request to Arvo to do something for us. In this case, the only valid
move will be
%wait which we'll discuss in a bit.
It's important to note that the names
move are arbitrary; you can call them whatever you'd like. Commonly you will see them called
The sample of the
You can find the full definition of
sys/zuse.hoon, but for now it's enough to know that this is the default app state and includes various faces for information. Below are some important such faces:
ourThe ship this code is running on
srcThe ship the current event originated from
ostA reference to the current chain of events
nowThe current time
door we've made has two arms
wake. Gall is capable of dispatching
pokes, or requests, to an app based on the mark of the data given along with that poke. These are sent to the arm with the name that matches the mark of the data. Here we use the generic
++ poke-noun |= t=@dr ^+ [*(list card) +>.$] :_ +>.$ :_ ~ [ost %wait /egg-timer (add now t)]
In the above code, we create a gate that takes a single
@dr is an aura for a 128-bit relative date. Here are a few examples.
As a matter of good type hygiene, we explicitly cast the output of this gate with
^+ to ensure we are producing the correct thing for Gall to handle.
^+ is the rune for casting by example. Our example is a cell:
card, which we
*, is the head;
+>.$, the enclosing core which is our
door, is the tail.
Next we're going to use the
:_ rune which is just the inverted form a
:- the cell construction rune. We use it twice so the actual data will end up looking something like:
[[[ost %wait /egg-timer (add now t)] ~] +>.$]
ost is the
bone, the opaque reference to a chain of events, that comes from
bowl, so we're going to continue to use it here.
%wait is the name of the
move, in this case a request to the Behn vain to start a timer for us.
%wait, we have the
cords that serves as the unique identifier for this
/ here is an irregular syntax to make a
The final part of this
(add now t)
now is the current time of type
t was declared as
@dr. Because they are both atoms, we can add
t these two to get an atom that is
t units of time into the future from
now. That produced atom can be interpreted as a
That's all for our
poke-noun arm. But what about when the timer goes off? Behn will create a
gift, a similar construct to how we created an
card, only this time it will end up being dispatched back to us via Gall in the
++wake arm. Any app that wants to use a timer trigger needs to have an arm called
++ wake |= [=wire error=(unit tang)] ^+ [*(list card) +>.$] ~& "Timer went off!" [~ +>.$]
wake is a
gate that has two arguments: a
wire, and a
(unit tang) with the face
error. The syntax of
=wire is a shortcut for
wire=wire; it's a common pattern to shadow the name of a type when you only have one instance of the type and are not going to refer to the type itself. A
wire is just an alias for
wire will be the
path that we gave original
move. If we needed to do something based on which request caused this gate to be called, we could use the wire to do so. In this case, we don't do perform such a dispatch.
Next we have the same cast we used in
++poke-noun to make sure we are producing the correct thing for Gall. These casts are not strictly necessary, as the type system can infer what the type will be, but they can be very useful both for debugging our own code and for someone else trying to determine what our code should be producing.
~& "Timer went off!"
~& is the debugging printf rune. Here we're using it to output a message. We could, however, modify this line of code to do any other computation we would want to happen when
++wait gets called.
Finally, we need to produce our
card. Here the effect is simply
[~ +>.$], since we have no
move to make and we are not changing the state of our app. We just use the
door without changing its sample.