-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Feature add SynchronizeAsync Add SubscribeSynchronus * Update ReactiveExtensionsTests.cs * Update README.md
- Loading branch information
1 parent
7b5b4ca
commit f7bed15
Showing
4 changed files
with
274 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
// Copyright (c) 2019-2023 ReactiveUI Association Incorporated. All rights reserved. | ||
// ReactiveUI Association Incorporated licenses this file to you under the MIT license. | ||
// See the LICENSE file in the project root for full license information. | ||
|
||
using System; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
|
||
namespace ReactiveMarbles.Extensions; | ||
|
||
/// <summary> | ||
/// Continuation. | ||
/// </summary> | ||
public class Continuation : IDisposable | ||
{ | ||
private readonly Barrier _phaseSync = new(2); | ||
private bool _disposedValue; | ||
private bool _locked; | ||
|
||
/// <summary> | ||
/// Gets the number of completed phases. | ||
/// </summary> | ||
/// <value> | ||
/// The completed phases. | ||
/// </value> | ||
public long CompletedPhases => _phaseSync.CurrentPhaseNumber; | ||
|
||
/// <summary> | ||
/// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources. | ||
/// </summary> | ||
public void Dispose() | ||
{ | ||
// Do not change this code. Put cleanup code in 'Dispose(bool disposing)' method | ||
Dispose(disposing: true); | ||
GC.SuppressFinalize(this); | ||
} | ||
|
||
/// <summary> | ||
/// Locks this instance. | ||
/// </summary> | ||
/// <typeparam name="T">The type of the elements in the source sequence.</typeparam> | ||
/// <param name="item">The item.</param> | ||
/// <param name="observer">The observer.</param> | ||
/// <returns> | ||
/// A <see cref="Task" /> representing the asynchronous operation. | ||
/// </returns> | ||
public Task Lock<T>(T item, IObserver<(T value, IDisposable Sync)> observer) | ||
{ | ||
if (_locked) | ||
{ | ||
return Task.CompletedTask; | ||
} | ||
|
||
_locked = true; | ||
observer.OnNext((item, this)); | ||
return Task.Run(() => _phaseSync?.SignalAndWait(CancellationToken.None)); | ||
} | ||
|
||
/// <summary> | ||
/// Releases unmanaged and - optionally - managed resources. | ||
/// </summary> | ||
/// <param name="disposing"><c>true</c> to release both managed and unmanaged resources; <c>false</c> to release only unmanaged resources.</param> | ||
protected virtual async void Dispose(bool disposing) | ||
{ | ||
if (!_disposedValue) | ||
{ | ||
if (disposing) | ||
{ | ||
await UnLock(); | ||
_phaseSync.Dispose(); | ||
} | ||
|
||
_disposedValue = true; | ||
} | ||
} | ||
|
||
/// <summary> | ||
/// UnLocks this instance. | ||
/// </summary> | ||
/// <returns>A <see cref="Task"/> representing the asynchronous operation.</returns> | ||
private Task UnLock() | ||
{ | ||
if (!_locked) | ||
{ | ||
return Task.CompletedTask; | ||
} | ||
|
||
_locked = false; | ||
return Task.Run(() => _phaseSync?.SignalAndWait(CancellationToken.None)); | ||
} | ||
} |
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