proc-throttle 0.0.2 → 0.1.0
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.
- data/lib/proc/throttle.rb +16 -3
- data/proc-throttle.gemspec +1 -1
- data/spec/throttle_spec.rb +33 -0
- metadata +5 -5
data/lib/proc/throttle.rb
CHANGED
@@ -1,16 +1,29 @@
|
|
1
1
|
class Proc
|
2
|
-
def throttle(
|
2
|
+
def throttle(delay_sec, debounce = false)
|
3
3
|
thread = nil
|
4
|
+
last_exec = nil
|
4
5
|
|
5
6
|
Proc.new do |*args|
|
7
|
+
now = Time.now.to_f
|
8
|
+
|
6
9
|
if thread && thread.alive?
|
7
10
|
thread.kill
|
8
11
|
end
|
9
12
|
|
10
|
-
|
11
|
-
sleep(sec)
|
13
|
+
if !debounce && (last_exec.nil? || (last_exec + delay_sec < now))
|
12
14
|
self.call(*args)
|
15
|
+
last_exec = now
|
16
|
+
else
|
17
|
+
thread = Thread.new do
|
18
|
+
sleep(delay_sec)
|
19
|
+
self.call(*args)
|
20
|
+
last_exec = Time.now.to_f
|
21
|
+
end
|
13
22
|
end
|
14
23
|
end
|
15
24
|
end
|
25
|
+
|
26
|
+
def debounce(delay_sec)
|
27
|
+
throttle(delay_sec, true)
|
28
|
+
end
|
16
29
|
end
|
data/proc-throttle.gemspec
CHANGED
data/spec/throttle_spec.rb
CHANGED
@@ -11,6 +11,39 @@ describe Proc do
|
|
11
11
|
|
12
12
|
throttled_proc = proc.throttle(1)
|
13
13
|
|
14
|
+
throttled_proc.call
|
15
|
+
|
16
|
+
count.should eql(1)
|
17
|
+
|
18
|
+
sleep 1.1
|
19
|
+
|
20
|
+
10.times do
|
21
|
+
throttled_proc.call
|
22
|
+
sleep 0.2
|
23
|
+
end
|
24
|
+
|
25
|
+
sleep 1.1
|
26
|
+
|
27
|
+
count.should eql(4)
|
28
|
+
|
29
|
+
sleep 1.1
|
30
|
+
|
31
|
+
throttled_proc.call(10)
|
32
|
+
|
33
|
+
count.should eql(14)
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
describe '#debounce' do
|
38
|
+
it 'should debounce proc call' do
|
39
|
+
count = 0
|
40
|
+
|
41
|
+
proc = Proc.new do |increment|
|
42
|
+
count += increment || 1
|
43
|
+
end
|
44
|
+
|
45
|
+
throttled_proc = proc.debounce(1)
|
46
|
+
|
14
47
|
10.times do
|
15
48
|
throttled_proc.call
|
16
49
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: proc-throttle
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0
|
4
|
+
version: 0.1.0
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -13,7 +13,7 @@ date: 2012-05-24 00:00:00.000000000 Z
|
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: rake
|
16
|
-
requirement: &
|
16
|
+
requirement: &70357420018840 !ruby/object:Gem::Requirement
|
17
17
|
none: false
|
18
18
|
requirements:
|
19
19
|
- - ! '>='
|
@@ -21,7 +21,7 @@ dependencies:
|
|
21
21
|
version: '0'
|
22
22
|
type: :development
|
23
23
|
prerelease: false
|
24
|
-
version_requirements: *
|
24
|
+
version_requirements: *70357420018840
|
25
25
|
description: ! 'Proc#throttle: throttling calls'
|
26
26
|
email:
|
27
27
|
- youpy@buycheapviagraonlinenow.com
|
@@ -53,7 +53,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
53
53
|
version: '0'
|
54
54
|
segments:
|
55
55
|
- 0
|
56
|
-
hash:
|
56
|
+
hash: 740962742363662978
|
57
57
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
58
58
|
none: false
|
59
59
|
requirements:
|
@@ -62,7 +62,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
62
62
|
version: '0'
|
63
63
|
segments:
|
64
64
|
- 0
|
65
|
-
hash:
|
65
|
+
hash: 740962742363662978
|
66
66
|
requirements: []
|
67
67
|
rubyforge_project:
|
68
68
|
rubygems_version: 1.8.10
|