Skip to content

Commit

Permalink
feat(redux-saga): move all effects to CommonEffects
Browse files Browse the repository at this point in the history
  • Loading branch information
protoman92 committed Jun 18, 2019
1 parent 27033df commit 71a6fde
Show file tree
Hide file tree
Showing 29 changed files with 205 additions and 234 deletions.
4 changes: 4 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -38,3 +38,7 @@ before_install:

script:
- ./gradlew build

after_script:
- eval "$(ssh-agent -s)"
- ssh-agent -k
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import org.junit.runner.RunWith
import org.robolectric.RobolectricTestRunner
import org.robolectric.annotation.Config
import org.swiften.redux.android.saga.rx.livedata.LiveDataEffects.takeLiveData
import org.swiften.redux.saga.common.SagaEffects.await
import org.swiften.redux.saga.common.CommonEffects.await
import org.swiften.redux.saga.common.SagaInput
import org.swiften.redux.saga.common.SagaMonitor
import org.swiften.redux.saga.common.flatMap
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,24 @@

package org.swiften.redux.saga.common

import io.reactivex.Flowable
import org.swiften.redux.core.IReduxAction
import kotlin.reflect.KClass

/** Created by haipham on 2019/01/07 */
/** Top-level namespace for [ISagaEffect] helpers */
/** Created by haipham on 2019/01/13 */
/** Top-level namespace for Rx-based [ISagaEffect] */
object CommonEffects {
/**
* Create an [AwaitEffect] instance.
* @param R The result emission type.
* @param creator See [AwaitEffect.creator].
* @return A [SingleSagaEffect] instance.
*/
@JvmStatic
fun <R> await(creator: IAwaitCreator<R>): AwaitEffect<R> where R : Any {
return AwaitEffect(creator)
}

/**
* Create a [DelayEffect].
* @param millis See [DelayEffect.millis].
Expand All @@ -20,6 +33,14 @@ object CommonEffects {
return DelayEffect(millis)
}

/**
* Create a [NothingEffect] instance.
* @param R The result emission type.
* @return A [SagaEffect] instance.
*/
@JvmStatic
fun <R> doNothing(): SagaEffect<R> where R : Any = NothingEffect()

/**
* Create a [FlatMapEffect] instance with [FlatMapEffect.Mode.EVERY].
* @param P The source emission type.
Expand All @@ -32,6 +53,73 @@ object CommonEffects {
return { FlatMapEffect(it, FlatMapEffect.Mode.EVERY, transformer) }
}

/**
* Create a [FromEffect].
* @param R The result emission type.
* @param stream See [FromEffect.stream].
* @return A [SagaEffect] instance.
*/
@JvmStatic
fun <R> from(stream: Flowable<R>): SagaEffect<R> where R : Any = FromEffect(stream)

/**
* Create an [AllEffect] instance.
* @param R The result emission type.
* @param sources See [AllEffect.sources].
* @return An [AllEffect] instance.
*/
@JvmStatic
fun <R> mergeAll(sources: Collection<SagaEffect<R>>): SagaEffect<R> where R : Any {
return AllEffect(sources)
}

/**
* Create an [AllEffect] instance.
* @param R The result emission type.
* @param sources See [AllEffect.sources].
* @return An [AllEffect] instance.
*/
@JvmStatic
fun <R> mergeAll(vararg sources: SagaEffect<R>): SagaEffect<R> where R : Any {
return mergeAll(sources.asList())
}

/**
* Create a [PutEffect].
* @param action See [PutEffect.action].
* @return A [PutEffect] instance.
*/
@JvmStatic
fun putInStore(action: IReduxAction): PutEffect {
return PutEffect(action)
}

/**
* Create a [SelectEffect].
* @param State The state type to select from.
* @param R The result emission type.
* @param cls See [SelectEffect.cls].
* @param selector See [SelectEffect.selector].
* @return A [SelectEffect] instance.
*/
@JvmStatic
fun <State, R> selectFromState(cls: Class<State>, selector: (State) -> R): SelectEffect<State, R> where R : Any {
return SelectEffect(cls, selector)
}

/**
* Similar to [selectFromState], but uses a [KClass] instead of [Class].
* @param State The state type to select from.
* @param R The result emission type.
* @param cls See [SelectEffect.cls].
* @param selector See [SelectEffect.selector].
* @return A [SelectEffect] instance.
*/
@JvmStatic
fun <State, R> selectFromState(cls: KClass<State>, selector: (State) -> R): SelectEffect<State, R> where State : Any, R : Any {
return selectFromState(cls.java, selector)
}

/**
* Create a [FlatMapEffect] instance with [FlatMapEffect.Mode.LATEST].
* @param P The source emission type.
Expand All @@ -45,12 +133,61 @@ object CommonEffects {
}

/**
* Create a [PutEffect].
* @param action See [PutEffect.action].
* @return A [PutEffect] instance.
* Create a [TakeStateEffect] instance.
* @param cls See [TakeStateEffect.cls].
* @return A [TakeStateEffect] instance.
*/
@JvmStatic
fun putInStore(action: IReduxAction): PutEffect {
return PutEffect(action)
fun <State> takeState(cls: Class<State>): TakeStateEffect<State> where State : Any {
return TakeStateEffect(cls)
}

/**
* Create a [TakeStateEffect] instance.
* @param cls See [TakeStateEffect.cls].
* @return A [TakeStateEffect] instance.
*/
@JvmStatic
fun <State> takeState(cls: KClass<State>): TakeStateEffect<State> where State : Any {
return this.takeState(cls.java)
}

/**
* Create a [DebounceEffect] instance to perform debounce for a [SagaEffect].
* @param R The result emission type.
* @param millis See [DebounceEffect.millis].
* @return An [ISagaEffectTransformer] instance.
*/
@JvmStatic
fun <R> debounce(millis: Long): ISagaEffectTransformer<R, R> where R : Any {
return { DebounceEffect(it, millis) }
}

/**
* Create a [TakeActionEffect] instance.
* @param Action The [IReduxAction] type to perform param extraction.
* @param R The result emission type.
* @param cls See [TakeActionEffect.cls].
* @param extractor See [TakeActionEffect.extractor].
* @return A [SagaEffect] instance.
*/
@JvmStatic
fun <Action, R> takeAction(cls: Class<Action>, extractor: (Action) -> R?): SagaEffect<R>
where Action : IReduxAction, R : Any {
return TakeActionEffect(cls, extractor)
}

/**
* Create a [TakeActionEffect] instance.
* @param Action The [IReduxAction] type to perform param extraction.
* @param R The result emission type.
* @param cls See [TakeActionEffect.cls].
* @param extractor See [TakeActionEffect.extractor].
* @return A [SagaEffect] instance.
*/
@JvmStatic
fun <Action, R> takeAction(cls: KClass<Action>, extractor: (Action) -> R?): SagaEffect<R>
where Action : IReduxAction, R : Any {
return this.takeAction(cls.java, extractor)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,17 @@
package org.swiften.redux.saga.common

/** Created by haipham on 2019/01/26/1 */
/**
* Invoke a [DebounceEffect] on [this].
* @receiver See [DebounceEffect.source].
* @param R The result emission type.
* @param millis See [DebounceEffect.millis].
* @return A [SagaEffect] instance.
*/
fun <R> SagaEffect<R>.debounce(millis: Long): SagaEffect<R> where R : Any {
return (CommonEffects.debounce<R>(millis))(this)
}

/**
* Invoke a [FlatMapEffect] on the current [ISagaEffect] with [FlatMapEffect.Mode.EVERY].
* @receiver See [FlatMapEffect.source].
Expand Down

This file was deleted.

Loading

0 comments on commit 71a6fde

Please sign in to comment.