Coder Social home page Coder Social logo

Comments (3)

roomscape avatar roomscape commented on July 25, 2024 1

A flow is "cold", so on its own it can't be used in the way you describe. It's only active when the consumer is collecting it, and all of its state lives inside the collect function. Outside collect, the flow isn't running or producing values, and there's no way to make a stateful iterator.

To turn it into a stateful "hot" stream of values, we can add an additional producer coroutine using produceIn. The coroutine will be responsible for maintaining the flow in an active state, and will expose each successive value via a Channel.

With produceIn, your example code would look like this:

myCoroutineScope.launch {
  val flow = myComponent.getSomeThings()
  val channel = flow.produceIn(this)
  for (item in channel) {
      …
  }
}

Since the producer coroutine is now responsible for the lifecycle of the flow, including any resources it might hold, it's important to use correct structured concurrency to ensure everything's cleaned up afterwards.

from kotlinx.coroutines.

dkhalanskyjb avatar dkhalanskyjb commented on July 25, 2024 1

Every flow operator is open-source and fairly short. You can replace it with its implementation by getting rid of a few optimizations.

originalFlow
    .onStart { /* START */ }

becomes

flow {
  /* START */
  emitAll(originalFlow)
}
originalFlow
    .onStart { /* START */ }
    .onEmpty { /* EMPTY */ }

becomes

flow {
  var isEmpty = true
  flow {
    /* START */
    emitAll(originalFlow)
  }.collect {
    isEmpty = false
    emit(it)
  }
  if (isEmpty) {
    /* EMPTY */
  }
}

With onEach, this becomes

flow {
  flow {
    var isEmpty = true
    flow {
      /* START */
      emitAll(originalFlow)
    }.collect {
      isEmpty = false
      emit(it)
    }
    if (isEmpty) {
      /* EMPTY */
    }
  }.collect {
    /* EACH */
    emit(it)
  }
}

and so on.

from kotlinx.coroutines.

qwwdfsad avatar qwwdfsad commented on July 25, 2024

https://youtrack.jetbrains.com/issue/KT-33851/iterator-less-for-statement-operator-convention in the future also might help w.r.t. more imperative usages

Another option is to transform flow into the channel via produceIn or reuse the facility from #3274

But in general, our consensus is that we do intend to provide one more way to consume the flow which is drastically different (semantics-wise and performance-wise) than the regular pull model.

from kotlinx.coroutines.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    πŸ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. πŸ“ŠπŸ“ˆπŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❀️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.