Skip to content

Commit 8a3531f

Browse files
committed
Add store javascript module to act as backing for agent
1 parent b14be10 commit 8a3531f

File tree

4 files changed

+42
-15
lines changed

4 files changed

+42
-15
lines changed

lib/elixir_script/prelude/agent.ex

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,42 @@
11
defmodule ElixirScript.Agent do
22

33
def start(fun) do
4-
pid = Elixir.Core.processes.spawn(fn() -> end)
5-
Elixir.Core.processes.put(pid, "state", fun.());
4+
pid = Elixir.Core.Functions.new_pid()
5+
Elixir.Core.Store.create(pid, fun.())
66
{ :ok, pid }
77
end
88

99
def start(fun, options) do
10-
pid = Elixir.Core.processes.spawn(fn() ->
11-
end)
12-
1310
pid = if Elixir.Keyword.has_key?(options, :name) do
14-
Elixir.Core.processes.register(Elixir.Keyword.get(options, :name), pid)
11+
Elixir.Keyword.get(options, :name)
1512
else
16-
pid
13+
Elixir.Core.Functions.new_pid()
1714
end
1815

19-
Elixir.Core.processes.put(pid, "state", fun.())
16+
Elixir.Core.Store.create(pid, fun.())
2017
{ :ok, pid }
2118
end
2219

2320
def stop(agent) do
24-
Elixir.Core.processes.exit(agent)
21+
Elixir.Core.Store.remove(agent)
2522
:ok
2623
end
2724

2825
def update(agent, fun) do
29-
current_state = Elixir.Core.processes.get(agent, "state")
30-
Elixir.Core.processes.put(agent, "state", fun.(current_state));
26+
current_state = Elixir.Core.Store.read(agent)
27+
Elixir.Core.Store.update(agent, fun.(current_state))
3128
:ok
3229
end
3330

3431
def get(agent, fun) do
35-
current_state = JS.global().processes.get(agent, "state")
32+
current_state = Elixir.Core.Store.read(agent)
3633
fun.(current_state)
3734
end
3835

3936
def get_and_update(agent, fun) do
40-
current_state = Elixir.Core.processes.get(agent, "state")
37+
current_state = Elixir.Core.Store.read(agent)
4138
{val, new_state} = fun.(current_state)
42-
Elixir.Core.processes.put(agent, "state", new_state);
39+
Elixir.Core.Store.update(agent, new_state)
4340
val
4441
end
4542

src/javascript/lib/core.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import Patterns from 'tailored';
33
import ErlangTypes from 'erlang-types';
44
import Functions from './core/functions';
55
import SpecialForms from './core/special_forms';
6+
import Store from './core/store';
67

78
let processes = new Processes.ProcessSystem();
89

@@ -19,5 +20,6 @@ export default {
1920
Integer,
2021
Float,
2122
Functions,
22-
SpecialForms
23+
SpecialForms,
24+
Store
2325
};

src/javascript/lib/core/functions.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -400,6 +400,10 @@ function maps_from_list(list){
400400
return Object.freeze(m);
401401
}
402402

403+
function new_pid(){
404+
return new Core.PID();
405+
}
406+
403407
export default {
404408
call_property,
405409
run,

src/javascript/lib/core/store.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
let store = new Map();
2+
3+
function create(key, value){
4+
store.set(key, value);
5+
}
6+
7+
function update(key, value){
8+
store.set(key, value);
9+
}
10+
11+
function read(key){
12+
return store.get(key);
13+
}
14+
15+
function remove(key){
16+
return store.delete(key);
17+
}
18+
19+
export default {
20+
create,
21+
read,
22+
update,
23+
remove
24+
};

0 commit comments

Comments
 (0)