-
Notifications
You must be signed in to change notification settings - Fork 110
Introduce renderAsState, a handy way to run the workflow runtime from composables. #493
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
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A couple questions.
workflow-ui/compose/src/main/java/com/squareup/workflow1/ui/compose/RenderAsState.kt
Outdated
Show resolved
Hide resolved
workflow-ui/compose/src/main/java/com/squareup/workflow1/ui/compose/RenderAsState.kt
Outdated
Show resolved
Hide resolved
workflow-ui/compose/src/main/java/com/squareup/workflow1/ui/compose/RenderAsState.kt
Show resolved
Hide resolved
workflow-ui/compose/src/main/java/com/squareup/workflow1/ui/compose/RenderAsState.kt
Show resolved
Hide resolved
I really want to use this in prod. 🤤 |
// the workflow runtime when it leaves the composition or if the composition doesn't commit. | ||
// The remember is keyed on any values that we can't update the runtime with dynamically, and | ||
// therefore require completely restarting the runtime to take effect. | ||
val state = remember(workflow, scope, interceptors) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Everything that was hard about bootstrapping, Compose is making seamless. Thank God you came up with making the first rendering synchronous.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
TBH I'm still not sure this won't blow up on us. I would be honestly quite surprised if, with all the workflows we've written internally at this point, at least one isn't doing something in its initialState
method that requires being on the main thread.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Wouldn't want anyone to get bored.
Should we put this in a dedicated This is marked as experimental, so I'm going to leave it here for now, and do the initial release this way, but if we want to move it out it won't be hard. |
590cb80
to
819a3d7
Compare
Comments addressed, merging on green. |
… composables. Part of #458. `renderAsState` is a wrapper around the main workflow runtime entry point, `renderWorkflowIn`, that manages saving and restoring snapshot states as well as updating props and getting renderings and outputs from the workflow. It's designed to follow the compose pattern for subscribing to reactive types: `Flow.collectAsState()`, `LiveData.observeAsState()`, `Observable.subscribeAsState()`. Example: ```kotlin @composable fun App(workflow: Workflow<…>, props: Props) { val scaffoldState = … // Run the workflow in the current composition's coroutine scope. val rendering by workflow.renderAsState(props, onOutput = { output -> // Note that onOutput is a suspend function, so you can run animations // and call other suspend functions. scaffoldState.snackbarHostState .showSnackbar(output.toString()) }) val viewEnvironment = remember { ViewEnvironment(mapOf(ViewRegistry to appViewRegistry)) } Scaffold(…) { padding -> // Display the root rendering using the view environment's ViewRegistry. WorkflowRendering(rendering, viewEnvironment, Modifier.padding(padding)) } } ```
819a3d7
to
6e3a5db
Compare
Part of #458.
renderAsState
is a wrapper around the main workflow runtime entry point,renderWorkflowIn
, that manages saving and restoring snapshot states as well as updating props and getting renderings and outputs from the workflow.It's designed to follow the compose pattern for subscribing to reactive types:
Flow.collectAsState()
,LiveData.observeAsState()
,Observable.subscribeAsState()
.Example: