Browse Source

use function references for callbacks + change TNotifyEvent to TFPHTTPClientPoolSimpleCallback

Ondrej Pokorny 2 years ago
parent
commit
7ee1dea027
1 changed files with 29 additions and 10 deletions
  1. 29 10
      packages/fcl-web/src/base/fphttpclientasyncpool.pas

+ 29 - 10
packages/fcl-web/src/base/fphttpclientasyncpool.pas

@@ -9,8 +9,15 @@ unit FPHTTPClientAsyncPool;
     check (TODO: URL)
     check (TODO: URL)
 }
 }
 
 
+{$IF (FPC_FULLVERSION >= 30301)}
+  {$define use_functionreferences}
+{$ENDIF}
+
 {$mode ObjFPC}{$H+}
 {$mode ObjFPC}{$H+}
 {$modeswitch advancedrecords}
 {$modeswitch advancedrecords}
+{$IFDEF use_functionreferences}
+  {$modeswitch functionreferences}
+{$ENDIF}
 
 
 interface
 interface
 
 
@@ -68,13 +75,25 @@ type
 
 
   TFPHTTPClientAsyncPoolRequestThread = class;
   TFPHTTPClientAsyncPoolRequestThread = class;
 
 
+  TFPHTTPClientPoolProgressDirection = (pdDataSent, pdDataReceived);
+
+{$IFDEF use_functionreferences}
+  TFPHTTPClientPoolInit = reference to procedure(const aRequest: TFPHTTPClientAsyncPoolRequest; const aClient: TFPHTTPClient);
+  TFPHTTPClientPoolFinish = reference to procedure(const aResult: TFPHTTPClientPoolResult);
+  TFPHTTPClientPoolProgress = reference to procedure(
+    Sender: TFPHTTPClientAsyncPoolRequestThread;
+    const aDirection: TFPHTTPClientPoolProgressDirection;
+    const aPosition, aContentLength: Int64; var ioStop: Boolean);
+  TFPHTTPClientPoolSimpleCallback = reference to procedure;
+{$ELSE}
   TFPHTTPClientPoolInit = procedure(const aRequest: TFPHTTPClientAsyncPoolRequest; const aClient: TFPHTTPClient) of object;
   TFPHTTPClientPoolInit = procedure(const aRequest: TFPHTTPClientAsyncPoolRequest; const aClient: TFPHTTPClient) of object;
   TFPHTTPClientPoolFinish = procedure(const aResult: TFPHTTPClientPoolResult) of object;
   TFPHTTPClientPoolFinish = procedure(const aResult: TFPHTTPClientPoolResult) of object;
-  TFPHTTPClientPoolProgressDirection = (pdDataSent, pdDataReceived);
   TFPHTTPClientPoolProgress = procedure(
   TFPHTTPClientPoolProgress = procedure(
     Sender: TFPHTTPClientAsyncPoolRequestThread;
     Sender: TFPHTTPClientAsyncPoolRequestThread;
     const aDirection: TFPHTTPClientPoolProgressDirection;
     const aDirection: TFPHTTPClientPoolProgressDirection;
     const aPosition, aContentLength: Int64; var ioStop: Boolean) of object;
     const aPosition, aContentLength: Int64; var ioStop: Boolean) of object;
+  TFPHTTPClientPoolSimpleCallback = procedure of object;
+{$ENDIF}
 
 
   TFPCustomHTTPClientAsyncPool = class;
   TFPCustomHTTPClientAsyncPool = class;
   TFPHTTPClientAsyncPoolRequest = class(TPersistent)
   TFPHTTPClientAsyncPoolRequest = class(TPersistent)
@@ -150,7 +169,7 @@ type
   private
   private
     fTimeoutMS: Integer;
     fTimeoutMS: Integer;
     fOwner: TComponent;
     fOwner: TComponent;
-    fOnAllDone: TNotifyEvent;
+    fOnAllDone: TFPHTTPClientPoolSimpleCallback;
     fSynchronizeOnAllDone: Boolean;
     fSynchronizeOnAllDone: Boolean;
 
 
     procedure ExecOnAllDone;
     procedure ExecOnAllDone;
@@ -166,7 +185,7 @@ type
     // access only through LockProperties
     // access only through LockProperties
     function GetOwner: TComponent; override;
     function GetOwner: TComponent; override;
   public
   public
-    constructor Create(aPool: TFPCustomHTTPClientAsyncPool; aOnAllDone: TNotifyEvent;
+    constructor Create(aPool: TFPCustomHTTPClientAsyncPool; aOnAllDone: TFPHTTPClientPoolSimpleCallback;
       const aSynchronizeOnAllDone: Boolean;
       const aSynchronizeOnAllDone: Boolean;
       const aOwner: TComponent; const aTimeoutMS: Integer);
       const aOwner: TComponent; const aTimeoutMS: Integer);
   end;
   end;
@@ -254,7 +273,7 @@ type
 
 
     function CreatePool: TFPCustomHTTPClientPool; virtual;
     function CreatePool: TFPCustomHTTPClientPool; virtual;
     function CreateRequestThread(aRequest: TFPHTTPClientAsyncPoolRequest; aClient: TFPHTTPClient): TFPHTTPClientAsyncPoolRequestThread; virtual;
     function CreateRequestThread(aRequest: TFPHTTPClientAsyncPoolRequest; aClient: TFPHTTPClient): TFPHTTPClientAsyncPoolRequestThread; virtual;
-    function CreateWaitForAllRequestsThread(const aOnAllDone: TNotifyEvent; const aSynchronizeOnAllDone: Boolean;
+    function CreateWaitForAllRequestsThread(const aOnAllDone: TFPHTTPClientPoolSimpleCallback; const aSynchronizeOnAllDone: Boolean;
       const aOwner: TComponent; const aTimeoutMS: Integer): TFPHTTPClientAsyncPoolWaitForAllThread; virtual;
       const aOwner: TComponent; const aTimeoutMS: Integer): TFPHTTPClientAsyncPoolWaitForAllThread; virtual;
     procedure WaitForThreadsToFinish; virtual;
     procedure WaitForThreadsToFinish; virtual;
 
 
@@ -279,7 +298,7 @@ type
 
 
     // wait until all requests are finished
     // wait until all requests are finished
     //  all new requests will be blocked in between
     //  all new requests will be blocked in between
-    procedure WaitForAllRequests(const aOnAllDone: TNotifyEvent; const aSynchronizeOnAllDone: Boolean;
+    procedure WaitForAllRequests(const aOnAllDone: TFPHTTPClientPoolSimpleCallback; const aSynchronizeOnAllDone: Boolean;
       const aOwner: TComponent; const aTimeoutMS: Integer);
       const aOwner: TComponent; const aTimeoutMS: Integer);
   public
   public
     constructor Create(AOwner: TComponent); override;
     constructor Create(AOwner: TComponent); override;
@@ -309,7 +328,7 @@ end;
 { TFPHTTPClientAsyncPoolWaitForAllThread }
 { TFPHTTPClientAsyncPoolWaitForAllThread }
 
 
 constructor TFPHTTPClientAsyncPoolWaitForAllThread.Create(aPool: TFPCustomHTTPClientAsyncPool;
 constructor TFPHTTPClientAsyncPoolWaitForAllThread.Create(aPool: TFPCustomHTTPClientAsyncPool;
-  aOnAllDone: TNotifyEvent; const aSynchronizeOnAllDone: Boolean; const aOwner: TComponent; const aTimeoutMS: Integer);
+  aOnAllDone: TFPHTTPClientPoolSimpleCallback; const aSynchronizeOnAllDone: Boolean; const aOwner: TComponent; const aTimeoutMS: Integer);
 begin
 begin
   fOnAllDone := aOnAllDone;
   fOnAllDone := aOnAllDone;
   fSynchronizeOnAllDone := aSynchronizeOnAllDone;
   fSynchronizeOnAllDone := aSynchronizeOnAllDone;
@@ -322,7 +341,7 @@ end;
 procedure TFPHTTPClientAsyncPoolWaitForAllThread.DoOnAllDone;
 procedure TFPHTTPClientAsyncPoolWaitForAllThread.DoOnAllDone;
 begin
 begin
   if Assigned(fOnAllDone) then
   if Assigned(fOnAllDone) then
-    fOnAllDone(Self);
+    fOnAllDone();
 end;
 end;
 
 
 procedure TFPHTTPClientAsyncPoolWaitForAllThread.ExecOnAllDone;
 procedure TFPHTTPClientAsyncPoolWaitForAllThread.ExecOnAllDone;
@@ -613,7 +632,7 @@ begin
   Result := TFPHTTPClientAsyncPoolRequestThread.Create(Self, aRequest, aClient);
   Result := TFPHTTPClientAsyncPoolRequestThread.Create(Self, aRequest, aClient);
 end;
 end;
 
 
-function TFPCustomHTTPClientAsyncPool.CreateWaitForAllRequestsThread(const aOnAllDone: TNotifyEvent;
+function TFPCustomHTTPClientAsyncPool.CreateWaitForAllRequestsThread(const aOnAllDone: TFPHTTPClientPoolSimpleCallback;
   const aSynchronizeOnAllDone: Boolean; const aOwner: TComponent;
   const aSynchronizeOnAllDone: Boolean; const aOwner: TComponent;
   const aTimeoutMS: Integer): TFPHTTPClientAsyncPoolWaitForAllThread;
   const aTimeoutMS: Integer): TFPHTTPClientAsyncPoolWaitForAllThread;
 begin
 begin
@@ -911,13 +930,13 @@ begin
   fWorkingThreads.UnlockList;
   fWorkingThreads.UnlockList;
 end;
 end;
 
 
-procedure TFPCustomHTTPClientAsyncPool.WaitForAllRequests(const aOnAllDone: TNotifyEvent;
+procedure TFPCustomHTTPClientAsyncPool.WaitForAllRequests(const aOnAllDone: TFPHTTPClientPoolSimpleCallback;
   const aSynchronizeOnAllDone: Boolean; const aOwner: TComponent; const aTimeoutMS: Integer);
   const aSynchronizeOnAllDone: Boolean; const aOwner: TComponent; const aTimeoutMS: Integer);
 begin
 begin
   if ActiveAsyncMethodCount=0 then
   if ActiveAsyncMethodCount=0 then
   begin
   begin
     if Assigned(aOnAllDone) then
     if Assigned(aOnAllDone) then
-      aOnAllDone(Self);
+      aOnAllDone();
     Exit;
     Exit;
   end;
   end;