{"id":1829,"url":"https://github.com/blendle/Hanson","last_synced_at":"2025-08-02T04:32:41.872Z","repository":{"id":55884574,"uuid":"80512326","full_name":"blendle/Hanson","owner":"blendle","description":"Lightweight observations and bindings in Swift","archived":false,"fork":false,"pushed_at":"2021-10-25T21:29:58.000Z","size":51,"stargazers_count":516,"open_issues_count":2,"forks_count":12,"subscribers_count":19,"default_branch":"master","last_synced_at":"2025-07-28T08:13:39.716Z","etag":null,"topics":["binding","kvc","kvo","observation","swift"],"latest_commit_sha":null,"homepage":null,"language":"Swift","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"isc","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/blendle.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2017-01-31T10:53:07.000Z","updated_at":"2025-06-30T20:31:12.000Z","dependencies_parsed_at":"2022-08-15T08:31:53.662Z","dependency_job_id":null,"html_url":"https://github.com/blendle/Hanson","commit_stats":null,"previous_names":[],"tags_count":6,"template":false,"template_full_name":null,"purl":"pkg:github/blendle/Hanson","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/blendle%2FHanson","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/blendle%2FHanson/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/blendle%2FHanson/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/blendle%2FHanson/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/blendle","download_url":"https://codeload.github.com/blendle/Hanson/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/blendle%2FHanson/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":268334618,"owners_count":24233793,"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","status":"online","status_checked_at":"2025-08-02T02:00:12.353Z","response_time":74,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"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","kvc","kvo","observation","swift"],"created_at":"2024-01-05T20:15:56.798Z","updated_at":"2025-08-02T04:32:41.617Z","avatar_url":"https://github.com/blendle.png","language":"Swift","funding_links":[],"categories":["Reactive Programming","Swift"],"sub_categories":["Other free courses","Prototyping","Other Parsing"],"readme":"\u003cp align=\"center\"\u003e\n    \u003cimg width=\"900px\" src=\"https://cloud.githubusercontent.com/assets/8394738/22552794/a4489eb6-e95a-11e6-90bf-37b7ccbf3fab.png\"\u003e\n\u003c/p\u003e\n\n\u003cp align=\"center\"\u003e\n\t\u003cimg src=\"https://travis-ci.org/blendle/Hanson.svg?style=flat)]\" /\u003e\n\t\u003cimg src=\"https://img.shields.io/badge/language-swift-orange.svg\"\n         alt=\"Language: Swift\" /\u003e\n    \u003cimg src=\"https://img.shields.io/badge/platform-macos%20%7C%20ios%20%7C%20watchos%20%7C%20tvos-lightgrey.svg\"\n         alt=\"Platform: macOS | iOS | watchOS | tvOS\" /\u003e\n    \u003cimg src=\"https://img.shields.io/badge/Carthage-compatible-4BC51D.svg?style=flat\"\n         alt=\"Carthage\" /\u003e\n    \u003cimg src=\"https://img.shields.io/badge/license-ISC-000000.svg\"\n         alt=\"License\" /\u003e\n\u003c/p\u003e\n\n## What is Hanson?\n\nHanson is a simple, lightweight library to observe and bind values in Swift. It's been developed to support the MVVM architecture in our [Blendle iOS app](https://itunes.apple.com/nl/app/blendle/id947936149). Hanson provides several advantages to using KVO in Swift, such as a Swiftier syntax, no boilerplate code, and the ability to use it in pure Swift types.\n\n## Example Usage\n\nThe most basic use case is to simply observe an `Observable` for changes:\n```swift\nlet observable = Observable(\"Hello World\")\nobserve(observable) { event in\n    // Invoked whenever observable.value is set.\n    print(\"Value changed from \\(event.oldValue) to \\(event.newValue)\")\n}\n```\n\nHanson also provides a wrapper around KVO, so you can do the following to observe a `UITextField`'s `text` property for changes:\n```swift\nlet textField = UITextField()\nlet textFieldObservable = textField.dynamicObservable(keyPath: #keyPath(UITextField.text), type: String.self)\nobserve(textFieldObservable) { event in\n    print(\"Text field value changed from \\(event.oldValue) to \\(event.newValue)\")\n}\n```\n\nFurthermore, you can also use Hanson to bind an observable to another observable. Let's say we have a view model that's responsible for loading data, and we want the view to show an activity indicator while the view model is loading data:\n```swift\nclass ViewModel {\n\n    let isLoadingData = Observable(false)\n\n}\n\nclass View {\n\n    let showsActivityIndicator = Observable(false)\n\n}\n\nlet viewModel = ViewModel()\nlet view = View()\nbind(viewModel.isLoadingData, to: view.showsActivityIndicator)\n```\n\nNow, whenever the view model's `isLoadingData` property is set to a different value, it will automatically be set to the view's `showsActivityIndicator` property.\n\nBinding is also supported from and to KVO-backed observables. To bind a text field's content to a label:\n```swift\nlet textField = UITextField()\nlet textFieldObservable = textField.dynamicObservable(keyPath: #keyPath(UITextField.text), type: String.self)\n\nlet label = UILabel()\nlet labelObservable = label.dynamicObservable(keyPath: #keyPath(UILabel.text), type: String.self)\n\nbind(textFieldObservable, to: labelObservable)\n```\n\nIf you want to handle the binding yourself, you can also provide a closure that will be invoked when a new value should be set. In the following example, we'll bind an `isLoadingData` observable to a `UIActivityIndicatorView`:\n```swift\nlet isLoadingData = Observable(false)\nlet activityIndicatorView = UIActivityIndicatorView()\nbind(isLoadingData, to: activityIndicatorView) { activityIndicatorView, isLoadingData in\n    if isLoadingData {\n        activityIndicatorView.startAnimating()\n    } else {\n        activityIndicatorView.stopAnimating()\n    }\n}\n```\n\nHanson also supports observering notifications sent through a `NotificationCenter`. For example, to observe when an application is entering the background:\n```swift\nlet observable = NotificationCenter.default.observable(for: Notification.Name.UIApplicationDidEnterBackground)\nobserve(observable) { notification in\n    print(\"Application did enter background\")\n}\n```\n\n### Schedulers\n\nSchedulers can be used to schedule the events of your observation. By default, Hanson uses the `CurrentThreadScheduler`, which immediatly sends events on whatever thread it currently is. Hanson also offers the `MainThreadScheduler`, which ensures events are sent on the main thread. This is useful when observing a value that can change from a background thread and you want to do UI changes based on that value. For example:\n\n```swift\nlet observable = Observable(\"Hello World\")\nobserve(observable, with: MainThreadScheduler()) { event in\n    // It's safe to do UI work here without calling DispatchQueue.main.async here\n}\n\nperformOnBackground {\n    observable.value = \"Hello from a background\"\n}\n```\n\nSchedulers are also supported when binding observables:\n\n```swift\nlet isLoadingData = Observable(true)\nlet activityIndicatorView = UIActivityIndicatorView()\nbind(isLoadingData, with: MainThreadScheduler(), to: activityIndicatorView) { activityIndicatorView, isLoadingData in\n    // It's safe to do UI work here without calling DispatchQueue.main.async here\n}\n\nperformOnBackground {\n    isLoadingData.value = false\n}\n```\n\nYou can create your own scheduler by conforming to the `EventScheduler` protocol.\n\n## Requirements\n\n* iOS 8.0+ / macOS 10.9+ / tvOS 9.0+\n* Xcode 8\n\n## Installation\n\nHanson is available through either [CocoaPods](http://cocoapods.org) or [Carthage](https://github.com/Carthage/Carthage).\n\n### Cocoapods\n\n1. Add `pod 'Hanson'` to your `Podfile`.\n2. Run `pod install`.\n\n### Carthage\n\n1. Add `github 'blendle/Hanson'` to your `Cartfile`.\n2. Run `carthage update`.\n3. Link the framework with your target as described in [Carthage Readme](https://github.com/Carthage/Carthage#adding-frameworks-to-an-application).\n\n### Swift Package Manager\n\n1. In Xcode, select your project and scroll to `Frameworks, Libraries, and Embedded Content`.\n2. Click the `+`.\n3. At the bottom of the frameworks and libraries window that opens, select `Add other...` and then `Add package dependency...`.\n4. Paste `https://github.com/blendle/Hanson.git` in the search textfield and follow through with the assistant.\n\n## Building\n\nThe project obviously builds fine through Xcode, just load up `Hanson.xcodeproj` and run it.\n\nFor convenience, we've included a few scripts and a `Makefile` that allow you to build Hanson from the command line and through continuous integration. They are inspired by GitHub's [Scripts to Rule Them All](https://github.com/github/scripts-to-rule-them-all) boilerplate:\n\n```\n|-- script/\n  |-- etc/\n    |-- config.sh   # Contains basic configuration parameters\n  |-- bootstrap     # Prepares the project\n  |-- setup         # Sets up the local building process\n  |-- test          # Runs tests locally\n  |-- cisetup       # Sets up the CI building process\n  |-- citest        # Runs tests in a CI environment\n```\n\nTo get started:\n\n`$ make`\n\nTo skip setup and immediately start testing:\n\n`$ make test`\n\nMake sure all tests pass before opening a Pull Request.\n\n## Release Notes\n\nSee [CHANGELOG.md](https://github.com/blendle/Hanson/blob/master/CHANGELOG.md) for a list of changes.\n\n## License\n\nHanson is released under the ISC license. See [LICENSE](https://github.com/blendle/Hanson/blob/master/LICENSE) for details.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fblendle%2FHanson","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fblendle%2FHanson","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fblendle%2FHanson/lists"}