-
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 6 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,48 @@ extension Signal.Event { | |
} | ||
} | ||
} | ||
|
||
internal static func collect(every interval: DispatchTimeInterval, on scheduler: DateScheduler, skipEmpty: Bool, discardsWhenCompleted: Bool) -> Transformation<[Value], Error> { | ||
return { action, lifetime in | ||
let values = Atomic<[Value]>([]) | ||
let d = SerialDisposable() | ||
var lastDelivered: Date = scheduler.currentDate | ||
|
||
d.inner = scheduler.schedule(after: scheduler.currentDate.addingTimeInterval(interval), interval: interval, leeway: interval * 0.1) { | ||
let currentValues = values.swap([]) | ||
if !(currentValues.isEmpty && skipEmpty) { | ||
action(.value(currentValues)) | ||
lastDelivered = scheduler.currentDate | ||
} | ||
} | ||
|
||
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: | ||
let currentValues = values.swap([]) | ||
if !discardsWhenCompleted, !(currentValues.isEmpty && skipEmpty) { | ||
d.inner = scheduler.schedule(after: lastDelivered.addingTimeInterval(interval)) { | ||
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 I think we should solve this by removing this Something like this: state CollectEveryState<Value> {
var skipEmpty: Bool
var values: [Value] = []
var isCompleted: Bool
var hasValues: Bool {
return !values.isEmpty || !skipEmpty
}
func collect() -> [Value]? {
if !hasValues {
return nil
}
defer { values.removeAll() }
return values
}
} That should clean up the implementation a bit too, which is nice. 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. @mdiep Done 👍 |
||
action(.value(currentValues)) | ||
action(.completed) | ||
} | ||
} else { | ||
d.inner = scheduler.schedule { action(.completed) } | ||
} | ||
case .interrupted: | ||
d.inner = scheduler.schedule { action(.interrupted) } | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
private struct ThrottleState<Value> { | ||
|
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.
Oh, let's make this
discardWhenCompleted
(no s) since we're doing that in #605 as well.