{"id":16684983,"url":"https://github.com/roberthein/observable","last_synced_at":"2025-04-05T10:09:57.615Z","repository":{"id":55474112,"uuid":"100533210","full_name":"roberthein/Observable","owner":"roberthein","description":"The easiest way to observe values in Swift.","archived":false,"fork":false,"pushed_at":"2020-12-28T17:09:32.000Z","size":4943,"stargazers_count":372,"open_issues_count":4,"forks_count":38,"subscribers_count":10,"default_branch":"master","last_synced_at":"2024-10-13T14:45:54.646Z","etag":null,"topics":["carthage","cocoapods","easy","functional","generic","generics","libraries","library","observable","observe","observer","programming","reactive","swift","swift-5","swift5","value","values"],"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/roberthein.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}},"created_at":"2017-08-16T21:14:38.000Z","updated_at":"2024-10-06T13:06:39.000Z","dependencies_parsed_at":"2022-08-15T01:10:34.346Z","dependency_job_id":null,"html_url":"https://github.com/roberthein/Observable","commit_stats":null,"previous_names":[],"tags_count":16,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/roberthein%2FObservable","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/roberthein%2FObservable/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/roberthein%2FObservable/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/roberthein%2FObservable/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/roberthein","download_url":"https://codeload.github.com/roberthein/Observable/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247318745,"owners_count":20919484,"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":["carthage","cocoapods","easy","functional","generic","generics","libraries","library","observable","observe","observer","programming","reactive","swift","swift-5","swift5","value","values"],"created_at":"2024-10-12T14:45:41.359Z","updated_at":"2025-04-05T10:09:57.590Z","avatar_url":"https://github.com/roberthein.png","language":"Swift","readme":"\u003cp align=\"center\"\u003e\n    \u003cimg src=\"art/header.png\" width=\"890\" alt=\"Observable\"/\u003e\n\u003c/p\u003e\n\n**Observable** is the easiest way to observe values in Swift.\n\n## How to\n### Create an Observable and MutableObservable \nUsing `MutableObservable` you can create and observe event.\nUsing `Observable` you can observe event, in order to avoid side-effects on our internal API. \n```swift\nclass SomeViewModel {\n    /// Public property, that can be read / observed by external classes (e.g. view controller), but not changed.\n    var position: Observable\u003cCGPoint\u003e = {\n        return positionSubject\n    }\n    // Or use the helper method Observable.asObservable()\n    // lazy var position = positionSubject.asObservable()\n\n    /// Private property, that can be changed / observed inside this view model.\n    private let positionSubject = MutableObservable(CGPoint.zero)\n}\n```\n\n### Create Observer with custom onDispose functionality\n\nIn some cases Observables require resources while they're active that must be cleaned up when they're disposed of.  To handle such cases you can pass an optional block to the Observable initializer to be executed when the Observable is disposed of.\n\n```swift\nurl.startAccessingSecurityScopedResource()\nlet observable = Observable([URL]()) {\n    url.stopAccessingSecurityScopedResource()\n}\n```\n\n### Model Properties as @MutableObservable\n\nNow mark your binded/mapped properties as observable and export public observable\n\n```swift\n//Private Observer\n@MutableObservable var text: String = \"Test\"\n\n//add observer\n\n_text.observe { (newValue, oldValue) in\n    print(newValue)\n}.add(to: \u0026disposable)\n        \n//Public Observer\n\nvar textObserve: ImmutableObservable\u003cString\u003e {\n    return _text\n}\n\n```\n### Add an observer\n\n```swift\nposition.observe { p in\n    // handle new position\n}\n```\n\n### Add an observer and specify the DispatchQueue\n\n```swift\nposition.observe(DispatchQueue.main) { p in\n// handle new position\n}\n```\n\n### Change the value\n\n```swift\nposition.wrappedValue = p\n```\n\n### Stop observing new values\n\n```swift\nposition.observe {\n    // This will stop all observers added to `disposal`\n    self.disposal.dispose()\n}.add(to: \u0026disposal)\n\n```\n\n## Memory management\n\nFor a single observer you can store the returned `Disposable` to a variable\n\n```swift\ndisposable = position.observe { p in\n\n```\n\nFor multiple observers you can add the disposable to a `Disposal` variable\n\n```swift\nposition.observe { }.add(to: \u0026disposal)\n```\n\nAnd always weakify `self` when referencing `self` inside your observer\n\n```swift\nposition.observe { [weak self] position in\n```\n\n## Installation\n\n### CocoaPods\n\n**Observable** is available through [CocoaPods](http://cocoapods.org). To install\nit, simply add the following line to your Podfile:\n\n```ruby\npod 'Observable'\n```\n\n### Swift Package Manager\n\n**Observable** is available through `Swift Package Manager`.\n[Swift Package Manager](https://swift.org/package-manager/) (SwiftPM) is a tool for automating the distribution of Swift code. \nIt is integrated into the swift compiler and from Xcode 11, SwiftPM got natively integrated with Xcode.\n\n```swift\ndependencies: [\n    .package(url: \"https://github.com/roberthein/Observable\", from: \"VERSION\")\n]\n```\n\n## Migrations\n\n### 1.x.y to 2.0.0\n- `Observable` is now `MutableObservable`\n- `ImmutableObservable` is now `Observable`\n- `Observable.asImmutableObservable()` is now `Observable.asObservable()`\n- `Observable.value` is now `Observable.wrappedValue`\n\n\n## Suggestions or feedback?\n\nFeel free to create a pull request, open an issue or find [me on Twitter](https://twitter.com/roberthein).\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Froberthein%2Fobservable","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Froberthein%2Fobservable","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Froberthein%2Fobservable/lists"}