unit Myc.Futures; interface uses System.SysUtils, Myc.Signals, Myc.TaskManager; type // Represents the eventual result of an asynchronous operation. IMycFuture = interface {$REGION 'property access'} function GetResult: T; function GetDone: IMycState; {$ENDREGION} // Provides access to the computed result. property Result: T read GetResult; // Provides access to the completion state. property Done: IMycState read GetDone; end; Future = record strict private FFuture: IMycFuture; class constructor CreateClass; class destructor DestroyClass; constructor Create(const AFuture: IMycFuture); public class function Construct(const Proc: TFunc): Future; overload; static; class function Construct(const Gate: IMycState; const Proc: TFunc): Future; overload; static; function Chain(const Proc: TFunc): Future; end; implementation uses Myc.Core.Futures, Myc.Core.Tasks; constructor Future.Create(const AFuture: IMycFuture); begin FFuture := AFuture; end; class constructor Future.CreateClass; begin TMycTaskFactory.AquireTaskManager; end; class destructor Future.DestroyClass; begin TMycTaskFactory.ReleaseTaskManager; end; function Future.Chain(const Proc: TFunc): Future; begin var Cap := FFuture; Result := Future.Construct( FFuture.Done, function: S begin Result := Proc( Cap.Result ); end ); end; class function Future.Construct(const Proc: TFunc): Future; begin Result.Create( TMycInitStateFuncFuture.Create( TaskManager, nil, Proc ) ); end; class function Future.Construct(const Gate: IMycState; const Proc: TFunc): Future; begin Result.Create( TMycInitStateFuncFuture.Create( TaskManager, Gate, Proc ) ); end; end.