snail
A lightweight observables framework, also available in Kotlin
Installation
Carthage
You can install Carthage with Homebrew using the following command:
To integrate Snail into your Xcode project using Carthage, specify it in your Cartfile
where "x.x.x"
is the current release:
Swift Package Manager
To install using Swift Package Manager have your Swift package set up, and add Snail as a dependency to your Package.swift
.
Manually
Add all the files from Snail/Snail
to your project
Developing Locally
- Run the setup script to install required dependencies
./scripts/setup.sh
Creating Observables
Disposer
A disposer is in charge of removing all the subscriptions. A disposer is usually located in a centralized place where most of the subscriptions happen (ie: UIViewController in an MVVM architecture). Since most of the subscriptions are to different observables, and those observables are tied to type, all the things that are going to be disposed need to comform to Disposable
.
If the Disposer
helps get rid of the closures and prevent retention cycles (see weak self section). For the sake of all the examples, let's have a disposer created:
Closure Wrapper
The main usage for the Disposer
is to get rid of subscription closures that we create on Observables
, but the other usage that we found handy, is the ability to dispose of regular closures. As part of the library, we created a small Closure
wrapper class that complies with Disposable
. This way you can wrap simple closures to be disposed.
Please note that this would not dispose of the closureCall
reference to closure, it would only Dispose the content of the Closure
.
Subscribing to Observables
Closures are optional too...
Creating Observables Variables
Combining Observable Variables
Miscellaneous Observables
Operators
Snail provides some basic operators in order to transform and operate on observables.
-
map
: This operator allows to map the value of an obsverable into another value. Similar tomap
onCollection
types. -
filter
: This operator allows filtering out certain values from the observable chain. Similar tofilter
onCollection
types. You simply returntrue
if the value should be emitted andfalse
to filter it out. -
flatMap
: This operator allows mapping values into other observables, for example you may want to create an observable for a network request when a user tap observable emits.
Subscribing to Control Events
Queues
You can specify which queue an observables will be notified on by using .subscribe(queue: <desired queue>)
. If you don't specify, then the observable will be notified on the same queue that the observable published on.
There are 3 scenarios:
-
You don't specify the queue. Your observer will be notified on the same thread as the observable published on.
-
You specified
main
queue AND the observable published on themain
queue. Your observer will be notified synchronously on themain
queue. -
You specified a queue. Your observer will be notified async on the specified queue.
Examples
Subscribing on DispatchQueue.main
Weak self is optional
You can use [weak self]
if you want, but with the introduction of Disposer
, retention cycles are destroyed when calling disposer.disposeAll()
.
One idea would be to call disposer.disposeAll()
when you pop a view controller from the navigation stack.