You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardexpand all lines: CHANGELOG.md
+18-1
Original file line number
Diff line number
Diff line change
@@ -4,7 +4,24 @@ All notable changes to this project will be documented in this file.
4
4
The format is based on [Keep a Changelog](http://keepachangelog.com/); this project adheres to [Semantic Versioning](http://semver.org/).
5
5
6
6
-----------------------
7
-
## [0.9.5] - unreleased
7
+
## [0.9.6] - unreleased
8
+
9
+
### Added
10
+
- Added `Play`/`Wait` extension methods for `Animation` and `Animator`.
11
+
- Added `AsyncResult.IsStarted` helper property.
12
+
13
+
### Changed
14
+
- Changed `AsyncResult` constructors argument order to avoid ambiguity in some cases.
15
+
- Moved the package content to Plugins folder and remove assembly definition file.
16
+
- Moved web request related helpers from `AsyncUtility` to `AsyncWww` class.
17
+
- Changed `AsyncUtility.SendToMainThread`, `AsyncUtility.PostToMainThread` and `AsyncUtility.InvokeOnMainThread` implementation to use `ConcurrentQueue` for net46+ to avoid unnesesary locks.
18
+
- Changed interface of `AsyncResultQueue`.
19
+
20
+
### Removed
21
+
- Removed `IAsyncOperationEvents.TryAddCompletionCallback` and `IAsyncOperationEvents.TryAddProgressCallback` methods. These methods are not needed in 99% of cases and may lead to logic errors in multi-threaded environment.
22
+
23
+
-----------------------
24
+
## [0.9.5] - 2018.07.31
8
25
9
26
### Added
10
27
- Added `IAsyncOperation.Id` property for easy operation identification.
Unity Asset Store | [](https://assetstore.unity.com/packages/tools/asynchronous-operations-for-unity-96696)
8
+
Unity Asset Store | [](https://assetstore.unity.com/packages/tools/asynchronous-operations-for-unity-96696)
9
9
10
10
**If you enjoy using the library - please, [rate and review](https://assetstore.unity.com/packages/tools/asynchronous-operations-for-unity-96696) it on the Asset Store!**
11
11
@@ -156,7 +156,7 @@ catch (Exception e)
156
156
```
157
157
In fact the only notable difference from synchronous implementation is usage of the mentioned `async` and `await` keywords. It's worth mentioning that a lot of hidden work is done by both the C# compliter and asynchronous operation to allow this.
158
158
159
-
*UnityFx.Async* supports all of the asynchronous programming approaches described.
159
+
*UnityFx.Async* supports all the asynchronous programming approaches described.
160
160
161
161
## Using the library
162
162
Reference the DLL and import the namespace:
@@ -166,22 +166,26 @@ using UnityFx.Async.Promises; // For promises-specific stuff.
166
166
```
167
167
Create an operation instance like this:
168
168
```csharp
169
-
varop=newAsyncCompletionSource<string>();
169
+
varacs=newAsyncCompletionSource<string>();
170
+
varop=acs.Operation;
170
171
```
171
172
The type of the operation should reflect its result type. In this case we create a special kind of operation - a completion source, that incapsulates both producer and consumer interfaces (consumer side is represented via `IAsyncOperation` / `IAsyncOperation<TResult>` interfaces and producer side is `IAsyncCompletionSource` / `IAsyncCompletionSource<TResult>`, `AsyncCompletionSource` implements both of the interfaces).
172
173
173
-
While operation is running its progress can be set like this:
174
+
While operation is running its progress can be set via `IAsyncCompletionSource`like this:
174
175
```csharp
175
-
op.SetProgress(progressValue);
176
+
acs.SetProgress(progressValue);
176
177
```
177
178
178
-
Upon completion of an asynchronous operation transition it to one of the final states (`RanToCompletion`, `Faulted`or `Canceled`):
179
+
Cancellation can be requested for any operation at any time (note that this call just *requests* cancellation, specific operation implementation may decide to postpone or even ignore it):
179
180
```csharp
180
-
op.SetResult(resultValue);
181
+
op.Cancel();
181
182
```
182
-
Or, if the operation has failed:
183
+
184
+
Upon completion an asynchronous operation transitions to one of the final states (`RanToCompletion`, `Faulted` or `Canceled`):
183
185
```csharp
184
-
op.SetException(ex);
186
+
acs.SetResult(resultValue); // Sets result value and transitions to RanToCompletion state.
187
+
acs.SetException(ex); // Transitions the operation to Faulted state.
188
+
acs.SetCanceled(); // Transitions the operation to Canceled state.
185
189
```
186
190
187
191
To see it in context, here is an example of a function that downloads text from URL using [UnityWebRequest](https://docs.unity3d.com/ScriptReference/Networking.UnityWebRequest.html):
`ContinueWith()` and `Finally()` delegates get called independently of the antecedent operation result. `ContinueWith()` also define overloads accepting `AsyncContinuationOptions` argument that allows to customize its behaviour. Note that `ContinueWith()`is analog to the corresponding [Task method](https://docs.microsoft.com/en-us/dotnet/api/system.threading.tasks.task.continuewith) and not a part of the JS promise pattern:
297
+
`ContinueWith()` and `Finally()` delegates get called independently of the antecedent operation result. `ContinueWith()` also define overloads accepting `AsyncContinuationOptions` argument that allows to customize its behaviour. Note that `ContinueWith()`matches the corresponding [Task method](https://docs.microsoft.com/en-us/dotnet/api/system.threading.tasks.task.continuewith) and is not a part of the JS promise pattern:
294
298
```csharp
295
299
DownloadTextAsync("http://www.google.com")
296
300
.ContinueWith(op=>Debug.Log("1"))
@@ -325,43 +329,43 @@ finally
325
329
### Cancellation
326
330
All library operations can be cancelled using `Cancel()` method:
327
331
```csharp
328
-
op.Cancel(); //attempts to cancel an operation
332
+
op.Cancel(); //Attempts to cancel an operation.
329
333
```
330
-
Or with `WithCancellation()` extension:
334
+
Or with `WithCancellation()` extension (if [CancellationToken](https://docs.microsoft.com/en-us/dotnet/api/system.threading.cancellationtoken) is needed):
331
335
```csharp
332
336
DownloadTextAsync("http://www.google.com")
333
337
.Then(text=>ExtractFirstParagraph(text))
334
338
.WithCancellation(cancellationToken);
335
339
```
336
-
If the [token](https://docs.microsoft.com/en-us/dotnet/api/system.threading.cancellationtoken) passed to `WithCancellation()` is cancelled, the target operation is cancelled as well (and that means cancelling all chained operations) as soon as possible. Cancellation might not be instant (depends on specific operation implementation). Also, please note that not all operations might support cancellation; in this case `Cancel()`will throw `NotSupportedException`.
340
+
If the [token](https://docs.microsoft.com/en-us/dotnet/api/system.threading.cancellationtoken) passed to `WithCancellation()` is cancelled, the target operation is cancelled as well (and that means cancelling all chained operations) as soon as possible. Cancellation might not be instant (depends on specific operation implementation). Also, please note that not all operations might support cancellation; in this case `Cancel()`might just do nothing.
337
341
338
342
### Progress reporting
339
343
Library operations support progress reporting via exposing `IAsyncOperation.Progress` property and progress reporting events:
340
344
```csharp
341
-
varprogress=op.Progress; //gets an operation progress as a float value in range [0, 1]
345
+
varprogress=op.Progress; //Gets an operation progress as a float value in range [0, 1].
342
346
343
-
//subscribe to progress changed event
347
+
//Subscribe to progress changed event.
344
348
op.ProgressChanged+= (sender, args) =>
345
349
{
346
350
Debug.Log("Progress = "+args.ProgressPercentage);
347
351
}
348
352
349
-
//add progress changed delegate
353
+
//Add progress changed delegate.
350
354
op.AddProgressCallback(op =>
351
355
{
352
356
Debug.Log("Progress = "+op.Progress);
353
357
});
354
358
```
355
-
There is `AsyncResult.GetProgress()` virtual method that is called when a progress values is requested. Finally there are producer-side methods like `AsyncCompletionSource.TrySetProgress()` that can set the progress value.
359
+
There is `AsyncResult.GetProgress()` virtual method that is called when a progress values is requested. Finally there are producer-side methods like `AsyncCompletionSource.SetProgress()` that can set the progress value.
356
360
357
361
### Synchronization context capturing
358
362
The default behaviour of all library methods is to capture current [SynchronizationContext](https://docs.microsoft.com/en-us/dotnet/api/system.threading.synchronizationcontext) and try to schedule continuations on it. If there is no synchronization context attached to current thread, continuations are executed on a thread that initiated an operation completion. The same behaviour applies to `async` / `await` implementation unless explicitly overriden with `ConfigureAwait()`:
@@ -401,7 +405,7 @@ All operations implement [IDisposable](https://docs.microsoft.com/en-us/dotnet/a
401
405
Please note that `Dispose()` implementation is NOT thread-safe and can only be called after an operation has completed (the same restrictions apply to [Task](https://docs.microsoft.com/en-us/dotnet/api/system.threading.tasks.task)).
402
406
403
407
### Completed asynchronous operations
404
-
There are a number of helper methods for creating completed operations:
408
+
There are a number of helper methods and properties for creating completed operations:
405
409
```csharp
406
410
varop1=AsyncResult.CompletedOperation;
407
411
varop2=AsyncResult.CanceledOperation;
@@ -427,10 +431,10 @@ var op5 = unityWWW.ToAsync();
427
431
428
432
### Creating own asynchronous operations
429
433
Most common way of creating own asynchronous operation is instantiating `AsyncCompletionSource` instance and call `SetResult()` / `SetException()` / `SetCanceled()` when done. Still there are cases when more control is required. For this purpose the library provides two public extendable implementations for asynchronous operations:
430
-
*`AsyncResult`: a generic asynchronous operation without a result value.
434
+
*`AsyncResult`: an asynchronous operation without a result value.
431
435
*`AsyncResult<TResult>`: an asynchronous operation with a result value.
432
436
433
-
The sample code below demostrates creating a delay operation:
437
+
The sample code below demostrates creating a delay operation (in fact library provides one, this is just a simplified example):
434
438
```csharp
435
439
publicclassTimerDelayResult : AsyncResult
436
440
{
@@ -469,6 +473,27 @@ public class TimerDelayResult : AsyncResult
469
473
}
470
474
```
471
475
476
+
### Unity3d helpers
477
+
The library consists of 3 major parts:
478
+
* Core tools (defined in `UnityFx.Async.dll` assembly, do not depend on Unity3d);
479
+
* Unity3d-specific tools (defined as a collection of C# scripts located in `Assets/Plugins/UnityFx.Async` if installed as an Asset Store package, require Unity3d to compile/execute).
480
+
* Unity3d samples (defined as a collection of C# scripts located in `Assets/UnityFx.Async` if installed as an Asset Store package, require Unity3d to compile/execute).
481
+
482
+
Everything described before (unless specified otherwise) does not require Unity and can be used in any application. The Unity-specific stuff is located in 3 classes:
483
+
*`AsyncUtility`. Defines helper methods for accessing main thread in Unity, running coroutines without actually using a `MonoBehaviour` and waiting for native Unity asynchronous operations outside of coroutines.
484
+
*`AsyncWww`. Defines web request related helpers.
485
+
*`UnityExtensions`. Defines extensions for native Unity classes (like `AsyncOperation` and `UnityWebRequest`).
486
+
487
+
For example, one can throw a few lines of code to be executed on a main thread using:
488
+
```csharp
489
+
// Sends a delegate to the main thread and blocks calling thread until it is executed.
490
+
AsyncUtility.SendToMainThread(args=>Debug.Log("On the main thread."), null);
491
+
// Posts a delegate to the main thread and returns immediately. Returns an asynchronous operation that can be used to track the delegate execution.
492
+
AsyncUtility.PostToMainThread(args=>Debug.Log("On the main thread."), null);
493
+
// If calling thread is the main thread executes the delegate synchronously, otherwise posts it to the main thread. Returns an asynchronous operation that can be used to track the delegate execution.
494
+
AsyncUtility.InvokeOnMainThread(args=>Debug.Log("On the main thread."), null);
495
+
```
496
+
472
497
## Comparison to .NET Tasks
473
498
The comparison table below shows how *UnityFx.Async* entities relate to [Tasks](https://docs.microsoft.com/en-us/dotnet/api/system.threading.tasks.task):
474
499
@@ -515,15 +540,12 @@ Please see the links below for extended information on the product:
Please see [contributing guide](.github/CONTRIBUTING.md) for details.
520
544
521
545
## Versioning
522
-
523
546
The project uses [SemVer](https://semver.org/) versioning pattern. For the versions available, see [tags in this repository](https://github.com/Arvtesh/UnityFx.Async/tags).
524
547
525
548
## License
526
-
527
549
Please see the [](LICENSE.md) for details.
0 commit comments