Skip to content
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

Add live-reload to new_example #19

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

axelson
Copy link
Collaborator

@axelson axelson commented Nov 9, 2018

The heavy lifting is done by exsync which supports both Mac and Linux
https://github.com/falood/exsync

Architecture

When exsync detects a file system change it recompiles the changed filed into
the BEAM. After this recompilation sequence finishes, it calls the configured
callback function which is handled in the Nav component. The Nav component
then restarts scenic by killing it's root process.

If there was an easy/non-invasive way to get the current scene then we could
restart just that instead of the entire process tree.

Note: currently relies on an unreleased version of exsync.

The heavy lifting is done by exsync which supports both Mac and Linux
https://github.com/falood/exsync

Architecture

When exsync detects a file system change it recompiles the changed filed into
the BEAM. After this recompilation sequence finishes, it calls the configured
callback function which is handled in the `Nav` component. The `Nav` component
then restarts scenic by killing it's root process.

If there was an easy/non-invasive way to get the current scene then we could
restart just that instead of the entire process tree.

Note: currently relies on an unreleased version of exsync.
@codecov
Copy link

codecov bot commented Nov 9, 2018

Codecov Report

Merging #19 into master will not change coverage.
The diff coverage is n/a.

Impacted file tree graph

@@          Coverage Diff           @@
##           master     #19   +/-   ##
======================================
  Coverage    96.5%   96.5%           
======================================
  Files           4       4           
  Lines         143     143           
======================================
  Hits          138     138           
  Misses          5       5

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 3c5fdc5...de26f2c. Read the comment docs.

@boydm
Copy link
Collaborator

boydm commented Nov 13, 2018

This is going to take me a while to think through. Not sure I want to take a dependency on exsync. There are Erlang apis to do similar tasks and may go that direction instead.

No matter how it goes, love the research on this!

@boydm
Copy link
Collaborator

boydm commented Nov 20, 2018

Currently thinking Live Reload type functionality should exist in an optional package. Still thinking about the overall approach.

@axelson
Copy link
Collaborator Author

axelson commented Nov 21, 2018

Yeah, that's the direction I was leaning towards as well, although we may need some API changes in scenic itself to support it cleanly. I've been playing with a more general approach (although still dependent on exsync) that is suitable to an external library and I think that it would be helpful if Scenic supported a way to get the pids of the active top-level scenes.

Here is a code snippet that has been working well for me so far (but obviously depends on Scenic internals):

  def reload_scenes() do
    scene_pids()
    |> Enum.each(fn
      {:ok, pid} ->
        Process.exit(pid, :kill)

      _ ->
        nil
    end)
  end

  # WARNING: This uses a private Scenic API
  defp scene_pids() do
    scenes_table = Scenic.ViewPort.Tables.scenes_table()

    :ets.match_object(scenes_table, :"$1")
    |> Enum.map(fn {_ref, registration} -> scene_pid(registration) end)
  end

  defp scene_pid({scene_pid, _child_pid, _supervisor_pid}) do
    {:ok, scene_pid}
  end

  defp scene_pid(other), do: {:error, :not_found}

@boydm
Copy link
Collaborator

boydm commented Nov 22, 2018

I'm willing to expose/change the necessary api for this. Hope to get a large enough block of time to work on it - prob tomorrow.

@axelson
Copy link
Collaborator Author

axelson commented Nov 24, 2018

Sounds great! If you want to play with my scene reloading code above you can find it at axelson/scenic_asteroids@3481230

I'll clean it up before I merge it to master, but it has been working in the use-cases that I've tried so far. I investigated using set_root to get the root of the graph but it didn't end up fruitful.

@axelson
Copy link
Collaborator Author

axelson commented Jul 20, 2019

@boydm I've further cleaned up my generic scene reloader into a re-usable library: https://github.com/axelson/scenic_live_reload/

Are you still willing to expose an api to get the pid of the root scene? Or provide a different way to reload the root scene?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants