8000 changed TCloneEventCallback with a function of object instead an anon… · spinettaro/delphi-event-bus@d223174 · GitHub
[go: up one dir, main page]

Skip to content

Commit d223174

Browse files
author
Daniele Spinetti
committed
changed TCloneEventCallback with a function of object instead an anonymous method
1 parent d8336ad commit d223174

File tree

3 files changed

+47
-37
lines changed

3 files changed

+47
-37
lines changed

source/EventBus.pas

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,8 @@ interface
2424

2525
type
2626

27-
TCloneEvent = TFunc<TObject,TObject>;
27+
TCloneEventCallback = function (const AObject: TObject): TObject of object;
28+
TCloneEventMethod = TFunc<TObject,TObject>;
2829

2930
IEventBus = Interface
3031
['{7BDF4536-F2BA-4FBA-B186-09E1EE6C7E35}']
@@ -42,8 +43,8 @@ TEventBus = class(TInterfacedObject, IEventBus)
4243
var
4344
FTypesOfGivenSubscriber: TObjectDictionary<TObject, TList<TClass>>;
4445
FSubscriptionsOfGivenEventType: TObjectDictionary<TClass, TObjectList<TSubscription>>;
45-
FCustomClonerDict: TDictionary<String, TCloneEvent>;
46-
FOnCloneEvent: TCloneEvent;
46+
FCustomClonerDict: TDictionary<String, TCloneEventMethod>;
47+
FOnCloneEvent: TCloneEventCallback;
4748
procedure Subscribe(ASubscriber: TObject;
4849
ASubscriberMethod: TSubscriberMethod);
4950
procedure UnsubscribeByEventType(ASubscriber: TObject; AEventType: TClass);
@@ -66,8 +67,8 @@ TEventBus = class(TInterfacedObject, IEventBus)
6667
property TypesOfGivenSubscriber: TObjectDictionary<TObject, TList<TClass>> read FTypesOfGivenSubscriber;
6768
property SubscriptionsOfGivenEventType: TObjectDictionary<TClass, TObjectList<TSubscription>> read
6869
FSubscriptionsOfGivenEventType;
69-
property OnCloneEvent: TCloneEvent write FOnCloneEvent;
70-
procedure AddCustomClassCloning(const AQualifiedClassName: String; const ACloneEvent: TCloneEvent);
70+
property OnCloneEvent: TCloneEventCallback write FOnCloneEvent;
71+
procedure AddCustomClassCloning(const AQualifiedClassName: String; const ACloneEvent: TCloneEventMethod);
7172
procedure RemoveCustomClassCloning(const AQualifiedClassName: String);
7273
end;
7374

@@ -92,7 +93,7 @@ constructor TEventBus.Create;
9293
TObjectList < TSubscription >>.Create([doOwnsValues]);
9394
FTypesOfGivenSubscriber := TObjectDictionary < TObject,
9495
TList < TClass >>.Create([doOwnsValues]);
95-
FCustomClonerDict := TDictionary<String, TCloneEvent>.Create;
96+
FCustomClonerDict := TDictionary<String, TCloneEventMethod>.Create;
9697
end;
9798

9899
destructor TEventBus.Destroy;
@@ -104,14 +105,14 @@ destructor TEventBus.Destroy;
104105
end;
105106

106107
procedure TEventBus.AddCustomClassCloning(const AQualifiedClassName: String;
107-
const ACloneEvent: TCloneEvent);
108+
const ACloneEvent: TCloneEventMethod);
108109
begin
109110
FCustomClonerDict.Add(AQualifiedClassName, ACloneEvent);
110111
end;
111112

112113
function TEventBus.CloneEvent(AEvent: TObject): TObject;
113114
var
114-
LCloneEvent: TCloneEvent;
115+
LCloneEvent: TCloneEventMethod;
115116
begin
116117
if FCustomClonerDict.TryGetValue(AEvent.QualifiedClassName, LCloneEvent) then
117118
Result := LCloneEvent(AEvent)

tests/BaseTestU.pas

Lines changed: 37 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -3,44 +3,63 @@
33
interface
44

55
uses
6-
DUnitX.TestFramework, BOs;
6+
DUnitX.TestFramework,
7+
BOs;
78

89
type
910

10-
[TestFixture]
11-
TBaseTest = class(TObject)
12-
private
13-
FSubscriber: TSubscriber;
14-
procedure SetSubscriber(const Value: TSubscriber);
15-
public
16-
property Subscriber: TSubscriber read FSubscriber write SetSubscriber;
17-
[Setup]
18-
procedure Setup;
19-
[TearDown]
20-
procedure TearDown;
21-
end;
11+
[TestFixture]
12+
TBaseTest = class(TObject)
13+
private
14+
FSubscriber: TSubscriber;
15+
procedure SetSubscriber(const Value: TSubscriber);
16+
protected
17+
function SimpleCustomClone(const AObject: TObject): TObject;
18+
public
19+
property Subscriber: TSubscriber read FSubscriber write SetSubscriber;
20+
[Setup]
21+
procedure Setup;
22+
[TearDown]
23+
procedure TearDown;
24+
end;
2225

2326
implementation
2427

2528
uses
26-
System.SysUtils, EventBus;
29+
System.SysUtils,
30+
EventBus.Commons,
31+
EventBus;
2732

2833
{ TBaseTest }
2934

3035
procedure TBaseTest.SetSubscriber(const Value: TSubscriber);
3136
begin
32-
FSubscriber := Value;
37+
FSubscriber := Value;
3338
end;
3439

3540
procedure TBaseTest.Setup;
3641
begin
37-
FSubscriber := TSubscriber.Create;
42+
FSubscriber := TSubscriber.Create;
43+
end;
44+
45+
function TBaseTest.SimpleCustomClone(const AObject: TObject): TObject;
46+
var
47+
LEvent: TDEBEvent<TPerson>;
48+
begin
49+
LEvent := TDEBEvent<TPerson>.Create;
50+
LEvent.DataOwner := (AObject as TDEBEvent<TPerson>).DataOwner;
51+
LEvent.Data := TPerson.Create;
52+
LEvent.Data.Firstname := (AObject as TDEBEvent<TPerson>).Data.Firstname
53+
+ 'Custom';
54+
LEvent.Data.Lastname := (AObject as TDEBEvent<TPerson>).Data.Lastname
55+
+ 'Custom';
56+
Result := LEvent;
3857
end;
3958

4059
procedure TBaseTest.TearDown;
4160
begin
42-
if Assigned(FSubscriber) then
43-
FreeAndNil(FSubscriber);
61+
if Assigned(FSubscriber) then
62+
FreeAndNil(FSubscriber);
4463
end;
4564

4665
end.

tests/EventBusTestU.pas

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -255,17 +255,7 @@ procedure TEventBusTest.TestPostEntityWithCustomCloneEvent;
255255
LPerson.Firstname := 'Howard';
256256
LPerson.Lastname := 'Stark';
257257

258-
TEventBus.GetDefault.OnCloneEvent := function(AObject: TObject):TObject
259-
var
260-
LEvent: TDEBEvent<TPerson>;
261-
begin
262-
LEvent:= TDEBEvent<TPerson>.Create;
263-
LEvent.DataOwner := (AObject as TDEBEvent<TPerson>).DataOwner;
264-
LEvent.Data := TPerson.Create;
265-
LEvent.Data.Firstname := (AObject as TDEBEvent<TPerson>).Data.Firstname + 'Custom';
266-
LEvent.Data.Lastname := (AObject as TDEBEvent<TPerson>).Data.Lastname + 'Custom';
267-
Result := LEvent;
268-
end;
258+
TEventBus.GetDefault.OnCloneEvent := SimpleCustomClone;
269259

270260
TEventBus.GetDefault.Post(TDEBEvent<TPerson>.Create(LPerson));
271261
Assert.AreEqual('HowardCustom', LSubscriber.Person.Firstname);

0 commit comments

Comments
 (0)
0