Browse Source

trigger_rolling_event

main
pvincent 3 months ago
parent
commit
aefa069dbc
  1. 3
      config/initializers/semantic_logger.rb
  2. 66
      lib/live/definable.rb

3
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

66
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)

Loading…
Cancel
Save