{"id":21886259,"url":"https://github.com/cookednick/watchable","last_synced_at":"2025-04-15T08:41:55.828Z","repository":{"id":46171933,"uuid":"348517845","full_name":"CookedNick/Watchable","owner":"CookedNick","description":"A generic ObservableObject for every property!","archived":false,"fork":false,"pushed_at":"2024-06-20T21:09:17.000Z","size":80,"stargazers_count":43,"open_issues_count":0,"forks_count":2,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-03-28T18:13:38.485Z","etag":null,"topics":["binding","combine","generic","observableobject","observedobject","publisher","swift","swiftui","view"],"latest_commit_sha":null,"homepage":"","language":"Swift","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/CookedNick.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2021-03-16T23:10:54.000Z","updated_at":"2025-02-18T13:59:30.000Z","dependencies_parsed_at":"2024-11-28T10:34:01.288Z","dependency_job_id":"5b679e53-28e3-4444-b51f-192c44f9bfb5","html_url":"https://github.com/CookedNick/Watchable","commit_stats":null,"previous_names":[],"tags_count":5,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/CookedNick%2FWatchable","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/CookedNick%2FWatchable/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/CookedNick%2FWatchable/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/CookedNick%2FWatchable/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/CookedNick","download_url":"https://codeload.github.com/CookedNick/Watchable/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":249038887,"owners_count":21202803,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["binding","combine","generic","observableobject","observedobject","publisher","swift","swiftui","view"],"created_at":"2024-11-28T10:33:52.208Z","updated_at":"2025-04-15T08:41:55.803Z","avatar_url":"https://github.com/CookedNick.png","language":"Swift","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Watchable\n\nA property wrapper type that creates an `ObservableObject` for just a single property.\n\nThis can have **enormous performance benefits** due to how SwiftUI recomputes views.\n\n## SwiftUI + Performance\n\nViews get called to update each time **any** of an object's properties change. Even if it's irrelevant to a given view.\n\nConsider the following common pattern of a typical custom view with one `@State` property.\n\n```swift\nstruct BedroomLabel: View {\n    @State var areTheLightsOn = false\n    var numberOfPeople = 0\n    \n    var body: some View {\n        VStack {\n            Text(\"This bedroom has \\(numberOfPeople) people in it.\")\n\n            Toggle(\"Lights On/Off\", isOn: $areTheLightsOn)\n        }\n    }\n}\n```\n\nThis is a very common pattern in SwiftUI, and it works alright, but the `BedroomLabel` will update in its entirety whenever the lights get toggled. This means the CPU spends *unnecessary* time checking the entire body whenever this happens.\n\n**How can we do better?**\n\n## One `ObservableObject` per Property\n\nHere is that same example written using `Watchable`.\n\n```swift\nstruct BedroomLabel: View {\n    @Watchable var areTheLightsOn = false\n    var numberOfPeople = 0\n    \n    var body: some View {\n        VStack {\n            Text(\"This bedroom has \\(numberOfPeople) people in it.\")\n            \n            Watching(bindingOf: $areTheLightsOn) { isOn in\n                Toggle(\"Lights On/Off\", isOn: isOn)\n            }\n        }\n    }\n}\n```\n\n`@Watchable` writes just like `@State`, except it stores the value in an implicit generic `ObservableObject` type. `Watching` takes care of unwrapping that and recomputing a view closure whenever its changed.\n\nThis means `BedroomLabel` won't be computed on changes. Only the `Toggle` will be.\n\n## Installation\n\nUse Swift Package Manager with this GitHub URL.\n\n## License\n\n````\nMIT License\n\nCopyright © 2021-2024 Nicolas Cook Leon\n\nPermission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:\n\nThe above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.\n\nTHE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.\n````\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcookednick%2Fwatchable","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fcookednick%2Fwatchable","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcookednick%2Fwatchable/lists"}