-
Notifications
You must be signed in to change notification settings - Fork 638
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix hanged node search when the user types faster than serach
- Loading branch information
1 parent
6d5ea68
commit 104e63d
Showing
4 changed files
with
63 additions
and
8 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
using System; | ||
using System.Threading.Tasks; | ||
|
||
namespace Dynamo.Wpf.Utilities | ||
{ | ||
public static class JobDebouncer | ||
{ | ||
public class DebounceQueueToken | ||
{ | ||
public bool IsDirty = false; | ||
public readonly object JobLock = new(); | ||
}; | ||
/// <summary> | ||
/// Action <paramref name="job"/> is guaranteed to run at most once for every call, and exactly once after the last call. | ||
/// Execution is sequential, and jobs that share a <see cref="DebounceQueueToken"/> with a newer job will be ignored. | ||
/// </summary> | ||
/// <param name="job"></param> | ||
/// <param name="token"></param> | ||
/// <returns></returns> | ||
public static Task EnqueueJobAsync(Action job, DebounceQueueToken token) | ||
{ | ||
token.IsDirty = true; | ||
return Task.Run(() => | ||
{ | ||
lock (token.JobLock) | ||
{ | ||
while (token.IsDirty) | ||
{ | ||
token.IsDirty = false; | ||
job(); | ||
} | ||
} | ||
}); | ||
} | ||
} | ||
} |
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