Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added flow combine extension #42

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,98 @@ fun <T : Any> combineContent(
)
}

inline fun <A : Any, B : Any, C : Any, D : Any, R : Any> combineContent(
a: Flow<Data<A>>,
b: Flow<Data<B>>,
c: Flow<Data<C>>,
d: Flow<Data<D>>,
crossinline block: suspend (A, B, C, D) -> R,
): Flow<Data<R>> = combineContent(combineContent(a, b, c), d)
.mapContentSuspended { (abc, d) -> block(abc.first, abc.second, abc.third, d) }

inline fun <A : Any, B : Any, C : Any, D : Any, E : Any, R : Any> combineContent(
a: Flow<Data<A>>,
b: Flow<Data<B>>,
c: Flow<Data<C>>,
d: Flow<Data<D>>,
e: Flow<Data<E>>,
crossinline block: suspend (A, B, C, D, E) -> R,
): Flow<Data<R>> = combineContent(combineContent(a, b, c), combineContent(d, e))
.mapContentSuspended { (abc, de) -> block(abc.first, abc.second, abc.third, de.first, de.second) }

inline fun <A : Any, B : Any, C : Any, D : Any, E : Any, F : Any, R : Any> combineContent(
a: Flow<Data<A>>,
b: Flow<Data<B>>,
c: Flow<Data<C>>,
d: Flow<Data<D>>,
e: Flow<Data<E>>,
f: Flow<Data<F>>,
crossinline block: suspend (A, B, C, D, E, F) -> R,
): Flow<Data<R>> = combineContent(combineContent(a, b, c), combineContent(d, e, f))
.mapContentSuspended { (abc, def) -> block(abc.first, abc.second, abc.third, def.first, def.second, def.third) }

inline fun <A : Any, B : Any, C : Any, D : Any, E : Any, F : Any, G : Any, R : Any> combineContent(
a: Flow<Data<A>>,
b: Flow<Data<B>>,
c: Flow<Data<C>>,
d: Flow<Data<D>>,
e: Flow<Data<E>>,
f: Flow<Data<F>>,
g: Flow<Data<G>>,
crossinline block: suspend (A, B, C, D, E, F, G) -> R,
): Flow<Data<R>> = combineContent(combineContent(a, b, c), combineContent(d, e, f), g)
.mapContentSuspended { (abc, def, g) -> block(abc.first, abc.second, abc.third, def.first, def.second, def.third, g) }

inline fun <A : Any, B : Any, C : Any, D : Any, E : Any, F : Any, G : Any, H : Any, R : Any> combineContent(
a: Flow<Data<A>>,
b: Flow<Data<B>>,
c: Flow<Data<C>>,
d: Flow<Data<D>>,
e: Flow<Data<E>>,
f: Flow<Data<F>>,
g: Flow<Data<G>>,
h: Flow<Data<H>>,
crossinline block: suspend (A, B, C, D, E, F, G, H) -> R,
): Flow<Data<R>> = combineContent(combineContent(a, b, c), combineContent(d, e, f), combineContent(g, h))
.mapContentSuspended { (abc, def, gh) ->
block(
abc.first,
abc.second,
abc.third,
def.first,
def.second,
def.third,
gh.first,
gh.second
)
}

inline fun <A : Any, B : Any, C : Any, D : Any, E : Any, F : Any, G : Any, H : Any, I : Any, R : Any> combineContent(
a: Flow<Data<A>>,
b: Flow<Data<B>>,
c: Flow<Data<C>>,
d: Flow<Data<D>>,
e: Flow<Data<E>>,
f: Flow<Data<F>>,
g: Flow<Data<G>>,
h: Flow<Data<H>>,
i: Flow<Data<I>>,
crossinline block: suspend (A, B, C, D, E, F, G, H, I) -> R,
): Flow<Data<R>> = combineContent(combineContent(a, b, c), combineContent(d, e, f), combineContent(g, h, i))
.mapContentSuspended { (abc, def, ghi) ->
block(
abc.first,
abc.second,
abc.third,
def.first,
def.second,
def.third,
ghi.first,
ghi.second,
ghi.third
)
}

fun <T : Any> combineContent(
vararg flows: Flow<Data<T>>
): Flow<Data<List<T>>> = combineContent(flows.toList())
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,99 @@ class CombineContentKtIterableTest {
Assertions.assertEquals(combinedListData, combinedTripleData)
}

@Test
fun `Combined value should be the same for quadruple and content`() = runTest {
val content = combineContent(
flowOf(Data("a")),
flowOf(Data("b")),
flowOf(Data("c")),
flowOf(Data("d")),
) { a, b, c, d -> a + b + c + d }
.first()

Assertions.assertEquals(content, Data("abcd"))
}

@Test
fun `Combined value should be the same for quintuple and content`() = runTest {
val content = combineContent(
flowOf(Data("a")),
flowOf(Data("b")),
flowOf(Data("c")),
flowOf(Data("d")),
flowOf(Data("e")),
) { a, b, c, d, e -> a + b + c + d + e }
.first()

Assertions.assertEquals(content, Data("abcde"))
}

@Test
fun `Combined value should be the same for sextuple and content`() = runTest {
val content = combineContent(
flowOf(Data("a")),
flowOf(Data("b")),
flowOf(Data("c")),
flowOf(Data("d")),
flowOf(Data("e")),
flowOf(Data("f")),
) { a, b, c, d, e, f -> a + b + c + d + e + f }
.first()

Assertions.assertEquals(content, Data("abcdef"))
}

@Test
fun `Combined value should be the same for septuple and content`() = runTest {
val content = combineContent(
flowOf(Data("a")),
flowOf(Data("b")),
flowOf(Data("c")),
flowOf(Data("d")),
flowOf(Data("e")),
flowOf(Data("f")),
flowOf(Data("g")),
) { a, b, c, d, e, f, g -> a + b + c + d + e + f + g }
.first()

Assertions.assertEquals(content, Data("abcdefg"))
}

@Test
fun `Combined value should be the same for octuple and content`() = runTest {
val content = combineContent(
flowOf(Data("a")),
flowOf(Data("b")),
flowOf(Data("c")),
flowOf(Data("d")),
flowOf(Data("e")),
flowOf(Data("f")),
flowOf(Data("g")),
flowOf(Data("h")),
) { a, b, c, d, e, f, g, h -> a + b + c + d + e + f + g + h }
.first()

Assertions.assertEquals(content, Data("abcdefgh"))
}

@Test
fun `Combined value should be the same for nonuple and content`() = runTest {
val content = combineContent(
flowOf(Data("a")),
flowOf(Data("b")),
flowOf(Data("c")),
flowOf(Data("d")),
flowOf(Data("e")),
flowOf(Data("f")),
flowOf(Data("g")),
flowOf(Data("h")),
flowOf(Data("i")),
) { a, b, c, d, e, f, g, h, i -> a + b + c + d + e + f + g + h + i }
.first()

Assertions.assertEquals(content, Data("abcdefghi"))
}

@Test
fun `Combined value should be the same for vararg and list parameters`() = runFlowTest(testCombined) {
flows.forEachIndexed { index, flow -> flow.emit(Data("$index")) }
Expand Down
Loading