-
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.
library node search fixes, fix node creation on enter
- Loading branch information
1 parent
104e63d
commit f085765
Showing
10 changed files
with
177 additions
and
51 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
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 |
---|---|---|
@@ -1,36 +1,43 @@ | ||
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(); | ||
public long LastExecutionId = 0; | ||
public SerialQueue SerialQueue = 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. | ||
/// Execution is sequential, and optional jobs that share a <see cref="DebounceQueueToken"/> with a newer optional job will be ignored. | ||
/// </summary> | ||
/// <param name="job"></param> | ||
/// <param name="token"></param> | ||
/// <returns></returns> | ||
public static Task EnqueueJobAsync(Action job, DebounceQueueToken token) | ||
public static void EnqueueOptionalJobAsync(Action job, DebounceQueueToken token) | ||
{ | ||
token.IsDirty = true; | ||
return Task.Run(() => | ||
lock (token) | ||
{ | ||
lock (token.JobLock) | ||
token.LastExecutionId++; | ||
var myExecutionId = token.LastExecutionId; | ||
token.SerialQueue.DispatchAsync(() => | ||
{ | ||
while (token.IsDirty) | ||
{ | ||
token.IsDirty = false; | ||
job(); | ||
} | ||
} | ||
}); | ||
if (myExecutionId < token.LastExecutionId) return; | ||
job(); | ||
}); | ||
} | ||
} | ||
public static void EnqueueMandatoryJobAsync(Action job, DebounceQueueToken token) | ||
{ | ||
lock (token) | ||
{ | ||
token.SerialQueue.DispatchAsync(() => | ||
{ | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
//https://github.com/gentlee/SerialQueue/blob/master/SerialQueue/SerialQueue.cs | ||
using System; | ||
using System.Threading; | ||
|
||
namespace Dynamo.Wpf.Utilities | ||
{ | ||
public class SerialQueue | ||
{ | ||
class LinkedListNode(Action action) | ||
{ | ||
public readonly Action Action = action; | ||
public LinkedListNode Next; | ||
} | ||
|
||
public event Action<Action, Exception> UnhandledException = delegate { }; | ||
|
||
private LinkedListNode _queueFirst; | ||
private LinkedListNode _queueLast; | ||
private bool _isRunning = false; | ||
|
||
public void DispatchAsync(Action action) | ||
{ | ||
var newNode = new LinkedListNode(action); | ||
|
||
lock (this) | ||
{ | ||
if (_queueFirst == null) | ||
{ | ||
_queueFirst = newNode; | ||
_queueLast = newNode; | ||
|
||
if (!_isRunning) | ||
{ | ||
_isRunning = true; | ||
ThreadPool.QueueUserWorkItem(Run); | ||
} | ||
} | ||
else | ||
{ | ||
_queueLast!.Next = newNode; | ||
_queueLast = newNode; | ||
} | ||
} | ||
} | ||
|
||
private void Run(object _) | ||
{ | ||
while (true) | ||
{ | ||
LinkedListNode firstNode; | ||
|
||
lock (this) | ||
{ | ||
if (_queueFirst == null) | ||
{ | ||
_isRunning = false; | ||
return; | ||
} | ||
firstNode = _queueFirst; | ||
_queueFirst = null; | ||
_queueLast = null; | ||
} | ||
|
||
while (firstNode != null) | ||
{ | ||
var action = firstNode.Action; | ||
firstNode = firstNode.Next; | ||
try | ||
{ | ||
action(); | ||
} | ||
catch (Exception error) | ||
{ | ||
UnhandledException.Invoke(action, error); | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} |
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