TLazy + Data-Endpoints refactoring
This commit is contained in:
+63
-19
@@ -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.
|
||||
|
||||
Reference in New Issue
Block a user