-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #30 from ewrogers/ability-conditions
Ability conditions
- Loading branch information
Showing
29 changed files
with
813 additions
and
161 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,6 @@ | ||
using System; | ||
|
||
namespace SleepHunter.Common | ||
{ | ||
public readonly record struct DeferredAction(Action Action, DateTime ExecutionTime) { } | ||
} |
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,69 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Threading; | ||
|
||
namespace SleepHunter.Common | ||
{ | ||
public sealed class DeferredDispatcher | ||
{ | ||
private readonly ReaderWriterLockSlim readerWriterLock = new(); | ||
private readonly List<DeferredAction> actions = new(); | ||
|
||
public DeferredDispatcher() { } | ||
|
||
public void DispatchAfter(Action action, TimeSpan delay) => DispatchAt(action, DateTime.Now + delay); | ||
|
||
public void DispatchAt(Action action, DateTime timestamp) | ||
{ | ||
if (action == null) | ||
throw new ArgumentNullException(nameof(action)); | ||
|
||
readerWriterLock.EnterWriteLock(); | ||
|
||
try | ||
{ | ||
var deferred = new DeferredAction(action, timestamp); | ||
actions.Add(deferred); | ||
} | ||
finally | ||
{ | ||
readerWriterLock.ExitWriteLock(); | ||
} | ||
} | ||
|
||
public void Tick() | ||
{ | ||
readerWriterLock.EnterUpgradeableReadLock(); | ||
|
||
try | ||
{ | ||
for (var i = actions.Count - 1; i >= 0; i--) | ||
{ | ||
var action = actions[i]; | ||
|
||
// If execution time is in the future, wait | ||
if (action.ExecutionTime > DateTime.Now) | ||
continue; | ||
|
||
// Remove the entry from the deferred action queue | ||
readerWriterLock.EnterWriteLock(); | ||
try | ||
{ | ||
actions.RemoveAt(i); | ||
} | ||
finally | ||
{ | ||
readerWriterLock.ExitWriteLock(); | ||
} | ||
|
||
// Perform the action (outside of the write lock) | ||
action.Action(); | ||
} | ||
} | ||
finally | ||
{ | ||
readerWriterLock.ExitUpgradeableReadLock(); | ||
} | ||
} | ||
} | ||
} |
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
Oops, something went wrong.