This repository has been archived by the owner on Aug 8, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[core] Throttle tiles to redo symbol placement at most once every 300ms.
- Loading branch information
Showing
5 changed files
with
74 additions
and
2 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,36 @@ | ||
#include <mbgl/util/throttler.hpp> | ||
|
||
namespace mbgl { | ||
namespace util { | ||
|
||
Throttler::Throttler(Duration frequency_, std::function<void()>&& function_) | ||
: frequency(frequency_) | ||
, function(std::move(function_)) | ||
, pendingInvocation(false) | ||
, lastInvocation(TimePoint::min()) | ||
{} | ||
|
||
void Throttler::invoke() { | ||
if (pendingInvocation) { | ||
return; | ||
} | ||
|
||
Duration timeToNextInvocation = lastInvocation == TimePoint::min() | ||
? Duration::zero() | ||
: (lastInvocation + frequency) - Clock::now(); | ||
|
||
if (timeToNextInvocation <= Duration::zero()) { | ||
lastInvocation = Clock::now(); | ||
function(); | ||
} else { | ||
pendingInvocation = true; | ||
timer.start(timeToNextInvocation, Duration::zero(), [this]{ | ||
pendingInvocation = false; | ||
lastInvocation = Clock::now(); | ||
function(); | ||
}); | ||
} | ||
} | ||
|
||
} // namespace util | ||
} // namespace mbgl |
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,22 @@ | ||
#include <mbgl/util/chrono.hpp> | ||
#include <mbgl/util/timer.hpp> | ||
|
||
namespace mbgl { | ||
namespace util { | ||
|
||
class Throttler { | ||
public: | ||
Throttler(Duration frequency, std::function<void()>&& function); | ||
|
||
void invoke(); | ||
private: | ||
Duration frequency; | ||
std::function<void()> function; | ||
|
||
Timer timer; | ||
bool pendingInvocation; | ||
TimePoint lastInvocation; | ||
}; | ||
|
||
} // namespace util | ||
} // namespace mbgl |