/// <summary>
 /// Attempts to update context of disposable action stored in this field, Return <c>false</c> if the field is <c>null</c>.
 /// </summary>
 /// <param name="contextUpdater"></param>
 /// <returns></returns>
 public bool TryUpdateContext(Func <T, T> contextUpdater)
 {
     while (true)
     {
         var origin = Interlocked.CompareExchange(ref _field, _field, _field);
         if (origin == null)
         {
             return(false);
         }
         var updatedContext = new AsynchronousDisposableAction <T>(origin, contextUpdater);
         var ret            = Interlocked.CompareExchange(ref _field, updatedContext, origin);
         if (ReferenceEquals(origin, ret))
         {
             return(true);
         }
     }
 }
Ejemplo n.º 2
0
 /// <summary>
 /// Create a new disposable that executes dispose when disposed.
 /// </summary>
 /// <param name="disposableAction"></param>
 /// <returns></returns>
 public static AsynchronousAnonymousDisposableObject Create(AsynchronousDisposableAction disposableAction) => new AsynchronousAnonymousDisposableObject(disposableAction);
Ejemplo n.º 3
0
 /// <summary>
 /// Add dispose <see cref="Action"/>.
 /// </summary>
 /// <param name="disposableAction"></param>
 public void Add(AsynchronousDisposableAction disposableAction)
 {
     Add(disposableAction?.InternalAction);
 }
Ejemplo n.º 4
0
 /// <summary>
 /// Create a new <see cref="AsynchronousAnonymousDisposableObject"/> instance.
 /// </summary>
 /// <param name="disposableAction"></param>
 public AsynchronousAnonymousDisposableObject(AsynchronousDisposableAction disposableAction) : base(disposableAction?.InternalAction)
 {
 }
 /// <summary>
 /// Create a new <see cref="AsynchronousDisposableActionField{T}"/> instance.
 /// </summary>
 /// <param name="action"></param>
 /// <param name="context"></param>
 public AsynchronousDisposableActionField(Func <T, ValueTask> action, T context)
 {
     _field = new AsynchronousDisposableAction <T>(action, context);
 }
 /// <summary>
 /// Create a new <see cref="AsynchronousDisposableAction{T}"/> instance.
 /// </summary>
 /// <param name="originalDisposableAction"></param>
 /// <param name="contextUpdater"></param>
 public AsynchronousDisposableAction(AsynchronousDisposableAction <T> originalDisposableAction, Func <T, T> contextUpdater)
     : base(originalDisposableAction.Action, originalDisposableAction.Context, contextUpdater)
 {
 }