TLazy + Data-Endpoints refactoring

This commit is contained in:
Michael Schimmel
2025-07-17 00:48:46 +02:00
parent abad66ae52
commit b3359a4d73
8 changed files with 271 additions and 250 deletions
+63 -19
View File
@@ -73,15 +73,38 @@ type
end;
TLazy<T> = record
type
ILazy = interface
{$REGION 'property access'}
function GetChanged: TState;
{$ENDREGION}
function Update(var Value: T): Boolean;
property Changed: TState read GetChanged;
end;
{$REGION 'private'}
strict private
class var
FNull: ILazy;
class constructor CreateClass;
private
FMutable: TMutable<T>;
FChanged: TFlag;
FChangeState: TSignal.TSubscription;
function GetValue: T; inline;
FLazy: ILazy;
function GetChanged: TState; inline;
{$ENDREGION}
public
constructor Create(const AMutable: TMutable<T>);
function Update: Boolean;
property Value: T read GetValue;
constructor Create(const ALazy: ILazy);
class operator Initialize(out Dest: TLazy<T>);
class operator Implicit(const A: ILazy): TLazy<T>; overload;
class operator Implicit(const A: TLazy<T>): ILazy; overload;
class property Null: ILazy read FNull;
class function Constant(const Value: T): TLazy<T>; overload; static;
function Update(var Value: T): Boolean; inline;
property Changed: TState read GetChanged;
end;
implementation
@@ -99,12 +122,12 @@ end;
class constructor TMutable<T>.CreateClass;
begin
FNull := TMycNullMutable<T>.Create(Default(T));
FNull := TMycConstMutable<T>.Create(Default(T));
end;
class function TMutable<T>.Constant(const Value: T): TMutable<T>;
begin
Result := TMycNullMutable<T>.Create(Value);
Result := TMycConstMutable<T>.Create(Value);
end;
class function TMutable<T>.Construct(const Changing: TSignal; const Proc: TFunc<T>): TMutable<T>;
@@ -195,23 +218,44 @@ end;
{ TLazy<T> }
constructor TLazy<T>.Create(const AMutable: TMutable<T>);
constructor TLazy<T>.Create(const ALazy: ILazy);
begin
FMutable := AMutable;
FChanged := TFlag.CreateFlag;
FChangeState := AMutable.Changed.Subscribe(FChanged);
FLazy := ALazy;
end;
function TLazy<T>.GetValue: T;
class constructor TLazy<T>.CreateClass;
begin
Result := FMutable.Value;
FNull := TMycConstLazy<T>.Create(Default(T));
end;
function TLazy<T>.Update: Boolean;
class function TLazy<T>.Constant(const Value: T): TLazy<T>;
begin
Result := FChanged.State.IsSet;
if Result then
FChanged.Reset;
Result := TMycConstLazy<T>.Create(Value);
end;
function TLazy<T>.GetChanged: TState;
begin
Result := FLazy.Changed;
end;
function TLazy<T>.Update(var Value: T): Boolean;
begin
Result := FLazy.Update(Value);
end;
class operator TLazy<T>.Implicit(const A: TLazy<T>): ILazy;
begin
Result := A.FLazy;
end;
class operator TLazy<T>.Implicit(const A: ILazy): TLazy<T>;
begin
Result.FLazy := A;
end;
class operator TLazy<T>.Initialize(out Dest: TLazy<T>);
begin
Dest.FLazy := FNull;
end;
end.