继续
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
|
||||
namespace SLSFramework.General
|
||||
{
|
||||
@@ -17,15 +18,16 @@ namespace SLSFramework.General
|
||||
context = new Dictionary<string, object>();
|
||||
}
|
||||
|
||||
public CommandContext(string key, object value)
|
||||
public CommandContext((string, object)[] pairs)
|
||||
{
|
||||
context = new Dictionary<string, object>
|
||||
context = new Dictionary<string, object>();
|
||||
foreach ((string, object) pair in pairs)
|
||||
{
|
||||
{ key, value }
|
||||
};
|
||||
context[pair.Item1] = pair.Item2;
|
||||
}
|
||||
}
|
||||
|
||||
public CommandContext(List<KeyValuePair<string, object>> initialInfo)
|
||||
|
||||
public CommandContext(Dictionary<string, object> initialInfo)
|
||||
{
|
||||
context = new Dictionary<string, object>();
|
||||
foreach (var pair in initialInfo)
|
||||
@@ -33,7 +35,7 @@ namespace SLSFramework.General
|
||||
context[pair.Key] = pair.Value;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public CommandContext Clone()
|
||||
{
|
||||
var newContext = new CommandContext();
|
||||
@@ -43,6 +45,15 @@ namespace SLSFramework.General
|
||||
}
|
||||
return newContext;
|
||||
}
|
||||
|
||||
public CommandContext Merge(CommandContext other)
|
||||
{
|
||||
foreach (var pair in other.context)
|
||||
{
|
||||
this.context[pair.Key] = pair.Value;
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
||||
public T GetInfo<T>(string key)
|
||||
{
|
||||
@@ -50,6 +61,8 @@ namespace SLSFramework.General
|
||||
{
|
||||
return typedValue;
|
||||
}
|
||||
|
||||
Debug.LogWarning($"CommandContext 中不存在键 '{key}',或其类型不匹配。返回默认值。");
|
||||
return default;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -12,10 +12,23 @@ namespace SLSFramework.General
|
||||
{
|
||||
public readonly List<CommandBase> commands = new List<CommandBase>();
|
||||
public readonly ExecutionMode mode;
|
||||
|
||||
/// <summary>
|
||||
/// 包含了指令组执行过程中产生的所有上下文信息。不论是内部还是外部的上下文皆可获取。
|
||||
/// </summary>
|
||||
public readonly CommandContext groupContext;
|
||||
|
||||
public CommandGroup(ExecutionMode mode, params CommandBase[] commands)
|
||||
{
|
||||
this.mode = mode;
|
||||
this.groupContext = new CommandContext();
|
||||
this.commands.AddRange(commands);
|
||||
}
|
||||
|
||||
public CommandGroup(ExecutionMode mode, CommandContext groupContext, params CommandBase[] commands)
|
||||
{
|
||||
this.mode = mode;
|
||||
this.groupContext = groupContext;
|
||||
this.commands.AddRange(commands);
|
||||
}
|
||||
|
||||
@@ -42,7 +55,13 @@ namespace SLSFramework.General
|
||||
// 而是创建一个“延迟执行”的 Observable 序列。
|
||||
// Defer 会将对 Execute 的调用推迟到 Concat/WhenAll 真正订阅它的时候。
|
||||
var lazyCommandObservables = commands.Select(cmd =>
|
||||
Observable.Defer(() => cmd.Execute(outerContext))
|
||||
Observable.Defer(() =>
|
||||
{
|
||||
IObservable<Unit> result = cmd.Execute(outerContext);
|
||||
groupContext.Merge(outerContext);
|
||||
groupContext.Merge(cmd.selfContext);
|
||||
return result;
|
||||
})
|
||||
);
|
||||
|
||||
IObservable<Unit> executionFlow;
|
||||
@@ -58,6 +77,7 @@ namespace SLSFramework.General
|
||||
executionFlow = Observable.WhenAll(lazyCommandObservables).AsUnitObservable();
|
||||
}
|
||||
|
||||
|
||||
return executionFlow;
|
||||
// 注意:基类中的 TakeUntil(forceCompleteSubject) 会自动应用到这里返回的流上,所以无需重复添加。
|
||||
}
|
||||
|
||||
@@ -70,7 +70,7 @@ namespace SLSFramework.General
|
||||
*/
|
||||
}
|
||||
|
||||
public void AddCommand(CommandBase command, CommandContext context = null)
|
||||
public CommandBase AddCommand(CommandBase command, CommandContext context = null)
|
||||
{
|
||||
context ??= new CommandContext();
|
||||
// 将指令和其上下文入队
|
||||
@@ -80,6 +80,8 @@ namespace SLSFramework.General
|
||||
// 如果队列当前不忙,这个调用会立即开始处理我们刚刚添加的指令。
|
||||
// 如果队列正忙,这个调用什么也不做,因为当前指令完成后会自动处理下一个。
|
||||
ProcessNextInQueue();
|
||||
|
||||
return command;
|
||||
}
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user