-
Notifications
You must be signed in to change notification settings - Fork 3.5k
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
Eager throttle #1396
Comments
I have written - (RACSignal *)throttleForInterval:(NSTimeInterval)interval
afterAllowing:(uint)count {
__block NSTimeInterval timeFirstEventPassedThrough;
__block NSTimeInterval timeFirstEventThrottled = 0;
__block uint numberEventsPassedThrough = 0;
return [self throttle:interval
valuesPassingTest:^BOOL(id next) {
NSTimeInterval now = [NSDate timeIntervalSinceReferenceDate];
if (numberEventsPassedThrough == 0) {
timeFirstEventPassedThrough = now;
numberEventsPassedThrough = 1;
return NO;
}
if (timeFirstEventThrottled) {
if (now > (timeFirstEventThrottled + interval)) {
// First event after most recent interval. No throttle
timeFirstEventPassedThrough = now;
timeFirstEventThrottled = 0;
numberEventsPassedThrough = 1;
return NO;
}
else {
// Still inside current throttle interval. Keep throttling
return YES;
}
}
else if (timeFirstEventPassedThrough) {
if (now > (timeFirstEventPassedThrough + interval)) {
// First event after most recent interval. No throttle
timeFirstEventPassedThrough = now;
timeFirstEventThrottled = 0;
numberEventsPassedThrough = 1;
return NO;
}
if (numberEventsPassedThrough < count) {
numberEventsPassedThrough++;
return NO;
}
}
timeFirstEventThrottled = now;
return YES;
}];
} |
Thanks for sharing. But that is an lot of code and state. I was hoping for something a little more elegant based on composing existing signal methods. |
Fair enough. If you can do it without any state, I'll throw this code out in favor of yours. 😉 The way I think about reactive stuff is that you move the state from one form (manual and imperative) to another (declarative and hidden). The state's still there inside the signal compositions and subscriptions; it's just not something the client code has to worry about. In case it wasn't clear, my code is a |
If I could go back in time, this is how I would want |
@epatey Your solution works really well for me actually. I guess you are right it is not easy to implement this just by composing existing signal operators. |
Implements the behavior proposed in #1396.
@olegam, thanks. Glad it was useful. |
This was addressed in c8807fb. Don't know why I left this open. ⚡ |
I need to operate on a signal with the following behavior:
next
is received and it did not send anext
for the lastinterval
seconds it should be forwarded immediately.next
is received and another next was just sent it will be queued and sentinterval
seconds after the previous one unless a newnext
is received before that.next
is received while one is waiting the first one is discarded and the latest will be sentinterval
seconds after the lastnext
was sent.The signature could be like:
Anyone who has done something like this? I have a lot of use cases where I prefer this behavior over
throttle:
.The text was updated successfully, but these errors were encountered: