Skip to content

Commit

Permalink
changed TCloneEventCallback with a function of object instead an anon…
Browse files Browse the repository at this point in the history
…ymous method
  • Loading branch information
Daniele Spinetti committed Aug 5, 2019
1 parent d8336ad commit d223174
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 37 deletions.
17 changes: 9 additions & 8 deletions source/EventBus.pas
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ interface

type

TCloneEvent = TFunc<TObject,TObject>;
TCloneEventCallback = function (const AObject: TObject): TObject of object;
TCloneEventMethod = TFunc<TObject,TObject>;

IEventBus = Interface
['{7BDF4536-F2BA-4FBA-B186-09E1EE6C7E35}']
Expand All @@ -42,8 +43,8 @@ TEventBus = class(TInterfacedObject, IEventBus)
var
FTypesOfGivenSubscriber: TObjectDictionary<TObject, TList<TClass>>;
FSubscriptionsOfGivenEventType: TObjectDictionary<TClass, TObjectList<TSubscription>>;
FCustomClonerDict: TDictionary<String, TCloneEvent>;
FOnCloneEvent: TCloneEvent;
FCustomClonerDict: TDictionary<String, TCloneEventMethod>;
FOnCloneEvent: TCloneEventCallback;
procedure Subscribe(ASubscriber: TObject;
ASubscriberMethod: TSubscriberMethod);
procedure UnsubscribeByEventType(ASubscriber: TObject; AEventType: TClass);
Expand All @@ -66,8 +67,8 @@ TEventBus = class(TInterfacedObject, IEventBus)
property TypesOfGivenSubscriber: TObjectDictionary<TObject, TList<TClass>> read FTypesOfGivenSubscriber;
property SubscriptionsOfGivenEventType: TObjectDictionary<TClass, TObjectList<TSubscription>> read
FSubscriptionsOfGivenEventType;
property OnCloneEvent: TCloneEvent write FOnCloneEvent;
procedure AddCustomClassCloning(const AQualifiedClassName: String; const ACloneEvent: TCloneEvent);
property OnCloneEvent: TCloneEventCallback write FOnCloneEvent;
procedure AddCustomClassCloning(const AQualifiedClassName: String; const ACloneEvent: TCloneEventMethod);
procedure RemoveCustomClassCloning(const AQualifiedClassName: String);
end;

Expand All @@ -92,7 +93,7 @@ constructor TEventBus.Create;
TObjectList < TSubscription >>.Create([doOwnsValues]);
FTypesOfGivenSubscriber := TObjectDictionary < TObject,
TList < TClass >>.Create([doOwnsValues]);
FCustomClonerDict := TDictionary<String, TCloneEvent>.Create;
FCustomClonerDict := TDictionary<String, TCloneEventMethod>.Create;
end;

destructor TEventBus.Destroy;
Expand All @@ -104,14 +105,14 @@ destructor TEventBus.Destroy;
end;

procedure TEventBus.AddCustomClassCloning(const AQualifiedClassName: String;
const ACloneEvent: TCloneEvent);
const ACloneEvent: TCloneEventMethod);
begin
FCustomClonerDict.Add(AQualifiedClassName, ACloneEvent);
end;

function TEventBus.CloneEvent(AEvent: TObject): TObject;
var
LCloneEvent: TCloneEvent;
LCloneEvent: TCloneEventMethod;
begin
if FCustomClonerDict.TryGetValue(AEvent.QualifiedClassName, LCloneEvent) then
Result := LCloneEvent(AEvent)
Expand Down
55 changes: 37 additions & 18 deletions tests/BaseTestU.pas
Original file line number Diff line number Diff line change
Expand Up @@ -3,44 +3,63 @@
interface

uses
DUnitX.TestFramework, BOs;
DUnitX.TestFramework,
BOs;

type

[TestFixture]
TBaseTest = class(TObject)
private
FSubscriber: TSubscriber;
procedure SetSubscriber(const Value: TSubscriber);
public
property Subscriber: TSubscriber read FSubscriber write SetSubscriber;
[Setup]
procedure Setup;
[TearDown]
procedure TearDown;
end;
[TestFixture]
TBaseTest = class(TObject)
private
FSubscriber: TSubscriber;
procedure SetSubscriber(const Value: TSubscriber);
protected
function SimpleCustomClone(const AObject: TObject): TObject;
public
property Subscriber: TSubscriber read FSubscriber write SetSubscriber;
[Setup]
procedure Setup;
[TearDown]
procedure TearDown;
end;

implementation

uses
System.SysUtils, EventBus;
System.SysUtils,
EventBus.Commons,
EventBus;

{ TBaseTest }

procedure TBaseTest.SetSubscriber(const Value: TSubscriber);
begin
FSubscriber := Value;
FSubscriber := Value;
end;

procedure TBaseTest.Setup;
begin
FSubscriber := TSubscriber.Create;
FSubscriber := TSubscriber.Create;
end;

function TBaseTest.SimpleCustomClone(const AObject: TObject): TObject;
var
LEvent: TDEBEvent<TPerson>;
begin
LEvent := TDEBEvent<TPerson>.Create;
LEvent.DataOwner := (AObject as TDEBEvent<TPerson>).DataOwner;
LEvent.Data := TPerson.Create;
LEvent.Data.Firstname := (AObject as TDEBEvent<TPerson>).Data.Firstname
+ 'Custom';
LEvent.Data.Lastname := (AObject as TDEBEvent<TPerson>).Data.Lastname
+ 'Custom';
Result := LEvent;
end;

procedure TBaseTest.TearDown;
begin
if Assigned(FSubscriber) then
FreeAndNil(FSubscriber);
if Assigned(FSubscriber) then
FreeAndNil(FSubscriber);
end;

end.
12 changes: 1 addition & 11 deletions tests/EventBusTestU.pas
Original file line number Diff line number Diff line change
Expand Up @@ -255,17 +255,7 @@ procedure TEventBusTest.TestPostEntityWithCustomCloneEvent;
LPerson.Firstname := 'Howard';
LPerson.Lastname := 'Stark';

TEventBus.GetDefault.OnCloneEvent := function(AObject: TObject):TObject
var
LEvent: TDEBEvent<TPerson>;
begin
LEvent:= TDEBEvent<TPerson>.Create;
LEvent.DataOwner := (AObject as TDEBEvent<TPerson>).DataOwner;
LEvent.Data := TPerson.Create;
LEvent.Data.Firstname := (AObject as TDEBEvent<TPerson>).Data.Firstname + 'Custom';
LEvent.Data.Lastname := (AObject as TDEBEvent<TPerson>).Data.Lastname + 'Custom';
Result := LEvent;
end;
TEventBus.GetDefault.OnCloneEvent := SimpleCustomClone;

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

1 comment on commit d223174

@edwinyzh
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good choice! A object method should be much better than anon. methods for this purpose, and that's why I didn't upgrade :)

Please sign in to comment.