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

Fix Timer #18

Merged
merged 1 commit into from
Oct 11, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 12 additions & 20 deletions Sources/Bulk/Core/BulkSink.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,45 +7,38 @@ public protocol BulkSinkType<Element>: Actor {
}

public actor BulkSink<B: Buffer>: BulkSinkType {

public typealias Element = B.Element

private let targets: [any TargetType<Element>]

private let timer: BulkBufferTimer

private let buffer: B

public init(
buffer: B,
debounceDueTime: Duration = .seconds(1),
targets: [any TargetType<Element>]
) {

self.buffer = buffer
self.targets = targets

weak var instance: BulkSink?
self.timer = BulkBufferTimer(interval: debounceDueTime) { [instance] in
await instance?.purge()

self.timer = BulkBufferTimer(interval: debounceDueTime) {
await instance?.flush()
}

instance = self

}

deinit {

}

private func purge() {
let elements = buffer.purge()
elements.forEach {
self.send($0)
}
}

public func send(_ newElement: Element) {
timer.tap()
switch buffer.write(element: newElement) {
Expand All @@ -58,9 +51,8 @@ public actor BulkSink<B: Buffer>: BulkSinkType {
break
}
}

public func flush() {
timer.tap()
let elements = buffer.purge()
targets.forEach {
$0.write(items: elements)
Expand Down
Loading