feat: make Unity integration optional
This commit is contained in:
27 files changed
+916
-69
No files matched your search
@@ -0,0 +1,12 @@
|
||||
#if UNITY_5_3_OR_NEWER
|
||||
using System;
|
||||
|
||||
namespace ShrinkEventBus
|
||||
{
|
||||
public interface IShrinkEventBusMainThreadDispatcher
|
||||
{
|
||||
bool IsMainThread { get; }
|
||||
bool TryPost(Action action);
|
||||
}
|
||||
}
|
||||
#endif
|
||||
@@ -0,0 +1,2 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 9ff0b939fcee4292a6183fd899974c9c
|
||||
@@ -6,7 +6,11 @@ using System.Diagnostics;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using Cysharp.Threading.Tasks;
|
||||
using ShrinkSDK.Runtime;
|
||||
#if UNITY_5_3_OR_NEWER
|
||||
using MainThreadDispatcherContract = ShrinkEventBus.IShrinkEventBusMainThreadDispatcher;
|
||||
#else
|
||||
using MainThreadDispatcherContract = ShrinkSDK.Runtime.IShrinkMainThreadDispatcher;
|
||||
#endif
|
||||
|
||||
namespace ShrinkEventBus
|
||||
{
|
||||
@@ -208,7 +212,7 @@ namespace ShrinkEventBus
|
||||
internal sealed class ShrinkMainThreadScheduler : IShrinkBusScheduler
|
||||
{
|
||||
private readonly ShrinkSchedulerQueue _queue;
|
||||
private readonly IShrinkMainThreadDispatcher _dispatcher;
|
||||
private readonly MainThreadDispatcherContract _dispatcher;
|
||||
private int _pumpScheduled;
|
||||
private int _disposed;
|
||||
|
||||
|
||||
@@ -1,13 +1,12 @@
|
||||
{
|
||||
"name": "ShrinkEventBus.Runtime",
|
||||
"rootNamespace": "ShrinkEventBus",
|
||||
"references": [
|
||||
"UniTask",
|
||||
"ShrinkRuntime.Abstractions"
|
||||
],
|
||||
"includePlatforms": [],
|
||||
"excludePlatforms": [],
|
||||
"allowUnsafeCode": false,
|
||||
"overrideReferences": false,
|
||||
"autoReferenced": true
|
||||
"name": "ShrinkEventBus.Runtime",
|
||||
"rootNamespace": "ShrinkEventBus",
|
||||
"references": [
|
||||
"UniTask"
|
||||
],
|
||||
"includePlatforms": [],
|
||||
"excludePlatforms": [],
|
||||
"allowUnsafeCode": false,
|
||||
"overrideReferences": false,
|
||||
"autoReferenced": true
|
||||
}
|
||||
@@ -2,25 +2,29 @@
|
||||
|
||||
using System;
|
||||
using System.Threading;
|
||||
using ShrinkSDK.Runtime;
|
||||
#if UNITY_5_3_OR_NEWER
|
||||
using MainThreadDispatcherContract = ShrinkEventBus.IShrinkEventBusMainThreadDispatcher;
|
||||
#else
|
||||
using MainThreadDispatcherContract = ShrinkSDK.Runtime.IShrinkMainThreadDispatcher;
|
||||
#endif
|
||||
|
||||
namespace ShrinkEventBus
|
||||
{
|
||||
public static class ShrinkEventBusRuntime
|
||||
{
|
||||
private static IShrinkMainThreadDispatcher _mainThreadDispatcher = CreateDefaultDispatcher();
|
||||
private static MainThreadDispatcherContract _mainThreadDispatcher = CreateDefaultDispatcher();
|
||||
|
||||
public static IShrinkMainThreadDispatcher MainThreadDispatcher =>
|
||||
public static MainThreadDispatcherContract MainThreadDispatcher =>
|
||||
Volatile.Read(ref _mainThreadDispatcher);
|
||||
|
||||
public static void ConfigureMainThreadDispatcher(IShrinkMainThreadDispatcher dispatcher)
|
||||
public static void ConfigureMainThreadDispatcher(MainThreadDispatcherContract dispatcher)
|
||||
{
|
||||
if (dispatcher == null)
|
||||
throw new ArgumentNullException(nameof(dispatcher));
|
||||
Volatile.Write(ref _mainThreadDispatcher, dispatcher);
|
||||
}
|
||||
|
||||
private static IShrinkMainThreadDispatcher CreateDefaultDispatcher()
|
||||
private static MainThreadDispatcherContract CreateDefaultDispatcher()
|
||||
{
|
||||
#if UNITY_5_3_OR_NEWER
|
||||
return new ShrinkUnityMainThreadDispatcher();
|
||||
@@ -33,7 +37,7 @@ namespace ShrinkEventBus
|
||||
}
|
||||
|
||||
#if UNITY_5_3_OR_NEWER
|
||||
internal sealed class ShrinkUnityMainThreadDispatcher : IShrinkMainThreadDispatcher
|
||||
internal sealed class ShrinkUnityMainThreadDispatcher : MainThreadDispatcherContract
|
||||
{
|
||||
public bool IsMainThread => Cysharp.Threading.Tasks.PlayerLoopHelper.IsMainThread;
|
||||
|
||||
@@ -48,7 +52,7 @@ namespace ShrinkEventBus
|
||||
}
|
||||
}
|
||||
#else
|
||||
internal sealed class ShrinkSynchronizationContextDispatcher : IShrinkMainThreadDispatcher
|
||||
internal sealed class ShrinkSynchronizationContextDispatcher : MainThreadDispatcherContract
|
||||
{
|
||||
private readonly SynchronizationContext? _context;
|
||||
private readonly int _threadId;
|
||||
|
||||
Reference in new issue
Block a user