ADD: add the common Event notification mechanism for FileSource (by improving the original one that is only related to Reload Event)

This commit is contained in:
rich2014 2025-03-02 07:12:47 +08:00
commit 4aeb2801a3
3 changed files with 58 additions and 45 deletions

View file

@ -58,8 +58,7 @@ type
}
FFileSource: IFileSource;
procedure FileSourceReloadEvent(const aFileSource: IFileSource;
const ReloadedPaths: TPathsArray);
procedure FileSourceEventListener(var params: TFileSourceEventParams);
protected
function GetFileList: TFileTree;
@ -129,7 +128,7 @@ end;
destructor TMultiListFileSource.Destroy;
begin
if Assigned(FFileSource) then begin
FFileSource.RemoveReloadEventListener(@FileSourceReloadEvent);
FFileSource.RemoveEventListener(@FileSourceEventListener);
end;
inherited Destroy;
FreeAndNil(FFileList);
@ -145,7 +144,7 @@ begin
aFileList := nil;
FFileSource := aFileSource;
FFileSource.AddReloadEventListener(@FileSourceReloadEvent);
FFileSource.AddEventListener(@FileSourceEventListener);
end;
function TMultiListFileSource.GetSupportedFileProperties: TFilePropertiesTypes;
@ -204,10 +203,10 @@ begin
Result:= FFileSource.CanRetrieveProperties(AFile, PropertiesToSet);
end;
procedure TMultiListFileSource.FileSourceReloadEvent(
const aFileSource: IFileSource; const ReloadedPaths: TPathsArray);
procedure TMultiListFileSource.FileSourceEventListener(var params: TFileSourceEventParams);
begin
Reload(ReloadedPaths);
if params.eventType = TFileSourceEventType.reload then
Reload(params.paths);
end;
function TMultiListFileSource.GetFileList: TFileTree;

View file

@ -80,11 +80,20 @@ type
TFileSourceFields = array of TFileSourceField;
TPathsArray = array of string;
TFileSourceOperationsClasses = array[TFileSourceOperationType] of TFileSourceOperationClass;
TFileSourceReloadEventNotify = procedure(const aFileSource: IFileSource;
const ReloadedPaths: TPathsArray) of object;
TPathsArray = array of string;
{$scopedEnums on}
TFileSourceEventType = ( reload, relocation );
TFileSourceEventParams = record
eventType: TFileSourceEventType;
fs: IFileSource;
paths: TPathsArray;
end;
TFileSourceEventListener = procedure(var params: TFileSourceEventParams) of object;
{ IFileSource }
@ -174,8 +183,8 @@ type
procedure AddChild(AFileSource: IFileSource);
procedure Reload(const PathsToReload: TPathsArray);
procedure Reload(const PathToReload: String);
procedure AddReloadEventListener(FunctionToCall: TFileSourceReloadEventNotify);
procedure RemoveReloadEventListener(FunctionToCall: TFileSourceReloadEventNotify);
procedure AddEventListener(FunctionToCall: TFileSourceEventListener);
procedure RemoveEventListener(FunctionToCall: TFileSourceEventListener);
property URI: TURI read GetURI;
property ClassName: String read GetClassName;
@ -192,7 +201,7 @@ type
TFileSource = class(TInterfacedObject, IFileSource)
private
FReloadEventListeners: TMethodList;
FEventListeners: TMethodList;
{en
File source on which this file source is dependent on
(files that it accesses are on the parent file source).
@ -382,8 +391,8 @@ type
procedure Reload(const PathsToReload: TPathsArray); virtual; overload;
procedure Reload(const PathToReload: String); overload;
procedure AddReloadEventListener(FunctionToCall: TFileSourceReloadEventNotify);
procedure RemoveReloadEventListener(FunctionToCall: TFileSourceReloadEventNotify);
procedure AddEventListener(FunctionToCall: TFileSourceEventListener);
procedure RemoveEventListener(FunctionToCall: TFileSourceEventListener);
property CurrentAddress: String read GetCurrentAddress;
property ParentFileSource: IFileSource read GetParentFileSource write SetParentFileSource;
@ -461,7 +470,7 @@ begin
raise Exception.Create('Cannot construct abstract class');
inherited Create;
FReloadEventListeners := TMethodList.Create;
FEventListeners := TMethodList.Create;
FileSourceManager.Add(Self); // Increases RefCount
@ -524,7 +533,7 @@ begin
DCDebug('Error: Cannot remove file source - manager already destroyed!');
FreeAndNil(FChildrenFileSource);
FreeAndNil(FReloadEventListeners);
FreeAndNil(FEventListeners);
inherited Destroy;
end;
@ -953,15 +962,20 @@ end;
procedure TFileSource.Reload(const PathsToReload: TPathsArray);
var
i: Integer;
FunctionToCall: TFileSourceReloadEventNotify;
params: TFileSourceEventParams;
FunctionToCall: TFileSourceEventListener;
begin
DoReload(PathsToReload);
if Assigned(FReloadEventListeners) then
for i := 0 to FReloadEventListeners.Count - 1 do
params.eventType:= TFileSourceEventType.reload;
params.fs:= Self;
params.paths:= PathsToReload;
if Assigned(FEventListeners) then
for i := 0 to FEventListeners.Count - 1 do
begin
FunctionToCall := TFileSourceReloadEventNotify(FReloadEventListeners.Items[i]);
FunctionToCall(Self, PathsToReload);
FunctionToCall := TFileSourceEventListener(FEventListeners.Items[i]);
FunctionToCall(params);
end;
end;
@ -974,14 +988,14 @@ begin
Reload(PathsToReload);
end;
procedure TFileSource.AddReloadEventListener(FunctionToCall: TFileSourceReloadEventNotify);
procedure TFileSource.AddEventListener(FunctionToCall: TFileSourceEventListener);
begin
FReloadEventListeners.Add(TMethod(FunctionToCall));
FEventListeners.Add(TMethod(FunctionToCall));
end;
procedure TFileSource.RemoveReloadEventListener(FunctionToCall: TFileSourceReloadEventNotify);
procedure TFileSource.RemoveEventListener(FunctionToCall: TFileSourceEventListener);
begin
FReloadEventListeners.Remove(TMethod(FunctionToCall));
FEventListeners.Remove(TMethod(FunctionToCall));
end;
{ TFileSourceConnection }

View file

@ -223,7 +223,7 @@ type
NewFilesPosition: TNewFilesPosition;
UpdatedFilesPosition: TUpdatedFilesPosition);
procedure LoadingFileListTimer(Sender: TObject);
procedure ReloadEvent(const aFileSource: IFileSource; const ReloadedPaths: TPathsArray);
procedure FileSourceEventListener(var params: TFileSourceEventParams);
procedure ReloadTimerEvent(Sender: TObject);
procedure WatcherEvent(const EventData: TFSWatcherEventData);
@ -626,7 +626,7 @@ begin
FHistory.AddFileSource(AFileSource);
ChangePathAndSetActiveFile(aPath);
FileSource.AddReloadEventListener(@ReloadEvent);
FileSource.AddEventListener(@FileSourceEventListener);
// Update view before making file source file list,
// so that file list isn't unnecessarily displayed twice.
@ -645,7 +645,7 @@ begin
CreateDefault(AOwner);
AFileView.CloneTo(Self);
if Assigned(FileSource) then
FileSource.AddReloadEventListener(@ReloadEvent);
FileSource.AddEventListener(@FileSourceEventListener);
UpdateView;
finally
EnableAutoSizing;
@ -1028,7 +1028,7 @@ begin
StopWorkers;
for i := 0 to FHistory.Count - 1 do
FHistory.FileSource[i].RemoveReloadEventListener(@ReloadEvent);
FHistory.FileSource[i].RemoveEventListener(@FileSourceEventListener);
ClearRecentlyUpdatedFiles;
ClearPendingFilesChanges;
@ -2722,7 +2722,7 @@ begin
if Assigned(aFileSource) then
begin
FSortingProperties := GetSortingProperties;
FileSource.AddReloadEventListener(@ReloadEvent);
FileSource.AddEventListener(@FileSourceEventListener);
end;
//TODO: probably it's not the best place for calling SetActiveFile() :
@ -3024,7 +3024,7 @@ begin
FFlatView := False;
if Assigned(FileSource) and IsNewFileSource then
FileSource.RemoveReloadEventListener(@ReloadEvent);
FileSource.RemoveEventListener(@FileSourceEventListener);
EnableWatcher(False);
@ -3035,7 +3035,7 @@ begin
if Assigned(FileSource) and IsNewFileSource then
begin
UpdatePath(True);
FileSource.AddReloadEventListener(@ReloadEvent);
FileSource.AddEventListener(@FileSourceEventListener);
end;
EnableWatcher(True);
@ -3064,7 +3064,7 @@ begin
PrevIndex := FHistory.CurrentFileSourceIndex - 1;
if PrevIndex < 0 then
begin
FileSource.RemoveReloadEventListener(@ReloadEvent);
FileSource.RemoveEventListener(@FileSourceEventListener);
EnableWatcher(False);
FHistory.Clear;
@ -3080,7 +3080,7 @@ begin
IsNewFileSource := not NewFileSource.Equals(FileSource);
if IsNewFileSource then
FileSource.RemoveReloadEventListener(@ReloadEvent);
FileSource.RemoveEventListener(@FileSourceEventListener);
EnableWatcher(False);
@ -3091,7 +3091,7 @@ begin
if Assigned(FileSource) and IsNewFileSource then
begin
UpdatePath(True);
FileSource.AddReloadEventListener(@ReloadEvent);
FileSource.AddEventListener(@FileSourceEventListener);
end;
EnableWatcher(True);
@ -3110,7 +3110,7 @@ procedure TFileView.RemoveAllFileSources;
begin
if FileSourcesCount > 0 then
begin
FileSource.RemoveReloadEventListener(@ReloadEvent);
FileSource.RemoveEventListener(@FileSourceEventListener);
EnableWatcher(False);
FHistory.Clear;
@ -3124,11 +3124,11 @@ end;
procedure TFileView.AssignFileSources(const otherFileView: TFileView);
begin
FileSource.RemoveReloadEventListener(@ReloadEvent);
FileSource.RemoveEventListener(@FileSourceEventListener);
EnableWatcher(False);
FHistory.Assign(otherFileView.FHistory);
UpdatePath(True);
FileSource.AddReloadEventListener(@ReloadEvent);
FileSource.AddEventListener(@FileSourceEventListener);
AfterChangePath;
EnableWatcher(True);
end;
@ -3432,17 +3432,17 @@ begin
Key := 0;
end;
procedure TFileView.ReloadEvent(const aFileSource: IFileSource; const ReloadedPaths: TPathsArray);
procedure TFileView.FileSourceEventListener(var params: TFileSourceEventParams);
var
NoWatcher: Boolean;
begin
if aFileSource.Equals(FileSource) then
if params.fs.Equals(FileSource) then
begin
// Reload file view but only if the file source is currently viewed
// and FileSourceWatcher is not being used.
NoWatcher:= not (WatcherActive and
FileSource.GetWatcher.canWatch(ReloadedPaths));
if (NoWatcher or FlatView) then Reload(ReloadedPaths);
FileSource.GetWatcher.canWatch(params.paths));
if (NoWatcher or FlatView) then Reload(params.paths);
end;
end;
@ -3539,7 +3539,7 @@ begin
FilenameFromHistory := FHistory.Filename[aFileSourceIndex, aPathIndex];
if Assigned(FileSource) and IsNewFileSource then
FileSource.RemoveReloadEventListener(@ReloadEvent);
FileSource.RemoveEventListener(@FileSourceEventListener);
EnableWatcher(False);
FHistory.SetIndexes(aFileSourceIndex, aPathIndex);
@ -3547,7 +3547,7 @@ begin
if Assigned(FileSource) and IsNewFileSource then
begin
UpdatePath(True);
FileSource.AddReloadEventListener(@ReloadEvent);
FileSource.AddEventListener(@FileSourceEventListener);
end;
AfterChangePath;