Skip to content

Commit

Permalink
Merge pull request #86 from gene-pavlovsky/make-nexttime-condition-op…
Browse files Browse the repository at this point in the history
…tional

Make nexttime condition optional
  • Loading branch information
back2dos authored Feb 26, 2024
2 parents c3dcbad + c765f74 commit 5740bff
Show file tree
Hide file tree
Showing 9 changed files with 42 additions and 29 deletions.
2 changes: 1 addition & 1 deletion .haxerc
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{
"version": "4.2.0",
"version": "4.3.3",
"resolveLibs": "scoped"
}
6 changes: 3 additions & 3 deletions haxe_libraries/tink_core.hxml
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
# @install: lix --silent download "gh://github.com/haxetink/tink_core#e0ed6c33f6f396fb83397a590bee4c3d48ab2e17" into tink_core/2.0.2/github/e0ed6c33f6f396fb83397a590bee4c3d48ab2e17
-cp ${HAXE_LIBCACHE}/tink_core/2.0.2/github/e0ed6c33f6f396fb83397a590bee4c3d48ab2e17/src
-D tink_core=2.0.2
# @install: lix --silent download "haxelib:/tink_core#2.1.1" into tink_core/2.1.1/haxelib
-cp ${HAXE_LIBCACHE}/tink_core/2.1.1/haxelib/src
-D tink_core=2.1.1
23 changes: 17 additions & 6 deletions src/tink/state/Observable.hx
Original file line number Diff line number Diff line change
Expand Up @@ -77,15 +77,26 @@ abstract Observable<T>(ObservableObject<T>) from ObservableObject<T> to Observab
public function combine<A, R>(that:Observable<A>, f:T->A->R):Observable<R>
return Observable.auto(() -> f(value, that.value));

/**
* Be careful with `butNotNow`, its semantics are a bit counter-intuitive:
* If the condition returns true for the current value, it will skip the current value,
* then all future values as long as the condition keeps returning true,
* then the first and all consecutive future values for which the condition returns false.
* Example: If the condition is `v -> v > 0`, and the values sequence is [ 0, 10, 20, -1, -2, 42 ],
* the future will resolve to 42.
*/
public function nextTime(?options:{ ?butNotNow: Bool, ?hires:Bool }, ?condition:T->Bool):Future<T>
return getNext(options,
if (condition == null) Some
else v -> if (condition(v)) Some(v) else None
);
return
if (condition == null) {
var i = 0;
getNext({ butNotNow: true, hires: options?.hires ?? false }, v -> if (i++ == 0) None else Some(v));
}
else getNext(options, v -> if (condition(v)) Some(v) else None);

// Be careful with `butNotNow`, its semantics has the same peculiarity as when using `nextTime`, see the comments above.
public function getNext<R>(?options:{ ?butNotNow: Bool, ?hires:Bool }, select:T->Option<R>):Future<R> {
var ret = Future.trigger(),
waiting = options != null && options.butNotNow;
waiting = options?.butNotNow;

var link = bind(value -> {
var out = select(value);
Expand All @@ -95,7 +106,7 @@ abstract Observable<T>(ObservableObject<T>) from ObservableObject<T> to Observab
case Some(value): ret.trigger(value);
case None:
}
}, if (options != null && options.hires) Scheduler.direct else null);
}, if (options?.hires) Scheduler.direct else null);

ret.handle(link.cancel);

Expand Down
1 change: 0 additions & 1 deletion tests.hxml
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
-cp src
-cp tests
-D no-deprecation-warnings
-lib tink_unittest
Expand Down
8 changes: 4 additions & 4 deletions tests/TestArrays.hx
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ package ;
import tink.state.*;
import tink.state.Scheduler.direct;

using tink.CoreApi;
using Lambda;
using StringTools;
using tink.CoreApi;

@:asserts
class TestArrays {
Expand Down Expand Up @@ -97,7 +97,7 @@ class TestArrays {
.bind(() -> keysChanges++, direct);

Observable.auto(() -> {
final first = 0;
var first = 0;
for (v in a) {
first += v;
break;
Expand Down Expand Up @@ -131,7 +131,7 @@ class TestArrays {
public function clear() {
final o = new ObservableArray<Null<Int>>([1,2,3]);

final log = '';
var log = '';

Observable.auto(() -> o.length).bind(v -> log += 'len:$v', direct);
for(i in 0...o.length) o.entry(i).bind(v -> log += ',$i:$v', direct);
Expand All @@ -152,4 +152,4 @@ class TestArrays {
asserts.assert(v.value == '0:20:30:40:50:60:70:80:90:123:');
return asserts.done();
}
}
}
14 changes: 7 additions & 7 deletions tests/TestAuto.hx
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package;

import tink.state.Scheduler.direct;
import tink.state.Promised;
import tink.state.Observable;
import tink.state.*;
import tink.state.Observable;
import tink.state.Promised;
import tink.state.Scheduler.direct;

using tink.CoreApi;

Expand Down Expand Up @@ -89,9 +89,9 @@ class TestAuto {
function inc()
counter.set(counter.value + 1);

var last = None;
var last:Option<{ t:Int, c:Int }> = None;

final o = Observable.auto(l -> {
final o = Observable.auto((l:Option<{ t:Int, c:Int }>) -> {
final t = new FutureTrigger();
last = l;
triggers.push(t);
Expand Down Expand Up @@ -231,7 +231,7 @@ class TestAuto {
final select = new State([for (i in 0...states.length) i % 3 == 0]);

function add() {
final ret = 0;
var ret = 0;
for (i => s in select.value)
if (s) ret += states[i].value;
return ret;
Expand All @@ -258,4 +258,4 @@ class TestAuto {

return asserts.done();
}
}
}
5 changes: 4 additions & 1 deletion tests/TestBasic.hx
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class TestBasic {
public function donotFireEqual() {
final s = new State(0),
sLog = [];
final watch = s.observe().bind(sLog.push, (_, _) -> true, direct);
var watch = s.observe().bind(sLog.push, (_, _) -> true, direct);

final o1Log = [],
o1 = Observable.auto(() -> {
Expand Down Expand Up @@ -127,6 +127,9 @@ class TestBasic {
asserts.assert(fired == 4);

await(o.nextTime({ hires: true, }));

asserts.assert(fired == 4);

set(0);

asserts.assert(fired == 5);
Expand Down
8 changes: 4 additions & 4 deletions tests/TestDate.hx
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package ;

import tink.state.*;
import deepequal.DeepEqual.*;
import tink.state.*;

using tink.CoreApi;
using DateTools;
using tink.CoreApi;

@:asserts
class TestDate {
Expand All @@ -15,7 +15,7 @@ class TestDate {
final d = new ObservableDate(),
log = [];

final watch = d.becomesOlderThan(1.seconds()).bind(log.push);
var watch = d.becomesOlderThan(1.seconds()).bind(log.push);
watch &= d.becomesOlderThan(10.seconds()).bind(log.push);

Observable.updateAll();
Expand All @@ -27,4 +27,4 @@ class TestDate {
return asserts.done();
});
}
}
}
4 changes: 2 additions & 2 deletions tests/TestScheduler.hx
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ class TestScheduler {

final log = [];

final watch = s1.observe().bind(v -> {
var watch = s1.observe().bind(v -> {
s2.set('foo($v)');
s3.set('bar($v)');
});
Expand Down Expand Up @@ -83,4 +83,4 @@ class TestScheduler {

return asserts.done();
}
}
}

0 comments on commit 5740bff

Please sign in to comment.