Skip to content

Commit

Permalink
fix: separate delegate handler cache
Browse files Browse the repository at this point in the history
  • Loading branch information
KennanChan committed Feb 27, 2020
1 parent 3931735 commit ff5be80
Showing 1 changed file with 14 additions and 6 deletions.
20 changes: 14 additions & 6 deletions Revit.Async.Shared/RevitTask.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
using Revit.Async.ExternalEvents;
using Revit.Async.Interfaces;
using Revit.Async.Utils;

#if NET40
using Revit.Async.Extensions;

Expand All @@ -24,18 +25,25 @@ public class RevitTask
{
#region Fields

private static ConcurrentDictionary<Type, ExternalEventPair> _delegateExternalEvents;
private static ConcurrentDictionary<Type, ExternalEventPair> _asyncDelegateExternalEvents;
private static ConcurrentDictionary<Type, ExternalEventPair> _syncDelegateExternalEvents;
private static ConcurrentDictionary<Type, ExternalEventPair> _registeredExternalEvents;

#endregion

#region Properties

/// <summary>
/// Cache all the created <see cref="ExternalEvent" />s and their <see cref="IExternalEventHandler" />
/// Cache all the created <see cref="ExternalEvent" />s and their <see cref="IExternalEventHandler" /> running async code
/// </summary>
private static ConcurrentDictionary<Type, ExternalEventPair> AsyncDelegateExternalEvents =>
_asyncDelegateExternalEvents ?? (_asyncDelegateExternalEvents = new ConcurrentDictionary<Type, ExternalEventPair>());

/// <summary>
/// Cache all the created <see cref="ExternalEvent" />s and their <see cref="IExternalEventHandler" /> running sync code
/// </summary>
private static ConcurrentDictionary<Type, ExternalEventPair> DelegateExternalEvents =>
_delegateExternalEvents ?? (_delegateExternalEvents = new ConcurrentDictionary<Type, ExternalEventPair>());
private static ConcurrentDictionary<Type, ExternalEventPair> SyncDelegateExternalEvents =>
_syncDelegateExternalEvents ?? (_syncDelegateExternalEvents = new ConcurrentDictionary<Type, ExternalEventPair>());

/// <summary>
/// Use to create any other external events
Expand Down Expand Up @@ -125,7 +133,7 @@ public static Task<TResult> RunAsync<TResult>(Func<TResult> function)
/// <returns>The result</returns>
public static Task<TResult> RunAsync<TResult>(Func<UIApplication, TResult> function)
{
var externalEventPair = DelegateExternalEvents.GetOrAdd(typeof(TResult), _ =>
var externalEventPair = SyncDelegateExternalEvents.GetOrAdd(typeof(TResult), _ =>
{
var handler = new SyncDelegateExternalEventHandler<TResult>();
return new ExternalEventPair(handler, () => CreateExternalEvent(handler));
Expand Down Expand Up @@ -158,7 +166,7 @@ public static Task<TResult> RunAsync<TResult>(Func<Task<TResult>> function)
/// <returns></returns>
public static Task<TResult> RunAsync<TResult>(Func<UIApplication, Task<TResult>> function)
{
var externalEventPair = DelegateExternalEvents.GetOrAdd(typeof(TResult), _ =>
var externalEventPair = AsyncDelegateExternalEvents.GetOrAdd(typeof(TResult), _ =>
{
var handler = new AsyncDelegateExternalEventHandler<TResult>();
return new ExternalEventPair(handler, () => CreateExternalEvent(handler));
Expand Down

0 comments on commit ff5be80

Please sign in to comment.