Skip to content
This repository was archived by the owner on Jul 2, 2021. It is now read-only.

UrbanCompass/Snail-Kotlin

Repository files navigation

Snail-Kotlin ๐ŸŒ CircleCI codecov

A lightweight observables framework, also available in Swift

Download

You can download a jar from GitHub's releases page.

Jitpack

allprojects {
 repositories {
    ...
    maven { url "https://jitpack.io" }
 }
}

dependencies {
  compile 'com.github.urbancompass:snail-kotlin:x.x.x'
}

Or Maven:

<dependency>
  <groupId>com.compass.snail</groupId>
  <artifactId>snail</artifactId>
  <version>0.0.1</version>
  <type>pom</type>
</dependency>

Creating Observables

val observable = Observable<thing>()

Subscribing to Observables

observable.subscribe(
        next = { thing in ... }, // do something with thing
        error = { error in ... }, // do something with error
        done = { ... } // do something when it's done
)

Closures are optional too...

observable.subscribe(
    next = { thing in ... } // do something with thing
)
observable.subscribe(
    error = { error in ... } // do something with error
)

Creating Observables Variables

val variable = Variable<whatever>(some initial value)
val optionalString = Variable<String?>(null)
optionalString.asObservable().subscribe(
    next = { string in ... } // do something with value changes
)

optionalString.value = "something"
val int = Variable<Int>(12)
int.asObservable().subscribe(
    next = { int in ... } // do something with value changes
)

int.value = 42

Miscellaneous Observables

val just = Just(1) // always returns the initial value (1 in this case)

val failure = Fail(RunTimeException()) // always returns error
failure.subscribe(
	error = { it }  //it is RuntimeException
)

val n = 5
let replay = Replay(n) // replays the last N events when a new observer subscribes

Threads

You can specify which thread an observables will be notified on by using .subscribe(thread: <desired thread>). If you don't specify, then the observable will be notified on the same thread that the observable published on.

There are 3 scenarios:

  1. You don't specify the thread. Your observer will be notified on the same thread as the observable published on.

  2. You specified MAIN thread AND the observable published on the MAIN thread. Your observer will be notified synchronously on the MAIN thread.

  3. You specified a thread. Your observer will be notified async on the specified thread.

Examples

Subscribing on EventThread.MAIN

observable.subscribe(EventThread.MAIN,
    next = { thing in ... }
)