Skip to content

Commit 0e890d5

Browse files
committed
upgrade to erlang and otp v1
1 parent 1a497a1 commit 0e890d5

17 files changed

+317
-349
lines changed

README.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,12 @@ pub fn main() {
2828
let assert Ok(session_b) = game.start(FireDice)
2929
let assert Ok(session_c) = game.start(PlayChip)
3030
31-
chip.register(registry, GroupA, session_a)
32-
chip.register(registry, GroupB, session_b)
33-
chip.register(registry, GroupA, session_c)
31+
chip.register(registry.data, GroupA, session_a.data)
32+
chip.register(registry.data, GroupB, session_b.data)
33+
chip.register(registry.data, GroupA, session_c.data)
3434
3535
chip.members(registry, GroupA, 50)
36-
|> list.each(fn(session) { game.next(session) })
36+
|> list.each(fn(session) { game.next(session.data) })
3737
}
3838
```
3939

gleam.toml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,9 @@ pages = [
1919

2020
[dependencies]
2121
gleam_stdlib = ">= 0.36.0 and < 2.0.0"
22-
gleam_erlang = ">= 0.24.0 and < 2.0.0"
23-
gleam_otp = ">= 0.10.0 and < 2.0.0"
24-
lamb = ">= 0.1.0 and < 2.0.0"
22+
gleam_erlang = ">= 1.0.0 and < 2.0.0"
23+
gleam_otp = ">= 1.0.0 and < 2.0.0"
24+
lamb = { git = "https://github.com/trescenzi/lamb.git", ref = "9b9b0e2" }
2525

2626
[dev-dependencies]
2727
gleeunit = ">= 1.0.0 and < 2.0.0"

manifest.toml

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

44
packages = [
5-
{ name = "benchee", version = "1.3.1", build_tools = ["mix"], requirements = ["deep_merge", "statistex", "table"], otp_app = "benchee", source = "hex", outer_checksum = "76224C58EA1D0391C8309A8ECBFE27D71062878F59BD41A390266BF4AC1CC56D" },
5+
{ name = "benchee", version = "1.4.0", build_tools = ["mix"], requirements = ["deep_merge", "statistex", "table"], otp_app = "benchee", source = "hex", outer_checksum = "299CD10DD8CE51C9EA3DDB74BB150F93D25E968F93E4C1FA31698A8E4FA5D715" },
66
{ name = "deep_merge", version = "1.0.0", build_tools = ["mix"], requirements = [], otp_app = "deep_merge", source = "hex", outer_checksum = "CE708E5F094B9CD4E8F2BE4F00D2F4250C4095BE93F8CD6D018C753894885430" },
7-
{ name = "gleam_erlang", version = "0.33.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "gleam_erlang", source = "hex", outer_checksum = "A1D26B80F01901B59AABEE3475DD4C18D27D58FA5C897D922FCB9B099749C064" },
8-
{ name = "gleam_otp", version = "0.16.0", build_tools = ["gleam"], requirements = ["gleam_erlang", "gleam_stdlib"], otp_app = "gleam_otp", source = "hex", outer_checksum = "FA0EB761339749B4E82D63016C6A18C4E6662DA05BAB6F1346F9AF2E679E301A" },
9-
{ name = "gleam_stdlib", version = "0.47.0", build_tools = ["gleam"], requirements = [], otp_app = "gleam_stdlib", source = "hex", outer_checksum = "3B22D46743C46498C8355365243327AC731ECD3959216344FA9CF9AD348620AC" },
10-
{ name = "gleeunit", version = "1.2.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "gleeunit", source = "hex", outer_checksum = "F7A7228925D3EE7D0813C922E062BFD6D7E9310F0BEE585D3A42F3307E3CFD13" },
11-
{ name = "lamb", version = "0.6.1", build_tools = ["gleam"], requirements = ["gleam_erlang", "gleam_stdlib"], otp_app = "lamb", source = "hex", outer_checksum = "A74714DE60B3BADB623DFFF910C843793AE660222A9AD63C70053D33C0C3D311" },
7+
{ name = "gleam_erlang", version = "1.1.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "gleam_erlang", source = "hex", outer_checksum = "D7A2E71CE7F6B513E62F9A9EF6DFDE640D9607598C477FCCADEF751C45FD82E7" },
8+
{ name = "gleam_otp", version = "1.0.0", build_tools = ["gleam"], requirements = ["gleam_erlang", "gleam_stdlib"], otp_app = "gleam_otp", source = "hex", outer_checksum = "7020E652D18F9ABAC9C877270B14160519FA0856EE80126231C505D719AD68DA" },
9+
{ name = "gleam_stdlib", version = "0.60.0", build_tools = ["gleam"], requirements = [], otp_app = "gleam_stdlib", source = "hex", outer_checksum = "621D600BB134BC239CB2537630899817B1A42E60A1D46C5E9F3FAE39F88C800B" },
10+
{ name = "gleeunit", version = "1.5.1", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "gleeunit", source = "hex", outer_checksum = "D33B7736CF0766ED3065F64A1EBB351E72B2E8DE39BAFC8ADA0E35E92A6A934F" },
11+
{ name = "lamb", version = "0.6.1", build_tools = ["gleam"], requirements = ["gleam_erlang", "gleam_stdlib"], source = "git", repo = "https://github.com/trescenzi/lamb.git", commit = "9b9b0e2593213f0f94a0950a1193a810a57f1f1b" },
1212
{ name = "statistex", version = "1.0.0", build_tools = ["mix"], requirements = [], otp_app = "statistex", source = "hex", outer_checksum = "FF9D8BEE7035028AB4742FF52FC80A2AA35CECE833CF5319009B52F1B5A86C27" },
1313
]
1414

1515
[requirements]
1616
benchee = { version = ">= 1.3.0 and < 2.0.0" }
17-
gleam_erlang = { version = ">= 0.24.0 and < 2.0.0" }
18-
gleam_otp = { version = ">= 0.10.0 and < 2.0.0" }
17+
gleam_erlang = { version = ">= 1.0.0 and < 2.0.0" }
18+
gleam_otp = { version = ">= 1.0.0 and < 2.0.0" }
1919
gleam_stdlib = { version = ">= 0.36.0 and < 2.0.0" }
2020
gleeunit = { version = ">= 1.0.0 and < 2.0.0" }
21-
lamb = { version = ">= 0.1.0 and < 2.0.0" }
21+
lamb = { git = "https://github.com/trescenzi/lamb.git", ref = "9b9b0e2" }

src/chip.gleam

Lines changed: 32 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,9 @@
33
//// automatically delist dead processes.
44

55
import gleam/dynamic
6-
import gleam/erlang
76
import gleam/erlang/atom
87
import gleam/erlang/process.{type Pid, type Subject}
9-
import gleam/function
8+
import gleam/erlang/reference.{type Reference}
109
import gleam/otp/actor
1110
import gleam/result.{try}
1211
import lamb.{Bag, Private, Protected, Public, Set}
@@ -47,9 +46,12 @@ pub type Named {
4746
/// ```gleam
4847
/// > let _ = chip.start(chip.Named("sessions"))
4948
/// ```
50-
pub fn start(named: Named) -> Result(Registry(msg, group), actor.StartError) {
51-
let init = fn() { init(named) }
52-
actor.start_spec(actor.Spec(init: init, init_timeout: 100, loop: loop))
49+
pub fn start(named: Named)
50+
-> Result(actor.Started(Subject(Message(a, b))), actor.StartError) {
51+
let init = fn(self) { init(self, named) }
52+
actor.new_with_initialiser(100, init)
53+
|> actor.on_message(loop)
54+
|> actor.start()
5355
}
5456

5557
/// Retrieves a previously named registry.
@@ -139,7 +141,7 @@ pub fn members(
139141
group: group,
140142
timeout: Int,
141143
) -> List(Subject(msg)) {
142-
let group_store = process.call(registry, GroupStore2(_), timeout)
144+
let group_store = process.call(registry, timeout, GroupStore2)
143145

144146
lamb.lookup(group_store, group)
145147
}
@@ -159,7 +161,7 @@ pub fn stop(registry: Registry(msg, group)) -> Nil {
159161
// Server Code ::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
160162

161163
type Monitor =
162-
erlang.Reference
164+
Reference
163165

164166
/// Chip's internal message type.
165167
pub opaque type Message(msg, group) {
@@ -185,10 +187,8 @@ type ProcessDown {
185187
ProcessDown(monitor: Monitor, pid: Pid)
186188
}
187189

188-
fn init(
189-
named: Named,
190-
) -> actor.InitResult(State(msg, group), Message(msg, group)) {
191-
let self = process.new_subject()
190+
fn init(self, named: Named) {
191+
//let self = process.new_subject()
192192

193193
let table = initialize_named_registries_store()
194194

@@ -206,16 +206,19 @@ fn init(
206206

207207
let selector =
208208
process.new_selector()
209-
|> process.selecting(self, function.identity)
210-
|> process.selecting_anything(process_down)
209+
|> process.select(self)
210+
|> process.select_other(process_down)
211211

212-
actor.Ready(state, selector)
212+
actor.initialised(state)
213+
|> actor.selecting(selector)
214+
|> actor.returning(self)
215+
|> Ok
213216
}
214217

215218
fn loop(
216-
message: Message(msg, group),
217219
state: State(msg, group),
218-
) -> actor.Next(Message(msg, group), State(msg, group)) {
220+
message: Message(msg, group),
221+
) -> actor.Next(State(msg, group), Message(msg, group)) {
219222
case message {
220223
GroupStore2(client) -> {
221224
// priority is given through selective receive
@@ -228,9 +231,15 @@ fn loop(
228231
Register(subject, group) -> {
229232
let pid = process.subject_owner(subject)
230233

231-
let Nil = monitor(state.monitors, pid)
232-
lamb.insert(state.monitors, pid, Nil)
233-
lamb.insert(state.groups, group, subject)
234+
case pid {
235+
Ok(pid) -> {
236+
let Nil = monitor(state.monitors, pid)
237+
lamb.insert(state.monitors, pid, Nil)
238+
lamb.insert(state.groups, group, subject)
239+
}
240+
Error(_) -> Nil
241+
//TODO what do we want to do in this case?
242+
}
234243

235244
state
236245
|> actor.continue()
@@ -256,7 +265,7 @@ fn loop(
256265
}
257266

258267
Stop -> {
259-
actor.Stop(process.Normal)
268+
actor.stop()
260269
}
261270
}
262271
}
@@ -311,7 +320,7 @@ fn monitor(monitors: lamb.Table(Pid, Nil), pid: Pid) -> Nil {
311320
case lamb.any(monitors, pid) {
312321
True -> Nil
313322
False -> {
314-
let _monitor = process.monitor_process(pid)
323+
let _monitor = process.monitor(pid)
315324
Nil
316325
}
317326
}
@@ -321,7 +330,7 @@ fn monitor(monitors: lamb.Table(Pid, Nil), pid: Pid) -> Nil {
321330
fn decode_down_message(message: dynamic.Dynamic) -> Result(ProcessDown, Nil)
322331

323332
fn schedulers() -> Int {
324-
ffi_system_info(atom.create_from_string("schedulers"))
333+
ffi_system_info(atom.create("schedulers"))
325334
}
326335

327336
type Option =
@@ -331,7 +340,7 @@ type Option =
331340
fn ffi_system_info(option: Option) -> Int
332341

333342
fn demonitor(reference: Monitor) -> Nil {
334-
let _ = ffi_demonitor(reference, [atom.create_from_string("flush")])
343+
let _ = ffi_demonitor(reference, [atom.create("flush")])
335344
Nil
336345
}
337346

test/artifacts/clock.gleam

Lines changed: 19 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
11
import chip
22
import gleam/erlang/process
3-
import gleam/function.{identity}
4-
import gleam/option
53
import gleam/otp/actor
6-
import gleam/otp/supervisor
74

85
pub opaque type Message {
96
Inc
@@ -18,19 +15,10 @@ pub type Group {
1815
}
1916

2017
pub fn start(registry: chip.Registry(Message, Group), group: Group, count: Int) {
21-
let init = fn() { init(registry, group, count) }
22-
actor.start_spec(actor.Spec(init: init, init_timeout: 10, loop: loop))
23-
}
24-
25-
pub fn childspec(count) {
26-
supervisor.worker(fn(param) {
27-
let #(registry, group) = param
28-
start(registry, group, count)
29-
})
30-
|> supervisor.returning(fn(param, _self) {
31-
let #(registry, group) = param
32-
#(registry, group, count)
33-
})
18+
let init = fn(self) { init(self, registry, group, count) }
19+
actor.new_with_initialiser(10, init)
20+
|> actor.on_message(loop)
21+
|> actor.start()
3422
}
3523

3624
pub fn stop(counter: process.Subject(Message)) -> Nil {
@@ -42,38 +30,39 @@ pub fn increment(counter: process.Subject(Message)) -> Nil {
4230
}
4331

4432
pub fn current(counter: process.Subject(Message)) -> Int {
45-
actor.call(counter, Current(_), 10)
33+
actor.call(counter, 10, Current)
4634
}
4735

48-
fn init(registry: chip.Registry(Message, Group), group: Group, count: Int) {
49-
// Create a reference to self
50-
let self = process.new_subject()
51-
36+
fn init(self, registry: chip.Registry(Message, Group), group: Group, count: Int) {
5237
// Register the counter under an id on initialization
5338
chip.register(registry, group, self)
5439

40+
let selector =
41+
process.new_selector()
42+
|> process.select(self)
43+
5544
// The registry may send messages through the self subject to this actor
5645
// adding self to this actor selector will allow us to handle those messages.
57-
actor.Ready(
58-
count,
59-
process.new_selector()
60-
|> process.selecting(self, identity),
61-
)
46+
47+
actor.initialised(count)
48+
|> actor.selecting(selector)
49+
|> actor.returning(self)
50+
|> Ok
6251
}
6352

64-
fn loop(message: Message, count: Int) {
53+
fn loop(count: Int, message: Message) {
6554
case message {
6655
Inc -> {
67-
actor.Continue(count + 1, option.None)
56+
actor.continue(count + 1)
6857
}
6958

7059
Current(client) -> {
7160
process.send(client, count)
72-
actor.Continue(count, option.None)
61+
actor.continue(count)
7362
}
7463

7564
Stop -> {
76-
actor.Stop(process.Normal)
65+
actor.stop()
7766
}
7867
}
7968
}

test/artifacts/game.gleam

Lines changed: 22 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
import chip
22
import gleam/erlang/process
3-
import gleam/function
43
import gleam/otp/actor
5-
import gleam/otp/supervisor
4+
import gleam/otp/supervision as supervisor
65

76
type SessionRegistry =
87
chip.Registry(Message, Int)
@@ -27,52 +26,55 @@ pub type Session {
2726
}
2827

2928
pub fn start(action) {
30-
actor.start(action, loop)
29+
actor.new(action)
30+
|> actor.on_message(loop)
31+
|> actor.start
3132
}
3233

33-
pub fn start_with(registry: SessionRegistry, id: Int, action: Action) {
34-
let init = fn() { init(registry, id, action) }
35-
actor.start_spec(actor.Spec(init: init, init_timeout: 10, loop: loop))
34+
pub fn start_with(id: Int, registry: SessionRegistry, action: Action) {
35+
let init = fn(self) { init(self, registry, id, action) }
36+
actor.new_with_initialiser(10, init)
37+
|> actor.on_message(loop)
38+
|> actor.start
3639
}
3740

38-
pub fn childspec(registry: SessionRegistry, action) {
39-
supervisor.worker(fn(id) { start_with(registry, id, action) })
40-
|> supervisor.returning(fn(id, _self) { id + 1 })
41+
pub fn childspec(id: Int, registry: SessionRegistry, action) {
42+
supervisor.worker(fn() { start_with(id, registry, action) })
4143
}
4244

4345
pub fn next(game: Game) -> Nil {
4446
actor.send(game, Next)
4547
}
4648

4749
pub fn current(game: Game) -> String {
48-
actor.call(game, Current(_), 100)
50+
actor.call(game, 100, Current)
4951
}
5052

5153
pub fn stop(game: Game) -> Nil {
5254
actor.send(game, Stop)
5355
}
5456

55-
fn init(registry, id, action) {
56-
// Create a reference to self
57-
let self = process.new_subject()
58-
57+
fn init(self, registry, id, action) {
5958
// Register the counter under an id on initialization
6059
chip.register(registry, id, self)
6160

6261
// The registry may send messages through the self subject to this actor
6362
// adding self to this actor selector will allow us to handle those messages.
64-
actor.Ready(
65-
action,
63+
let selector =
6664
process.new_selector()
67-
|> process.selecting(self, function.identity),
68-
)
65+
|> process.select(self)
66+
67+
actor.initialised(action)
68+
|> actor.selecting(selector)
69+
|> actor.returning(self)
70+
|> Ok
6971
}
7072

71-
fn loop(message, action) {
73+
fn loop(action, message) {
7274
case message {
7375
Next -> next_state(action)
7476
Current(client) -> send_unicode(client, action)
75-
Stop -> actor.Stop(process.Normal)
77+
Stop -> actor.stop()
7678
}
7779
}
7880

test/artifacts/pubsub.gleam

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import chip
22
import gleam/erlang/process
33
import gleam/list
4-
import gleam/otp/supervisor
4+
import gleam/otp/supervision
55

66
pub type PubSub(message, channel) =
77
chip.Registry(message, channel)
@@ -11,8 +11,7 @@ pub fn start() {
1111
}
1212

1313
pub fn childspec() {
14-
supervisor.worker(fn(_param) { start() })
15-
|> supervisor.returning(fn(_param, pubsub) { pubsub })
14+
supervision.worker(fn() { start() })
1615
}
1716

1817
pub fn subscribe(

0 commit comments

Comments
 (0)