Skip to content
Discussion options

You must be logged in to vote

Would probably roll this way...

extension Container {
    var myRepo: Factory<MyRepo> {
        self { MyRepo() }.singleton
    }
    @MainActor
    var myViewModel: Factory<MyViewModel> {
        self { @MainActor in MyViewModel(container: self ) }
    }
}

@Observable
final class MyRepo {
    var hash = 0
    func increment() {
        hash += 1
    }
}

@MainActor
@Observable
final class MyViewModel {
    let myRepo: MyRepo
    init(container: Container) {
        self.myRepo = container.myRepo()
    }
}

struct MyView: View {
    @InjectedObservable(\.myViewModel) var vm: MyViewModel
    var body: some View {
        VStack {
            Text("hash \(vm.myRepo.hash)")
            Button(

Replies: 1 comment 3 replies

Comment options

You must be logged in to vote
3 replies
@hmlongco
Comment options

@mikeymike9000
Comment options

@mikeymike9000
Comment options

Answer selected by mikeymike9000
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants