SodiumFRP / sodium

Sodium - Functional Reactive Programming (FRP) Library for multiple languages
http://sodium.nz/
Other
848 stars 138 forks source link

Behaviour of Operational.value(...) in java version. #113

Closed clinuxrulz closed 6 years ago

clinuxrulz commented 6 years ago

The following piece of code:

public static void main(String[] params) {
    System.out.println("First Test");
    {
        nz.sodium.CellSink<Integer> cs = new nz.sodium.CellSink<>(1);
        nz.sodium.Listener l = nz.sodium.Operational.value(cs).listen(System.out::println);
        for (int i = 2; i < 5; ++i) {
            cs.send(i);
        }
        l.unlisten();
    }
    System.out.println("Second Test");
    {
        nz.sodium.CellSink<Integer> cs = new nz.sodium.CellSink<>(1);
        nz.sodium.Listener l = cs.listen(System.out::println);
        for (int i = 2; i < 5; ++i) {
            cs.send(i);
        }
        l.unlisten();
    }
}

Outputed:

First Test
2
3
4
Second Test
1
2
3
4

I expected the first test to output the same values as the second test. Is above a miss-use of Operational.value, or is it a bug?

the-real-blackh commented 6 years ago

It's not a bug. The issue is that in the first test, value() and listen() are operating within different transactions. You can make it behave as you expect by wrapping them into a single transaction context using Transaction.run(...).

clinuxrulz commented 6 years ago

OK. Thanks.