Thanks. I added your suggestion (note: a sprout is a post from the original tutorial), a bit modified to fit:
def delete_sprout(%Sprout{} = sprout) do
{:ok, %{sprout: deleted_sprout}} = Ecto.Multi.new() |> Ecto.Multi.delete(:sprout, sprout) |> Repo.transaction()
broadcast({:ok, deleted_sprout}, :sprout_deleted)
end
and the handlers are:
def handle_event("delete", %{"id" => id}, socket) do
IO.puts "HANDLE EVENT delete"
sprout = Timeline.get_sprout!(id)
{:ok, _} = Timeline.delete_sprout(sprout)
{:noreply, assign(socket, :sprouts, fetch_sprouts())}
end
# and
def handle_info({:sprout_deleted, sprout}, socket) do
IO.puts "HANDLE INFO :sprout_deleted"
{:noreply, assign(socket, :sprouts, fetch_sprouts())}
end
And the output is:
HANDLE EVENT delete
[debug] QUERY OK source="sprouts" db=5.4ms idle=1592.2ms
SELECT s0."id", s0."body", s0."likes_count", s0."reposts_count", s0."username", s0."inserted_at", s0."updated_at" FROM "sprouts" AS s0 WHERE (s0."id" = $1) [71]
[debug] QUERY OK db=0.7ms queue=0.5ms idle=1597.1ms
begin []
HANDLE INFO :sprout_deleted
[debug] QUERY OK db=0.7ms
DELETE FROM "sprouts" WHERE "id" = $1 [71]
[debug] QUERY OK db=0.8ms
commit []
HANDLE INFO :sprout_deleted
[debug] QUERY OK source="sprouts" db=0.6ms idle=1600.4ms
SELECT s0."id", s0."body", s0."likes_count", s0."reposts_count", s0."username", s0."inserted_at", s0."updated_at" FROM "sprouts" AS s0 ORDER BY s0."id" DESC []
[debug] QUERY OK source="sprouts" db=0.6ms idle=1600.7ms
SELECT s0."id", s0."body", s0."likes_count", s0."reposts_count", s0."username", s0."inserted_at", s0."updated_at" FROM "sprouts" AS s0 ORDER BY s0."id" DESC []
[debug] QUERY OK source="sprouts" db=0.4ms idle=1601.3ms
SELECT s0."id", s0."body", s0."likes_count", s0."reposts_count", s0."username", s0."inserted_at", s0."updated_at" FROM "sprouts" AS s0 ORDER BY s0."id" DESC []
As you can see, there is no error message – it just locks up. Based on the number of SELECT queries shown to reload the list; I’m now thinking it’s a runaway loop that gets killed.
NOTE that both handle_event
and handle_info
are calling fetch_sprouts()
to reload the posts. I’m thinking that this is a problem. Maybe I need to use PubSub.broadcast_from/5 which takes a PID as a from
argument; then, ignore the event if I’m the process that initiated the delete. I’ll experiment…