Skip to content

Commit

Permalink
chore: Memoize debounce time
Browse files Browse the repository at this point in the history
  • Loading branch information
julianrubisch committed Mar 1, 2023
1 parent cbeefc3 commit 68ae3c7
Showing 1 changed file with 6 additions and 4 deletions.
10 changes: 6 additions & 4 deletions app/models/concerns/cable_ready/updatable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -206,15 +206,13 @@ def broadcast_updates(model_class, options)
return if skip_updates_classes.any? { |klass| klass >= self }
raise("ActionCable must be enabled to use Updatable") unless defined?(ActionCable)

@debounce_time ||= 0.seconds

if @debounce_time > 0.seconds
if debounce_time > 0.seconds
key = compound([model_class, *options])
old_wait_until = CableReady::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
new_wait_until = now + debounce_time.to_f
CableReady::Updatable.debounce_adapter[key] = new_wait_until
ActionCable.server.broadcast(model_class, options)
end
Expand All @@ -223,6 +221,10 @@ def broadcast_updates(model_class, options)
end
end

def debounce_time
@debounce_time ||= 0.seconds
end

def skip_updates_classes
Thread.current[:skip_updates_classes] ||= []
end
Expand Down

0 comments on commit 68ae3c7

Please sign in to comment.