Skip to content

Commit

Permalink
Debounce adapter config option (#280)
Browse files Browse the repository at this point in the history
  • Loading branch information
julianrubisch committed Jun 9, 2023
1 parent 48ea770 commit 9da5cfe
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 5 deletions.
7 changes: 3 additions & 4 deletions app/models/concerns/cable_ready/updatable.rb
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
# frozen_string_literal: true

require "active_support/concern"
require "cable_ready/config"

module CableReady
module Updatable
extend ::ActiveSupport::Concern

mattr_accessor :debounce_adapter, default: ::CableReady::Updatable::MemoryCacheDebounceAdapter.instance

included do |base|
if defined?(ActiveRecord) && base < ActiveRecord::Base
include ExtendHasMany
Expand Down Expand Up @@ -190,12 +189,12 @@ def broadcast_updates(model_class, options)

if debounce_time.to_f > 0
key = compound([model_class, *options])
old_wait_until = CableReady::Updatable.debounce_adapter[key]
old_wait_until = CableReady.config.updatable_debounce_adapter[key]
now = Time.now.to_f

if old_wait_until.nil? || old_wait_until < now
new_wait_until = now + debounce_time.to_f
CableReady::Updatable.debounce_adapter[key] = new_wait_until
CableReady.config.updatable_debounce_adapter[key] = new_wait_until
ActionCable.server.broadcast(model_class, options)
end
else
Expand Down
2 changes: 1 addition & 1 deletion lib/cable_ready/config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ class Config
include Observable
include Singleton

attr_accessor :on_failed_sanity_checks, :broadcast_job_queue, :precompile_assets, :updatable_debounce_time
attr_accessor :on_failed_sanity_checks, :broadcast_job_queue, :precompile_assets, :updatable_debounce_time, :updatable_debounce_adapter
attr_writer :verifier_key

def on_new_version_available
Expand Down
4 changes: 4 additions & 0 deletions lib/cable_ready/engine.rb
Original file line number Diff line number Diff line change
Expand Up @@ -55,5 +55,9 @@ class Engine < Rails::Engine
app.config.importmap.cache_sweepers << Engine.root.join("app/assets/javascripts")
end
end

config.after_initialize do
CableReady.config.updatable_debounce_adapter ||= CableReady::Updatable::MemoryCacheDebounceAdapter.instance
end
end
end

0 comments on commit 9da5cfe

Please sign in to comment.