From aefa069dbc05faaaebb75b1da72b5cfcdedc110c Mon Sep 17 00:00:00 2001 From: pvincent Date: Mon, 16 Sep 2024 18:55:19 +0400 Subject: [PATCH] trigger_rolling_event --- config/initializers/semantic_logger.rb | 3 +- lib/live/definable.rb | 66 +++++++++++++++----------- 2 files changed, 41 insertions(+), 28 deletions(-) diff --git a/config/initializers/semantic_logger.rb b/config/initializers/semantic_logger.rb index 7edb78f..ab55c76 100644 --- a/config/initializers/semantic_logger.rb +++ b/config/initializers/semantic_logger.rb @@ -16,6 +16,7 @@ Rails.configuration.after_initialize do end ActiveSupport::Notifications.subscribe('rolling.live_constant') do |event| - SemanticLogger[:live_notifications].warn('rolling.live_constant', event.payload) # FIXME: to be continued... + SemanticLogger[:live_notifications].warn('rolling.live_constant', event.payload) + # FIXME: to be continued... end end diff --git a/lib/live/definable.rb b/lib/live/definable.rb index 9118d0e..35d0da4 100644 --- a/lib/live/definable.rb +++ b/lib/live/definable.rb @@ -14,43 +14,55 @@ module Live def reload_from_env logger.debug('reload from env') - @env_values = Dotenv.parse(*Dotenv::Rails.files) - changes = 0 - prefix = 'Constant' - env_values.each_pair do |constant, raw| - next unless definitions.include?(constant) + changes = [] + new_env_values = env_values(cached: false) + new_env_keys = new_env_values.keys - value = typed_value(definitions[constant][:type], raw, definitions[constant][:default]) - next unless value != definitions[constant][:value] + new_env_values.each_pair { |constant, raw| changes << override(constant, raw) } + definitions.except(*new_env_keys).each_pair { |constant, definition| changes << restore(constant, definition) } + changes.compact! - define_value(constant, value) - logger.warn "#{prefix} overriden from environment:#{Semantic::AnsiColors::CLEAR} #{constant} = #{value.ai}" - changes += 1 - end - definitions.except(*env_values.keys).each_pair do |constant, options| - default = options[:default] - next unless options[:value] != default + trigger_rolling_event(changes) if changes.any? + end - define_value(constant, default) - logger.warn "#{prefix} restored:#{Semantic::AnsiColors::CLEAR} #{constant} = #{default.ai}" - changes += 1 - end + private - return unless changes.positive? + def trigger_rolling_event(changes) + ActiveSupport::Notifications.instrument('rolling.live_constant', changes:) + FileUtils.touch(MAIN_CSS) if defined?(RailsLiveReload) # triggering RailsLiveReload + end - # TODO: ... - # changes=[] - # changes << {kind: :overriden, constant: 'ACTION_VIEW', type :boolean, old_value: false, new_value:false} - # changes << {kind: :restored, constant: 'ACTION_VIEW', type :boolean, old_value: false, new_value:false} - # ActiveSupport::Notifications.instrument 'rolling.live_constant', this: changes + def override(constant, raw) + return unless definitions.include?(constant) - FileUtils.touch(MAIN_CSS) if defined?(RailsLiveReload) # triggering RailsLiveReload + type = definitions[constant][:type] + new_value = typed_value(type, raw, definitions[constant][:default]) + old_value = definitions[constant][:value] + return if new_value == old_value + + define_value(constant, new_value) + logger.warn "Constant overriden from environment:#{Semantic::AnsiColors::CLEAR} #{constant} = #{new_value.ai}" + { kind: :overriden, constant:, type:, old_value:, new_value: } end - private + def restore(constant, definition) + new_value = definition[:default] + old_value = definition[:value] + return if old_value == new_value + + type = definition[:type] + define_value(constant, new_value) + logger.warn "Constant restored:#{Semantic::AnsiColors::CLEAR} #{constant} = #{new_value.ai}" + { kind: :restored, constant:, type:, old_value:, new_value: } + end + + def env_values(cached: true) + return @env_values if @env_values && cached + + @env_values = Dotenv.parse(*Dotenv::Rails.files) + end def logger = @logger ||= SemanticLogger[self] - def env_values = @env_values ||= Dotenv.parse def definitions = @definitions ||= {} def define_value(constant, value)