Skip to content

Commit

Permalink
Fix Timer (#18)
Browse files Browse the repository at this point in the history
  • Loading branch information
muukii authored Oct 11, 2024
1 parent 4f9a344 commit 9170850
Showing 1 changed file with 12 additions and 20 deletions.
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

0 comments on commit 9170850

Please sign in to comment.