-
Notifications
You must be signed in to change notification settings - Fork 429
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
Add a recurring collect function and associated unit tests #619
Changes from 4 commits
16a620e
7322bf9
b4b4dc4
45a49b9
ca922e6
e5e0839
c139a09
a903fd5
a7c5e4f
5ab5bcf
ebbe803
e5a1c86
3a2e8ee
710540e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -861,6 +861,42 @@ extension Signal.Event { | |
} | ||
} | ||
} | ||
|
||
internal static func collect(_ interval: DispatchTimeInterval, on scheduler: DateScheduler, ignoreWhenEmpty: Bool) -> Transformation<[Value], Error> { | ||
return { action, lifetime in | ||
let values = Atomic<[Value]>([]) | ||
let d = SerialDisposable() | ||
|
||
d.inner = scheduler.schedule(after: scheduler.currentDate.addingTimeInterval(interval), interval: interval, leeway: interval * 0.1, action: { | ||
let currentValues: [Value]? = values.modify { values in | ||
guard !(values.isEmpty && ignoreWhenEmpty) else { return nil } | ||
defer { values = [] } | ||
return values | ||
} | ||
if let values = currentValues { | ||
action(.value(values)) | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think this could be better written like so: let currentValues = values.swap([])
if !currentValues.isEmpty {
action(.value(currentValues))
} |
||
}) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This should use trailing closure syntax. |
||
|
||
lifetime.observeEnded { | ||
d.dispose() | ||
scheduler.schedule { action(.interrupted) } | ||
} | ||
|
||
return { event in | ||
switch event { | ||
case let .value(value): | ||
values.modify { $0.append(value) } | ||
case let .failed(error): | ||
d.inner = scheduler.schedule { action(.failed(error)) } | ||
case .completed: | ||
d.inner = scheduler.schedule { action(.completed) } | ||
case .interrupted: | ||
d.inner = scheduler.schedule { action(.interrupted) } | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
private struct ThrottleState<Value> { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1506,6 +1506,25 @@ extension Signal { | |
public func debounce(_ interval: TimeInterval, on scheduler: DateScheduler) -> Signal<Value, Error> { | ||
return flatMapEvent(Signal.Event.debounce(interval, on: scheduler)) | ||
} | ||
|
||
/// Forward the latest values on `scheduler` every `interval`. | ||
/// | ||
/// - seealso: `throttle` | ||
/// - seealso: `debounce` | ||
/// | ||
/// - note: If the input signal terminates, the returned signal will | ||
/// terminate immediately without forwarding the values | ||
/// currently being accumulated. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In the spirit of #605, maybe there should be an option to forward the values first? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good point! There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. After looking into this I can't find a way to make it work consistently, given: So should it only not discard in the case of There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah, values should only be forwarded in the case of |
||
/// | ||
/// - parameters: | ||
/// - interval: A repetition interval. | ||
/// - scheduler: A scheduler to send values on. | ||
/// | ||
/// - returns: A signal that sends all values that are sent from `self` at | ||
/// `interval` seconds apart. | ||
public func collect(_ interval: DispatchTimeInterval, on scheduler: DateScheduler, ignoreWhenEmpty: Bool = false) -> Signal<[Value], Error> { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We've standardized on There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think we should name the interval parameter. Maybe |
||
return flatMapEvent(Signal.Event.collect(interval, on: scheduler, ignoreWhenEmpty: ignoreWhenEmpty)) | ||
} | ||
} | ||
|
||
extension Signal { | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This needs to be updated.