{"id":18031724,"url":"https://github.com/dagronf/dsfvaluebinders","last_synced_at":"2025-03-27T05:30:53.755Z","repository":{"id":45503465,"uuid":"469566824","full_name":"dagronf/DSFValueBinders","owner":"dagronf","description":"Simple Swift shared value binders","archived":false,"fork":false,"pushed_at":"2024-08-06T06:49:47.000Z","size":47,"stargazers_count":6,"open_issues_count":0,"forks_count":2,"subscribers_count":1,"default_branch":"main","last_synced_at":"2024-10-30T03:59:34.344Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"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/dagronf.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}},"created_at":"2022-03-14T03:25:52.000Z","updated_at":"2024-08-06T06:28:43.000Z","dependencies_parsed_at":"2024-04-24T02:57:12.341Z","dependency_job_id":null,"html_url":"https://github.com/dagronf/DSFValueBinders","commit_stats":{"total_commits":20,"total_committers":1,"mean_commits":20.0,"dds":0.0,"last_synced_commit":"faa38ac769f8efdc73270730e56086775a26f655"},"previous_names":[],"tags_count":17,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dagronf%2FDSFValueBinders","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dagronf%2FDSFValueBinders/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dagronf%2FDSFValueBinders/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dagronf%2FDSFValueBinders/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/dagronf","download_url":"https://codeload.github.com/dagronf/DSFValueBinders/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":245791329,"owners_count":20672665,"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":[],"created_at":"2024-10-30T10:10:34.392Z","updated_at":"2025-03-27T05:30:53.173Z","avatar_url":"https://github.com/dagronf.png","language":"Swift","funding_links":[],"categories":[],"sub_categories":[],"readme":"# DSFValueBinders\n\n\u003cp align=\"center\"\u003e\n    \u003cimg src=\"https://img.shields.io/github/v/tag/dagronf/DSFValueBinders\" /\u003e\n    \u003cimg src=\"https://img.shields.io/badge/macOS-10.12+-red\" /\u003e\n    \u003cimg src=\"https://img.shields.io/badge/iOS-13+-blue\" /\u003e\n    \u003cimg src=\"https://img.shields.io/badge/tvOS-13+-orange\" /\u003e\n    \u003cimg src=\"https://img.shields.io/badge/mac Catalyst-supported-green\" /\u003e\n\u003c/p\u003e\n\n\u003cp align=\"center\"\u003e\n    \u003cimg src=\"https://img.shields.io/badge/Swift-5.3-orange.svg\" /\u003e\n    \u003cimg src=\"https://img.shields.io/badge/License-MIT-lightgrey\" /\u003e\n    \u003ca href=\"https://swift.org/package-manager\"\u003e\n        \u003cimg src=\"https://img.shields.io/badge/spm-compatible-brightgreen.svg?style=flat\" alt=\"Swift Package Manager\" /\u003e\n    \u003c/a\u003e\n\u003c/p\u003e\n\n## ValueBinder\n\nA ValueBinder creates a two-way binding object to allow sharing of a value between objects.\n\nThis is mildly similar to `@Binding` in SwiftUI but doesn't rely on SwiftUI - meaning it can be used pretty much anywhere Swift can.\n\n### Creating\n\nYou can define a binder using the standard initializer.\n\nThis initializer also allows you to supply a callback block that gets triggered when the `wrappedValue` changes.\n\n```swift\nlet countBinder = ValueBinder\u003cInt\u003e(0) { newValue in\n   Swift.print(\"countBinder changed: \\(newValue)\")\n}\n...\ncountBinder.wrappedValue = 4  // triggers the update callback\n```\n\n### Registering for change updates\n\nYou can hand your ValueBinding object to another class which can supply a block to be called when the `ValueBinder` wrapped value changes.\n\n```swift\nclass AnotherClass {\n   init(_ binder: ValueBinder\u003cInt\u003e) {\n      binder.register(self) { newValue in\n         Swift.print(\"Binding detected change: \\(newValue)\")\n      }\n   }\n}\n```\n\nAdditionally, if you hold on to the binder object your class can update the ValueBinder value too!\n\n```swift\nclass AnotherClass {\n   let countBinder: ValueBinder\u003cInt\u003e \n   init(_ binder: ValueBinder\u003cInt\u003e) {\n      countBinder = binder\n      countBinder.register(self) { newValue in\n         Swift.print(\"Binding detected change: \\(newValue)\")\n      }\n   }\n   \n   func userPressed() {\n      countBinder.wrappedValue += 1\n   }\n}\n```\n\n### Updating the ValueBinder value\n\nAny object that holds a `ValueBinder` object can update the wrapped value. \n\n```swift\n_countBinder.wrappedValue += 1\n```\n\nAll objects that have registered for change callbacks will be notified of the change in value.\n\n### `ValueBinding` PropertyWrapper\n\n`ValueBinding` is a property wrapper implementation for the `ValueBinder` type.\nThanks to [Mx-Iris](https://github.com/Mx-Iris) for sharing their implementation. \n\n#### Example\n\n```swift\n@ValueBinding var countValue = 0\n\n// Register a block for updates\n$countValue.register { newValue in \n   Swift.print(\"countValue is now \\(newValue)\")\n}\n\ncountValue = 4  // triggers the update callback\n// prints \"countValue is now 4\"\n\n// Register for combine updates\n$countValue.publisher?.publisher\n   .receive(on: DispatchQueue.global(qos: .background))\n   .sink { newValue in\n      // Do something with 'newValue'\n   }\n   .store(in: \u0026subscribers)\n```\n\n## KeyPathBinder\n\nA `KeyPathBinder` is a specialization of the `ValueBinder` that can track a dynamic keypath\n\n```swift\n// The dynamic property to bind to. This might be (for example) bound to a control from interface builder.\n@objc dynamic var state: NSControl.State = .on\n\n// Our binding object\nlazy var boundKeyPath: KeyPathBinder\u003cMyViewController, NSControl.StateValue\u003e = {\n   return try! .init(self, keyPath: \\.buttonState) { newValue in\n      Swift.print(\"boundKeyPath notifies change: \\(String(describing: newValue))\")\n   }\n}()\n```\n\n## EnumKeyPathBinder\n\nAn `EnumKeyPathBinder` is a keypath binder for observing Swift `enum` types.\n\nI had a situation where I was tring to use a `KeyPathBinder` on the size mode for a toolbar which is of type \n`NSToolbar.SizeMode`, and it continually failed. However, binding to `NSControl.StateValue` on a control worked fine.\n\nThe result was that `NSControl.StateValue`, while appearing _like_ an enum is actually a struct, whereas `NSToolbar.SizeMode`\nis an enum (specifically, a RawRepresentable).  The issue appears when trying to observe enum type changes, so this class\nis a specialization of `KeyPathBinder` specifically for observing such enum types.\n\n## Combine\n\nBoth binder types expose a property `publisher` which you can hook up to your combine workflow.\n\nIf the OS doesn't support Combine, the `publisher` property will be nil.\n\n```swift\nlet binder = ValueBinder(0) \n...\nlet cancellable = binder.publisher?.sink { newValue in\n   // do something with `newValue`\n}\n```\n\n## License\n\n```\nMIT License\n\nCopyright (c) 2023 Darren Ford\n\nPermission is hereby granted, free of charge, to any person obtaining a copy of this software\nand associated documentation files (the \"Software\"), to deal in the Software without\nrestriction, including without limitation the rights to use, copy, modify, merge, publish,\ndistribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the\nSoftware 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\nsubstantial portions of the Software.\n\nTHE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING\nBUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND\nNONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,\nDAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\nOUT 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%2Fdagronf%2Fdsfvaluebinders","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fdagronf%2Fdsfvaluebinders","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdagronf%2Fdsfvaluebinders/lists"}