Skip to content

Commit

Permalink
Feat: better handling for task ids in handle_cast
Browse files Browse the repository at this point in the history
  • Loading branch information
mindreframer committed Nov 9, 2023
1 parent 5bae881 commit 3e16169
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 22 deletions.
34 changes: 28 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,38 @@ Surrealix.live(pid, "person", true)
## for more complex LIVE queries
Surrealix.query(pid, "LIVE SELECT * FROM person;")
Surrealix.query(pid, "LIVE SELECT DIFF FROM person;")
```

```elixir
## Example with live query callbacks


## start dispatch registry

Surrealix.Dispatch.attach("first", [:live_query], fn (event, data, config)-> IO.inspect({:res, event, data}) end)
Surrealix.Dispatch.remove("first")
## try running following query in the SurrealDB shell: `create person:1 set name = "John"`
{:ok, pid} = Surrealix.start_link(namespace: "test", database: "test")
Surrealix.signin(pid, %{user: "root", pass: "root"})
Surrealix.use(pid, "test", "test")
{:ok,
%{
"result" => [
%{
"result" => lq_id,
}
]
}} = Surrealix.query(pid, "LIVE SELECT * FROM person;")

event = [:live_query, lq_id]
Surrealix.Dispatch.attach("logger", event, fn (event, data, config) ->
IO.puts ">>>>>> CALLBACK WORKS!"
IO.inspect(event, label: :EVENT)
IO.inspect(data, label: :DATA)
end)

## now change data in the person table
Surrealix.query(pid, "update person:2 set name = 1")

## to remove the callback
# Surrealix.Dispatch.remove_by_event(event)
```


## Installation

If [available in Hex](https://hex.pm/docs/publish), the package can be installed
Expand Down
8 changes: 6 additions & 2 deletions lib/surrealix/dispatch.ex
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,11 @@ defmodule Surrealix.Dispatch do
HandlerTable.insert(id, event, fun, config)
end

def remove(id) do
HandlerTable.remove(id)
def remove_by_id(id) do
HandlerTable.remove_by_id(id)
end

def remove_by_event(id) do
HandlerTable.remove_by_event(id)
end
end
6 changes: 5 additions & 1 deletion lib/surrealix/handler_table.ex
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,14 @@ defmodule Surrealix.HandlerTable do
end
end

def remove(id) do
def remove_by_id(id) do
:ets.match_delete(@table, {id, :_, :_, :_})
end

def remove_by_event(event) do
:ets.match_delete(@table, {:_, event, :_, :_})
end

# Debug / dev functions #############
def delete_all() do
:ets.delete_all_objects(@table)
Expand Down
28 changes: 15 additions & 13 deletions lib/surrealix/socket.ex
Original file line number Diff line number Diff line change
Expand Up @@ -46,29 +46,31 @@ defmodule Surrealix.Socket do
exit(:normal)
end

def handle_cast(caller, state) do
def handle_cast({method, args, id, task}, state) do
Logger.debug("[surrealix] [handle_cast] #{inspect(state)}")
{method, args, id} = caller

payload = build_cast_payload(method, args, id)

state = SocketState.add_task(state, id, task)
frame = {:text, payload}
{:reply, frame, args}
{:reply, frame, state}
end

def handle_frame({_type, msg}, state) do
task = Keyword.get(state, :__receiver__)
json = Jason.decode!(msg)
id = Map.get(json, "id")

if not Process.alive?(task.pid) do
Surrealix.Dispatch.execute([:live_query], json)
end

if Process.alive?(task.pid) do
Process.send(task.pid, {:ok, json, id}, [])
task = SocketState.get_task(state, id)

if is_nil(task) do
# there is no registered task for this ID, it must be a live query update!
lq_id = get_in(json, ["result", "id"])
Surrealix.Dispatch.execute([:live_query, lq_id], json)
else
if Process.alive?(task.pid) do
Process.send(task.pid, {:ok, json, id}, [])
end
end

state = SocketState.delete_task(state, id)
{:ok, state}
end

Expand All @@ -93,7 +95,7 @@ defmodule Surrealix.Socket do
end)

args = Keyword.merge([__receiver__: task], args)
WebSockex.cast(pid, {method, args, id})
WebSockex.cast(pid, {method, args, id, task})

task_timeout = Keyword.get(opts, :timeout, :infinity)
res = Task.await(task, task_timeout)
Expand Down

0 comments on commit 3e16169

Please sign in to comment.