Skip to content

Commit

Permalink
Small refactor of prevew and pages lazy loading
Browse files Browse the repository at this point in the history
  • Loading branch information
allmarkedup committed Oct 23, 2024
1 parent cb4375b commit 03fc4c8
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 12 deletions.
20 changes: 10 additions & 10 deletions lib/lookbook/engine.rb
Original file line number Diff line number Diff line change
Expand Up @@ -68,12 +68,12 @@ class Engine < Rails::Engine
end

config.after_initialize do
unless opts.lazy_load_previews_and_pages
if Engine.reloading?
reloaders.add(:previews, Engine.preview_watch_paths, opts.listen_extensions, &Engine.method(:load_previews))
reloaders.add(:pages, Engine.page_watch_paths, opts.page_extensions, &Engine.method(:load_pages))
reloaders.execute
else
if Engine.reloading?
reloaders.add(:previews, Engine.preview_watch_paths, opts.listen_extensions, &Engine.method(:load_previews))
reloaders.add(:pages, Engine.page_watch_paths, opts.page_extensions, &Engine.method(:load_pages))
reloaders.execute unless opts.lazy_load_previews_and_pages
else
unless opts.lazy_load_previews_and_pages
Engine.load_previews
Engine.load_pages
end
Expand Down Expand Up @@ -203,8 +203,7 @@ def pages
@_loaded_pages = true

if reloading?
reloader = reloaders.add(:pages, Engine.page_watch_paths, opts.page_extensions, &Engine.method(:load_pages))
reloader.execute
reloaders.execute(:pages)
else
load_pages
end
Expand All @@ -220,8 +219,7 @@ def previews
@_loaded_previews = true

if reloading?
reloader = reloaders.add(:previews, Engine.preview_watch_paths, opts.listen_extensions, &Engine.method(:load_previews))
reloader.execute
reloaders.execute(:previews)
else
load_previews
end
Expand All @@ -236,6 +234,7 @@ def preview_controller
end

def load_previews(changes = nil)
puts "----------------------------- LOADING PREVIEWS"
changed_files = [*changes[:added], *changes[:modified]] if changes
parser.parse(changed_files) do |code_objects|
previews.load(code_objects.all(:class), changes)
Expand All @@ -246,6 +245,7 @@ def load_previews(changes = nil)
end

def load_pages(changes = nil)
puts "----------------------------- LOADING PAGES"
pages.load(Engine.page_paths, changes)
rescue => e
Lookbook.logger.error(e)
Expand Down
6 changes: 4 additions & 2 deletions lib/lookbook/reloaders.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,10 @@ def add(name, directories, extensions, &callback)
reloader
end

def execute
reloaders.each { |reloader| reloader.execute }
def execute(name = nil)
reloaders.each do |reloader|
reloader.execute if name.nil? || name == reloader.name
end
end

def register_changes(changes)
Expand Down

0 comments on commit 03fc4c8

Please sign in to comment.