Skip to content

Commit

Permalink
fixed issue with wrong notifications
Browse files Browse the repository at this point in the history
  • Loading branch information
sglienke committed Oct 19, 2015
1 parent 13f0dd2 commit 11effcb
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 2 deletions.
6 changes: 4 additions & 2 deletions Knockoff.Observable.pas
Original file line number Diff line number Diff line change
Expand Up @@ -262,10 +262,12 @@ procedure TObservableBase.ClearDependencies;

procedure TObservableBase.Notify(const value: TValue; trigger: TNotifyTrigger);
var
subscribers: TArray<TObservableBase>;
i: Integer;
begin
for i := 0 to fSubscribers.Count - 1 do
fSubscribers[i].Notify(value, trigger);
subscribers := fSubscribers.ToArray;
for i := 0 to High(subscribers) do
subscribers[i].Notify(value, trigger);
inherited Notify(value, trigger);
end;

Expand Down
30 changes: 30 additions & 0 deletions ObservableTests.pas
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ TObservableTests = class(TTestCase)
procedure DependentObservableUpdatesValueWhenDependencyChanges;

procedure DependentObservableClearsOldDependencies;
procedure DependentObservableNotifiesMultipleDependenciesProperly;
end;

KO = Observable;
Expand Down Expand Up @@ -71,6 +72,35 @@ procedure TObservableTests.DependentObservableEvaluatesOnlyOnceAfterChange;
CheckEquals(1, count);
end;

procedure TObservableTests.DependentObservableNotifiesMultipleDependenciesProperly;
var
o: Observable<Integer>;
dependency1, dependency2: Observable<Boolean>;
callCount1, callCount2: Integer;
begin
o := KO.Create<Integer>(0);
dependency1 := KO.Computed<Boolean>(
function: Boolean
begin
Result := o < 10;
Inc(callCount1);
end);
dependency2 := KO.Computed<Boolean>(
function: Boolean
begin
Result := o < 10;
Inc(callCount2);
end);
CheckEquals(1, callCount1);
CheckEquals(1, callCount2);
Check(o < 10);
CheckEquals(1, callCount1);
CheckEquals(1, callCount2);
o(o+1);
CheckEquals(2, callCount1);
CheckEquals(2, callCount2);
end;

procedure TObservableTests.DependentObservableReturnsValue;
var
o: Observable<string>;
Expand Down

0 comments on commit 11effcb

Please sign in to comment.