HadesLang Doc
Search…
GenServer
GenServers are a concept directly taken from Elixir.
A GenServer is a process like any other Elixir process and it can be used to keep state, execute code asynchronously and so on. The advantage of using a generic server process (GenServer) implemented using this module is that it will have a standard set of interface functions and include functionality for tracing and error reporting. It will also fit into a supervision tree.
From the Elixir hexdocs
1
with GenServer from std:sync
2
with console from std:io
3
4
class Counter < GenServer
5
func! init(state)
6
put {:ok, state}
7
end
8
9
//If you want to use function matching, you have to explicetly state this
10
func! call(msg, from, state);
11
12
//We don't need ! since we already stated it above
13
func call(msg := :get, _, state)
14
put {:reply, state, state}
15
end
16
17
func! cast(msg, state);
18
19
func cast(msg := {:increment, number}, state)
20
put {:noreply, state + number}
21
end
22
end
23
24
try
25
var {:ok, ctrPid} = GenServer.start(Counter, 0)
26
27
send(ctrPid, {:increment, 10})
28
var nr = GenServer.call(ctrPid, :get)
29
console.out(nr) //Output: 10
30
catch(e)
31
//ignored
32
end
Copied!
Last modified 2yr ago
Copy link