Skip to content

Commit

Permalink
changed IObservable<T> to (an overloaded!) anonymous method type
Browse files Browse the repository at this point in the history
  • Loading branch information
sglienke committed Jun 26, 2015
1 parent 1382f90 commit d427d2a
Show file tree
Hide file tree
Showing 5 changed files with 119 additions and 106 deletions.
38 changes: 28 additions & 10 deletions MainViewModel.pas
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,12 @@ TViewModel = class(TComponent)
fTickets: TList<TTicket>;
fAvailableCountries: TArray<string>;
fCountry: IObservable<string>;
fActive: IObservable<Boolean>;

function GetLastName: string;
procedure SetLastName(const value: string);
function GetActive: Boolean;
procedure SetActive(const value: Boolean);
public
constructor Create(const firstName, lastName: string); reintroduce;
destructor Destroy; override;
Expand All @@ -53,6 +56,8 @@ TViewModel = class(TComponent)

property AvailableCountries: TArray<string> read fAvailableCountries;
property Country: IObservable<string> read fCountry;

property Active: Boolean read GetActive write SetActive;
end;

implementation
Expand All @@ -77,19 +82,19 @@ constructor TViewModel.Create(const firstName, lastName: string);
fFullName := TDependentObservable<string>.Create(
function: string
begin
Result := fFirstName.Value + ' ' + fLastName.Value;
Result := fFirstName + ' ' + fLastName;
end);

// Example 2
fNumberOfClicks := TObservable<Integer>.Create;
fNumberOfClicks := TObservable<Integer>.Create();
fHasClickedTooManyTimes := TDependentObservable<Boolean>.Create(
function: Boolean
begin
Result := fNumberOfClicks.Value >= 3;
Result := fNumberOfClicks >= 3;
end);

// Example 3
fChosenTicket := TObservable<TTicket>.Create;
fChosenTicket := TObservable<TTicket>.Create();
fTickets := TObjectList<TTicket>.Create;
fTickets.AddRange([
TTicket.Create('Economy', 199.95),
Expand All @@ -98,7 +103,10 @@ constructor TViewModel.Create(const firstName, lastName: string);

// Example 4
fAvailableCountries := TArray<string>.Create('AU', 'NZ', 'US');
fCountry := TObservable<string>.Create;
fCountry := TObservable<string>.Create();

// Example 5;
fActive := TObservable<Boolean>.Create(True);
end;

destructor TViewModel.Destroy;
Expand All @@ -107,29 +115,39 @@ destructor TViewModel.Destroy;
inherited;
end;

function TViewModel.GetActive: Boolean;
begin
Result := fActive;
end;

function TViewModel.GetLastName: string;
begin
Result := fLastName.Value;
Result := fLastName;
end;

procedure TViewModel.RegisterClick;
begin
fNumberOfClicks.Value := fNumberOfClicks.Value + 1;
fNumberOfClicks(fNumberOfClicks + 1);
end;

procedure TViewModel.ResetClicks;
begin
fNumberOfClicks.Value := 0;
fNumberOfClicks(0);
end;

procedure TViewModel.ResetTicket;
begin
fChosenTicket.Value := nil;
fChosenTicket(nil);
end;

procedure TViewModel.SetActive(const value: Boolean);
begin
fActive(value);
end;

procedure TViewModel.SetLastName(const value: string);
begin
fLastName.Value := value;
fLastName(value);
end;

end.
46 changes: 23 additions & 23 deletions ObservableTests.pas
Original file line number Diff line number Diff line change
Expand Up @@ -40,23 +40,23 @@ procedure TObservableTests.DependentObservableClearsOldDependencies;
function: string
begin
Inc(count);
if a.Value then
Result := b.Value
if a then
Result := b
else
Result := c.Value
Result := c
end);
count := 0;
CheckEquals('false', o.Value);
CheckEquals('false', o);
CheckEquals(0, count);
b.Value := 'TRUE';
b('TRUE');
CheckEquals(0, count);
c.Value := 'FALSE';
CheckEquals('FALSE', o.Value);
c('FALSE');
CheckEquals('FALSE', o);
CheckEquals(1, count);
a.Value := True;
CheckEquals('TRUE', o.Value);
a(True);
CheckEquals('TRUE', o);
CheckEquals(2, count);
c.Value := 'false';
c('false');
CheckEquals(2, count);
end;

Expand All @@ -67,8 +67,8 @@ procedure TObservableTests.DependentObservableEvaluatesOnlyOnceAfterChange;
begin
count := 0;
o := TDependentObservable<string>.Create(function: string begin Inc(count); Result := 'test' end);
CheckEquals('test', o.Value);
CheckEquals('test', o.Value);
CheckEquals('test', o);
CheckEquals('test', o);
CheckEquals(1, count);
end;

Expand All @@ -77,39 +77,39 @@ procedure TObservableTests.DependentObservableReturnsValue;
o: IObservable<string>;
begin
o := TDependentObservable<string>.Create(function: string begin Result := 'test' end);
CheckEquals('test', o.Value);
CheckEquals('test', o);
end;

procedure TObservableTests.DependentObservableUpdatesValueWhenDependencyChanges;
var
o1, o2: IObservable<string>;
called: Boolean;
begin
o1 := TObservable<string>.Create;
o2 := TDependentObservable<string>.Create(function: string begin Result := o1.Value; called := True; end);
CheckEquals('', o2.Value);
o1 := TObservable<string>.Create();
o2 := TDependentObservable<string>.Create(function: string begin Result := o1; called := True; end);
CheckEquals('', o2);
called := False;
o1.Value := 'test';
o1('test');
Check(called);
CheckEquals('test', o2.Value);
CheckEquals('test', o2);
end;

procedure TObservableTests.ObservableReturnsValue;
var
o: IObservable<string>;
begin
o := TObservable<string>.Create('test');
CheckEquals('test', o.Value);
CheckEquals('test', o);
end;

procedure TObservableTests.ObservableSetValueChangesValue;
var
o: IObservable<string>;
begin
o := TObservable<string>.Create;
CheckEquals('', o.Value);
o.Value := 'test';
CheckEquals('test', o.Value);
o := TObservable<string>.Create();
CheckEquals('', o);
o('test');
CheckEquals('test', o);
end;

initialization
Expand Down
2 changes: 1 addition & 1 deletion SimpleMVVM.Binding.pas
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ function CreateObservable(instance: TObject;
if StartsText('IObservable<', prop.PropertyType.Name) then
begin
Result := prop.GetValue(instance).AsInterface as IObservable;
typ := prop.PropertyType.GetMethod('GetValue').ReturnType;
typ := prop.PropertyType.BaseType.GetMethod('Invoke').ReturnType;
end
else
begin
Expand Down
Loading

0 comments on commit d427d2a

Please sign in to comment.