Skip to content

Commit f43a16c

Browse files
committed
complete compilable
1 parent 989af0e commit f43a16c

12 files changed

+118
-332
lines changed

Assembly-CSharp.csproj

-6
Original file line numberDiff line numberDiff line change
@@ -283,9 +283,6 @@
283283
<Reference Include="UnityEngine.UI">
284284
<HintPath>C:/Program Files/Unity/Hub/Editor/2019.3.0a2/Editor/Data/UnityExtensions/Unity/GUISystem/UnityEngine.UI.dll</HintPath>
285285
</Reference>
286-
<Reference Include="UnityEngine.Advertisements">
287-
<HintPath>C:/GitHubRepositories/UniRx/Library/PackageCache/[email protected]/UnityEngine.Advertisements.dll</HintPath>
288-
</Reference>
289286
<Reference Include="Unity.Analytics.Editor">
290287
<HintPath>C:/GitHubRepositories/UniRx/Library/PackageCache/[email protected]/Unity.Analytics.Editor.dll</HintPath>
291288
</Reference>
@@ -295,9 +292,6 @@
295292
<Reference Include="Unity.Analytics.Tracker">
296293
<HintPath>C:/GitHubRepositories/UniRx/Library/PackageCache/[email protected]/Unity.Analytics.Tracker.dll</HintPath>
297294
</Reference>
298-
<Reference Include="UnityEngine.Purchasing">
299-
<HintPath>C:/GitHubRepositories/UniRx/Library/PackageCache/[email protected]/UnityEngine.Purchasing.dll</HintPath>
300-
</Reference>
301295
<Reference Include="mscorlib">
302296
<HintPath>C:/Program Files/Unity/Hub/Editor/2019.3.0a2/Editor/Data/MonoBleedingEdge/lib/mono/4.7.1-api/mscorlib.dll</HintPath>
303297
</Reference>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
#if CSHARP_7_OR_LATER || (UNITY_2018_3_OR_NEWER && (NET_STANDARD_2_0 || NET_4_6))
2+
#pragma warning disable CS1591 // Missing XML comment for publicly visible type or member
3+
4+
using System;
5+
using System.Collections.Generic;
6+
using System.Linq;
7+
using System.Threading.Tasks;
8+
9+
namespace UniRx.InternalUtil
10+
{
11+
internal interface ICancellableTaskCompletionSource
12+
{
13+
bool TrySetException(Exception exception);
14+
bool TrySetCanceled();
15+
}
16+
17+
internal class CancellableTaskCompletionSource<T> : TaskCompletionSource<T>, ICancellableTaskCompletionSource
18+
{
19+
20+
}
21+
}
22+
23+
#endif

Assets/Plugins/UniRx/Scripts/Tasks/UniTaskObservableExtensions.cs.meta Assets/Plugins/UniRx/Scripts/InternalUtil/CancellableTaskCompletionSource.cs.meta

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
#if CSHARP_7_OR_LATER || (UNITY_2018_3_OR_NEWER && (NET_STANDARD_2_0 || NET_4_6))
2+
#pragma warning disable CS1591 // Missing XML comment for publicly visible type or member
3+
4+
using System.Collections.Generic;
5+
using System.Linq;
6+
using System.Threading.Tasks;
7+
8+
namespace UniRx.InternalUtil
9+
{
10+
internal static class PromiseHelper
11+
{
12+
internal static void TrySetResultAll<T>(IEnumerable<TaskCompletionSource<T>> source, T value)
13+
{
14+
var rentArray = source.ToArray(); // better to use Arraypool.
15+
var array = rentArray;
16+
var len = rentArray.Length;
17+
for (int i = 0; i < len; i++)
18+
{
19+
array[i].TrySetResult(value);
20+
array[i] = null;
21+
}
22+
}
23+
}
24+
}
25+
26+
#endif

Assets/Plugins/UniRx/Scripts/UnityEngineBridge/ReactivePropertyReusablePromise.cs.meta Assets/Plugins/UniRx/Scripts/InternalUtil/PromiseHelper.cs.meta

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Assets/Scripts/UnityTests/Rx/ArrayPoolTest.cs.meta Assets/Plugins/UniRx/Scripts/InternalUtil/UnityEqualityComparer.cs.meta

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Assets/Plugins/UniRx/Scripts/UnityEngineBridge/ReactiveCommand.cs

+42-152
Original file line numberDiff line numberDiff line change
@@ -4,28 +4,21 @@
44

55
#if CSHARP_7_OR_LATER || (UNITY_2018_3_OR_NEWER && (NET_STANDARD_2_0 || NET_4_6))
66
using System.Threading.Tasks;
7+
using UniRx.InternalUtil;
78
#endif
89
namespace UniRx
910
{
1011
public interface IReactiveCommand<T> : IObservable<T>
1112
{
1213
IReadOnlyReactiveProperty<bool> CanExecute { get; }
1314
bool Execute(T parameter);
14-
15-
#if CSHARP_7_OR_LATER || (UNITY_2018_3_OR_NEWER && (NET_STANDARD_2_0 || NET_4_6))
16-
Task<T> WaitUntilExecuteAsync(CancellationToken cancellationToken);
17-
#endif
1815
}
1916

2017
public interface IAsyncReactiveCommand<T>
2118
{
2219
IReadOnlyReactiveProperty<bool> CanExecute { get; }
2320
IDisposable Execute(T parameter);
2421
IDisposable Subscribe(Func<T, IObservable<Unit>> asyncAction);
25-
26-
#if CSHARP_7_OR_LATER || (UNITY_2018_3_OR_NEWER && (NET_STANDARD_2_0 || NET_4_6))
27-
Task<T> WaitUntilExecuteAsync(CancellationToken cancellationToken);
28-
#endif
2922
}
3023

3124
/// <summary>
@@ -103,15 +96,6 @@ public bool Execute(T parameter)
10396
if (canExecute.Value)
10497
{
10598
trigger.OnNext(parameter);
106-
107-
#if CSHARP_7_OR_LATER || (UNITY_2018_3_OR_NEWER && (NET_STANDARD_2_0 || NET_4_6))
108-
commonPromise?.InvokeContinuation(ref parameter);
109-
if (removablePromises != null)
110-
{
111-
PromiseHelper.TrySetResultAll(removablePromises.Values, parameter);
112-
}
113-
#endif
114-
11599
return true;
116100
}
117101
else
@@ -144,65 +128,7 @@ public void Dispose()
144128
trigger.OnCompleted();
145129
trigger.Dispose();
146130
canExecuteSubscription.Dispose();
147-
148-
#if CSHARP_7_OR_LATER || (UNITY_2018_3_OR_NEWER && (NET_STANDARD_2_0 || NET_4_6))
149-
commonPromise?.SetCanceled();
150-
commonPromise = null;
151-
if (removablePromises != null)
152-
{
153-
foreach (var item in removablePromises)
154-
{
155-
item.Value.SetCanceled();
156-
}
157-
removablePromises = null;
158-
}
159-
#endif
160131
}
161-
162-
#if CSHARP_7_OR_LATER || (UNITY_2018_3_OR_NEWER && (NET_STANDARD_2_0 || NET_4_6))
163-
164-
static readonly Action<object> Callback = CancelCallback;
165-
ReactivePropertyReusablePromise<T> commonPromise;
166-
Dictionary<CancellationToken, ReactivePropertyReusablePromise<T>> removablePromises;
167-
168-
public UniTask<T> WaitUntilExecuteAsync(CancellationToken cancellationToken)
169-
{
170-
if (IsDisposed) throw new ObjectDisposedException("ReadOnlyReactiveProperty");
171-
172-
if (!cancellationToken.CanBeCanceled)
173-
{
174-
if (commonPromise != null) return commonPromise.Task;
175-
commonPromise = new ReactivePropertyReusablePromise<T>(CancellationToken.None);
176-
return commonPromise.Task;
177-
}
178-
179-
if (removablePromises == null)
180-
{
181-
removablePromises = new Dictionary<CancellationToken, ReactivePropertyReusablePromise<T>>(CancellationTokenEqualityComparer.Default);
182-
}
183-
184-
if (removablePromises.TryGetValue(cancellationToken, out var newPromise))
185-
{
186-
return newPromise.Task;
187-
}
188-
189-
newPromise = new ReactivePropertyReusablePromise<T>(cancellationToken);
190-
removablePromises.Add(cancellationToken, newPromise);
191-
cancellationToken.RegisterWithoutCaptureExecutionContext(Callback, Tuple.Create(this, newPromise));
192-
193-
return newPromise.Task;
194-
}
195-
196-
static void CancelCallback(object state)
197-
{
198-
var tuple = (Tuple<ReactiveCommand<T>, ReactivePropertyReusablePromise<T>>)state;
199-
if (tuple.Item1.IsDisposed) return;
200-
201-
tuple.Item2.SetCanceled();
202-
tuple.Item1.removablePromises.Remove(tuple.Item2.RegisteredCancelationToken);
203-
}
204-
205-
#endif
206132
}
207133

208134
/// <summary>
@@ -302,14 +228,6 @@ public IDisposable Execute(T parameter)
302228
{
303229
try
304230
{
305-
#if CSHARP_7_OR_LATER || (UNITY_2018_3_OR_NEWER && (NET_STANDARD_2_0 || NET_4_6))
306-
commonPromise?.InvokeContinuation(ref parameter);
307-
if (removablePromises != null)
308-
{
309-
PromiseHelper.TrySetResultAll(removablePromises.Values, parameter);
310-
}
311-
#endif
312-
313231
var asyncState = a[0].Invoke(parameter) ?? Observable.ReturnUnit();
314232
return asyncState.Finally(() => canExecuteSource.Value = true).Subscribe();
315233
}
@@ -324,14 +242,6 @@ public IDisposable Execute(T parameter)
324242
var xs = new IObservable<Unit>[a.Length];
325243
try
326244
{
327-
#if CSHARP_7_OR_LATER || (UNITY_2018_3_OR_NEWER && (NET_STANDARD_2_0 || NET_4_6))
328-
commonPromise?.InvokeContinuation(ref parameter);
329-
if (removablePromises != null)
330-
{
331-
PromiseHelper.TrySetResultAll(removablePromises.Values, parameter);
332-
}
333-
#endif
334-
335245
for (int i = 0; i < a.Length; i++)
336246
{
337247
xs[i] = a[i].Invoke(parameter) ?? Observable.ReturnUnit();
@@ -372,66 +282,7 @@ public void Dispose()
372282

373283
IsDisposed = true;
374284
asyncActions = UniRx.InternalUtil.ImmutableList<Func<T, IObservable<Unit>>>.Empty;
375-
376-
#if CSHARP_7_OR_LATER || (UNITY_2018_3_OR_NEWER && (NET_STANDARD_2_0 || NET_4_6))
377-
commonPromise?.SetCanceled();
378-
commonPromise = null;
379-
if (removablePromises != null)
380-
{
381-
foreach (var item in removablePromises)
382-
{
383-
item.Value.SetCanceled();
384-
}
385-
removablePromises = null;
386-
}
387-
#endif
388-
}
389-
390-
#if CSHARP_7_OR_LATER || (UNITY_2018_3_OR_NEWER && (NET_STANDARD_2_0 || NET_4_6))
391-
392-
static readonly Action<object> Callback = CancelCallback;
393-
ReactivePropertyReusablePromise<T> commonPromise;
394-
Dictionary<CancellationToken, ReactivePropertyReusablePromise<T>> removablePromises;
395-
396-
public UniTask<T> WaitUntilExecuteAsync(CancellationToken cancellationToken)
397-
{
398-
if (IsDisposed) throw new ObjectDisposedException("ReadOnlyReactiveProperty");
399-
400-
if (!cancellationToken.CanBeCanceled)
401-
{
402-
if (commonPromise != null) return commonPromise.Task;
403-
commonPromise = new ReactivePropertyReusablePromise<T>(CancellationToken.None);
404-
return commonPromise.Task;
405-
}
406-
407-
if (removablePromises == null)
408-
{
409-
removablePromises = new Dictionary<CancellationToken, ReactivePropertyReusablePromise<T>>(CancellationTokenEqualityComparer.Default);
410-
}
411-
412-
if (removablePromises.TryGetValue(cancellationToken, out var newPromise))
413-
{
414-
return newPromise.Task;
415-
}
416-
417-
newPromise = new ReactivePropertyReusablePromise<T>(cancellationToken);
418-
removablePromises.Add(cancellationToken, newPromise);
419-
cancellationToken.RegisterWithoutCaptureExecutionContext(Callback, Tuple.Create(this, newPromise));
420-
421-
return newPromise.Task;
422285
}
423-
424-
static void CancelCallback(object state)
425-
{
426-
var tuple = (Tuple<AsyncReactiveCommand<T>, ReactivePropertyReusablePromise<T>>)state;
427-
if (tuple.Item1.IsDisposed) return;
428-
429-
tuple.Item2.SetCanceled();
430-
tuple.Item1.removablePromises.Remove(tuple.Item2.RegisteredCancelationToken);
431-
}
432-
433-
#endif
434-
435286
class Subscription : IDisposable
436287
{
437288
readonly AsyncReactiveCommand<T> parent;
@@ -473,7 +324,26 @@ public static ReactiveCommand<T> ToReactiveCommand<T>(this IObservable<bool> can
473324

474325
#if CSHARP_7_OR_LATER || (UNITY_2018_3_OR_NEWER && (NET_STANDARD_2_0 || NET_4_6))
475326

476-
public static UniTask<T>.Awaiter GetAwaiter<T>(this IReactiveCommand<T> command)
327+
static readonly Action<object> Callback = CancelCallback;
328+
329+
static void CancelCallback(object state)
330+
{
331+
var tuple = (Tuple<ICancellableTaskCompletionSource, IDisposable>)state;
332+
tuple.Item2.Dispose();
333+
tuple.Item1.TrySetCanceled();
334+
}
335+
336+
public static Task<T> WaitUntilExecuteAsync<T>(this IReactiveCommand<T> source, CancellationToken cancellationToken = default(CancellationToken))
337+
{
338+
var tcs = new CancellableTaskCompletionSource<T>();
339+
340+
var subscription = source.Subscribe(x => tcs.TrySetResult(x), ex => tcs.TrySetException(ex), () => tcs.TrySetCanceled());
341+
cancellationToken.Register(Callback, Tuple.Create(tcs, subscription), false);
342+
343+
return tcs.Task;
344+
}
345+
346+
public static System.Runtime.CompilerServices.TaskAwaiter<T> GetAwaiter<T>(this IReactiveCommand<T> command)
477347
{
478348
return command.WaitUntilExecuteAsync(CancellationToken.None).GetAwaiter();
479349
}
@@ -534,13 +404,33 @@ public static AsyncReactiveCommand<T> ToAsyncReactiveCommand<T>(this IReactivePr
534404

535405
#if CSHARP_7_OR_LATER || (UNITY_2018_3_OR_NEWER && (NET_STANDARD_2_0 || NET_4_6))
536406

537-
public static UniTask<T>.Awaiter GetAwaiter<T>(this IAsyncReactiveCommand<T> command)
407+
static readonly Action<object> Callback = CancelCallback;
408+
409+
static void CancelCallback(object state)
410+
{
411+
var tuple = (Tuple<ICancellableTaskCompletionSource, IDisposable>)state;
412+
tuple.Item2.Dispose();
413+
tuple.Item1.TrySetCanceled();
414+
}
415+
416+
public static Task<T> WaitUntilExecuteAsync<T>(this IAsyncReactiveCommand<T> source, CancellationToken cancellationToken = default(CancellationToken))
417+
{
418+
var tcs = new CancellableTaskCompletionSource<T>();
419+
420+
var subscription = source.Subscribe(x => { tcs.TrySetResult(x); return Observable.ReturnUnit(); });
421+
cancellationToken.Register(Callback, Tuple.Create(tcs, subscription), false);
422+
423+
return tcs.Task;
424+
}
425+
426+
public static System.Runtime.CompilerServices.TaskAwaiter<T> GetAwaiter<T>(this IAsyncReactiveCommand<T> command)
538427
{
539428
return command.WaitUntilExecuteAsync(CancellationToken.None).GetAwaiter();
540429
}
541430

542431
#endif
543432

433+
544434
#if !UniRxLibrary
545435

546436
// for uGUI(from 4.6)

0 commit comments

Comments
 (0)