Skip to content

Commit d2a2f2e

Browse files
committed
feat(task): add extension methods for invoking tasks in scope context
1 parent ace2c73 commit d2a2f2e

2 files changed

Lines changed: 64 additions & 1 deletion

File tree

‎FlowNet.Test/Core/FlowTaskTest.cs‎

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
using System;
12
using System.Threading.Tasks;
23
using FlowNet.Core;
34

@@ -9,6 +10,20 @@ public class FlowTaskTest
910
[TestMethod]
1011
public async Task TaskInvoke()
1112
{
12-
await FlowInterops.Initialize("app:test");
13+
await FlowInterops.Initialize("app:test:test2");
14+
}
15+
}
16+
17+
[Flow.Scope("app:test")]
18+
public static partial class AppTest
19+
{
20+
[Flow.Task]
21+
public static int Test() => 114514;
22+
23+
[Flow.Task]
24+
public static async Task Test2()
25+
{
26+
var r = await Context.InvokeTask<int>("test");
27+
Console.WriteLine(r);
1328
}
1429
}

‎FlowNet/Core/FlowTask.cs‎

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,54 @@ public static Task<TReturn> InvokeTask<TReturn, TArgument>(string globalIdentifi
7676
: throw new KeyNotFoundException($"There is no task with identifier '{globalIdentifier}'.");
7777
}
7878
}
79+
80+
/// <summary>
81+
/// 上下文中与 Flow 任务相关的扩展方法
82+
/// </summary>
83+
/// <param name="context">作用域上下文</param>
84+
extension(ScopeContext context)
85+
{
86+
/// <summary>
87+
/// 调用既无参数也无返回值的 Flow 任务,详情参考 <see cref="InvokeTask{TReturn, TArgument}(string, TArgument)"/>。
88+
/// </summary>
89+
public Task InvokeTask(string identifier)
90+
=> context.InvokeTask<None, None>(identifier, default);
91+
92+
/// <summary>
93+
/// 调用无参数的 Flow 任务,详情参考 <see cref="InvokeTask{TReturn, TArgument}(string, TArgument)"/>。
94+
/// </summary>
95+
public Task<TReturn> InvokeTask<TReturn>(string identifier)
96+
=> context.InvokeTask<TReturn, None>(identifier, default);
97+
98+
/// <summary>
99+
/// 调用无返回值的 Flow 任务,详情参考 <see cref="InvokeTask{TReturn, TArgument}(string, TArgument)"/>。
100+
/// </summary>
101+
public Task InvokeTask<TArgument>(string identifier, TArgument argument)
102+
=> context.InvokeTask<None, TArgument>(identifier, argument);
103+
104+
/// <summary>
105+
/// 调用 Flow 任务,传入参数并获取返回值。<br/>
106+
/// 当 <see cref="Flow.EnableTaskInvokingInfo"/> 为 <see langword="true"/>
107+
/// 时,使用该方法及其变体调用任务会产生包含调用方和目标标识符的调用信息。
108+
/// <b>NOTE</b>: 没有返回值或没有参数时用默认的 <see cref="None"/> 类型替代。
109+
/// </summary>
110+
/// <param name="identifier">任务标识符,可传入相对于当前作用域的短标识符,或包含作用域前缀的完整标识符</param>
111+
/// <param name="argument">任务参数,若大于一个,则该值应传入 <see cref="ValueTuple"/></param>
112+
/// <typeparam name="TReturn">返回值类型</typeparam>
113+
/// <typeparam name="TArgument">参数类型</typeparam>
114+
/// <returns>任务返回值</returns>
115+
/// <exception cref="KeyNotFoundException">不存在指定全局标识的任务</exception>
116+
public Task<TReturn> InvokeTask<TReturn, TArgument>(string identifier, TArgument argument)
117+
{
118+
var globalIdentifier = $"{context.Identifier}:{identifier}";
119+
if (!ExistsTask(globalIdentifier)) globalIdentifier = identifier;
120+
var invokingInfo = EnableTaskInvokingInfo ? new FlowTaskInvokingInfo {
121+
DirectCaller = context.Identifier,
122+
Target = globalIdentifier,
123+
} : default;
124+
return Internal.InvokeTask<TReturn, TArgument>(globalIdentifier, argument, invokingInfo);
125+
}
126+
}
79127
}
80128

81129
public interface IFlowTask

0 commit comments

Comments
 (0)