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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
| ///////////////////////////////////////////////////////////////////////////
// //
// Copyright (c) 2015 by Charta Software B.V. //
// All rights reserved //
// //
// Version: 1.7.0.83525 //
// Web site: https://pascal.chartasoftware.com/ //
// //
// This code and information are provided "as is" without warranty of //
// any kind. Dissemination of this information or reproduction of //
// this material is strictly forbidden unless prior written permission //
// is obtained from Charta Software B.V.. //
// //
///////////////////////////////////////////////////////////////////////////
unit Listeners;
interface
uses
Boolean,
Collection.List,
Event,
Listeners.Iterator,
Listeners.Registration,
Object_.Base,
Object_.ReferenceCounted,
Task.Scheduler;
type
TListeners = class(TReferenceCountedObject)
protected
Registrations: TList<TListenersRegistration>;
Iterators: TList<TListenersIterator>;
public
constructor Create(); override;
destructor Destroy(); override;
procedure Add(Listener: TBaseObject);
procedure Remove(Listener: TBaseObject);
procedure DistributeEvent(Event: TEvent; FreeEvent: TBoolean = True);
procedure DistributeEventInUserInterface(Event: TEvent; WaitType: TWaitType);
end;
implementation
uses
Integer._32,
Listeners.Task,
Object_.List;
{ TListeners }
procedure TListeners.Add(Listener: TBaseObject);
var
Registration: TListenersRegistration;
Index: TInteger32;
begin
Index := Registrations.IndexOf(TListenersRegistration.TWithListener.Create(Listener));
if Index <> -1 then
begin
Registration := Registrations[Index];
Inc(Registration.Count);
end
else
begin
Registration := TListenersRegistration.Create();
Registration.Listener := Listener;
Registration.Count := 1;
Registrations.Add(Registration);
end;
end;
constructor TListeners.Create();
begin
inherited Create();
Registrations := TObjectList<TListenersRegistration>.Create();
Iterators := TObjectList<TListenersIterator>.Create();
end;
destructor TListeners.Destroy();
begin
Registrations.Free();
Iterators.Free();
inherited Destroy();
end;
procedure TListeners.DistributeEvent(Event: TEvent; FreeEvent: TBoolean);
var
Iterator: TListenersIterator;
begin
// TODO: We add and release a reference here to ensure the object is not destroyed while distributing the event.
// However, we might as well need to abort the distribution entirely when the object is destroyed while
// handling events.
AddReference();
try
Iterator := TListenersIterator.Create(Registrations);
Iterators.Add(Iterator);
while Iterator.IsValid() do
begin
Iterator.Current().HandleEvent(Event);
Iterator.GotoNext();
end;
Iterators.Remove(Iterator);
if FreeEvent then
Event.Free();
finally
ReleaseReference();
end;
end;
procedure TListeners.DistributeEventInUserInterface(Event: TEvent; WaitType: TWaitType);
var
Task: TDistributeEventTask;
begin
if TTaskScheduler.Instance.HasUserInterface() then
begin
Task := TDistributeEventTask.Create();
Task.Listeners := Self;
Task.Event := Event;
TTaskScheduler.Instance.ExecuteInUserInterface(Task, WaitType);
end
else
Event.Free();
end;
procedure TListeners.Remove(Listener: TBaseObject);
var
Index: TInteger32;
Registration: TListenersRegistration;
i: TInteger32;
begin
Index := Registrations.IndexOf(TListenersRegistration.TWithListener.Create(Listener));
Registration := Registrations[Index];
Dec(Registration.Count);
if Registration.Count = 0 then
begin
Registrations.RemoveAt(Index);
for i := 0 to Iterators.Count - 1 do
Iterators[i].RemoveAt(Index);
end;
end;
end.
|