Class: Reedb::Debouncer

Inherits:
Object
  • Object
show all
Defined in:
lib/reedb/debouncer.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(core) ⇒ Object

Returns self.

Parameters:

  • core (Object)

    the owning container instance to register the debounce close of a vault



32
33
34
35
36
37
38
39
40
41
# File 'lib/reedb/debouncer.rb', line 32

def initialize(core)
	@reedb = core
	@running = true

	# Vault management
	@delta_vaults = {}
	@vaults = {}
	@token_set = {}
	@timeout = Reedb::KEY_CACHE_TIME
end

Instance Attribute Details

#runningObject

Returns the value of attribute running.



25
26
27
# File 'lib/reedb/debouncer.rb', line 25

def running
  @running
end

Instance Method Details

#add_vault(uuid, token) ⇒ Object

Updates the vault instances every time the vault set changes. Only changes values for vaults that change and attempts to leave old vaults unchanged.

Parameters:

  • uuid (String)

    of a vault for identification

  • token (String)

    as a Base64 encoded string

Returns:

  • Boolean to indicate whether there was already a token for this vault



96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/reedb/debouncer.rb', line 96

def add_vault(uuid, token)
	if @vaults.include?(uuid)

		# Marks the vault to debounce because it was just interacted with but already in scope
		@delta_vaults[uuid] = DRES
		return false
	else
		@delta_vaults[uuid] = VINS
		@token_set[uuid] = token
		return true
	end
end

#debounce_vault(vault_id) ⇒ Object

This is called every time an action is performed on a vault.



115
116
117
# File 'lib/reedb/debouncer.rb', line 115

def debounce_vault(vault_id)
	@delta_vaults[vault_id] = DRES
end

#get_token(uuid) ⇒ Object



124
125
126
# File 'lib/reedb/debouncer.rb', line 124

def get_token(uuid)
	return @token_set[uuid]
end

#knows_vault(uuid) ⇒ Object

Some utility and helper functions to plug into the Reedb main interface



120
121
122
# File 'lib/reedb/debouncer.rb', line 120

def knows_vault(uuid)
	return @vaults.include?(uuid)
end

#mainObject

The main loop to run in a thread



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/reedb/debouncer.rb', line 48

def main
	last = Time.new
	while @running
		# Update the delta time
		tmp = Time.new; r_delta = tmp - last

		# Work through the delta_vaults file to check what info needs to change
		@delta_vaults.each do |uuid, data|

			# Make sure that the vault set is current
			if data == VINS
				@vaults[uuid] = @timeout
			elsif data == VREM
				@vaults.delete(uuid)
			elsif data == DRES
				@vaults[uuid] = @timeout
			end
		end

		# Then reset it for the next delta
		@delta_vaults = {}

		# Now actually iterate through the vaults and subtract delta time
		@vaults.each do |uuid, data|

			# Subtract real delta time from timeset
			@vaults[uuid] = data - r_delta

			# Then check if that vault needs to be closed
			if @vaults[uuid] <= 0
				Reedb::Vault::close_vault(uuid, @token_set[uuid])
			end
		end
		
		last = tmp # Update last time and then sleep
		sleep(Reedb::DEBOUNCE_DELTA)
	end
	# puts 'I can feel my mind going, Dave'
end

#remove_vault(uuid) ⇒ Object



109
110
111
112
# File 'lib/reedb/debouncer.rb', line 109

def remove_vault(uuid)
	@delta_vaults[uuid] = VREM
	@token_set.delete(uuid)
end

#set_custom_timeout(time) ⇒ Object



43
44
45
# File 'lib/reedb/debouncer.rb', line 43

def set_custom_timeout(time)
	@timeout = time
end