Skip to content

Commit

Permalink
feat: introduces InvokeAsync extension method to support asynchrono…
Browse files Browse the repository at this point in the history
…us event handlers.

Signed-off-by: Leonardo Chaia <[email protected]>
  • Loading branch information
leonardochaia committed Nov 26, 2024
1 parent 9e40655 commit 68220bd
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 9 deletions.
39 changes: 39 additions & 0 deletions src/OrasProject.Oras/AsyncInvocationExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
// Copyright The ORAS Authors.
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

using System;
using System.Threading.Tasks;

namespace OrasProject.Oras;

internal static class AsyncInvocationExtensions
{
/// <summary>
/// Sequentially invokes an event that returns a <see cref="Task"/>.
/// Each event listener is executed in sequence, and it's returning
/// task awaited before executing the next one.
/// </summary>
/// <param name="eventDelegate"></param>
/// <param name="args"></param>
/// <typeparam name="TEventArgs"></typeparam>
internal static async Task InvokeAsync<TEventArgs>(
this Func<TEventArgs, Task>? eventDelegate, TEventArgs args)
{
if (eventDelegate == null) return;

foreach (var handler in eventDelegate.GetInvocationList())
{
await ((Task?)handler.DynamicInvoke(args) ?? Task.CompletedTask);
}
}
}
10 changes: 5 additions & 5 deletions src/OrasProject.Oras/CopyGraphOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,27 +25,27 @@ public struct CopyGraphOptions
/// <summary>
/// PreCopy handles the current descriptor before it is copied.
/// </summary>
public event Func<Descriptor, Task> PreCopy;
public event Func<Descriptor, Task>? PreCopy;

/// <summary>
/// PostCopy handles the current descriptor after it is copied.
/// </summary>
public event Func<Descriptor, Task> PostCopy;
public event Func<Descriptor, Task>? PostCopy;

/// <summary>
/// CopySkipped will be called when the sub-DAG rooted by the current node
/// is skipped.
/// </summary>
public event Func<Descriptor, Task> CopySkipped;
public event Func<Descriptor, Task>? CopySkipped;

internal Task OnPreCopyAsync(Descriptor descriptor)
{
return PreCopy?.Invoke(descriptor) ?? Task.CompletedTask;
return PreCopy?.InvokeAsync(descriptor) ?? Task.CompletedTask;
}

internal Task OnPostCopyAsync(Descriptor descriptor)
{
return PostCopy?.Invoke(descriptor) ?? Task.CompletedTask;
return PostCopy?.InvokeAsync(descriptor) ?? Task.CompletedTask;
}

internal Task OnCopySkippedAsync(Descriptor descriptor)
Expand Down
7 changes: 3 additions & 4 deletions src/OrasProject.Oras/Extensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ public static async Task CopyGraphAsync(this ITarget src, ITarget dst, Descripto
// check if node exists in target
if (await dst.ExistsAsync(node, cancellationToken).ConfigureAwait(false))
{
await copyGraphOptions.OnCopySkippedAsync(node);
await copyGraphOptions.OnCopySkippedAsync(node).ConfigureAwait(false);
return;
}

Expand All @@ -92,12 +92,11 @@ public static async Task CopyGraphAsync(this ITarget src, ITarget dst, Descripto
}

// perform the copy
await copyGraphOptions.OnPreCopyAsync(node);
await copyGraphOptions.OnPreCopyAsync(node).ConfigureAwait(false);
var dataStream = await src.FetchAsync(node, cancellationToken).ConfigureAwait(false);
await dst.PushAsync(node, dataStream, cancellationToken).ConfigureAwait(false);

// we copied it
await copyGraphOptions.OnPostCopyAsync(node);
await copyGraphOptions.OnPostCopyAsync(node).ConfigureAwait(false);
}
}

0 comments on commit 68220bd

Please sign in to comment.