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
+41 -53
View File
@@ -157,7 +157,7 @@ type
end;
// Endpoint that collects data into a series.
TMycDataEndpoint<T> = class(TInterfacedObject, TMutable<TSeries<T>>.IMutable)
TMycDataEndpoint<T> = class(TInterfacedObject, TLazy<TSeries<T>>.ILazy)
type
PItem = ^TItem;
TItem = record
@@ -169,16 +169,16 @@ type
FTag: TDataProvider<T>.TTag;
FDataProvider: TDataProvider<T>;
FLookback: Int64;
FData: TSeries<T>;
FChanged: TEvent;
FChanged: TFlag;
FLock: TLightweightMREW;
FFirst: PItem;
function GetChanged: TSignal;
function GetValue: TSeries<T>;
FCount: Integer;
function GetChanged: TState;
function ProcessData(const Value: T): TState;
public
constructor Create(const ADataProvider: TDataProvider<T>; ALookback: Int64);
destructor Destroy; override;
function Update(var Value: TSeries<T>): Boolean;
end;
TTickAggregation = class(TMycConverter<TDataPoint<Double>, TDataPoint<TOhlcItem>>)
@@ -475,55 +475,13 @@ begin
inherited;
end;
function TMycDataEndpoint<T>.GetChanged: TSignal;
function TMycDataEndpoint<T>.GetChanged: TState;
begin
Result := FChanged.Signal;
end;
function TMycDataEndpoint<T>.GetValue: TSeries<T>;
begin
FLock.BeginWrite;
try
var cnt := 0;
var tmp: PItem := nil;
var item: PItem;
while FFirst <> nil do
begin
item := FFirst;
FFirst := item.Next;
item.Next := tmp;
tmp := item;
inc(cnt);
end;
if cnt > 0 then
begin
var Arr: TArray<T>;
SetLength(Arr, cnt);
cnt := 0;
while tmp <> nil do
begin
item := tmp;
tmp := item.Next;
Arr[cnt] := item.Value;
inc(cnt);
Dispose(item);
end;
FData := FData.Add(Arr, 0, cnt, FLookback);
end;
Result := FData;
finally
FLock.EndWrite;
end;
Result := FChanged.State
end;
function TMycDataEndpoint<T>.ProcessData(const Value: T): TState;
begin
Result := TState.Null;
FLock.BeginWrite;
try
var P: PItem;
@@ -531,13 +489,43 @@ begin
P.Next := FFirst;
FFirst := P;
P.Value := Value;
// Add new data point, respecting the lookback period.
// FData := FData.Add(Value, FLookback);
inc(FCount);
FChanged.Notify;
finally
FLock.EndWrite;
end;
end;
function TMycDataEndpoint<T>.Update(var Value: TSeries<T>): Boolean;
begin
FLock.BeginWrite;
try
Result := FChanged.Reset;
if Result then
begin
if FCount > 0 then
begin
var item: PItem;
var Arr: TArray<T>;
SetLength(Arr, FCount);
while FFirst <> nil do
begin
item := FFirst;
FFirst := item.Next;
dec(FCount);
Assert(FCount >= 0);
Arr[FCount] := item.Value;
Dispose(item);
end;
Assert(FCount = 0);
Value := Value.Add(Arr, 0, Length(Arr), FLookback);
end;
end;
finally
FLock.EndWrite;
end;
FChanged.Notify;
end;
{ TTickAggregation }