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

Add standard instance to LoggerBundler #34

Merged
merged 3 commits into from
Jun 3, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 6 additions & 2 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,15 @@ let package = Package(
),
.testTarget(
name: "ParchmentTests",
dependencies: ["Parchment"]
dependencies: ["Parchment", "TestSupport"]
),
.testTarget(
name: "ParchmentDefaultTests",
dependencies: ["ParchmentDefault"]
dependencies: ["ParchmentDefault", "TestSupport"]
),
.target(
name: "TestSupport",
dependencies: ["Parchment"]
),
]
)
8 changes: 8 additions & 0 deletions Sources/ParchmentDefault/LoggerBundler+.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@

import Parchment

private let standardInstance = LoggerBundler.make(components: [])

public extension LoggerBundler {
static func make(
components: [any LoggerComponent],
Expand All @@ -21,4 +23,10 @@ public extension LoggerBundler {
mutations: mutations
)
}

/// This is a singleton instance.
/// Properties such as LoggerComponent and Mutation are empty, so set them as necessary.
static var `standard`: LoggerBundler {
standardInstance
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
// Created by Kohei Kawaguchi on 2023/05/17.
//

import Foundation
import Parchment

/// Logs changes to a value.
///
Expand Down Expand Up @@ -55,7 +55,7 @@ public struct Tracked<Value: Sendable, ScopeValue: Sendable> {
public init(
wrappedValue: Value,
name: String,
with logger: LoggerBundler,
with logger: LoggerBundler = .standard,
option: LoggerBundler.LoggingOption = .init()
) where ScopeValue == Never {
self.init(
Expand All @@ -66,7 +66,7 @@ public struct Tracked<Value: Sendable, ScopeValue: Sendable> {
public init(
wrappedValue: Value,
name: String,
with logger: LoggerBundler,
with logger: LoggerBundler = .standard,
scope: KeyPath<Value, ScopeValue>? = nil,
option: LoggerBundler.LoggingOption = .init()
) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
//

import SwiftUI
import Parchment

public struct ImpletionEvent: Loggable {
public var eventName = "ImpletionEvent"
Expand Down Expand Up @@ -39,7 +40,7 @@ public extension View {
/// Hook onAppear to send ImpletionEvent
func track(
screen name: String,
with logger: LoggerBundler,
with logger: LoggerBundler = .standard,
option: LoggerBundler.LoggingOption? = nil
) -> some View {
modifier(
Expand Down
36 changes: 18 additions & 18 deletions Tests/ParchmentTests/Stub.swift → Sources/TestSupport/Stub.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,41 +5,41 @@
// Created by Kohei Kawaguchi on 2023/05/17.
//

@testable import Parchment
import Parchment

private extension LoggerComponentID {
static let a = LoggerComponentID("A")
static let b = LoggerComponentID("B")
}

final class LoggerA: LoggerComponent, @unchecked Sendable {
static let id: LoggerComponentID = .a
public final class LoggerA: LoggerComponent, @unchecked Sendable {
public static let id: LoggerComponentID = .a

var _send: (([LoggerSendable]) -> (Bool))?
public var _send: (([LoggerSendable]) -> (Bool))?

func send(_ events: [LoggerSendable]) async -> Bool {
public func send(_ events: [LoggerSendable]) async -> Bool {
_send?(events) ?? true
}
}

final class LoggerB: LoggerComponent, @unchecked Sendable {
static let id: LoggerComponentID = .b
public final class LoggerB: LoggerComponent, @unchecked Sendable {
public static let id: LoggerComponentID = .b

var _send: (([LoggerSendable]) -> (Bool))?
public var _send: (([LoggerSendable]) -> (Bool))?

func send(_ events: [LoggerSendable]) async -> Bool {
public func send(_ events: [LoggerSendable]) async -> Bool {
_send?(events) ?? true
}
}

final class EventQueueMock: LogBuffer, @unchecked Sendable {
public final class EventQueueMock: LogBuffer, @unchecked Sendable {
private var records: [Payload] = []

func enqueue(_ e: [Payload]) {
public func enqueue(_ e: [Payload]) {
records += e
}

func dequeue(limit: Int?) async throws -> [Parchment.Payload] {
public func dequeue(limit: Int?) async throws -> [Parchment.Payload] {
let count: Int
if let limit {
count = limit
Expand All @@ -51,7 +51,7 @@ final class EventQueueMock: LogBuffer, @unchecked Sendable {
}
}

func count() -> Int {
public func count() -> Int {
records.count
}

Expand All @@ -65,25 +65,25 @@ final class EventQueueMock: LogBuffer, @unchecked Sendable {
}
}

final class BufferedEventFlushStrategyMock: BufferFlowController, @unchecked Sendable {
public final class BufferedEventFlushStrategyMock: BufferFlowController, @unchecked Sendable {
private var buffer: LogBuffer?

private var continuation: AsyncThrowingStream<[Payload], Error>.Continuation?

func input<T: LogBuffer>(_ payloads: [Payload], with buffer: T) async throws {
public func input<T: LogBuffer>(_ payloads: [Payload], with buffer: T) async throws {
try await buffer.enqueue(payloads)
}

func output<T: LogBuffer>(with buffer: T) async -> AsyncThrowingStream<[Payload], Error> {
public func output<T: LogBuffer>(with buffer: T) async -> AsyncThrowingStream<[Payload], Error> {
self.buffer = buffer
return .init { continuation in
self.continuation = continuation
}
}

func cancel() {}
public func cancel() {}

func flush() async {
public func flush() async {
let events = try! await buffer!.load()
continuation?.yield(events)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@

import XCTest
@testable import Parchment
@testable import ParchmentDefault
@testable import TestSupport

private let loggerA = LoggerA()
private var bundler = LoggerBundler(
Expand Down
1 change: 1 addition & 0 deletions Tests/ParchmentTests/LoggerBundlerTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
//

@testable import Parchment
@testable import TestSupport
import XCTest

@MainActor
Expand Down