-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathCromis.MVC.Events.pas
126 lines (105 loc) · 3.19 KB
/
Cromis.MVC.Events.pas
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
unit Cromis.MVC.Events;
interface
uses
SysUtils, Contnrs, Classes,
// cromis MVC units
Cromis.MVC.Common;
type
TMVCEvent = (meRequestStarted, meRequestComplete, meCommandStarted, meCommandComplete,
meViewActivated, meViewDeactivated, meViewUpdated);
TOnMVCEvent = procedure(const Event: TMVCEvent;
const ActionID: string;
const Controler: string;
const EventName: string;
const EventData: IMVCData
) of Object;
TEventFilter = set of TMVCEvent;
const
AllEvents = [Low(TMVCEvent)..High(TMVCEvent)];
type
TObserver = class
ID: Integer;
Method: TMethod;
Filter: TEventFilter;
end;
TMVCNotifications = class
private
FIDCounter: Integer;
FObservers: TObjectList;
public
constructor Create;
destructor Destroy; override;
procedure Notify(const Event: TMVCEvent;
const ActionID: string;
const Controler: string;
const EventName: string;
const EventData: IMVCData);
function AddNotificationHandler(const HandlerProc: TOnMVCEvent;
const Owner: TObject; const Filter: TEventFilter = AllEvents): Integer;
function RemoveNotificationHandler(const NotificationID: Integer): Boolean;
end;
var
MVCNotifications: TMVCNotifications;
implementation
{ TMVCNotifications }
constructor TMVCNotifications.Create;
begin
FObservers := TObjectList.Create;
end;
destructor TMVCNotifications.Destroy;
begin
FreeAndNil(FObservers);
inherited;
end;
function TMVCNotifications.AddNotificationHandler(const HandlerProc: TOnMVCEvent;
const Owner: TObject; const Filter: TEventFilter): Integer;
var
Observer: TObserver;
begin
Inc(FIDCounter);
Observer := TObserver.Create;
Observer.Method.Code := @HandlerProc;
Observer.Method.Data := Owner;
Observer.Filter := Filter;
Observer.ID := FIDCounter;
FObservers.Add(Observer);
Result := FIDCounter;
end;
procedure TMVCNotifications.Notify(const Event: TMVCEvent;
const ActionID: string;
const Controler: string;
const EventName: string;
const EventData: IMVCData);
var
I: Integer;
MVCEvent: TOnMVCEvent;
begin
for I := 0 to FObservers.Count - 1 do
begin
if Event in TObserver(FObservers[I]).Filter then
begin
MVCEvent := TOnMVCEvent(TObserver(FObservers[I]).Method);
MVCEvent(Event, ActionID, Controler, EventName, EventData);
end;
end;
end;
function TMVCNotifications.RemoveNotificationHandler(const NotificationID: Integer): Boolean;
var
I: Integer;
begin
Result := False;
for I := 0 to FObservers.Count - 1 do
begin
if TObserver(FObservers[I]).ID = NotificationID then
begin
FObservers.Remove(FObservers[I]);
Result := True;
Exit;
end;
end;
end;
initialization
MVCNotifications := TMVCNotifications.Create;
finalization
FreeAndNil(MVCNotifications);
end.