persist document to database after 2 seconds of no updates, initialize with current state when joining genserver

This commit is contained in:
2023-11-24 20:24:40 -05:00
parent 52a7a64d23
commit 1ec5459a79
5 changed files with 50 additions and 12 deletions

View File

@@ -1,6 +1,5 @@
defmodule PoexWeb.PadLive do
alias Poex.Pads
alias Poex.Pads.Document
alias Poex.Pads.{Document, DocumentDynamicSupervisor}
alias Poex.Repo
alias Poex.Utils
@@ -12,7 +11,7 @@ defmodule PoexWeb.PadLive do
Document.changeset(%Document{}, %{title: "Untitled"})
|> Repo.insert()
Poex.Pads.DocumentDynamicSupervisor.start_document_supervisor(new_document)
DocumentDynamicSupervisor.start_document_supervisor(new_document)
# Redirect to the new document with its ID
{:ok, push_navigate(socket, to: ~p"/pad/#{new_document.id}", replace: true)}
@@ -22,11 +21,13 @@ defmodule PoexWeb.PadLive do
%{
id: id,
title: title,
state: state
} = Repo.get!(Document, id)
contents: contents
} = document = Repo.get!(Document, id)
DocumentDynamicSupervisor.start_document_supervisor(document)
# init editor and assigns with latest state from doc
{:ok, assign(socket, id: id, title: title, state: state |> Utils.atomize_keys())}
{:ok, assign(socket, id: id, title: title, contents: contents |> Utils.atomize_keys())}
end
def handle_info(:new_document, socket) do