-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
57 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
require "../../../util/exponential-back-off.cr" | ||
require "./win-util" | ||
# WinWait, WinTitle, WinText, Seconds [, ExcludeTitle, ExcludeText] | ||
class Cmd::X11::Window::WinWait < Cmd::Base | ||
def self.min_args; 3 end | ||
def self.max_args; 5 end | ||
def self.sets_error_level; true end | ||
def run(thread, args) | ||
seconds = args[2].to_f? || 0.5 | ||
seconds = 0.5 if seconds == 0 | ||
match_conditions = args | ||
match_conditions.delete_at(2) | ||
match = ::Util::ExponentialBackOff.back_off(initial_wait: 20.milliseconds, factor: 1.15, max_wait: 0.8.seconds, timeout: seconds.seconds) do | ||
Util.match(thread, match_conditions, empty_is_last_found: false, a_is_active: false) do |win| | ||
thread.settings.last_found_window = win | ||
end | ||
end | ||
match ? "0" : "1" | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
class Util::ExponentialBackOff | ||
# Waits until the *&block* passes. Increases the sleep time in between operations | ||
# by *factor*, but sleeps max. *max_wait* seconds. If *max_wait* is unspecified, | ||
# the waiting gaps can get infinitely big. | ||
# Returns `true` when the *&block* returns true or `false` when *timeout* was exceeded. | ||
def self.back_off(*, initial_wait : Time::Span, factor : Float64, max_wait : Time::Span? = nil, timeout : Time::Span? = nil, &block : -> Bool) | ||
back_off_wait = initial_wait | ||
start = Time.monotonic | ||
loop do | ||
if yield | ||
break | ||
end | ||
if timeout | ||
if Time.monotonic - start > timeout | ||
return false | ||
end | ||
end | ||
sleep back_off_wait | ||
back_off_wait = back_off_wait * factor | ||
if max_wait | ||
back_off_wait = ::Math.min(back_off_wait, max_wait) | ||
end | ||
end | ||
true | ||
end | ||
end |