Skip to content

Instantly share code, notes, and snippets.

@pakoito
Last active April 14, 2017 13:10
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pakoito/1c87fcdff328d78918113ac2fcd5e3a9 to your computer and use it in GitHub Desktop.
Save pakoito/1c87fcdff328d78918113ac2fcd5e3a9 to your computer and use it in GitHub Desktop.
Headless Development in Fully Reactive Apps
/*
* Copyright (c) pakoito 2017
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import com.pacoworks.dereference.ReplUtils.c
import com.pacoworks.dereference.ReplUtils.p
import com.pacoworks.dereference.ReplUtils.u
import com.pacoworks.dereference.ReplUtils.into
import com.pacoworks.dereference.ScreenState
import com.pacoworks.dereference.Idle
import com.pacoworks.dereference.Loading
import com.pacoworks.dereference.Success
import com.pacoworks.dereference.Failure
import com.pacoworks.dereference.Profile
import com.pacoworks.dereference.Email
import com.pacoworks.dereference.authIdle
import com.pacoworks.dereference.authLoading
import com.pacoworks.dereference.authSuccess
import com.pacoworks.dereference.authFailure
import com.jakewharton.rxrelay.PublishRelay
import rx.Observable
import rx.Subscriber
import rx.functions.Action1
// Initialize state
val state = c<ScreenState>(Idle)
val userInput = PublishRelay.create<Email>()
// Subscribe user input processing
authIdle(state, userInput, { email -> email.address.length > 5 }, state)
Email("Paco") into userInput
Email("Pacoworks") into userInput
// Subscribe server call that always fails and return to Idle
val errorSub = authLoading(state, { Observable.just(Failure("Wrong!")) }, state)
authFailure(state, state)
errorSub.unsubscribe()
// Subscribe server call that always succeeds and return to Idle
authLoading(state, { Observable.just(Success(Profile("Logged in!"))) }, state)
authSuccess(state, state)
/*
* Copyright (c) pakoito 2017
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import com.jakewharton.rxrelay.Relay
/**
* A [SerializedRelay] backed by a [BehaviorRelay] used to represent the value of one piece of state over time
*/
typealias StateHolder<T> = SerializedRelay<T, T>
/**
* Creates a new [StateHolder]
*/
fun <T: Any> createStateHolder(value: T): StateHolder<T> =
SerializedRelay(BehaviorRelay.create<T>(value))
object ReplUtils {
fun <T: Any> c(value: T, name: String = "StateHolder"): StateHolder<T> =
createStateHolder(value)
.apply {
zipWith(skip(1), { one, two -> one to two })
.subscribe { (one, two) -> println("$name updated!\nFROM:\n$one\nINTO:\n$two\n") }
}
fun <T: Any, U: Relay<T, T>> u(state: U, value: T): Unit =
state.call(value)
infix fun <T: Any, U: Relay<T, T>> T.into(state: U): Unit =
u(state, this)
fun <T: Any, U: Relay<T, T>> p(state: U): Unit =
println(state.toBlocking().first().toString())
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment