-
Notifications
You must be signed in to change notification settings - Fork 1.5k
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
Conversation
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! |
Co-authored-by: Stephen Celis <[email protected]>
} | ||
} | ||
let store = TestStore(initialState: 0, reducer: DebuggedReducer()._printChanges()) | ||
store.dependencies.context = .preview |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
I refactored to use |
Is it possible to model |
@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. |
@tgrapperon Ah k. That makes sense :) |
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!