-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathYWEvents.pas
120 lines (103 loc) · 2.72 KB
/
YWEvents.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
unit YWEvents;
interface
uses
System.SysUtils, System.Classes, System.Generics.Collections;
type
TEventReciever = class;
TEventSender = class(TComponent)
private
{ Private declarations }
protected
{ Protected declarations }
RecieverList : TList<TEventReciever>;
public
{ Public declarations }
function DataModule : TDataModule;
constructor Create(AOwner : TComponent); override;
destructor Destroy; override;
procedure AddReciever(R : TEventReciever);
procedure RemoveReciever(R : TEventReciever);
procedure BoardcastEvent(Sender : TObject=nil);
published
{ Published declarations }
end;
TEventReciever = class(TComponent)
private
FEventSender: TEventSender;
FOnCall: TNotifyEvent;
procedure SetEventSender(const Value: TEventSender);
procedure SetOnCall(const Value: TNotifyEvent);
public
destructor Destroy; override;
published
property EventSender : TEventSender read FEventSender write SetEventSender;
property OnCall : TNotifyEvent read FOnCall write SetOnCall;
end;
implementation
{ TEventSender }
procedure TEventSender.AddReciever(R: TEventReciever);
begin
if assigned(R) then
if not RecieverList.Contains(R) then
RecieverList.Add(R);
end;
procedure TEventSender.BoardcastEvent(Sender : TObject);
var i : integer;
begin
for i := 0 to RecieverList.Count-1 do
try
if assigned(RecieverList[i].OnCall) then
RecieverList[i].OnCall(Sender);
except on E: Exception do
end;
end;
constructor TEventSender.Create(AOwner: TComponent);
begin
inherited;
RecieverList := TList<TEventReciever>.Create;
end;
function TEventSender.DataModule: TDataModule;
begin
if self.Owner is TDataModule then
Result := Owner as TDataModule
else
Result := nil;
end;
destructor TEventSender.Destroy;
var i : integer;
begin
for i := 0 to RecieverList.Count-1 do
try
RecieverList[i].FEventSender := nil;
except on E: Exception do
end;
RecieverList.Free;
inherited;
end;
procedure TEventSender.RemoveReciever(R: TEventReciever);
begin
if assigned(R) then begin
R.FEventSender := nil;
RecieverList.Remove(R);
end;
end;
{ TEventReciever }
destructor TEventReciever.Destroy;
begin
if assigned(FEventSender) then
FEventSender.RemoveReciever(self);
inherited;
end;
procedure TEventReciever.SetEventSender(const Value: TEventSender);
begin
if assigned(FEventSender) then
FEventSender.RemoveReciever(self);
FEventSender := Value;
if assigned(Value) then
Value.AddReciever(self);
end;
procedure TEventReciever.SetOnCall(const Value: TNotifyEvent);
begin
FOnCall := Value;
end;
end.