debounced 0.1.19 → 0.1.20

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (4) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +40 -28
  3. data/lib/debounced/version.rb +1 -1
  4. metadata +1 -1
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 5469038458279f90317b22acd88274c9a9cd5ddfe7d932e0fba6141230da1f65
4
- data.tar.gz: 2776dc85b634f88d5f9ec7f4903de124a092455e25cffba08b91aaab903d7667
3
+ metadata.gz: f0b2a5d98cbf80bf222866ad69db51d71b000ea1e13a7bf16d7ddc638aa91923
4
+ data.tar.gz: f031cae66cd65f40da805111ac8b31140de3e901f19f742bffe1ab593627bb27
5
5
  SHA512:
6
- metadata.gz: 9559170057bc4e13012e8d3d7234d1d2e2d1ed9eaa7100c8b2120b5c5b7b0d57cebfd4aa7e881b059bb9a6475b1dd45c4854467a883d09f7e88e159636faea4f
7
- data.tar.gz: 5dadd3b4312c8985a2e3ad44b7aceb948250031bd9ca0e26da903211a42dc7a9ff0b26f57fa3e30062a05947c7738af6b999c809548a1781ccfe08176be0fb13
6
+ metadata.gz: d498b7e15fb3aef46c5789d1df43d143970daf40a48bd9f71d5f0664330cda646d9d6d36e8936a334acefdb62d40c3e966ccff7b5fcfeaabf2ea47cf41edada9
7
+ data.tar.gz: 05ec409dbede721aab6457c29b564a8d8565d87208b3816ec37f1e8a4e3aa2049a4f663e8612e3969ab4e0023058e52f14351529f27f6e8eb710969d9be7b130
data/README.md CHANGED
@@ -1,6 +1,7 @@
1
1
  # Debounced
2
2
 
3
- 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.
3
+ Efficient debouncing mechanism for Ruby events. Use it for rate limiting, deduplication, or other
4
+ scenarios where you want to wait for a certain amount of time before processing a given event.
4
5
 
5
6
  ## Installation
6
7
 
@@ -26,15 +27,7 @@ $ gem install debounced
26
27
 
27
28
  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:
28
29
 
29
- - Node.js >= 14.0.0
30
- - npm (to install the required node packages)
31
-
32
- After installing the gem, run:
33
-
34
- ```bash
35
- $ cd $(bundle show debounced)
36
- $ npm install
37
- ```
30
+ - Node.js >= 20.0.0
38
31
 
39
32
  ## Usage
40
33
 
@@ -44,51 +37,70 @@ $ npm install
44
37
  # config/initializers/debounced.rb
45
38
  Debounced.configure do |config|
46
39
  config.socket_descriptor = '/tmp/my_app.debounceEvents'
40
+ config.wait_timeout = 3 # idle timeout in seconds for a given activity descriptor
47
41
  end
48
42
  ```
49
43
 
50
44
  ### Starting the server
51
45
 
52
- You can start the debounce server with:
46
+ Start the nodeJS debounce server with:
53
47
 
54
48
  ```bash
55
- $ bundle exec debounced-server
49
+ $ bundle exec debounced:server
56
50
  ```
57
51
 
58
- Or in your application code:
52
+ In your Ruby application code:
59
53
 
60
54
  ```ruby
61
55
  require 'debounced'
62
56
 
63
- # Start the listener thread
57
+ # Start a background thread to receive notification that events are ready to be handled after debounce wait is complete
64
58
  proxy = Debounced::ServiceProxy.new
65
- listener_thread = proxy.listen
59
+ proxy.listen
66
60
 
67
- # Debounce an event
61
+ # Define your event class; create a helper method that will produce a Debounced::Callback object, which
62
+ # is used to notify the server that the event is ready to be handled
68
63
  class MyEvent
69
- attr_reader :attributes
64
+ attr_reader :test_id
70
65
 
71
- def initialize(data)
72
- @attributes = data
66
+ def initialize(test_id:)
67
+ @test_id = test_id
73
68
  end
74
69
 
75
- def self.publish(data)
76
- # Publish logic here
77
- puts "Publishing event with data: #{data.inspect}"
70
+ def publish
71
+ # put logic here to publish the event after debouncing
72
+ puts "Publishing event: #{inspect}"
73
+ end
74
+
75
+ def debounce_callback
76
+ Debounced::Callback.new(
77
+ class_name: self.class.name,
78
+ params: { test_id: },
79
+ method_name: 'publish',
80
+ method_params: []
81
+ )
78
82
  end
79
83
  end
80
84
 
81
- event = MyEvent.new({ id: 1, message: "Hello World" })
82
- proxy.debounce_event("my-event-123", event, 5) # Debounce for 5 seconds
85
+ event = MyEvent.new({ test_id: "Hello World" })
86
+
87
+ # request the server to debounce the event, ignoring it if another event with the
88
+ # same descriptor arrives before the timeout
89
+ proxy.debounce_activity("my-event-123", 5, event.debounce_callback)
90
+ # 2 seconds later
91
+ proxy.debounce_activity("my-event-123", 5, event.debounce_callback)
92
+ # 4 seconds later
93
+ proxy.debounce_activity("my-event-123", 5, event.debounce_callback)
94
+ # 5 seconds later the event is published!
95
+ # > Publishing event: #<MyEvent:0x00007f9b1b8b3b40 @test_id="Hello World">
83
96
  ```
84
97
 
85
98
  ## How It Works
86
99
 
87
100
  1. The gem creates a Unix socket for communication between Ruby and Node.js
88
- 2. When you call `debounce_event`, it sends the event to the Node.js server
89
- 3. The Node.js server keeps track of events with the same descriptor
90
- 4. If another event with the same descriptor arrives before the timeout, it resets the timer
91
- 5. When the timeout expires, it sends the event back to Ruby to be published
101
+ 2. When you call `debounce_activity`, it sends the event to the Node.js server
102
+ 3. The Node.js server restarts a timer every time an event with a given activity_descriptor is received
103
+ 4. When the timeout expires, it sends the event back to Ruby to be published
92
104
 
93
105
  ## License
94
106
 
@@ -1,3 +1,3 @@
1
1
  module Debounced
2
- VERSION = "0.1.19"
2
+ VERSION = "0.1.20"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: debounced
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.19
4
+ version: 0.1.20
5
5
  platform: ruby
6
6
  authors:
7
7
  - Gary Passero