-
Notifications
You must be signed in to change notification settings - Fork 10
LocalLiveView POC #282
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
LocalLiveView POC #282
Conversation
85a5012 to
faab7d5
Compare
mat-hek
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
TBC
| transport_pid: nil, | ||
| sticky?: false | ||
|
|
||
| # @typedoc "Struct returned when `assigns` is not in the socket." |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Lot's of commented-out code in here. I don't think we want it to be this way 🤔
examples/local_live_view/lib/local_live_view/local_component.ex
Outdated
Show resolved
Hide resolved
examples/local_live_view/lib/local_live_view/local_live_view.ex
Outdated
Show resolved
Hide resolved
81bf8ac to
cd71ec0
Compare
57f5ac6 to
ae49fdd
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
- Compiling local_live_view generates a ton of warnings
- A brief overview of the architecture would be very useful for someone unfamiliar with LiveView internals
| @@ -0,0 +1,176 @@ | |||
| # defprotocol Phoenix.HTML.Safe do | |||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If there are any changes to the copied files, let's tag them and shortly describe their purpose, like we do in the patches
|
|
||
| defp f(n, p \\ 2) do | ||
| n | ||
| |> inspect |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please format the code and enforce formatting on the CI in the local live view and all examples
| let elements = document.querySelectorAll('[#{attribute}]'); | ||
| elements = Array.from(elements) | ||
| const found = elements.find((element) => element.getAttribute("#{attribute}") == args.view); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Wouldn't something like this work?
| let elements = document.querySelectorAll('[#{attribute}]'); | |
| elements = Array.from(elements) | |
| const found = elements.find((element) => element.getAttribute("#{attribute}") == args.view); | |
| const element = document.querySelectorAll(`[#{attribute}=${args.view}]`); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ok
| ``` | ||
| cd priv/demo_modal_offline | ||
| mix popcorn.cook | ||
| cd ../.. | ||
| ``` | ||
| * Run `mix setup` to install and setup dependencies |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
mix deps.getis missing- Maybe
mix setupof the parent project could set up the internal project as well? - I don't think the internal project needs to be in
priv- it could be just in its directory under the root - I'd name the internal project
compare_live_views_localand keep it in thelocaldirectory - After running
mix setupI get the following error:
✘ [ERROR] Could not resolve "phoenix-colocated/compare_live_views"
js/app.js:25:38:
25 │ ...oks as colocatedHooks} from "phoenix-colocated/compare_live_views"
╵ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
You can mark the path "phoenix-colocated/compare_live_views" as external to exclude it from the bundle, which will remove this error and leave the unresolved path in the bundle.
examples/local_thermostat/README.md
Outdated
| @@ -0,0 +1,21 @@ | |||
| # LocalThermostat | |||
|
|
|||
| **TODO: Add description** | |||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👀
mat-hek
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Compiling local_live_view still generates warnings
| LocalLiveView should be used exactly like its Phoenix equivalent: | ||
| defmodule MyAppWeb.DemoLive do | ||
| use Phoenix.LiveView |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| use Phoenix.LiveView | |
| use LocalLiveView |
|
|
||
| defmodule LocalLiveView.Server do | ||
| @moduledoc false | ||
| # A LocalLiveView is a process that receives events, updates |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| # A LocalLiveView is a process that receives events, updates | |
| # A LocalLiveView.Server is a process that receives events, updates |
| - "!examples/**" | ||
| - "!misc/**" | ||
| - "!.github/**" | ||
| - ".github/workflows/popcorn_build_test.yml" | ||
| pull_request: | ||
| branches: ["**"] | ||
| paths: | ||
| - "**" | ||
| - "!examples/**" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
These changes will trigger Popcorn build & test when an example changes. What's the point? Instead, I'd add an action that checks if local_live_view and the dependent examples compile successfully and without warnings
| cd local/compare_live_views_local | ||
| mix deps.get | ||
| mix popcorn.cook | ||
| cd ../.. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is already done by mix setup
examples/compare_live_views/mix.exs
Outdated
|
|
||
| defp build_local(_) do | ||
| Mix.shell().cmd(""" | ||
| cd local/compare_live_views_local |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I meant just to put it in the local dir
| cd local/compare_live_views_local | |
| cd local |
|
|
||
| This is a demo project that illustrates the differences between Popcorn's Local Live View and Phoenix Live View behaviors. | ||
|
|
||
| The page is split into two halves, each covered by a modal. One modal is implemented using Local LiveView and the other with Phoenix LiveView. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's a bit sad that you can only close the modal :P Let's at least add a button to show the modal again
| @@ -0,0 +1,25 @@ | |||
| # CompareLiveViews | |||
|
|
|||
| This is a demo project that illustrates the differences between Popcorn's Local Live View and Phoenix Live View behaviors. | |||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When I run it I see no difference at all :D Let's add an option to configure simulated latency using LiveView APIs for that
| @@ -0,0 +1,18 @@ | |||
| defmodule LocalLiveView.JS do | |||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Either add moduledoc or @moduledoc false. Generally, generate documentation and see how it looks like
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
let's remove this
| attr :id, :string, doc: "the optional id of flash container" | ||
| attr :flash, :map, default: %{}, doc: "the map of flash messages to display" | ||
| attr :title, :string, default: nil | ||
| attr :kind, :atom, values: [:info, :error], doc: "used for styling and flash lookup" | ||
| attr :rest, :global, doc: "the arbitrary HTML attributes to add to the flash container" | ||
| attr(:id, :string, doc: "the optional id of flash container") | ||
| attr(:flash, :map, default: %{}, doc: "the map of flash messages to display") | ||
| attr(:title, :string, default: nil) | ||
| attr(:kind, :atom, values: [:info, :error], doc: "used for styling and flash lookup") | ||
| attr(:rest, :global, doc: "the arbitrary HTML attributes to add to the flash container") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
seems like something went wrong with the formatting
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I reverted those changes.
examples/compare_live_views/mix.exs
Outdated
| Mix.shell().cmd(""" | ||
| cd local | ||
| mix deps.get | ||
| mix compile | ||
| """) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This way there's a risk that the working directory stays changed and messes something. It's better to do this:
| Mix.shell().cmd(""" | |
| cd local | |
| mix deps.get | |
| mix compile | |
| """) | |
| Mix.shell().cmd(""" | |
| mix deps.get | |
| mix compile | |
| """, cd: "local") |
examples/eval_in_wasm/\
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
leftover?
| defmodule LocalLiveView.JS do | ||
| def rerender(rendered, view, attribute \\ "data-pop-view") do | ||
| @moduledoc """ | ||
| Module responsible for running JS in the browser. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Isn't it rather about rendering a local live view in DOM? Perhaps the module name could be better too 🤔
| defmodule MyAppWeb.DemoLive do | ||
| use LocalLiveView | ||
| def render(assigns) do | ||
| ~H""" | ||
| Hello world! | ||
| """ | ||
| end | ||
| end | ||
| The LocalLiveView can be added to a page with: | ||
| ``` | ||
| <div data-pop-view="DemoLive"></div> | ||
| ``` |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Either use backticks or indent
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Files in the wasm directory should be gitignored
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If this file is copied automatically, it should be gitignored
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This looks nice, but I'm worried about duplication with readme. WDYT about putting this into readme and adding readme as a main page in the docs?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think thats a good idea.
| @moduledoc """ | ||
| This process dispatches events to different LocalLiveViews present on the page. | ||
| It uses Popcorn API and registers as a main process to handle wasm messages inside Popcorn runtime. | ||
| Uses GenServer. | ||
| """ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As far as I understand, there's no need for the user to call dispatcher in any way, so it shouldn't be in the API. Actually, the only module that the user needs is LocalLiveView, so all the rest should be a private API (i.e. moduledoc -> moduledoc false & comment)
|
|
||
| Now you can visit [`localhost:4000`](http://localhost:4000) from your browser. | ||
|
|
||
| _The project enables Phoenix LiveView latency simulation by using_ `liveSocket.enableLatencySim(1000)` _to show response time difference between the LiveViews._ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd mention this on the demo page too, with a notice you can change it by calling liveSocket.enableLatencySim in the console
| ... | ||
| defp deps do | ||
| [ | ||
| {:local_live_view, path: path_to_popcorn <> "/examples/local_live_view"} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| {:local_live_view, path: path_to_popcorn <> "/examples/local_live_view"} | |
| {:local_live_view, github: "software-mansion/popcorn", sparse: "examples/local_live_view"} |
| The local part of the project is located in `local` directory. | ||
|
|
||
| The most important files to look at are: | ||
|
|
||
| ```compare_live_views/lib/compare_live_views_web/live/demo_modal_online.ex``` - Phoenix LiveView that represents online part of the demo and is rendered on the left side of the page. | ||
|
|
||
| ```compare_live_views/local/lib/demo_modal_offline.ex``` - LocalLiveView that represents offline part of the demo and is rendered on the right side of the page. | ||
|
|
||
| ```compare_live_views/lib/compare_live_views_web/controllers/page_html/home.html.heex``` - home page that renders both views. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| The local part of the project is located in `local` directory. | |
| The most important files to look at are: | |
| ```compare_live_views/lib/compare_live_views_web/live/demo_modal_online.ex``` - Phoenix LiveView that represents online part of the demo and is rendered on the left side of the page. | |
| ```compare_live_views/local/lib/demo_modal_offline.ex``` - LocalLiveView that represents offline part of the demo and is rendered on the right side of the page. | |
| ```compare_live_views/lib/compare_live_views_web/controllers/page_html/home.html.heex``` - home page that renders both views. | |
| The local part of the project is located in the `local` directory. | |
| The most important files to look at are: | |
| `compare_live_views/lib/compare_live_views_web/live/demo_modal_online.ex` - Phoenix LiveView that represents online part of the demo and is rendered on the left side of the page. | |
| `compare_live_views/local/lib/demo_modal_offline.ex` - LocalLiveView that represents offline part of the demo and is rendered on the right side of the page. | |
| `compare_live_views/lib/compare_live_views_web/controllers/page_html/home.html.heex` - home page that renders both views. |
| plug Phoenix.LiveReloader | ||
| plug Phoenix.CodeReloader | ||
| socket("/phoenix/live_reload/socket", Phoenix.LiveReloader.Socket) | ||
| plug(Phoenix.LiveReloader) | ||
| plug(Phoenix.CodeReloader) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Formatting still messed up
This is a POC of LocalLiveView the LiveView that implements LiveView functionality in the browser using Popcorn.
Right now the functionality is limited to click events and does not support heex template files yet.