Skip to content

Commit

Permalink
Merge pull request #174 from energyworldnet/fix-stream-disposal
Browse files Browse the repository at this point in the history
Fix incompatibility w/ Portable.BouncyCastle 1.9.0
  • Loading branch information
mattosaurus authored Sep 2, 2022
2 parents bf6f1cc + 8b9719a commit 096baad
Show file tree
Hide file tree
Showing 3 changed files with 282 additions and 253 deletions.
27 changes: 27 additions & 0 deletions PgpCore/CompositeDisposable.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
using System;
using System.Collections.Concurrent;

namespace PgpCore
{
/// <seealso href="https://github.com/dotnet/reactive/blob/main/Rx.NET/Source/src/System.Reactive/Disposables/CompositeDisposable.cs">
/// Simplified adaptation from System.Reactive.
/// </seealso>
internal sealed class CompositeDisposable : IDisposable
{
private readonly ConcurrentQueue<IDisposable> _disposables = new ConcurrentQueue<IDisposable>();

public void Add(IDisposable disposable)
{
if (disposable == null)
throw new ArgumentNullException(nameof(disposable));

_disposables.Enqueue(disposable);
}

public void Dispose()
{
while (_disposables.TryDequeue(out IDisposable disposable))
disposable.Dispose();
}
}
}
22 changes: 22 additions & 0 deletions PgpCore/DisposableExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
using System;

namespace PgpCore
{
internal static class DisposableExtensions
{
/// <seealso href="https://github.com/reactiveui/ReactiveUI/blob/main/src/ReactiveUI/Mixins/DisposableMixins.cs#L28">
/// Adapted from ReactiveUI.
/// </seealso>
public static T DisposeWith<T>(this T @this, CompositeDisposable disposables)
where T : IDisposable
{
if (@this == null)
throw new ArgumentNullException(nameof(@this));
if (disposables == null)
throw new ArgumentNullException(nameof(disposables));

disposables.Add(@this);
return @this;
}
}
}
Loading

0 comments on commit 096baad

Please sign in to comment.