Skip to content

Disable printing by default for DebugReducer when running SwiftUI Previews #1625

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

Merged
merged 3 commits into from
Nov 5, 2022

Conversation

tgrapperon
Copy link
Contributor

I had a case where one of my features was lagging a lot without any apparent reason in a SwiftUI Preview. The motive became immediately clear when I launched a dedicated app and saw the console filled by a very large amount of data from a _printChanges() that I forgot.

I've added a preliminary check to the default _ReducerPrinter so they don't generate any output when running SwiftUI Previews. As this only affects the default printers, this behavior can be overridden by users willing to provide their own _ReducerPrinter implementation (for example with variants capable of displaying this information in SwiftUI Previews like experimented in #1234).

This should make SwiftUI Previews a little snappier when a ._printChanges() has been forgotten.

Please let me know what you think!

@stephencelis
Copy link
Member

Good to know! We typically don't use this modifier outside the app entry point, but know it's common enough for folks to do, so avoiding pitfalls like this sounds good to us!

}
}
let store = TestStore(initialState: 0, reducer: DebuggedReducer()._printChanges())
store.dependencies.context = .preview
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@stephencelis We can't really test the behavior directly, as there is no way to override at distance the default TextOuputStream from print() to my knowledge. Maybe there are some way to read stdout, but that seems overkill.

Switching this value to .live shows that something is printed, and setting it back to .preview shows that nothing is printed, like for the test above that runs in .test context.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah yeah. We should prob allow more customization in the future, but the API is underscored and barebones at the moment.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

there is no way to override at distance the default TextOuputStream from print()

@tgrapperon Back when you experimented with #1234 I had a go at just this :P

You can redirect stdout to Pipe using dup2

It took a little bit of trial and error but I ended up with something that worked okay. I haven't used it though. It was just a proof of concept.

iampatbrown/ConsoleView.swift

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@iampatbrown Super interesting! I'll give it a spin if I rework on #1234 idea. I'm currently using my Logger abstraction to duplicate prints to the UI console, but your approach could make things even simpler.

@tgrapperon
Copy link
Contributor Author

I refactored to use @Dependency(\.context) as a source of truth. This is indeed much nicer!

@iampatbrown
Copy link
Contributor

Is it possible to model _ReducerPrinter as a dependency?

@tgrapperon
Copy link
Contributor Author

tgrapperon commented Nov 4, 2022

@iampatbrown I think it was implemented as such at some point during the beta. But I'm not certain why it was rescinded. Likely because it wasn't worth the complexity at this stage. The whole feature is underscored after all.

@iampatbrown
Copy link
Contributor

@tgrapperon Ah k. That makes sense :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants