-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathNotificationService.pas
73 lines (57 loc) · 1.4 KB
/
NotificationService.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
unit NotificationService;
interface
uses
Classes,
Generics.Collections;
type
TNotificationService = class
private
FSubscribers: TDictionary<TNotifyEvent, TGUID>;
public
constructor Create;
destructor Destroy; override;
procedure SendMessage(Sender: TObject; MessageID: TGUID);
procedure Subscribe(Event: TNotifyEvent; MessageID: TGUID);
procedure UnSubscribe(Event: TNotifyEvent);
end;
function GetNotificationService: TNotificationService;
implementation
var
Instance: TNotificationService;
function GetNotificationService: TNotificationService;
begin
if Instance = nil then
Instance := TNotificationService.Create;
Result := Instance;
end;
constructor TNotificationService.Create;
begin
FSubscribers := TDictionary<TNotifyEvent, TGUID>.Create;
end;
destructor TNotificationService.Destroy;
begin
FSubscribers.Free;
inherited;
end;
procedure TNotificationService.SendMessage(Sender: TObject; MessageID: TGUID);
var
Pair: TPair<TNotifyEvent, TGUID>;
begin
for Pair in FSubscribers do
begin
if (Pair.Value = MessageID) then
Pair.Key(Sender);
end;
end;
procedure TNotificationService.Subscribe(Event: TNotifyEvent; MessageID: TGUID);
begin
FSubscribers.AddOrSetValue(Event, MessageID);
end;
procedure TNotificationService.UnSubscribe(Event: TNotifyEvent);
begin
FSubscribers.Remove(Event);
end;
initialization
finalization
Instance.Free;
end.