Lammert Westerhoff
01/13/2021, 9:36 PMMainScope to launch a new coroutine in which I collect the Flow. If I leave things like that and the component gets unmounted because the user for example logs out, then the coroutine keeps running and collecting from the flow after the component is unmounted, resulting in React errors/warnings. I solved it by assigning a Job and then cancelling it in componentWillUnmount. This doesn’t feel right though and I’d prefer some better solution in case anyone has suggestions. I was thinking something similar as Android view models have a viewModelScope that automatically gets cancelled when the view model is cleared, but I can’t seem to find a way to do this.turansky
01/13/2021, 11:37 PMuseEffect for example) contains cleanup logic. In my case I cancel Job which was started by hookLammert Westerhoff
01/13/2021, 11:54 PMLammert Westerhoff
01/14/2021, 12:02 AMval users = functionalComponent<UsersProps> { props ->
val (viewModelOutput, setViewModelOutput) = useState<UsersViewModel.Output?>(null)
useEffectWithCleanup(emptyList()) {
val job = mainScope.launch {
props.viewModel.output.collect { output ->
setViewModelOutput(output)
}
}
return@useEffectWithCleanup {
job.cancel()
}
}Lammert Westerhoff
01/14/2021, 12:03 AMclass UsersComponent: RComponent<UsersProps, UsersState>() {
override fun componentDidMount() {
job = mainScope.launch {
props.viewModel.output.collect { output ->
setState {
this.viewModelOutput = output
}
}
}
}
override fun componentWillUnmount() {
job?.cancel()
job = null
}turansky
01/14/2021, 12:24 AMLammert Westerhoff
01/14/2021, 1:03 AMturansky
01/14/2021, 1:45 AMGlobalScope)
3. I don’t see MainScope benefits in JSLammert Westerhoff
01/14/2021, 1:52 AMelizarov
01/14/2021, 8:13 AMturansky
01/18/2021, 4:19 PMGlobalScope is more preferable on JS, when created `Job`s cancelled automatically by another library (react in my case)?elizarov
01/20/2021, 8:00 AMMainScope for all kinds "UI actors" (that is, the things that directly work with UI) and GlobalScope for all kind of "background actors" that performs any kind of business logic. Again, on JS it is all irrelevant (any heavy code will lock your UI anyway), but still keeping distinction in mind might be helpful.steamstreet
05/18/2021, 10:27 PMankushg
05/28/2021, 1:27 AMturansky
05/28/2021, 11:27 AMelizarov
05/28/2021, 11:28 AMturansky
05/28/2021, 11:56 AMGlobalScope is legal in react case (with hooks).
Do I need custom global scope in that case or languageSettings configuration will be fine?elizarov
05/28/2021, 11:58 AMturansky
05/28/2021, 11:59 AMJob cancel works fine in that caseturansky
05/28/2021, 12:02 PMval user: User? by useState(null)
useEffect(id) {
val job = GlobalScope.launch {
user = Api.getUser(id)
}
// cleanup
// on `id` change and on unmount
job::cancel
}elizarov
05/28/2021, 12:06 PMturansky
05/28/2021, 12:16 PMturansky
05/28/2021, 12:16 PMsimilar to Compose’s rememberCoroutineScopeIs it component scope or application scope?
steamstreet
06/03/2021, 9:18 PMJob to all React components, and then within each one, create a scope with a new job… and when the component is unmounted, call and cancel the component’s job. Is there ANY examples of coroutines with React? Everything I’ve ever seen uses GlobalScope (including many official Jetbrains sources)steamstreet
06/03/2021, 9:19 PMBest Practices for Coroutines in the Browsersteamstreet
06/03/2021, 9:20 PMturansky
06/03/2021, 11:55 PMinline fun useAsyncEffect(
vararg dependencies: dynamic,
suspend effect: () -> Unit,
) {
useEffect(dependencies) {
val job = GlobalScope.launch(effect)
// cleanup
job::cancel
}
}