Timeless refactor
This commit is contained in:
@@ -0,0 +1,21 @@
|
||||
using FileTime.Core.Command;
|
||||
|
||||
namespace FileTime.Core.Timeline;
|
||||
|
||||
public class CommandTimeState
|
||||
{
|
||||
public ICommand Command { get; }
|
||||
public CanCommandRun CanRun { get; private set; } = CanCommandRun.False;
|
||||
public bool ForceRun { get; set; }
|
||||
|
||||
public CommandTimeState(ICommand command, PointInTime? startTime)
|
||||
{
|
||||
Command = command;
|
||||
Task.Run(async () => await UpdateState(startTime)).Wait();
|
||||
}
|
||||
|
||||
public async Task UpdateState(PointInTime? startPoint)
|
||||
{
|
||||
CanRun = startPoint == null ? CanCommandRun.False : await Command.CanRun(startPoint);
|
||||
}
|
||||
}
|
||||
16
src/Core/FileTime.Core.Abstraction/Timeline/Difference.cs
Normal file
16
src/Core/FileTime.Core.Abstraction/Timeline/Difference.cs
Normal file
@@ -0,0 +1,16 @@
|
||||
using FileTime.Core.Models;
|
||||
using FileTime.Core.Services;
|
||||
|
||||
namespace FileTime.Core.Timeline;
|
||||
|
||||
public class Difference
|
||||
{
|
||||
public AbsolutePath AbsolutePath { get; }
|
||||
public DifferenceActionType Action { get; }
|
||||
|
||||
public Difference(DifferenceActionType action, AbsolutePath absolutePath)
|
||||
{
|
||||
AbsolutePath = absolutePath;
|
||||
Action = action;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
namespace FileTime.Core.Timeline;
|
||||
|
||||
public enum DifferenceActionType
|
||||
{
|
||||
Create,
|
||||
Delete
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
namespace FileTime.Core.Timeline;
|
||||
|
||||
public interface ICommandScheduler
|
||||
{
|
||||
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
using System.Reactive.Subjects;
|
||||
using FileTime.Core.Enums;
|
||||
using FileTime.Core.Models;
|
||||
|
||||
namespace FileTime.Core.Timeline;
|
||||
|
||||
public interface ITimelessContentProvider
|
||||
{
|
||||
BehaviorSubject<PointInTime> CurrentPointInTime { get; }
|
||||
|
||||
Task<IItem> GetItemByFullNameAsync(FullName fullName,
|
||||
PointInTime? pointInTime,
|
||||
bool forceResolve = false,
|
||||
AbsolutePathType forceResolvePathType = AbsolutePathType.Unknown,
|
||||
ItemInitializationSettings itemInitializationSettings = default);
|
||||
}
|
||||
@@ -0,0 +1,90 @@
|
||||
using FileTime.Core.Command;
|
||||
|
||||
namespace FileTime.Core.Timeline;
|
||||
|
||||
public class ParallelCommands
|
||||
{
|
||||
private static ushort _idCounter;
|
||||
public List<CommandTimeState> _commands;
|
||||
public ushort Id { get; }
|
||||
public IReadOnlyList<CommandTimeState> Commands { get; }
|
||||
public PointInTime? Result { get; private set; }
|
||||
|
||||
public ParallelCommands(PointInTime? result)
|
||||
: this(new List<CommandTimeState>(), result)
|
||||
{
|
||||
}
|
||||
|
||||
private ParallelCommands(List<CommandTimeState> commands, PointInTime? result)
|
||||
{
|
||||
Id = _idCounter++;
|
||||
|
||||
_commands = commands;
|
||||
Commands = _commands.AsReadOnly();
|
||||
|
||||
Result = result;
|
||||
}
|
||||
|
||||
public static async Task<ParallelCommands> Create(PointInTime? startTime, IEnumerable<ICommand> commands)
|
||||
{
|
||||
var commandStates = new List<CommandTimeState>();
|
||||
var currentTime = startTime;
|
||||
foreach (var command in commands)
|
||||
{
|
||||
CommandTimeState commandTimeState = new(command, currentTime);
|
||||
if (currentTime != null)
|
||||
{
|
||||
var canRun = await command.CanRun(currentTime);
|
||||
if (canRun == CanCommandRun.True)
|
||||
{
|
||||
currentTime = await command.SimulateCommand(currentTime);
|
||||
}
|
||||
else
|
||||
{
|
||||
currentTime = null;
|
||||
}
|
||||
}
|
||||
|
||||
commandStates.Add(commandTimeState);
|
||||
}
|
||||
|
||||
return new ParallelCommands(commandStates, currentTime);
|
||||
}
|
||||
|
||||
public async Task AddCommand(ICommand command)
|
||||
{
|
||||
_commands.Add(new CommandTimeState(command, Result));
|
||||
if (Result != null)
|
||||
{
|
||||
Result = await command.SimulateCommand(Result);
|
||||
}
|
||||
}
|
||||
|
||||
public async Task<PointInTime?> RefreshResult(PointInTime? startPoint)
|
||||
{
|
||||
var result = startPoint;
|
||||
foreach (var command in _commands)
|
||||
{
|
||||
await command.UpdateState(result);
|
||||
if (result != null)
|
||||
{
|
||||
var canRun = await command.Command.CanRun(result);
|
||||
if (canRun == CanCommandRun.True || (canRun == CanCommandRun.Forcable && command.ForceRun))
|
||||
{
|
||||
result = await command.Command.SimulateCommand(result);
|
||||
}
|
||||
else
|
||||
{
|
||||
result = null;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Result = result;
|
||||
return Result;
|
||||
}
|
||||
|
||||
public void RemoveAt(int number) => _commands.RemoveAt(number);
|
||||
|
||||
internal void Remove(CommandTimeState command) => _commands.Remove(command);
|
||||
}
|
||||
41
src/Core/FileTime.Core.Abstraction/Timeline/PointInTime.cs
Normal file
41
src/Core/FileTime.Core.Abstraction/Timeline/PointInTime.cs
Normal file
@@ -0,0 +1,41 @@
|
||||
namespace FileTime.Core.Timeline;
|
||||
|
||||
public class PointInTime
|
||||
{
|
||||
private readonly List<Difference> _differences;
|
||||
public static readonly PointInTime Eternal = new PointInTime();
|
||||
public static readonly PointInTime Present = new PointInTime();
|
||||
|
||||
public IReadOnlyList<Difference> Differences { get; }
|
||||
|
||||
private PointInTime() : this(new List<Difference>())
|
||||
{
|
||||
}
|
||||
|
||||
private PointInTime(IEnumerable<Difference> differences)
|
||||
{
|
||||
_differences = new List<Difference>(differences);
|
||||
Differences = _differences.AsReadOnly();
|
||||
}
|
||||
|
||||
private PointInTime(PointInTime previous, IEnumerable<Difference> differences)
|
||||
: this(MergeDifferences(previous.Differences, differences))
|
||||
{
|
||||
}
|
||||
|
||||
public PointInTime WithDifferences(IEnumerable<Difference> differences) =>
|
||||
new(this, differences);
|
||||
|
||||
private static List<Difference> MergeDifferences(IEnumerable<Difference> previouses,
|
||||
IEnumerable<Difference> differences)
|
||||
{
|
||||
var merged = new List<Difference>();
|
||||
|
||||
merged.AddRange(previouses);
|
||||
merged.AddRange(differences);
|
||||
|
||||
return merged;
|
||||
}
|
||||
|
||||
public static PointInTime CreateEmpty() => new PointInTime();
|
||||
}
|
||||
Reference in New Issue
Block a user