Skip to content

Commit

Permalink
Resolves #73.
Browse files Browse the repository at this point in the history
  • Loading branch information
back2dos committed Aug 3, 2021
1 parent c733f98 commit 7048c78
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 5 deletions.
15 changes: 10 additions & 5 deletions src/tink/state/internal/AutoObservable.hx
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class AutoObservable<T> extends Dispatcher
#end
public var hot(default, null) = false;
final annex:Annex<{}>;
var status = Dirty;
var status = Fresh;
var last:T = null;
var subscriptions:Array<Subscription>;
var dependencies = new ObjectMap<ObservableObject<Dynamic>, Subscription>();
Expand Down Expand Up @@ -54,8 +54,14 @@ class AutoObservable<T> extends Dispatcher
return true;
}

public function swapComputation(c:Computation<T>) {
this.computation = c;
this.status = Fresh;
fire(this);
}

public function isValid()
return status != Dirty && (hot || subsValid());
return status == Computed && (hot || subsValid());

public function getComparator()
return comparator;
Expand Down Expand Up @@ -155,11 +161,9 @@ class AutoObservable<T> extends Dispatcher
#if tink_state.debug
logger.revalidating(this);
#end
var prevSubs = subscriptions;

if (++count == Observable.MAX_ITERATIONS)
throw 'no result after ${Observable.MAX_ITERATIONS} attempts';
else if (subscriptions != null) {
else if (status != Fresh) {
var valid = true;

for (s in subscriptions)
Expand Down Expand Up @@ -279,4 +283,5 @@ private class Subscription {
private enum abstract AutoObservableStatus(Int) {
var Dirty;
var Computed;
var Fresh;
}
1 change: 1 addition & 0 deletions tests/RunTests.hx
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ class RunTests {
new issues.Issue60(),
new issues.Issue61(),
new issues.Issue63(),
new issues.Issue73(),
])).handle(Runner.exit);
}
}
43 changes: 43 additions & 0 deletions tests/issues/Issue73.hx
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package issues;

import tink.state.internal.AutoObservable;
import tink.state.*;

@:asserts
class Issue73 {
public function new() {}
public function test() {

var log = '';

final s1 = new State(2, (active) -> log += (if (active) '+' else '-') + '1'),
s2 = new State(3, (active) -> log += (if (active) '+' else '-') + '2');

final sum = Observable.auto(() -> s1.value + s2.value),
product = Observable.auto(() -> s1.value * s2.value);

final a = new AutoObservable(() -> product.value - sum.value);

(a:Observable<Int>).bind(() -> {});

asserts.assert(log == '+1+2');
asserts.assert(a.getValue() == 1);

a.swapComputation(() -> product.value + sum.value);

asserts.assert(log == '+1+2');
asserts.assert(a.getValue() == 11);

a.swapComputation(() -> product.value);

asserts.assert(a.getValue() == 6);
asserts.assert(log == '+1+2');

a.swapComputation(() -> s1.value);

asserts.assert(a.getValue() == 2);
asserts.assert(log == '+1+2-2');

return asserts.done();
}
}

0 comments on commit 7048c78

Please sign in to comment.