Skip to content

Commit

Permalink
🌕
Browse files Browse the repository at this point in the history
  • Loading branch information
k-kohey committed Feb 8, 2022
1 parent 926581d commit 54650f9
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 35 deletions.
3 changes: 2 additions & 1 deletion Demo.swiftpm/Sources/AppModule/Demo.swift
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ extension TrackingEvent {
}

let logger = LoggerBundler.make(
components: [MyLogger()]
components: [MyLogger()],
loggingStrategy: RegularlyPollingScheduler(timeInterval: 5)
)


Expand Down
13 changes: 2 additions & 11 deletions Sources/Parchment/BufferedEventFlushScheduler.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,6 @@
import Foundation

public protocol BufferedEventFlushScheduler {
func schedule(with buffer: TrackingEventBufferAdapter, didFlush: @escaping ([BufferRecord])->())
}

extension BufferedEventFlushScheduler {
func schedule(with buffer: TrackingEventBufferAdapter) -> AsyncThrowingStream<[BufferRecord], Error> {
AsyncThrowingStream { continuation in
schedule(with: buffer) {
continuation.yield($0)
}
}
}
func schedule(with buffer: TrackingEventBufferAdapter) async -> AsyncThrowingStream<[BufferRecord], Error>
func cancel()
}
10 changes: 8 additions & 2 deletions Sources/Parchment/LoggerBundler.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ public final class LoggerBundler {
public var configMap: [LoggerComponentID: Configuration] = [:]
public var mutations: [Mutation] = []

private var loggingTask: Task<(), Never>?

public init(
components: [LoggerComponent],
buffer: TrackingEventBuffer,
Expand Down Expand Up @@ -90,13 +92,13 @@ public final class LoggerBundler {
}

public func startLogging() {
Task { [weak self] in
loggingTask = Task { [weak self] in
guard let self = self else {
assertionIfDebugMode("LoggerBundler instance should been retained by any object due to log events definitely")
return
}
do {
for try await records in self.flushStrategy.schedule(with: self.buffer) {
for try await records in await self.flushStrategy.schedule(with: self.buffer) {
await self.bloadcast(records)
}
} catch {
Expand All @@ -105,6 +107,10 @@ public final class LoggerBundler {
}
}

func cancell() {
loggingTask?.cancel()
}

private func bloadcast(_ records: [BufferRecord]) async {
let recordEachLogger = Dictionary(grouping: records) { record in
record.destination
Expand Down
21 changes: 14 additions & 7 deletions Sources/ParchmentDefault/RegularlyPollingScheduler.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public final class RegularlyPollingScheduler: BufferedEventFlushScheduler {

var lastFlushedDate: Date = Date()

private var timer: Timer?
private weak var timer: Timer?

public init(
timeInterval: TimeInterval,
Expand All @@ -27,17 +27,24 @@ public final class RegularlyPollingScheduler: BufferedEventFlushScheduler {
self.limitOnNumberOfEvent = limitOnNumberOfEvent
}

public func schedule(with buffer: TrackingEventBufferAdapter, didFlush: @escaping ([BufferRecord])->()) {
DispatchQueue.main.async { [weak self] in
self?.timer = Timer.scheduledTimer(withTimeInterval: 1, repeats: true) { _ in
guard let self = self else { return }
Task {
await self.tick(with: buffer, didFlush: didFlush)
public func schedule(with buffer: TrackingEventBufferAdapter) async -> AsyncThrowingStream<[BufferRecord], Error> {
return AsyncThrowingStream { continuation in
let timer = Timer(fire: .init(), interval: 1, repeats: true) { _ in
Task { [weak self] in
await self?.tick(with: buffer) {
continuation.yield($0)
}
}
}
RunLoop.main.add(timer, forMode: .common)
self.timer = timer
}
}

public func cancel() {
timer?.invalidate()
}

private func tick(with buffer: TrackingEventBufferAdapter, didFlush: @escaping ([BufferRecord])->()) async {
guard await buffer.count() > 0 else { return }

Expand Down
14 changes: 5 additions & 9 deletions Tests/ParchmentDefaultTests/RegularlyPollingSchedulerTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -43,17 +43,13 @@ class RegularlyPollingSchedulerTests: XCTestCase {
let scheduler = RegularlyPollingScheduler(timeInterval: 0.1)
let buffer = EventQueueMock()
let event = TrackingEvent(eventName: "hoge", parameters: [:])
let exp = expectation(description: "wait")

var result: [BufferRecord] = []
scheduler.schedule(with: .init(buffer)) { record in
result = record
exp.fulfill()
Task {
for try await result in await scheduler.schedule(with: .init(buffer)) {
XCTAssertEqual(event.eventName, result.first?.eventName)
XCTAssertTrue(NSDictionary(dictionary: event.parameters).isEqual(to: result.first?.parameters ?? [:]))
}
}
buffer.save([.init(destination: "hoge", event: event, timestamp: Date())])

wait(for: [exp], timeout: 1.1)
XCTAssertEqual(event.eventName, result.first?.eventName)
XCTAssertTrue(NSDictionary(dictionary: event.parameters).isEqual(to: result.first?.parameters ?? [:]))
}
}
16 changes: 11 additions & 5 deletions Tests/ParchmentTests/LoggerBundlerTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -62,18 +62,24 @@ final class EventQueueMock: TrackingEventBuffer {
}

final class BufferedEventFlushStrategyMock: BufferedEventFlushScheduler {
private var didFlush: (([BufferRecord]) -> ())? = nil
private var buffer: TrackingEventBufferAdapter?

func schedule(with buffer: TrackingEventBufferAdapter, didFlush: @escaping ([BufferRecord]) -> ()) {
self.didFlush = didFlush
private var continuation: AsyncThrowingStream<[BufferRecord], Error>.Continuation?

func schedule(with buffer: TrackingEventBufferAdapter) async -> AsyncThrowingStream<[BufferRecord], Error> {
self.buffer = buffer
return .init { continuation in
self.continuation = continuation
}
}

func flush() async {
await didFlush!(buffer!.load(limit: .max))
func cancel() {

}

func flush() async {
await continuation?.yield(buffer!.load(limit: .max))
}
}

class LoggerBundlerTests: XCTestCase {
Expand Down

0 comments on commit 54650f9

Please sign in to comment.