Skip to content

Commit

Permalink
minor refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
spinettaro committed Jan 5, 2018
1 parent 79aaddb commit c8f4c4e
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 16 deletions.
Binary file added docs/Built_for_Delphi.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
5 changes: 3 additions & 2 deletions source/EventBus.Attributes.pas
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ SubscribeAttribute = class(TCustomAttribute)
FThreadMode: TThreadMode;
public
constructor Create(AThreadMode: TThreadMode = TThreadMode.Posting;
AContext: String = '');
const AContext: String = '');
property ThreadMode: TThreadMode read FThreadMode;
property Context: String read FContext;
end;
Expand All @@ -38,8 +38,9 @@ implementation
{ SubscribeAttribute }

constructor SubscribeAttribute.Create(AThreadMode
: TThreadMode = TThreadMode.Posting; AContext: String = '');
: TThreadMode = TThreadMode.Posting; const AContext: String = '');
begin
inherited Create;
FContext := AContext;
FThreadMode := AThreadMode;
end;
Expand Down
11 changes: 5 additions & 6 deletions source/EventBus.Subscribers.pas
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ TSubscriberMethod = class(TObject)
procedure SetContext(const Value: String);
public
constructor Create(ARttiMethod: TRttiMethod; AEventType: TClass;
AThreadMode: TThreadMode; AContext: String = ''; APriority: Integer = 1);
AThreadMode: TThreadMode; const AContext: String = ''; APriority: Integer = 1);
destructor Destroy; override;
property EventType: TClass read FEventType write SetEventType;
property Method: TRttiMethod read FMethod write SetMethod;
Expand All @@ -52,10 +52,9 @@ TSubscription = class(TObject)
FSubscriberMethod: TSubscriberMethod;
FSubscriber: TObject;
FActive: Boolean;
FContext: string;
procedure SetActive(const Value: Boolean);
function GetActive: Boolean;
procedure SetSubcriberMethod(const Value: TSubscriberMethod);
procedure SetSubscriberMethod(const Value: TSubscriberMethod);
procedure SetSubscriber(const Value: TObject);
function GetContext: String;
public
Expand All @@ -65,7 +64,7 @@ TSubscription = class(TObject)
property Active: Boolean read GetActive write SetActive;
property Subscriber: TObject read FSubscriber write SetSubscriber;
property SubscriberMethod: TSubscriberMethod read FSubscriberMethod
write SetSubcriberMethod;
write SetSubscriberMethod;
property Context: String read GetContext;
function Equals(Obj: TObject): Boolean; override;

Expand All @@ -84,7 +83,7 @@ implementation
{ TSubscriberMethod }

constructor TSubscriberMethod.Create(ARttiMethod: TRttiMethod;
AEventType: TClass; AThreadMode: TThreadMode; AContext: String = '';
AEventType: TClass; AThreadMode: TThreadMode; const AContext: String = '';
APriority: Integer = 1);
begin
FMethod := ARttiMethod;
Expand Down Expand Up @@ -237,7 +236,7 @@ procedure TSubscription.SetActive(const Value: Boolean);
end;
end;

procedure TSubscription.SetSubcriberMethod(const Value: TSubscriberMethod);
procedure TSubscription.SetSubscriberMethod(const Value: TSubscriberMethod);
begin
FSubscriberMethod := Value;
end;
Expand Down
19 changes: 14 additions & 5 deletions source/EventBus.pas
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ interface
procedure RegisterSubscriber(ASubscriber: TObject);
function IsRegistered(ASubscriber: TObject): Boolean;
procedure Unregister(ASubscriber: TObject);
procedure Post(AEvent: TObject; AContext: String = '';
procedure Post(AEvent: TObject; const AContext: String = '';
AEventOwner: Boolean = true);
end;

Expand Down Expand Up @@ -57,7 +57,7 @@ TEventBus = class(TInterfacedObject, IEventBus)
procedure RegisterSubscriber(ASubscriber: TObject);
function IsRegistered(ASubscriber: TObject): Boolean;
procedure Unregister(ASubscriber: TObject);
procedure Post(AEvent: TObject; AContext: String = '';
procedure Post(AEvent: TObject; const AContext: String = '';
AEventOwner: Boolean = true);
class function GetDefault: TEventBus;
end;
Expand All @@ -69,16 +69,25 @@ implementation
{$IF CompilerVersion >= 28.0}
System.Threading,
{$ENDIF}
RttiUtilsU, ObjectsMappers, System.JSON;
System.JSON, REST.JSON, RTTIUtilsU;

var
FCS: TCriticalSection;

{ TEventBus }

function TEventBus.CloneEvent(AEvent: TObject): TObject;
var
LJSONObj: TJSONObject;
begin
Result := TRTTIUtils.Clone(AEvent);
Result := TRTTIUtils.CreateObject(AEvent.QualifiedClassName);
LJSONObj := TJson.ObjectToJsonObject(AEvent);
try
TJson.JsonToObject(Result, LJSONObj);
finally
LJSONObj.Free;
end;
// Result := TRTTIUtils.Clone(AEvent);
// LObj := Mapper.ObjectToJSONObject(AEvent);
// try
// Result := Mapper.JSONObjectToObject(AEvent.ClassType, LObj);
Expand Down Expand Up @@ -156,7 +165,7 @@ function TEventBus.IsRegistered(ASubscriber: TObject): Boolean;
end;
end;

procedure TEventBus.Post(AEvent: TObject; AContext: String = '';
procedure TEventBus.Post(AEvent: TObject; const AContext: String = '';
AEventOwner: Boolean = true);
var
LSubscriptions: TObjectList<TSubscription>;
Expand Down
2 changes: 1 addition & 1 deletion tests/DEBDUnitXTests.dproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<ProjectGuid>{803E5C08-94B4-4AC6-A3C0-C08D9DE5CB52}</ProjectGuid>
<ProjectVersion>18.2</ProjectVersion>
<ProjectVersion>18.3</ProjectVersion>
<MainSource>DEBDUnitXTests.dpr</MainSource>
<Base>True</Base>
<Config Condition="'$(Config)'==''">Debug</Config>
Expand Down
29 changes: 27 additions & 2 deletions tests/EventBusTestU.pas
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ TEventBusTest = class(TBaseTest)
[Test]
procedure TestPostEntityWithChildObject;
[Test]
procedure TestPostEntityWithItsSelfInChildObject;
[Test]
procedure TestPostEntityWithObjectList;
[Test]
procedure TestRegisterAndFree;
Expand Down Expand Up @@ -214,6 +216,29 @@ procedure TEventBusTest.TestPostContextOnMainThread;
end;

procedure TEventBusTest.TestPostEntityWithChildObject;
var
LPerson: TPerson;
LSubscriber: TPersonSubscriber;
begin
LSubscriber := TPersonSubscriber.Create;
try
LSubscriber.ObjOwner := true;
TEventBus.GetDefault.RegisterSubscriber(LSubscriber);
LPerson := TPerson.Create;
LPerson.Firstname := 'Howard';
LPerson.Lastname := 'Stark';
LPerson.Child := TPerson.Create;
LPerson.Child.Firstname := 'Tony';
LPerson.Child.Lastname := 'Stark';
TEventBus.GetDefault.Post(TDEBEvent<TPerson>.Create(LPerson));
Assert.AreEqual('Howard', LSubscriber.Person.Firstname);
Assert.AreEqual('Tony', LSubscriber.Person.Child.Firstname);
finally
LSubscriber.Free;
end;
end;

procedure TEventBusTest.TestPostEntityWithItsSelfInChildObject;
var
LPerson: TPerson;
LSubscriber: TPersonSubscriber;
Expand All @@ -226,10 +251,10 @@ procedure TEventBusTest.TestPostEntityWithChildObject;
LPerson.Firstname := 'Howard';
LPerson.Lastname := 'Stark';
// stackoverflow by TRTTIUtils.clone
// LPerson.Child := LPerson;
LPerson.Child := LPerson;
TEventBus.GetDefault.Post(TDEBEvent<TPerson>.Create(LPerson));
Assert.AreEqual('Howard', LSubscriber.Person.Firstname);
// Assert.AreEqual('Tony', LSubscriber.Person.Child.Firstname);
Assert.AreEqual('Tony', LSubscriber.Person.Child.Firstname);
finally
LSubscriber.Free;
end;
Expand Down

0 comments on commit c8f4c4e

Please sign in to comment.