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] SwiftUIRegistry perf fix #104

Merged
merged 2 commits into from
Nov 22, 2022
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 6 additions & 5 deletions ios/packages/core/Sources/Types/Assets/BaseAssetRegistry.swift
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,6 @@ open class BaseAssetRegistry<WrapperType>: PlayerRegistry where
WrapperType: AssetContainer,
WrapperType.AssetType: Decodable {

/// A key for storing information in the decoder userInfo
// public static let decodeFunctionKey = CodingUserInfoKey(rawValue: "decodeFunction")!

/// A type representing an entry in the registry
public typealias RegistryEntry = (assetType: AssetType.Type, match: [String: Any])

Expand Down Expand Up @@ -107,8 +104,12 @@ open class BaseAssetRegistry<WrapperType>: PlayerRegistry where
if let index = registry.firstIndex(where: { matched in
return NSDictionary(dictionary: match).isEqual(to: matched.match)
}) {
logger?.w("Overriding registration for match: \(String(describing: match))")
registry[index] = (assetType: asset, match: match)
if asset == registry[index].assetType {
self.logger?.t("Duplicate Registration skipped for \(String(describing: match)) asset: \(String(describing: asset))")
} else {
self.logger?.w("Overriding registration for match: \(String(describing: match))")
registry[index] = (assetType: asset, match: match)
}
} else {
registry.append((assetType: asset, match: match))
}
Expand Down
6 changes: 3 additions & 3 deletions ios/packages/swiftui/Sources/SwiftUIPlayer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -193,9 +193,9 @@ public struct SwiftUIPlayer: View, HeadlessPlayer {
bodyContent
.environment(\.inProgressState, (state as? InProgressState))
// forward results from our Context along to our result binding
.onReceive(context.$result, perform: { result in
self.result = result
})
.onReceive(context.$result.debounce(for: 0.1, scheduler: RunLoop.main)) {
self.result = $0
}
.onReceive(inspection.notice) { self.inspection.visit(self, $0) }
.onDisappear {
guard unloadOnDisappear else { return }
Expand Down
30 changes: 30 additions & 0 deletions ios/packages/swiftui/Tests/SwiftUIRegistryTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,36 @@ import SwiftUI
// swiftlint:disable type_body_length file_length
class SwiftUIRegistryTests: XCTestCase {
let context: JSContext = JSContext()

func testDuplicateRegistration() {
let operationSkipped = expectation(description: "Skipped duplicate registration")
let override = expectation(description: "Asset Overridden")
operationSkipped.expectedFulfillmentCount = 2
let logger = TapableLogger()
logger.logLevel = .trace
logger.hooks.trace.tap(name: "test", { message in
guard message.contains("Duplicate Registration skipped for") else { return }
operationSkipped.fulfill()
})

logger.hooks.warn.tap(name: "test", { message in
guard message.contains("Overriding registration for match") else { return }
override.fulfill()
})
let registry = SwiftUIRegistry(logger: logger)
registry.register(["type": "action"], for: ActionAsset.self)
XCTAssertEqual(1, registry.registeredAssets.count)
registry.register(["type": "action"], for: ActionAsset.self)
XCTAssertEqual(1, registry.registeredAssets.count)
registry.register(["type": "text", "metaData": ["role": "someRole"]], for: TextAsset.self)
XCTAssertEqual(2, registry.registeredAssets.count)
registry.register(["type": "text", "metaData": ["role": "someRole"]], for: TextAsset.self)
XCTAssertEqual(2, registry.registeredAssets.count)
registry.register(["type": "text", "metaData": ["role": "someRole"]], for: ActionAsset.self)
XCTAssertEqual(2, registry.registeredAssets.count)
wait(for: [operationSkipped, override], timeout: 5)
}

func testDecodeWrappedAsset() {
let val = context.evaluateScript("({asset: {id: 'someId', type: 'text', value: 'someValue'}})")

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,29 +57,36 @@ class ExternalActionViewModifierPluginTests: ViewInspectorTestCase {
let extra: String? = state.extraProperty
XCTAssertEqual(extra, "extraValue")
handlerExpectation.fulfill()
return AnyView(Text("External State").onDisappear {
transition("Next")
return AnyView(Text("External State").onAppear {
DispatchQueue.main.asyncAfter(deadline: .now() + 1) {
transition("Next")
}
})
}

let player = SwiftUIPlayer(flow: json, plugins: [ReferenceAssetsPlugin(), plugin], result: Binding(get: {nil}, set: { (result) in
let context = SwiftUIPlayer.Context()

let player = SwiftUIPlayer(flow: json, plugins: [PrintLoggerPlugin(level: .debug), ReferenceAssetsPlugin(), plugin], result: Binding(get: {nil}, set: { (result) in
print("RESULT \(String(describing: result))")
switch result {
case .success:
completionExpectation.fulfill()
default:
break
}
}))
}), context: context, unloadOnDisappear: false)

ViewHosting.host(view: player)

let exp = player.inspection.inspect(after: 0.5) { view in
XCTAssertNotNil(plugin.state)
let content = try view.vStack().first?.anyView().anyView().modifier(ExternalStateSheetModifier.self).viewModifierContent()
try content?.sheet().anyView().text().callOnDisappear()
try content?.sheet().anyView().text().callOnAppear()
}

wait(for: [exp, handlerExpectation, completionExpectation], timeout: 10)
wait(for: [handlerExpectation], timeout: 5)
wait(for: [exp], timeout: 5)
wait(for: [completionExpectation], timeout: 5)
XCTAssertNil(plugin.state)

ViewHosting.expel()
Expand Down Expand Up @@ -130,8 +137,9 @@ class ExternalActionViewModifierPluginTests: ViewInspectorTestCase {
switch result {
case .success:
XCTFail("Should have failed")
default:
case .failure:
completionExpectation.fulfill()
default: break
}
}))

Expand Down