commonMain.org.brightify.hyperdrive.property.Operators.kt Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of runtime Show documentation
Show all versions of runtime Show documentation
Hyperdrive implementation that's needed for observations and such
@file:Suppress("unused")
package org.brightify.hyperdrive.property
import org.brightify.hyperdrive.property.impl.CombineLatestObservableProperty
import org.brightify.hyperdrive.property.impl.ConstantObservableProperty
import org.brightify.hyperdrive.property.impl.DeferredFilterObservableProperty
import org.brightify.hyperdrive.property.impl.DeferredToImmediateObservablePropertyWrapper
import org.brightify.hyperdrive.property.impl.FilterObservableProperty
import org.brightify.hyperdrive.property.impl.FlatMapLatestDeferredObservableProperty
import org.brightify.hyperdrive.property.impl.FlatMapLatestObservableProperty
import org.brightify.hyperdrive.property.impl.ImmediateToDeferredObservablePropertyWrapper
import org.brightify.hyperdrive.property.impl.MapDeferredObservableProperty
import org.brightify.hyperdrive.property.impl.MapObservableProperty
import org.brightify.hyperdrive.property.impl.MergeObservableProperty
import org.brightify.hyperdrive.property.impl.NeverDeferredObservableProperty
public fun ObservableProperty.Companion.constant(value: T): ObservableProperty {
return ConstantObservableProperty(value)
}
public fun ObservableProperty.Companion.never(): DeferredObservableProperty {
return NeverDeferredObservableProperty()
}
/**
* A mapping function applied to each element collected from the [ObservableProperty].
*/
public fun ObservableProperty.map(
equalityPolicy: ObservableProperty.EqualityPolicy = defaultEqualityPolicy(),
transform: (T) -> U,
): ObservableProperty {
return MapObservableProperty(this, transform, equalityPolicy)
}
/**
* A mapping function applied to each element collected from the [DeferredObservableProperty].
*/
public fun DeferredObservableProperty.map(
equalityPolicy: ObservableProperty.EqualityPolicy = defaultEqualityPolicy(),
transform: (T) -> U,
): DeferredObservableProperty {
return MapDeferredObservableProperty(this, transform, equalityPolicy)
}
/**
* A mapping function applied to each element collected from the [ObservableProperty] to return another [ObservableProperty].
*/
public fun ObservableProperty.flatMapLatest(
equalityPolicy: ObservableProperty.EqualityPolicy> = identityEqualityPolicy(),
transform: (T) -> ObservableProperty,
): ObservableProperty {
return FlatMapLatestObservableProperty(this, transform, equalityPolicy)
}
/**
* A mapping function applied to each element collected from the [ObsrvableProperty] to return a [DeferredObservableProperty].
*/
public fun DeferredObservableProperty.flatMapLatest(
equalityPolicy: ObservableProperty.EqualityPolicy> = identityEqualityPolicy(),
transform: (T) -> DeferredObservableProperty,
): DeferredObservableProperty {
return FlatMapLatestDeferredObservableProperty(this, transform, equalityPolicy)
}
/**
* A filter function applied to each element collected from the [ObservableProperty].
*/
public fun ObservableProperty.filter(
equalityPolicy: ObservableProperty.EqualityPolicy = defaultEqualityPolicy(),
predicate: (T) -> Boolean,
): DeferredObservableProperty {
return DeferredFilterObservableProperty(this, predicate, equalityPolicy)
}
/**
* A filter function applied to each element collected from the [ObservableProperty].
*/
public fun ObservableProperty.filter(
initialValue: T,
equalityPolicy: ObservableProperty.EqualityPolicy = defaultEqualityPolicy(),
predicate: (T) -> Boolean,
): ObservableProperty {
return FilterObservableProperty(this, initialValue, predicate, equalityPolicy)
}
/**
* Keep each non-null element collected from the [ObservableProperty].
*/
public fun ObservableProperty.filterNotNull(
equalityPolicy: ObservableProperty.EqualityPolicy = defaultEqualityPolicy(),
): DeferredObservableProperty {
return filter { it != null }.map(equalityPolicy) { it!! }
}
public fun merge(sources: List>): DeferredObservableProperty {
return MergeObservableProperty(sources.map { it.deferred() })
}
public fun mergeDeferred(sources: List>): DeferredObservableProperty {
return MergeObservableProperty(sources)
}
/**
* Combine the last values of all provided [ObservableProperty] into a list of values.
*/
public fun combine(sources: List>): ObservableProperty> {
return CombineLatestObservableProperty(sources)
}
/**
* Combine the last values of two [ObservableProperty] into a [Pair].
*/
public fun combine(
source1: ObservableProperty,
source2: ObservableProperty,
equalityPolicy: ObservableProperty.EqualityPolicy> = defaultEqualityPolicy(),
): ObservableProperty> = combine(source1, source2, equalityPolicy, ::Pair)
/**
* Combine the last values of two [ObservableProperty] with a mapping function.
*/
@Suppress("UNCHECKED_CAST")
public fun combine(
source1: ObservableProperty,
source2: ObservableProperty,
equalityPolicy: ObservableProperty.EqualityPolicy = defaultEqualityPolicy(),
combine: (T1, T2) -> U,
): ObservableProperty {
return CombineLatestObservableProperty(
listOf(
source1 as ObservableProperty,
source2 as ObservableProperty,
)
)
.map(equalityPolicy) {
val (t1, t2) = it
combine(t1 as T1, t2 as T2)
}
}
/**
* Combine the last values of three [ObservableProperty] into a [Triple].
*/
public fun combine(
source1: ObservableProperty,
source2: ObservableProperty,
source3: ObservableProperty,
equalityPolicy: ObservableProperty.EqualityPolicy> = defaultEqualityPolicy(),
): ObservableProperty> = combine(source1, source2, source3, equalityPolicy, ::Triple)
/**
* Combine the last values of three [ObservableProperty] with a mapping function.
*/
@Suppress("UNCHECKED_CAST")
public fun combine(
source1: ObservableProperty,
source2: ObservableProperty,
source3: ObservableProperty,
equalityPolicy: ObservableProperty.EqualityPolicy = defaultEqualityPolicy(),
combine: (T1, T2, T3) -> U,
): ObservableProperty {
return CombineLatestObservableProperty(
listOf(
source1 as ObservableProperty,
source2 as ObservableProperty,
source3 as ObservableProperty,
)
).map(equalityPolicy) {
val (t1, t2, t3) = it
combine(t1 as T1, t2 as T2, t3 as T3)
}
}
/**
* Combine the last values of four [ObservableProperty] with a mapping function.
*/
@Suppress("UNCHECKED_CAST")
public fun combine(
source1: ObservableProperty,
source2: ObservableProperty,
source3: ObservableProperty,
source4: ObservableProperty,
equalityPolicy: ObservableProperty.EqualityPolicy = defaultEqualityPolicy(),
combine: (T1, T2, T3, T4) -> U,
): ObservableProperty {
return CombineLatestObservableProperty(
listOf(
source1 as ObservableProperty,
source2 as ObservableProperty,
source3 as ObservableProperty,
source4 as ObservableProperty,
)
).map(equalityPolicy) {
val (t1, t2, t3, t4) = it
combine(t1 as T1, t2 as T2, t3 as T3, t4 as T4)
}
}
/**
* Combine the last values of five [ObservableProperty] with a mapping function.
*/
@Suppress("UNCHECKED_CAST")
public fun combine(
source1: ObservableProperty,
source2: ObservableProperty,
source3: ObservableProperty,
source4: ObservableProperty,
source5: ObservableProperty,
equalityPolicy: ObservableProperty.EqualityPolicy = defaultEqualityPolicy(),
combine: (T1, T2, T3, T4, T5) -> U,
): ObservableProperty {
return CombineLatestObservableProperty(
listOf(
source1 as ObservableProperty,
source2 as ObservableProperty,
source3 as ObservableProperty,
source4 as ObservableProperty,
source5 as ObservableProperty,
)
).map(equalityPolicy) {
val (t1, t2, t3, t4, t5) = it
combine(t1 as T1, t2 as T2, t3 as T3, t4 as T4, t5 as T5)
}
}
/**
* Conversion method to asynchronous [DeferredObservableProperty].
*/
public fun ObservableProperty.deferred(): DeferredObservableProperty {
return ImmediateToDeferredObservablePropertyWrapper(this)
}
/**
* Conversion method to synchronous [ObservableProperty].
*/
public fun DeferredObservableProperty.startWith(initialValue: T): ObservableProperty {
return DeferredToImmediateObservablePropertyWrapper(initialValue, this)
}