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

[iOS] move logger into context to survive rerenders #110

Merged
merged 1 commit into from
Dec 14, 2022
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
11 changes: 6 additions & 5 deletions ios/packages/swiftui/Sources/SwiftUIPlayer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ public struct SwiftUIPlayer: View, HeadlessPlayer {

private var contextBuilder: () -> JSContext
private let partialMatchPlugin = PartialMatchFingerprintPlugin()
public let logger = TapableLogger()
private var flow: String?
private var registryWatch: AnyCancellable?
private var state: BaseFlowState?
Expand All @@ -50,16 +51,16 @@ public struct SwiftUIPlayer: View, HeadlessPlayer {
/// Load the supplied flow into this context. If the currently loaded flow is supplied this will do nothing.
/// If a new flow is supplied then the javascript environment is created or rebuilt around the new flow.
fileprivate func load(flow: String, plugins: [NativePlugin], player: SwiftUIPlayer) {
registry.logger = player.logger
registry.logger = logger
guard self.player == nil || flow != self.flow else {
player.logger.d("Reusing already loaded flow")
logger.d("Reusing already loaded flow")
return
}

let context: JSContext = contextBuilder()
let allPlugins = plugins + [partialMatchPlugin]
guard let playerValue = player.setupPlayer(context: context, plugins: allPlugins) else {
return player.logger.e("Failed to load player")
return logger.e("Failed to load player")
}

let hooks = SwiftUIPlayerHooks(from: playerValue)
Expand All @@ -82,7 +83,7 @@ public struct SwiftUIPlayer: View, HeadlessPlayer {
}

guard !flow.trimmingCharacters(in: .whitespacesAndNewlines).isEmpty else {
player.logger.d("Empty flow, not loading")
logger.d("Empty flow, not loading")
return
}

Expand Down Expand Up @@ -154,7 +155,7 @@ public struct SwiftUIPlayer: View, HeadlessPlayer {
@Binding private var result: Result<CompletedState, PlayerError>?
private let unloadOnDisappear: Bool
/// A reference to the shared logger
public let logger = TapableLogger()
public var logger: TapableLogger { context.logger }

/// A read only reference to the platform shared core player value in the `JSContext`
public var jsPlayerReference: JSValue? { context.player }
Expand Down