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

Introduced LifecycleExtensions overloads to set observation schedulers #179

Conversation

LachlanMcKee
Copy link
Collaborator

Description

Proposal for allowing developers to set observation scheduler for Binder and lifecycle extensions.

Check list

  • I have updated CHANGELOG.md if required.
  • I have updated documentation if required.

@LachlanMcKee LachlanMcKee force-pushed the lifecycle-binder-observe-on-implemenation branch from e0917ec to 8d1905d Compare February 8, 2023 11:59
fun Lifecycle.startStop(f: Binder.() -> Unit) {
Binder(StartStopBinderLifecycle(this)).apply(f)
}

fun Lifecycle.startStop(schedulers: LifecycleBinderSchedulers, f: LifecycleSchedulerScope.() -> Unit) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the API a little bit verbose, especially when we do not need to use it most of the time. Can we remove LifecycleBinderSchedulers and make API more lightweight?

lifecycle.createDestroy(scheduler) {
   bind(a to b using c) // implicit .observeOn scheduler
}

lifecycle.createDestroy {
  bind(a to b using c observeOn scheduler) // explicit
}

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

createDestroy(scheduler) and createDestroy(lifecycleBinderSchedulers) have one issue – when you start applying them to the existing code, you might change subscription order, leading to some unexpected bugs. So it might be helpful to consider only infix observeOn usage.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nothing stops developers using observeOn in the version that does not take a scheduler. (It's actually already in this PR).

I am hoping that we WILL use this 'verbose' api as it sets out a strict guideline, and is less boilerplate in case you want to set a schedule in a lot of places.

I could also change it so that you can also call bind outside of main/background and remove unbound

so it could look like

testLifecycleOwner.lifecycle.createDestroy(schedulers) {
    mainThread {
        bind(events to uiConsumer)
    }
    backgroundThread {
        bind(events to analyticsConsumer)
    }
    bind(events to dontSwitchThreadsConsumer)
    bind(events to dontSwitchThreadsConsumer observeOn mainThreadScheduler)
}

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

With regards to unexpected bugs, developers would need to be aware of this change and take that into account if/when migrating. At the moment there is no way to ensure that everything is happening on the main/background threads anyway, so it's likely already an issue

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I do not like that I need to create a separate LifecycleBinderSchedulers class instead of injecting schedulers directly.

Copy link
Collaborator Author

@LachlanMcKee LachlanMcKee Feb 8, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've changed this in my 3rd attempt at the API.

testLifecycleOwner.lifecycle.createDestroy {
    withScheduler(mainScheduler) {
        bind(events to uiConsumer)
    }
    withScheduler(backgroundScheduler) {
        bind(events to analyticsConsumer)
    }
    bind(events to dontSwitchThreadsConsumer)
    bind(events to dontSwitchThreadsConsumer observeOn mainThreadScheduler)
}

@LachlanMcKee LachlanMcKee force-pushed the lifecycle-binder-observe-on-implemenation branch from 74b8d6a to 3a10e05 Compare February 8, 2023 15:52
@LachlanMcKee LachlanMcKee marked this pull request as ready for review February 8, 2023 15:53
@LachlanMcKee LachlanMcKee force-pushed the lifecycle-binder-observe-on-implemenation branch 2 times, most recently from f6fddd4 to c16bb4c Compare February 8, 2023 15:55
@LachlanMcKee LachlanMcKee force-pushed the lifecycle-binder-observe-on-implemenation branch from c16bb4c to 0ac913a Compare February 8, 2023 16:39
@LachlanMcKee LachlanMcKee merged commit b2b0cc7 into badoo:master Feb 8, 2023
@LachlanMcKee LachlanMcKee deleted the lifecycle-binder-observe-on-implemenation branch February 8, 2023 16:49
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants