Last active
January 29, 2024 07:01
-
-
Save swhitty/bdfc75b02fb447279180496577aa7c07 to your computer and use it in GitHub Desktop.
Revisions
-
swhitty revised this gist
Jan 29, 2024 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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 types that conform to `ObservableObject`") var wrappedValue: Element { get { fatalError() } set { fatalError() } -
swhitty revised this gist
Jan 29, 2024 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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 { get { instance[keyPath: storageKeyPath]._wrappedValue } -
swhitty revised this gist
Jan 29, 2024 . 1 changed file with 3 additions and 4 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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 where T.ObjectWillChangePublisher == ObservableObjectPublisher { { get { instance[keyPath: storageKeyPath]._wrappedValue } set { guard instance[keyPath: storageKeyPath]._wrappedValue != newValue else { return } instance.objectWillChange.send() instance[keyPath: storageKeyPath]._wrappedValue = newValue } } -
swhitty revised this gist
Jan 29, 2024 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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 of unique changes @propertyWrapper struct UniqueChanges<Element: Equatable> { -
swhitty revised this gist
Jan 29, 2024 . No changes.There are no files selected for viewing
-
swhitty created this gist
Jan 29, 2024 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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 } } }