Class: Vedeu::Event

Inherits:
Object
  • Object
show all
Defined in:
lib/vedeu/support/event.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(closure, options = {}) ⇒ Event



7
8
9
10
11
12
13
# File 'lib/vedeu/support/event.rb', line 7

def initialize(closure, options = {})
  @closure      = closure
  @options      = options
  @deadline     = 0
  @executed_at  = 0
  @now          = 0
end

Instance Attribute Details

#closureObject (readonly, private)

Returns the value of attribute closure.



27
28
29
# File 'lib/vedeu/support/event.rb', line 27

def closure
  @closure
end

#deadlineObject (private)

Returns the value of attribute deadline.



28
29
30
# File 'lib/vedeu/support/event.rb', line 28

def deadline
  @deadline
end

#executed_atObject (private)

Returns the value of attribute executed_at.



28
29
30
# File 'lib/vedeu/support/event.rb', line 28

def executed_at
  @executed_at
end

#nowObject (private)

Returns the value of attribute now.



28
29
30
# File 'lib/vedeu/support/event.rb', line 28

def now
  @now
end

Instance Method Details

#debounceFixnum|Float (private)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



106
107
108
# File 'lib/vedeu/support/event.rb', line 106

def debounce
  options[:debounce]
end

#debouncing?TrueClass|FalseClass (private)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



52
53
54
55
56
57
58
# File 'lib/vedeu/support/event.rb', line 52

def debouncing?
  set_time

  set_deadline unless has_deadline?

  options[:debounce] > 0
end

#defaultsHash (private)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



124
125
126
127
128
129
# File 'lib/vedeu/support/event.rb', line 124

def defaults
  {
    delay:    0,
    debounce: 0
  }
end

#delayFixnum|Float (private)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



112
113
114
# File 'lib/vedeu/support/event.rb', line 112

def delay
  options[:delay]
end

#elapsed_timeFloat (private)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



62
63
64
# File 'lib/vedeu/support/event.rb', line 62

def elapsed_time
  now - @executed_at
end

#execute(*args) ⇒ Object (private)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns [].



32
33
34
35
36
37
38
39
40
# File 'lib/vedeu/support/event.rb', line 32

def execute(*args)
  reset_deadline

  set_executed

  reset_time

  closure.call(*args)
end

#has_deadline?TrueClass|FalseClass (private)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



86
87
88
# File 'lib/vedeu/support/event.rb', line 86

def has_deadline?
  @deadline > 0
end

#optionsHash (private)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



118
119
120
# File 'lib/vedeu/support/event.rb', line 118

def options
  defaults.merge!(@options)
end

#reset_deadlineFixnum (private)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



92
93
94
# File 'lib/vedeu/support/event.rb', line 92

def reset_deadline
  @deadline = 0
end

#reset_timeFixnum (private)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



80
81
82
# File 'lib/vedeu/support/event.rb', line 80

def reset_time
  @now = 0
end

#set_deadlineNilClass (private)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



98
99
100
101
102
# File 'lib/vedeu/support/event.rb', line 98

def set_deadline
  @deadline = now + debounce

  nil
end

#set_executedFloat (private)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



68
69
70
# File 'lib/vedeu/support/event.rb', line 68

def set_executed
  @executed_at = now
end

#set_timeFloat (private)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



74
75
76
# File 'lib/vedeu/support/event.rb', line 74

def set_time
  @now = Time.now.to_f
end

#throttling?TrueClass|FalseClass (private)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



44
45
46
47
48
# File 'lib/vedeu/support/event.rb', line 44

def throttling?
  set_time

  options[:delay] > 0
end

#trigger(*args) ⇒ Object



17
18
19
20
21
22
23
# File 'lib/vedeu/support/event.rb', line 17

def trigger(*args)
  return execute(*args) unless debouncing? || throttling?

  return execute(*args) if debouncing? && set_executed > deadline

  return execute(*args) if throttling? && elapsed_time > delay
end