Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Question: bind/unbind signal or event observer #12

Open
tower120 opened this issue Sep 15, 2015 · 2 comments
Open

Question: bind/unbind signal or event observer #12

tower120 opened this issue Sep 15, 2015 · 2 comments

Comments

@tower120
Copy link

It is not clear to me, how one can bind/unbind signal or event observer.

For example. I need to load a page. Page load took time.
When I start loading, I bind observer to work finish event/signal. Then, when work is done - I want to unbind observer. Then, I might need to load page again.

The nearest I found in the doc - is Observer.Detach() . But how to bind it again, is not clear to me.


And the second question - when the event queue clears?
How, for example, I can implement observer to left AND right mouse click (or double click)?
There will be definitely some delay between left and right MOUSE_DOWN event

@danbst
Copy link

danbst commented Oct 8, 2015

Regarding question 2

Event queue is not cleared, because it is not like vector at a point in time. The API is built in a way, that you can get current Signal value, but cannot get current Events value. It is the value itself.

One notable exception is Join, where event queue is simulated behind curtains. But you still cannot observe neither queue values nor queue emptied event.

However both click is easily implemented:

EventSourceT<> leftClick = MakeEventSource<D, Token>();
EventSourceT<> rightClick = MakeEventSource<D, Token>();
EventsT<> bothClick = ChangedTo(Delay(leftClick, 20) && Delay(rightClick, 20), true);

Delay(event, t) is a signal, that changes to true when event occurs and changes to false when t milliseconds are passed. Please, note I'm using time notion here, something that CppReact doesn't know about, so Delay implementation must use user-defined time.

Here is an example of implementation:
https://gist.github.com/danbst/30ef063cf7a6ea894b92

@danbst
Copy link

danbst commented Oct 8, 2015

Double click can be implemented like this:

    EventsT<> doubleLeftClick = Tokenize(Filter(Intervals(leftClick), [](long long diff /*milliseconds*/) {
        return diff < 100;
    }));

Intervals(e) event transformer replaces events e with intervals between those events and can be defined like

    EventsT<long long> Intervals(EventsT<> e) {
        using TDiff = pair < long long, long long > ;
        auto p1 = Iterate(Monitor(Snapshot(e, time)), TDiff(0, INT_MAX),
            [](long long t, TDiff tdiff) {
                return TDiff(t, t - tdiff.first);
            }
        );
        return Transform(Monitor(p1), [](TDiff tdiff){
            return tdiff.second;
        });
    }

Anyway, I'd like to hear @schlangster comments on my posts.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants