Files
MycLib/Src/Myc.TaskManager.pas
T
2025-06-24 18:32:32 +02:00

91 lines
2.1 KiB
ObjectPascal

unit Myc.TaskManager;
interface
uses
System.SysUtils,
Myc.Signals;
type
IMycTaskManager = interface
// TOD Dokumentation
function CreateTask(const Gate: TState; const Proc: TProc): TSignal.TSubscription;
// Waits for the operation associated with State to complete.
// Must not be called from a worker thread of this factory.
// After waiting, or if the state is already set, any first stored exception
// from any worker thread of this factory will be re-raised in the calling (main) thread.
// Raises ETaskException if called from within a worker thread.
procedure WaitFor(const State: TState.IState);
end;
var
TaskManager: IMycTaskManager;
procedure SetupTaskManagerMock;
implementation
uses
System.Generics.Collections;
type
TMycExecMock = class(TInterfacedObject, TSignal.ISubscriber)
private
FProc: TProc;
public
constructor Create(const AProc: TProc);
function Notify: Boolean;
end;
TMycTaskManagerMock = class(TInterfacedObject, IMycTaskManager)
public
// IMycTaskManager
function CreateTask(const Gate: TState; const Proc: TProc): TSignal.TSubscription;
procedure WaitFor(const State: TState.IState);
constructor Create;
end;
{ TMycExecMock }
constructor TMycExecMock.Create(const AProc: TProc);
begin
inherited Create;
FProc := AProc;
end;
function TMycExecMock.Notify: Boolean;
begin
Result := Assigned(FProc);
if Result then
begin
FProc();
FProc := nil;
end;
end;
{ TMycTaskManagerMock }
constructor TMycTaskManagerMock.Create;
begin
inherited Create;
end;
function TMycTaskManagerMock.CreateTask(const Gate: TState; const Proc: TProc): TSignal.TSubscription;
begin
Result := Gate.Signal.Subscribe(TMycExecMock.Create(Proc));
end;
procedure TMycTaskManagerMock.WaitFor(const State: TState.IState);
begin
Assert(State.IsSet);
end;
procedure SetupTaskManagerMock;
begin
TaskManager := TMycTaskManagerMock.Create;
end;
end.