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

defer, skipUntil & combine #78

Closed
3 tasks done
hoc081098 opened this issue Apr 21, 2022 · 0 comments · Fixed by #113
Closed
3 tasks done

defer, skipUntil & combine #78

hoc081098 opened this issue Apr 21, 2022 · 0 comments · Fixed by #113
Labels
enhancement New feature or request
Milestone

Comments

@hoc081098
Copy link
Owner

hoc081098 commented Apr 21, 2022

@Suppress("UNCHECKED_CAST")
public fun <T1, T2, T3, T4, T5, T6, R> combine(
  flow: Flow<T1>,
  flow2: Flow<T2>,
  flow3: Flow<T3>,
  flow4: Flow<T4>,
  flow5: Flow<T5>,
  flow6: Flow<T6>,
  transform: suspend (T1, T2, T3, T4, T5, T6) -> R,
): Flow<R> = combine(flow, flow2, flow3, flow4, flow5, flow6) { args: Array<*> ->
  transform(
    args[0] as T1,
    args[1] as T2,
    args[2] as T3,
    args[3] as T4,
    args[4] as T5,
    args[5] as T6,
  )
}

@Suppress("UNCHECKED_CAST")
public fun <T1, T2, T3, T4, T5, T6, T7, R> combine(
  flow: Flow<T1>,
  flow2: Flow<T2>,
  flow3: Flow<T3>,
  flow4: Flow<T4>,
  flow5: Flow<T5>,
  flow6: Flow<T6>,
  flow7: Flow<T7>,
  transform: suspend (T1, T2, T3, T4, T5, T6, T7) -> R,
): Flow<R> = combine(flow, flow2, flow3, flow4, flow5, flow6, flow7) { args: Array<*> ->
  transform(
    args[0] as T1,
    args[1] as T2,
    args[2] as T3,
    args[3] as T4,
    args[4] as T5,
    args[5] as T6,
    args[6] as T7,
  )
}

@Suppress("UNCHECKED_CAST")
public fun <T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, R> combine(
  flow: Flow<T1>,
  flow2: Flow<T2>,
  flow3: Flow<T3>,
  flow4: Flow<T4>,
  flow5: Flow<T5>,
  flow6: Flow<T6>,
  flow7: Flow<T7>,
  flow8: Flow<T8>,
  flow9: Flow<T9>,
  flow10: Flow<T10>,
  flow11: Flow<T11>,
  transform: suspend (T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11) -> R,
): Flow<R> = combine(
  flow,
  flow2,
  flow3,
  flow4,
  flow5,
  flow6,
  flow7,
  flow8,
  flow9,
  flow10,
  flow11
) { args: Array<*> ->
  transform(
    args[0] as T1,
    args[1] as T2,
    args[2] as T3,
    args[3] as T4,
    args[4] as T5,
    args[5] as T6,
    args[6] as T7,
    args[7] as T8,
    args[8] as T9,
    args[9] as T10,
    args[10] as T11
  )
}

fun <T> defer(flowFactory: () -> Flow<T>): Flow<T> = flow { emitAll(flowFactory()) }

fun <T> Flow<T>.skipUntil(notifier: Flow<Any?>): Flow<T> = flow {
  coroutineScope {
    val shouldEmit = AtomicBoolean(false)

    launch(start = CoroutineStart.UNDISPATCHED) {
      notifier
        .take(1)
        .collect()
      shouldEmit.set(true)
    }

    collect {
      if (shouldEmit.get()) {
        emit(it)
      }
    }
  }
}

  • defer
  • skipUntil / dropUntil
  • combine...
@hoc081098 hoc081098 added this to the 0.4.0 milestone May 3, 2022
@hoc081098 hoc081098 added the enhancement New feature or request label May 3, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant