Skip to content

Commit

Permalink
Allow an EnvIO to be created from a Token
Browse files Browse the repository at this point in the history
  • Loading branch information
louthy committed Oct 25, 2024
1 parent bd13224 commit ebcf771
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 20 deletions.
60 changes: 52 additions & 8 deletions LanguageExt.Core/Effects/IO/EnvIO.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,59 @@ public class EnvIO : IDisposable
public readonly CancellationToken Token;
public readonly CancellationTokenSource Source;
public readonly SynchronizationContext? SyncContext;
private readonly CancellationTokenRegistration? Registration;
readonly int Own;
int disposed;

EnvIO(Resources resources,
CancellationToken token,
CancellationTokenSource source,
SynchronizationContext? syncContext,
CancellationTokenRegistration? registration,
int own)
{
Resources = resources;
Token = token;
Source = source;
SyncContext = syncContext;
Own = own;
Resources = resources;
Token = token;
Source = source;
SyncContext = syncContext;
Registration = registration;
Own = own;
}

/// <summary>
/// Creates a new EnvIO and registers it with the `token` provided, so if the `token` gets
/// a cancellation signal then so will any computation that uses the `EnvIO`.
/// </summary>
/// <param name="token">Token to register with</param>
/// <returns>Constructed EnvIO</returns>
public static EnvIO FromToken(CancellationToken token)
{
if (!token.CanBeCanceled) return New();
var nsource = new CancellationTokenSource();
var ntoken = nsource.Token;
var ncontext = SynchronizationContext.Current;
using var nreg = token.Register(() => nsource.Cancel());
var nresources = new Resources(null);
var own = 3;
return new EnvIO(nresources, ntoken, nsource, ncontext, nreg, own);
}

/// <summary>
/// Creates a new EnvIO and registers it with the `token` provided, so if the `token` gets
/// a cancellation signal then so will any computation that uses the `EnvIO`.
/// </summary>
/// <param name="resources">Resources collection to use instead of letting EnvIO construct one</param>
/// <param name="token">Token to register with</param>
/// <returns>Constructed EnvIO</returns>
public static EnvIO FromToken(Resources resources, CancellationToken token)
{
if (!token.CanBeCanceled) return New(resources);
var nsource = new CancellationTokenSource();
var ntoken = nsource.Token;
var ncontext = SynchronizationContext.Current;
using var nreg = token.Register(() => nsource.Cancel());
var own = 1;
return new EnvIO(resources, ntoken, nsource, ncontext, nreg, own);
}

public static EnvIO New(
Expand All @@ -48,7 +88,7 @@ public static EnvIO New(

token = token.CanBeCanceled ? token : source.Token;
syncContext ??= SynchronizationContext.Current;
return new EnvIO(resources, token, source, syncContext, own);
return new EnvIO(resources, token, source, syncContext, null, own);
}

public EnvIO LocalResources =>
Expand All @@ -62,8 +102,12 @@ public static EnvIO New(

public void Dispose()
{
if ((Own & 2) == 2) Resources.DisposeU(this);
if ((Own & 1) == 1) Source.Dispose();
if (Interlocked.CompareExchange(ref disposed, 1, 0) == 0)
{
if ((Own & 2) == 2) Resources.DisposeU(this);
if ((Own & 1) == 1) Source.Dispose();
Registration?.Dispose();
}
}

public override string ToString() =>
Expand Down
12 changes: 0 additions & 12 deletions LanguageExt.Core/Obsolete and Deprecated/Change.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,6 @@

internal class Change
{
public const string UseEffMonadInstead =
"This type has been deprecated. Please use the Eff monad instead. https://github.com/louthy/language-ext/discussions/1269";

public const string UseIOMonadInstead =
"This type has been deprecated. Please use the IO monad instead. https://github.com/louthy/language-ext/discussions/1269";

public const string UseEffMonadInsteadOfAff =
"Aff has been deprecated. Please use the Eff monad instead. https://github.com/louthy/language-ext/discussions/1269";

public const string UseTransducersInstead =
"This functionality is now available and has many more features in Transducers. See the wiki on Transducers to see how to get use transducers";

public const string UseCollectionIntialiser =
"Use collection intialiser instead. So, instead of: (x, y, z), you should now call [x, y, z]";

Expand Down

0 comments on commit ebcf771

Please sign in to comment.