-
Notifications
You must be signed in to change notification settings - Fork 16
feat(copy): introduces CopyGraphOptions with events support #145
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from 25 commits
6817504
b453fbc
6a7a1e9
88c6793
4c52467
36342c9
dbe3509
571820c
138120e
77e8079
06fd442
5787c04
c9be0a2
77ba179
ecaf1b1
de9ee0d
6ef7e74
be7b1c5
a12ff78
a7a1baf
493a8d9
9e40655
68220bd
e35fc7e
827f62b
6d1b0ba
2372ac6
f7b326d
fe10a1a
c1c7182
7b31ec3
b9f93d0
20ac068
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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.Linq; | ||
using System.Threading.Tasks; | ||
|
||
namespace OrasProject.Oras; | ||
|
||
internal static class AsyncInvocationExtensions | ||
{ | ||
/// <summary> | ||
/// Asynchronously invokes all handlers from an event that returns a <see cref="Task"/>. | ||
/// <remarks>All handlers are executed in parallel</remarks> | ||
/// </summary> | ||
/// <param name="eventDelegate"></param> | ||
/// <param name="args"></param> | ||
/// <typeparam name="TEventArgs"></typeparam> | ||
internal static Task InvokeAsync<TEventArgs>( | ||
this Func<TEventArgs, Task>? eventDelegate, TEventArgs args) | ||
{ | ||
if (eventDelegate == null) return Task.CompletedTask; | ||
|
||
var tasks = eventDelegate.GetInvocationList() | ||
.Select(d => (Task?)d.DynamicInvoke(args) ?? Task.CompletedTask); | ||
|
||
return Task.WhenAll(tasks); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
// 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; | ||
using OrasProject.Oras.Oci; | ||
|
||
namespace OrasProject.Oras; | ||
|
||
/// <summary> | ||
/// CopyGraphOptions contains parameters for <see cref="Extensions.CopyGraphAsync(OrasProject.Oras.ITarget,OrasProject.Oras.ITarget,OrasProject.Oras.Oci.Descriptor,System.Threading.CancellationToken)"/> | ||
/// </summary> | ||
public struct CopyGraphOptions | ||
{ | ||
/// <summary> | ||
/// PreCopyAsync handles the current descriptor before it is copied. | ||
/// </summary> | ||
public event Func<Descriptor, Task>? PreCopyAsync; | ||
|
||
/// <summary> | ||
/// PreCopy handles the current descriptor before it is copied. | ||
/// </summary> | ||
public event Action<Descriptor>? PreCopy; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. just curious about the rationale behind providing this synchronous version of handlers There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's more convenient to use and users don't have to return |
||
|
||
/// <summary> | ||
/// PostCopyAsync handles the current descriptor after it is copied. | ||
/// </summary> | ||
public event Func<Descriptor, Task>? PostCopyAsync; | ||
|
||
/// <summary> | ||
/// PostCopy handles the current descriptor after it is copied. | ||
/// </summary> | ||
public event Action<Descriptor>? PostCopy; | ||
|
||
/// <summary> | ||
/// CopySkippedAsync will be called when the sub-DAG rooted by the current node | ||
/// is skipped. | ||
/// </summary> | ||
public event Func<Descriptor, Task>? CopySkippedAsync; | ||
|
||
/// <summary> | ||
/// CopySkipped will be called when the sub-DAG rooted by the current node | ||
/// is skipped. | ||
/// </summary> | ||
public event Action<Descriptor>? CopySkipped; | ||
Comment on lines
+28
to
+55
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Just realized that the event handlers cannot be overwritten, which might not meet our need. |
||
|
||
internal Task OnPreCopyAsync(Descriptor descriptor) | ||
{ | ||
PreCopy?.Invoke(descriptor); | ||
return PreCopyAsync?.InvokeAsync(descriptor) ?? Task.CompletedTask; | ||
} | ||
Wwwsylvia marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
||
internal Task OnPostCopyAsync(Descriptor descriptor) | ||
{ | ||
PostCopy?.Invoke(descriptor); | ||
return PostCopyAsync?.InvokeAsync(descriptor) ?? Task.CompletedTask; | ||
} | ||
|
||
internal Task OnCopySkippedAsync(Descriptor descriptor) | ||
{ | ||
CopySkipped?.Invoke(descriptor); | ||
return CopySkippedAsync?.Invoke(descriptor) ?? Task.CompletedTask; | ||
Wwwsylvia marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
// 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. | ||
|
||
namespace OrasProject.Oras; | ||
|
||
/// <summary> | ||
/// CopyOptions contains parameters for <see cref="Extensions.CopyAsync(OrasProject.Oras.ITarget,string,OrasProject.Oras.ITarget,string,System.Threading.CancellationToken)"/> | ||
/// </summary> | ||
public struct CopyOptions | ||
{ | ||
public CopyGraphOptions CopyGraphOptions; | ||
} |
Uh oh!
There was an error while loading. Please reload this page.