Skip to content

Instantly share code, notes, and snippets.

@nalexn
Last active November 1, 2023 06:43
Show Gist options
  • Star 27 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save nalexn/33f14af1d163ea476ee499c0459824b2 to your computer and use it in GitHub Desktop.
Save nalexn/33f14af1d163ea476ee499c0459824b2 to your computer and use it in GitHub Desktop.
Collecting AnyCancellable tokens in declarative SwiftUI fashion
// 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
}
}
}
@yoni-g
Copy link

yoni-g commented May 2, 2020

Can you please share an example of how to use this?

@nalexn
Copy link
Author

nalexn commented May 2, 2020

Here you go

@nalexn
Copy link
Author

nalexn commented May 2, 2020

And here is the article where I elaborate on using this: https://nalexn.github.io/cancelbag-for-combine/

@yoni-g
Copy link

yoni-g commented May 13, 2020

THANKS!! real king 👑

@NunciosChums
Copy link

thank you

@filimo
Copy link

filimo commented May 18, 2020

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