A Ruby gem that provides a NodeJS-based event debouncing service for Ruby applications. It uses the JavaScript micro event loop to efficiently debounce events.
Add this line to your application's Gemfile:
gem 'debounced'
And then execute:
$ bundle install
Or install it yourself as:
$ gem install debounced
This gem requires Node.js to be installed on your system, as it uses a Node.js server to handle the debouncing logic. You'll need:
- Node.js >= 14.0.0
- npm (to install the required node packages)
After installing the gem, run:
$ cd $(bundle show debounced)
$ npm install
# config/initializers/debounced.rb
Debounced.configure do |config|
config.socket_descriptor = '/tmp/my_app.debounceEvents'
end
You can start the debounce server with:
$ bundle exec debounced-server
Or in your application code:
require 'debounced'
# Start the listener thread
proxy = Debounced::ServiceProxy.new
listener_thread = proxy.listen
# Debounce an event
class MyEvent
attr_reader :attributes
def initialize(data)
@attributes = data
end
def self.publish(data)
# Publish logic here
puts "Publishing event with data: #{data.inspect}"
end
end
event = MyEvent.new({ id: 1, message: "Hello World" })
proxy.debounce_event("my-event-123", event, 5) # Debounce for 5 seconds
- The gem creates a Unix socket for communication between Ruby and Node.js
- When you call
debounce_event
, it sends the event to the Node.js server - The Node.js server keeps track of events with the same descriptor
- If another event with the same descriptor arrives before the timeout, it resets the timer
- When the timeout expires, it sends the event back to Ruby to be published
The gem is available as open source under the terms of the MIT License.