-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
bb0faaa
commit 8e3b1da
Showing
16 changed files
with
148 additions
and
133 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
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
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,61 @@ | ||
using Speckle.Connectors.Common.Threading; | ||
|
||
namespace Speckle.Connectors.DUI.Bridge; | ||
|
||
public class ExceptionEvent(IThreadContext threadContext) : SpeckleEvent<Exception>(threadContext); | ||
|
||
public class ThreadContextEventSubscription<T>( | ||
IDelegateReference actionReference, | ||
IDelegateReference filterReference, | ||
IThreadContext threadContext | ||
) : EventSubscription<T>(actionReference, filterReference) | ||
{ | ||
public override void InvokeAction(Action<T> action, T payload) => | ||
threadContext.RunOnMain(() => action.Invoke(payload)); | ||
} | ||
|
||
public class SpeckleEvent<T>(IThreadContext threadContext) : PubSubEvent<T> | ||
{ | ||
public override SubscriptionToken Subscribe( | ||
Action<T> action, | ||
ThreadOption threadOption, | ||
bool keepSubscriberReferenceAlive, | ||
Predicate<T>? filter | ||
) | ||
{ | ||
IDelegateReference actionReference = new DelegateReference(action, keepSubscriberReferenceAlive); | ||
IDelegateReference filterReference; | ||
if (filter != null) | ||
{ | ||
filterReference = new DelegateReference(filter, keepSubscriberReferenceAlive); | ||
} | ||
else | ||
{ | ||
filterReference = new DelegateReference( | ||
new Predicate<T>( | ||
delegate | ||
{ | ||
return true; | ||
} | ||
), | ||
true | ||
); | ||
} | ||
EventSubscription<T> subscription; | ||
switch (threadOption) | ||
{ | ||
case ThreadOption.BackgroundThread: | ||
subscription = new BackgroundEventSubscription<T>(actionReference, filterReference); | ||
break; | ||
case ThreadOption.UIThread: | ||
subscription = new ThreadContextEventSubscription<T>(actionReference, filterReference, threadContext); | ||
break; | ||
case ThreadOption.PublisherThread: | ||
default: | ||
subscription = new EventSubscription<T>(actionReference, filterReference); | ||
break; | ||
} | ||
|
||
return InternalSubscribe(subscription); | ||
} | ||
} |
35 changes: 35 additions & 0 deletions
35
DUI3/Speckle.Connectors.DUI/Bridge/SpeckleEventAggregator.cs
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,35 @@ | ||
using Microsoft.Extensions.DependencyInjection; | ||
|
||
namespace Speckle.Connectors.DUI.Bridge; | ||
|
||
public interface ISpeckleEventAggregator | ||
{ | ||
TEventType GetEvent<TEventType>() | ||
where TEventType : EventBase; | ||
} | ||
|
||
public class SpeckleEventAggregator : ISpeckleEventAggregator | ||
{ | ||
private readonly IServiceProvider _serviceProvider; | ||
|
||
private readonly Dictionary<Type, EventBase> _events = new(); | ||
|
||
public SpeckleEventAggregator(IServiceProvider serviceProvider) | ||
{ | ||
_serviceProvider = serviceProvider; | ||
} | ||
|
||
public TEventType GetEvent<TEventType>() | ||
where TEventType : EventBase | ||
{ | ||
lock (_events) | ||
{ | ||
if (!_events.TryGetValue(typeof(TEventType), out var existingEvent)) | ||
{ | ||
existingEvent = (TEventType)_serviceProvider.GetRequiredService(typeof(TEventType)); | ||
_events[typeof(TEventType)] = existingEvent; | ||
} | ||
return (TEventType)existingEvent; | ||
} | ||
} | ||
} |
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.