From 11effcb578d4a8a344781d95e61596ce6d607617 Mon Sep 17 00:00:00 2001 From: Stefan Glienke Date: Mon, 19 Oct 2015 15:08:00 +0200 Subject: [PATCH] fixed issue with wrong notifications --- Knockoff.Observable.pas | 6 ++++-- ObservableTests.pas | 30 ++++++++++++++++++++++++++++++ 2 files changed, 34 insertions(+), 2 deletions(-) diff --git a/Knockoff.Observable.pas b/Knockoff.Observable.pas index 02c693b..5ca8c04 100644 --- a/Knockoff.Observable.pas +++ b/Knockoff.Observable.pas @@ -262,10 +262,12 @@ procedure TObservableBase.ClearDependencies; procedure TObservableBase.Notify(const value: TValue; trigger: TNotifyTrigger); var + subscribers: TArray; 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; diff --git a/ObservableTests.pas b/ObservableTests.pas index 09e57c0..d33accd 100644 --- a/ObservableTests.pas +++ b/ObservableTests.pas @@ -17,6 +17,7 @@ TObservableTests = class(TTestCase) procedure DependentObservableUpdatesValueWhenDependencyChanges; procedure DependentObservableClearsOldDependencies; + procedure DependentObservableNotifiesMultipleDependenciesProperly; end; KO = Observable; @@ -71,6 +72,35 @@ procedure TObservableTests.DependentObservableEvaluatesOnlyOnceAfterChange; CheckEquals(1, count); end; +procedure TObservableTests.DependentObservableNotifiesMultipleDependenciesProperly; +var + o: Observable; + dependency1, dependency2: Observable; + callCount1, callCount2: Integer; +begin + o := KO.Create(0); + dependency1 := KO.Computed( + function: Boolean + begin + Result := o < 10; + Inc(callCount1); + end); + dependency2 := KO.Computed( + 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;