Skip to content

Commit

Permalink
fix logger bundler warning
Browse files Browse the repository at this point in the history
  • Loading branch information
k-kohey committed Nov 10, 2022
1 parent 93c41f8 commit e4035d6
Show file tree
Hide file tree
Showing 7 changed files with 27 additions and 42 deletions.
4 changes: 2 additions & 2 deletions Sources/Parchment/BufferRecord.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public struct BufferRecord: Loggable, LoggerSendable, Equatable {
public let id: String
public let destination: String
public let eventName: String
public let parameters: [String: Any]
public let parameters: [String: Sendable]
public let timestamp: Date

public init(id: String = UUID().uuidString, destination: String, event: Loggable, timestamp: Date) {
Expand All @@ -26,7 +26,7 @@ public struct BufferRecord: Loggable, LoggerSendable, Equatable {
id: String = UUID().uuidString,
destination: String,
eventName: String,
parameters: [String: Any],
parameters: [String: Sendable],
timestamp: Date
) {
self.id = id
Expand Down
8 changes: 4 additions & 4 deletions Sources/Parchment/Loggable.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,16 @@

import Foundation

public protocol Loggable {
public protocol Loggable: Sendable {
var eventName: String { get }
var parameters: [String: Any] { get }
var parameters: [String: Sendable] { get }
}

public struct TrackingEvent: Loggable {
public let eventName: String
public let parameters: [String: Any]
public let parameters: [String: Sendable]

public init(eventName: String, parameters: [String: Any]) {
public init(eventName: String, parameters: [String: Sendable]) {
self.eventName = eventName
self.parameters = parameters
}
Expand Down
6 changes: 3 additions & 3 deletions Sources/Parchment/LoggableDictonary.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,14 @@ import Foundation

/// Experimental API

public typealias LoggableDictonary = [PartialKeyPath<Loggable>: Any]
public typealias LoggableDictonary = [PartialKeyPath<Loggable>: Sendable]

extension LoggableDictonary: Loggable {
public var eventName: String {
self[\.eventName] as? String ?? ""
}

public var parameters: [String: Any] {
self[\.parameters] as? [String: Any] ?? [:]
public var parameters: [String: Sendable] {
self[\.parameters] as? [String: Sendable] ?? [:]
}
}
24 changes: 5 additions & 19 deletions Sources/Parchment/LoggerBundler.swift
Original file line number Diff line number Diff line change
Expand Up @@ -83,28 +83,14 @@ public final class LoggerBundler {
}
}

public func startLogging() {
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 await self.flushStrategy.schedule(with: self.buffer) {
await self.bloadcast(records)
}
} catch {
// console()?.log("\(error.localizedDescription)")
public func startLogging() -> Task<Void, Error> {
Task {
for try await records in await flushStrategy.schedule(with: self.buffer) {
await self.bloadcast(records)
}
}
}

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

private func bloadcast(_ records: [BufferRecord]) async {
let recordEachLogger = Dictionary(grouping: records) { record in
record.destination
Expand Down Expand Up @@ -156,7 +142,7 @@ public extension LoggerBundler {
await send(event, with: option)
}

func send(event: [PartialKeyPath<Loggable>: Any], with option: LoggingOption = .init()) async {
func send(event: [PartialKeyPath<Loggable>: Sendable], with option: LoggingOption = .init()) async {
await send(event, with: option)
}
}
Expand Down
23 changes: 11 additions & 12 deletions Sources/ParchmentDefault/RegularlyPollingScheduler.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,8 @@ public struct RegularlyPollingScheduler: BufferedEventFlushScheduler, Sendable {

public func schedule(with buffer: TrackingEventBufferAdapter) async -> AsyncThrowingStream<[BufferRecord], Error> {
AsyncThrowingStream { continuation in
Task {
while true {
if Task.isCancelled {
continuation.finish()
break
}
let pollingTask = Task {
while !Task.isCancelled {
let records = await buffer.load()
continuation.yield(records)
do {
Expand All @@ -37,14 +33,11 @@ public struct RegularlyPollingScheduler: BufferedEventFlushScheduler, Sendable {
break
}
}
continuation.finish()
}

Task {
while true {
if Task.isCancelled {
continuation.finish()
break
}
let checkLimitationTask = Task {
while !Task.isCancelled {
let count = await buffer.count()
if limitOnNumberOfEvent < count {
let records = await buffer.load()
Expand All @@ -57,6 +50,12 @@ public struct RegularlyPollingScheduler: BufferedEventFlushScheduler, Sendable {
break
}
}
continuation.finish()
}

continuation.onTermination = { @Sendable _ in
pollingTask.cancel()
checkLimitationTask.cancel()
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion Tests/ParchmentDefaultTests/MutationTest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ private enum Event: Loggable {
}
}

var parameters: [String: Any] {
var parameters: [String: Sendable] {
switch self {
case .hoge:
return ["hello": "world"]
Expand Down
2 changes: 1 addition & 1 deletion Tests/ParchmentTests/LoggerBundlerTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ class LoggerBundlerTests: XCTestCase {
loggingStrategy: strategy
)

bundler.startLogging()
_ = bundler.startLogging()

// workaround
// bundler is released, hcausing the test to fail
Expand Down

0 comments on commit e4035d6

Please sign in to comment.