Skip to content

Commit

Permalink
[iOS] move logger into context to survive rerenders (#110)
Browse files Browse the repository at this point in the history
move logger into context to survive rerenders
  • Loading branch information
hborawski authored Dec 14, 2022
1 parent 6f7a8b3 commit f66def4
Showing 1 changed file with 6 additions and 5 deletions.
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

0 comments on commit f66def4

Please sign in to comment.