Skip to content

Commit

Permalink
Switch to tracking descendants manually
Browse files Browse the repository at this point in the history
  • Loading branch information
jaredcwhite committed Apr 13, 2024
1 parent ec0d3cf commit 4f02007
Show file tree
Hide file tree
Showing 8 changed files with 36 additions and 50 deletions.
19 changes: 11 additions & 8 deletions bridgetown-core/lib/bridgetown-core.rb
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@ def require_all(path)
require "active_support/core_ext/string/output_safety"
require "active_support/core_ext/string/starts_ends_with"
require "active_support/current_attributes"
require "active_support/descendants_tracker"
require "hash_with_dot_access"
require "addressable/uri"
require "liquid"
Expand Down Expand Up @@ -129,7 +128,6 @@ module Bridgetown
require_all "bridgetown-core/drops"
require_all "bridgetown-core/generators"
require_all "bridgetown-core/tags"
require_all "bridgetown-core/core_ext"

class << self
# Tells you which Bridgetown environment you are building in so
Expand Down Expand Up @@ -385,6 +383,7 @@ def build_errors_path
end

module Bridgetown
module CoreExt; end
module Model; end

module Resource
Expand All @@ -399,11 +398,15 @@ def self.register_extension(mod)
end
end

# This method is available in Ruby 3, monkey patching for older versions
Psych.extend Bridgetown::CoreExt::Psych::SafeLoadFile unless Psych.respond_to?(:safe_load_file)
Zeitwerk::Loader.new.tap do |loader|
loader.push_dir File.join(__dir__, "bridgetown-core/core_ext"), namespace: Bridgetown::CoreExt
loader.setup
loader.eager_load
end

loader = Zeitwerk::Loader.new
loader.push_dir File.join(__dir__, "bridgetown-core/model"), namespace: Bridgetown::Model
loader.push_dir File.join(__dir__, "bridgetown-core/resource"), namespace: Bridgetown::Resource
loader.setup # ready!
Zeitwerk::Loader.new.tap do |loader|
loader.push_dir File.join(__dir__, "bridgetown-core/model"), namespace: Bridgetown::Model
loader.push_dir File.join(__dir__, "bridgetown-core/resource"), namespace: Bridgetown::Resource
loader.setup # ready!
end
Bridgetown::Model::Origin # this needs to load first
Original file line number Diff line number Diff line change
Expand Up @@ -88,11 +88,10 @@ def locate_resource_layouts_and_partials_for_fash_refresh(path, layouts_to_reloa
end
end

def locate_components_for_fast_refresh(path) # rubocop:todo Metrics/AbcSize
comp = Bridgetown::Component.subclasses.find do |item|
def locate_components_for_fast_refresh(path)
comp = Bridgetown::Component.descendants.find do |item|
item.component_template_path == path || item.source_location == path
rescue StandardError
Bridgetown.logger.debug "Fast refresh:", "couldn't locate component for #{path}"
rescue StandardError # rubocop:disable Lint/SuppressedException
end
return unless comp

Expand Down
21 changes: 21 additions & 0 deletions bridgetown-core/lib/bridgetown-core/core_ext/class.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# frozen_string_literal: true

module Bridgetown
module CoreExt
module Class
module Descendants
def descendants
direct_children = subclasses.select do |klass|
klass == Kernel.const_get(klass.name)
rescue NameError
nil
end

(direct_children + direct_children.map(&:descendants)).flatten
end
end

::Class.include Descendants unless ::Class.respond_to?(:descendants)
end
end
end
15 changes: 0 additions & 15 deletions bridgetown-core/lib/bridgetown-core/core_ext/psych.rb

This file was deleted.

2 changes: 1 addition & 1 deletion bridgetown-core/lib/bridgetown-core/model/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ module Bridgetown
module Model
class Base
include ActiveModel::Model
extend ActiveModel::Callbacks # also extends with DescendantsTracker
extend ActiveModel::Callbacks
define_model_callbacks :load, :save, :destroy

class << self
Expand Down
2 changes: 0 additions & 2 deletions bridgetown-core/lib/bridgetown-core/model/origin.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ module Bridgetown
module Model
# Abstract Superclass
class Origin
extend ActiveSupport::DescendantsTracker

EAGER_LOAD_DESCENDANTS = %i(BuilderOrigin RepoOrigin PluginOrigin).freeze

# @return [String]
Expand Down
1 change: 0 additions & 1 deletion bridgetown-core/lib/bridgetown-core/plugin.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

module Bridgetown
class Plugin
extend ActiveSupport::DescendantsTracker
include Bridgetown::Prioritizable

self.priorities = {
Expand Down
19 changes: 0 additions & 19 deletions bridgetown-core/lib/bridgetown-core/utils/loaders_manager.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,24 +28,6 @@ def reloading_enabled?(load_path)
load_path.start_with?(root_dir) && ENV["BRIDGETOWN_ENV"] != "production"
end

def clear_descendants_for_reload(_cpath, value, _abspath)
unless value.is_a?(Class) && value.singleton_class < ActiveSupport::DescendantsTracker
return
end

if defined?(ActiveSupport::RubyFeatures) && ActiveSupport::RubyFeatures::CLASS_SUBCLASSES
ActiveSupport::DescendantsTracker.clear([value.superclass])
return
end

# TODO: this could probably be refactored to work like the above
if ActiveSupport::DescendantsTracker.class_variables.include?(:@@direct_descendants)
ActiveSupport::DescendantsTracker.class_variable_get(
:@@direct_descendants
)[value.superclass]&.reject! { _1 == value }
end
end

def setup_loaders(autoload_paths = []) # rubocop:todo Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/MethodLength, Metrics/PerceivedComplexity
(autoload_paths.presence || config.autoload_paths).each do |load_path| # rubocop:todo Metrics/BlockLength
if @loaders.key?(load_path)
Expand All @@ -71,7 +53,6 @@ def setup_loaders(autoload_paths = []) # rubocop:todo Metrics/AbcSize, Metrics/C

loader.collapse(collapsed_path)
end
loader.on_unload(&method(:clear_descendants_for_reload)) # rubocop:disable Performance/MethodObjectAsBlock
Bridgetown::Hooks.trigger :loader, :pre_setup, loader, load_path
loader.setup
loader.eager_load if config.eager_load_paths.include?(load_path)
Expand Down

0 comments on commit 4f02007

Please sign in to comment.