Skip to content

Commit d77088a

Browse files
committed
Improve documentation
1 parent dba4388 commit d77088a

File tree

4 files changed

+88
-13
lines changed

4 files changed

+88
-13
lines changed

README.md

Lines changed: 42 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,48 @@ Fault tolerant multi-core programs with OTP, the BEAM actor framework.
55
[![Package Version](https://img.shields.io/hexpm/v/gleam_otp)](https://hex.pm/packages/gleam_otp)
66
[![Hex Docs](https://img.shields.io/badge/hex-docs-ffaff3)](https://hexdocs.pm/gleam_otp/)
77

8+
```shell
9+
gleam add gleam_otp
10+
```
11+
```gleam
12+
import gleam/erlang/process.{type Subject}
13+
import gleam/otp/actor
14+
15+
pub fn main() {
16+
// Start an actor
17+
let assert Ok(actor) =
18+
actor.new(0)
19+
|> actor.on_message(handle_message)
20+
|> actor.start
21+
22+
// Send some messages to the actor
23+
actor.send(actor.data, Add(5))
24+
actor.send(actor.data, Add(3))
25+
26+
// Send a message and get a reply
27+
assert actor.call(actor.data, 10, Get) == 8
28+
}
29+
30+
pub fn handle_message(state: Int, message: Message) -> actor.Next(Int, Message) {
31+
case message {
32+
Add(i) -> {
33+
let state = state + i
34+
actor.continue(state)
35+
}
36+
Get(reply) -> {
37+
actor.send(reply, state)
38+
actor.continue(state)
39+
}
40+
}
41+
}
42+
43+
pub type Message {
44+
Add(Int)
45+
Get(Subject(Int))
46+
}
47+
```gleam
48+
49+
850
Gleam’s actor system is built with a few primary goals:
951
1052
- Full type safety of actors and messages.
@@ -21,14 +63,6 @@ Not all Erlang/OTP functionality is included in this library. Some is not
2163
possible to represent in a type safe way, so it is not included. Other features
2264
are still in development, such as further process supervision strategies.
2365
24-
## Usage
25-
26-
Add this library to your Gleam project.
27-
28-
```shell
29-
gleam add gleam_otp
30-
```
31-
3266
## Common types of actor
3367
3468
This library provides several different types of actor that can be used in

gleam.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ version = "1.0.0"
33
licences = ["Apache-2.0"]
44
description = "Fault tolerant multi-core programs with OTP, the BEAM actor framework."
55

6-
gleam = ">= 1.7.0"
6+
gleam = ">= 1.11.0"
77

88
repository = { type = "github", user = "gleam-lang", repo = "otp" }
99
links = [
@@ -13,7 +13,7 @@ links = [
1313

1414
[dependencies]
1515
gleam_stdlib = ">= 0.60.0 and < 2.0.0"
16-
gleam_erlang = ">= 1.0.0-rc1 and < 2.0.0"
16+
gleam_erlang = ">= 1.0.0 and < 2.0.0"
1717

1818
[dev-dependencies]
1919
gleeunit = ">= 1.0.0 and < 2.0.0"

manifest.toml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,14 @@
22
# You typically do not need to edit this file
33

44
packages = [
5-
{ name = "gleam_erlang", version = "1.0.0-rc1", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "gleam_erlang", source = "hex", outer_checksum = "6E0CF4E1F66E2C9226B7554589544F00F12CE14858440EB1BF7EFDACDE1BBC64" },
5+
{ name = "gleam_erlang", version = "1.0.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "gleam_erlang", source = "hex", outer_checksum = "7E6A5234F927C4B24F8054AB1E4572206C41F9E6D5C6C02273CB7531E7E5CED0" },
66
{ name = "gleam_stdlib", version = "0.60.0", build_tools = ["gleam"], requirements = [], otp_app = "gleam_stdlib", source = "hex", outer_checksum = "621D600BB134BC239CB2537630899817B1A42E60A1D46C5E9F3FAE39F88C800B" },
7-
{ name = "gleeunit", version = "1.4.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "gleeunit", source = "hex", outer_checksum = "12A8A2B510D7063547CD73AE5345544AE92124247A82F46B05F5B0913EE28BC8" },
7+
{ name = "gleeunit", version = "1.5.1", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "gleeunit", source = "hex", outer_checksum = "D33B7736CF0766ED3065F64A1EBB351E72B2E8DE39BAFC8ADA0E35E92A6A934F" },
88
{ name = "logging", version = "1.3.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "logging", source = "hex", outer_checksum = "1098FBF10B54B44C2C7FDF0B01C1253CAFACDACABEFB4B0D027803246753E06D" },
99
]
1010

1111
[requirements]
12-
gleam_erlang = { version = ">= 1.0.0-rc1 and < 2.0.0" }
12+
gleam_erlang = { version = ">= 1.0.0 and < 2.0.0" }
1313
gleam_stdlib = { version = ">= 0.60.0 and < 2.0.0" }
1414
gleeunit = { version = ">= 1.0.0 and < 2.0.0" }
1515
logging = { version = ">= 1.3.0 and < 2.0.0" }
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
pub fn main_test() {
2+
main()
3+
}
4+
5+
// ----- Example begins here --------------------------------------------------
6+
7+
import gleam/erlang/process.{type Subject}
8+
import gleam/otp/actor
9+
10+
pub fn main() {
11+
// Start an actor
12+
let assert Ok(actor) =
13+
actor.new(0)
14+
|> actor.on_message(handle_message)
15+
|> actor.start
16+
17+
// Send some messages to the actor
18+
actor.send(actor.data, Add(5))
19+
actor.send(actor.data, Add(3))
20+
21+
// Send a message and get a reply
22+
assert actor.call(actor.data, 10, Get) == 8
23+
}
24+
25+
pub fn handle_message(state: Int, message: Message) -> actor.Next(Int, Message) {
26+
case message {
27+
Add(i) -> {
28+
let state = state + i
29+
actor.continue(state)
30+
}
31+
Get(reply) -> {
32+
actor.send(reply, state)
33+
actor.continue(state)
34+
}
35+
}
36+
}
37+
38+
pub type Message {
39+
Add(Int)
40+
Get(Subject(Int))
41+
}

0 commit comments

Comments
 (0)