Skip to content

Instantly share code, notes, and snippets.

@DandyLyons
Last active March 29, 2024 17:50
Show Gist options
  • Save DandyLyons/c185418ec296923bdaced1bcef951c44 to your computer and use it in GitHub Desktop.
Save DandyLyons/c185418ec296923bdaced1bcef951c44 to your computer and use it in GitHub Desktop.
Filter ._printChanges() so that it only prints what you want
import ComposableArchitecture
extension _ReducerPrinter {
public static func filtered(actions includingActions: @escaping (Action) -> Bool) -> Self {
Self { receivedAction, oldState, newState in
if includingActions(receivedAction) {
// repeated from _ReducerPrinter.customDump
// TODO: Return a custom dump so that you don't need to repeat code
var target = ""
target.write("received action:\n")
CustomDump.customDump(receivedAction, to: &target, indent: 2)
target.write("\n")
target.write(diff(oldState, newState).map { "\($0)\n" } ?? " (No state changes)\n")
print(target)
}
}
}
public static func filtered(changes includingChanges: @escaping (_ oldState: State, _ newState: State) -> Bool) -> Self {
Self { receivedAction, oldState, newState in
if includingChanges(oldState, newState) {
// repeated from _ReducerPrinter.customDump
// TODO: Return a custom dump so that you don't need to repeat code
var target = ""
target.write("received action:\n")
CustomDump.customDump(receivedAction, to: &target, indent: 2)
target.write("\n")
target.write(diff(oldState, newState).map { "\($0)\n" } ?? " (No state changes)\n")
print(target)
}
}
}
}
// MARK: Example Usage
// SharedVALUEAudioPlayerReducer()
//// Print only certain actions
// ._printChanges(
// .filtered(
// actions: { action in
// switch action {
// case .childPlayer(.updateProgress):
// return false
// default: return true
// }
// }
// )
// )
//
//// Print only certain changes
// ._printChanges(
// .filtered(
// changes: { oldState, newState in
// // only print when sheetPlayer changes
// oldState.sheetPlayer != newState.sheetPlayer
// }
// )
// )
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment