-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathEventDispatcher.pas
74 lines (59 loc) · 1.94 KB
/
EventDispatcher.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
unit EventDispatcher;
interface
uses
System.Classes, System.SysUtils, Data.db;
(*
Note:
HINS OFF in order for hiding
H2269 Overriding virtual method XX has lower visibility (strict protected) than base class YY (public)
*)
type
{$HINTS OFF}
TEventDispatcher<T> = class abstract(TComponent)
strict protected
FClosure: TProc<T>;
constructor Create(aOwner: TComponent); override;
procedure Notify(Sender: T);
end;
{$HINTS ON}
TNotifyEventDispatcher = class sealed(TEventDispatcher<TObject>)
public
class function Construct(Owner: TComponent; Closure: TProc<TObject>): TNotifyEvent; overload;
function Attach(Closure: TProc<TObject>): TNotifyEvent;
end;
TDataSetNotifyEventDispatcher = class sealed(TEventDispatcher<TDataSet>)
public
class function Construct(Owner: TComponent; Closure: TProc<TDataSet>): TDataSetNotifyEvent; overload;
function Attach(Closure: TProc<TDataSet>): TDataSetNotifyEvent;
end;
implementation
class function TNotifyEventDispatcher.Construct(Owner: TComponent; Closure: TProc<TObject>): TNotifyEvent;
begin
Result := TNotifyEventDispatcher.Create(Owner).Attach(Closure)
end;
function TNotifyEventDispatcher.Attach(Closure: TProc<TObject>): TNotifyEvent;
begin
FClosure := Closure;
Result := Notify;
end;
{ TDataSetNotifyEventDispatcher }
function TDataSetNotifyEventDispatcher.Attach(Closure: TProc<TDataSet>): TDataSetNotifyEvent;
begin
FClosure := Closure;
Result := Notify;
end;
class function TDataSetNotifyEventDispatcher.Construct(Owner: TComponent; Closure: TProc<TDataSet>): TDataSetNotifyEvent;
begin
Result := TDataSetNotifyEventDispatcher.Create(Owner).Attach(Closure);
end;
{ TEventDispatcher }
constructor TEventDispatcher<T>.Create(aOwner: TComponent);
begin
inherited;
end;
procedure TEventDispatcher<T>.Notify(Sender: T);
begin
if Assigned(FClosure) then
FClosure(Sender)
end;
end.