-
Notifications
You must be signed in to change notification settings - Fork 0
/
scratchpad.exs
68 lines (54 loc) · 1.2 KB
/
scratchpad.exs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
alias :mnesia, as: Mnesia
Mnesia.create_schema([node()])
Mnesia.start()
Mnesia.create_table(State, [attributes: [:id, :tick, :index, :current]])
###
state = %State{previous: _previous, current: _current} = Cadex.state
Cadex.update(var)
_state_ = %State{previous: _previous_, current: current_} = Cadex.state
Mnesia.dirty_write({
State,
Kernel.inspect(tick) <> Kernel.inspect(index),
tick,
index,
current_
})
:sys.replace_state(context[:pid], fn _s -> state end)
###
{result, states} = Mnesia.transaction(
fn ->
Mnesia.select(
State,
[{
{State, :"$1", :"$2", :"$3", :"$4"},
[{:==, :"$2", tick}],
[:"$4"]
}]
)
end
)
case result do
:atomic ->
:sys.replace_state(context[:pid], fn s -> {s | states} end)
_ ->
:nothing
end
data = variables
|> Enum.with_index
|> Enum.map(fn({_var, index}) ->
Mnesia.transaction(fn -> Mnesia.match_object({State, tick, index, :_}) end)
end)
# delta_ = %{var => increment}
# state
# |> update_in([Access.key!(:delta), Access.key!(var)], fn(var) ->
# increment
# end)
# state
# |> struct(%{delta: delta_})
{
:noreply,
state
|> update_in([Access.key!(:delta), Access.key!(var)], fn ->
increment
end)
}