Skip to content

0x0c/StaticPublished

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

StaticPublished

StaticPublished is Swift Macro to avoid boilerplate codes to observe changes of static variables.

import Combine
import StaticPublished

struct Example {
    // Adding @StaticPublished macro to observe changes of the static variable.
    @StaticPublished
    static var input: Int = 10
}

// @StaticPublished generates `static let inputPublisher: AnyPublisher<Int, Never>`
// and we can observe changes of `input`.
var cancellable = Set<AnyCancellable>()
Example.inputPublisher.sink { newValue in
    print("receive \(newValue)")
}.store(in: &cancellable)

@StaticPublished expands this code

@StaticPublished
static var input: Int = 0

to below.

static var input: Int = 0 {
    didSet {
        _inputSubject.send(input)
    }
}

private static let _inputSubject = PassthroughSubject<Int, Never>()

static let inputPublisher = _inputSubject.eraseToAnyPublisher()

About

StaticPublished is Swift Macro to avoid boilerplate codes that observe changes of static variables.

Resources

License

Stars

Watchers

Forks

Packages

 
 
 

Contributors

Languages