@@ -61,7 +61,9 @@ defmodule GenServer do
6161
6262 Every time you do a `GenServer.call/3`, the client will send a message
6363 that must be handled by the `c:handle_call/3` callback in the GenServer.
64- A `cast/2` message must be handled by `c:handle_cast/2`.
64+ A `cast/2` message must be handled by `c:handle_cast/2`. There are 7 possible
65+ callbacks to be implemented when you use a `GenServer`. The only required
66+ callback is `init/1`.
6567
6668 ## Client / Server APIs
6769
@@ -111,14 +113,33 @@ defmodule GenServer do
111113 the same module. If the server and/or client implementations are growing
112114 complex, you may want to have them in different modules.
113115
114- ## use GenServer and callbacks
116+ ## How to supervise
115117
116- There are 7 callbacks to be implemented when you use a `GenServer`.
117- The only required callback is `init/1`.
118+ A `GenServer` is most commonly started under a supervision tree.
119+ When we invoke `use GenServer`, it automatically defines a `child_spec/1`
120+ function that allows us to start the `Stack` directly under a supervisor.
121+ To start a default stack of `[:hello]` under a supervisor, one may do:
118122
119- `use GenServer` also defines a `child_spec/1` function, allowing the
120- defined module to be put under a supervision tree. The generated
121- `child_spec/1` can be customized with the following options:
123+ children = [
124+ {Stack, [:hello]}
125+ ]
126+
127+ Supervisor.start_link(children, strategy: :one_for_all)
128+
129+ Note you can also start it simply as `Stack`, which is the same as
130+ `{Stack, []}`:
131+
132+ children = [
133+ Stack # The same as {Stack, []}
134+ ]
135+
136+ Supervisor.start_link(children, strategy: :one_for_all)
137+
138+ In both cases, `Stack.start_link/1` is alwaus invoked.
139+
140+ `use GenServer` also accepts a list of options which configures the
141+ child specification and therefore how it runs under a supervisor.
142+ The generated `child_spec/1` can be customized with the following options:
122143
123144 * `:id` - the child specification identifier, defaults to the current module
124145 * `:start` - how to start the child process (defaults to calling `__MODULE__.start_link/1`)
0 commit comments