Skip to content

Instantly share code, notes, and snippets.

@swhitty
Last active January 29, 2024 07:01
Show Gist options
  • Save swhitty/bdfc75b02fb447279180496577aa7c07 to your computer and use it in GitHub Desktop.
Save swhitty/bdfc75b02fb447279180496577aa7c07 to your computer and use it in GitHub Desktop.

Revisions

  1. swhitty revised this gist Jan 29, 2024. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion UniqueChanges.swift
    Original file line number Diff line number Diff line change
    @@ -6,7 +6,7 @@ struct UniqueChanges<Element: Equatable> {
    self._wrappedValue = wrappedValue
    }

    @available(*, unavailable, message: "@UniqueChanges can only be applied to ObservableObject instances")
    @available(*, unavailable, message: "@UniqueChanges can only be applied to types that conform to `ObservableObject`")
    var wrappedValue: Element {
    get { fatalError() }
    set { fatalError() }
  2. swhitty revised this gist Jan 29, 2024. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion UniqueChanges.swift
    Original file line number Diff line number Diff line change
    @@ -17,7 +17,7 @@ struct UniqueChanges<Element: Equatable> {
    // Classes get and set `wrappedValue` using this subscript.
    static subscript<T: ObservableObject>(_enclosingInstance instance: T,
    wrapped wrappedKeyPath: ReferenceWritableKeyPath<T, Element>,
    storage storageKeyPath: ReferenceWritableKeyPath<T, UniqueChanges>) -> Element where T.ObjectWillChangePublisher == ObservableObjectPublisher { {
    storage storageKeyPath: ReferenceWritableKeyPath<T, UniqueChanges>) -> Element where T.ObjectWillChangePublisher == ObservableObjectPublisher {
    get {
    instance[keyPath: storageKeyPath]._wrappedValue
    }
  3. swhitty revised this gist Jan 29, 2024. 1 changed file with 3 additions and 4 deletions.
    7 changes: 3 additions & 4 deletions UniqueChanges.swift
    Original file line number Diff line number Diff line change
    @@ -17,15 +17,14 @@ struct UniqueChanges<Element: Equatable> {
    // Classes get and set `wrappedValue` using this subscript.
    static subscript<T: ObservableObject>(_enclosingInstance instance: T,
    wrapped wrappedKeyPath: ReferenceWritableKeyPath<T, Element>,
    storage storageKeyPath: ReferenceWritableKeyPath<T, UniqueChanges>) -> Element {
    storage storageKeyPath: ReferenceWritableKeyPath<T, UniqueChanges>) -> Element where T.ObjectWillChangePublisher == ObservableObjectPublisher { {
    get {
    instance[keyPath: storageKeyPath]._wrappedValue
    }
    set {
    guard instance[keyPath: storageKeyPath]._wrappedValue != newValue,
    let subject = (instance.objectWillChange as Any) as? ObservableObjectPublisher else { return }
    guard instance[keyPath: storageKeyPath]._wrappedValue != newValue else { return }

    subject.send()
    instance.objectWillChange.send()
    instance[keyPath: storageKeyPath]._wrappedValue = newValue
    }
    }
  4. swhitty revised this gist Jan 29, 2024. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion UniqueChanges.swift
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,4 @@
    /// PropertyWrapper that can be applied to `Equatable` properties of `ObservableObject` to notify observers or unique changes
    /// PropertyWrapper that can be applied to `Equatable` properties of `ObservableObject` to notify observers of unique changes
    @propertyWrapper
    struct UniqueChanges<Element: Equatable> {

  5. swhitty revised this gist Jan 29, 2024. No changes.
  6. swhitty created this gist Jan 29, 2024.
    32 changes: 32 additions & 0 deletions UniqueChanges.swift
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,32 @@
    /// PropertyWrapper that can be applied to `Equatable` properties of `ObservableObject` to notify observers or unique changes
    @propertyWrapper
    struct UniqueChanges<Element: Equatable> {

    init(wrappedValue: Element) {
    self._wrappedValue = wrappedValue
    }

    @available(*, unavailable, message: "@UniqueChanges can only be applied to ObservableObject instances")
    var wrappedValue: Element {
    get { fatalError() }
    set { fatalError() }
    }

    private var _wrappedValue: Element

    // Classes get and set `wrappedValue` using this subscript.
    static subscript<T: ObservableObject>(_enclosingInstance instance: T,
    wrapped wrappedKeyPath: ReferenceWritableKeyPath<T, Element>,
    storage storageKeyPath: ReferenceWritableKeyPath<T, UniqueChanges>) -> Element {
    get {
    instance[keyPath: storageKeyPath]._wrappedValue
    }
    set {
    guard instance[keyPath: storageKeyPath]._wrappedValue != newValue,
    let subject = (instance.objectWillChange as Any) as? ObservableObjectPublisher else { return }

    subject.send()
    instance[keyPath: storageKeyPath]._wrappedValue = newValue
    }
    }
    }