Last active
November 1, 2023 06:43
-
-
Save nalexn/33f14af1d163ea476ee499c0459824b2 to your computer and use it in GitHub Desktop.
Collecting AnyCancellable tokens in declarative SwiftUI fashion
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Copyright © 2019 Alexey Naumov. MIT License | |
import Combine | |
typealias CancelBag = Set<AnyCancellable> | |
extension CancelBag { | |
mutating func collect(@Builder _ cancellables: () -> [AnyCancellable]) { | |
formUnion(cancellables()) | |
} | |
@_functionBuilder | |
struct Builder { | |
static func buildBlock(_ cancellables: AnyCancellable...) -> [AnyCancellable] { | |
return cancellables | |
} | |
} | |
} |
Here you go
And here is the article where I elaborate on using this: https://nalexn.github.io/cancelbag-for-combine/
THANKS!! real king 👑
thank you
Thank @nalexn
I've created the simple code to help others your nice idea.
https://gist.github.com/filimo/6f826b352f75cd1640e4e705340c0301
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Can you please share an example of how to use this?