Skip to content

Commit

Permalink
Spaces -> tabs
Browse files Browse the repository at this point in the history
  • Loading branch information
Wouter01 committed Jan 18, 2024
1 parent 0c4a0f5 commit e9214de
Showing 1 changed file with 40 additions and 40 deletions.
80 changes: 40 additions & 40 deletions Sources/Defaults/SwiftUI.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,59 +6,59 @@ extension Defaults {
final class Observable<Value: Serializable>: ObservableObject {
private var cancellable: AnyCancellable?
private var task: Task<Void, Never>?
var key: Defaults.Key<Value> {
didSet {
if oldValue != key {
observe()
}
}
}
var key: Defaults.Key<Value> {
didSet {
if oldValue != key {
observe()
}
}
}

var value: Value {
get { Defaults[key] }
set {
Defaults[key] = newValue
}
}

init(_ key: Key<Value>) {
self.key = key

observe()
observe()
}

deinit {
task?.cancel()
}
func observe() {
// We only use this on the latest OSes (as of adding this) since the backdeploy library has a lot of bugs.
if #available(macOS 13, iOS 16, tvOS 16, watchOS 9, *) {
task?.cancel()
// The `@MainActor` is important as the `.send()` method doesn't inherit the `@MainActor` from the class.
task = .detached(priority: .userInitiated) { @MainActor [weak self, key] in
for await _ in Defaults.updates(key) {
guard let self else {
return
}

self.objectWillChange.send()
}
}
} else {
cancellable = Defaults.publisher(key, options: [.prior])
.sink { [weak self] change in
guard change.isPrior else {
return
}

Task { @MainActor in
self?.objectWillChange.send()
}
}
}
}
func observe() {
// We only use this on the latest OSes (as of adding this) since the backdeploy library has a lot of bugs.
if #available(macOS 13, iOS 16, tvOS 16, watchOS 9, *) {
task?.cancel()
// The `@MainActor` is important as the `.send()` method doesn't inherit the `@MainActor` from the class.
task = .detached(priority: .userInitiated) { @MainActor [weak self, key] in
for await _ in Defaults.updates(key) {
guard let self else {
return
}
self.objectWillChange.send()
}
}
} else {
cancellable = Defaults.publisher(key, options: [.prior])
.sink { [weak self] change in
guard change.isPrior else {
return
}
Task { @MainActor in
self?.objectWillChange.send()
}
}
}
}

/**
Reset the key back to its default value.
Expand Down

0 comments on commit e9214de

Please sign in to comment.