-
Notifications
You must be signed in to change notification settings - Fork 102
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
safeAction
, safeEventHandler
#1221
Conversation
189207d
to
a5d53ea
Compare
workflow-core/src/commonMain/kotlin/com/squareup/workflow1/StatefulWorkflow.kt
Outdated
Show resolved
Hide resolved
740efac
to
6b001b0
Compare
ad8a4f9
to
1485fa0
Compare
4c4f30a
to
04c0440
Compare
@@ -155,6 +155,7 @@ public final class com/squareup/workflow1/Snapshots { | |||
public abstract class com/squareup/workflow1/StatefulWorkflow : com/squareup/workflow1/IdCacheable, com/squareup/workflow1/Workflow { | |||
public fun <init> ()V | |||
public final fun asStatefulWorkflow ()Lcom/squareup/workflow1/StatefulWorkflow; | |||
public final fun defaultOnFailedCast (Ljava/lang/String;Lkotlin/reflect/KClass;Ljava/lang/Object;)V |
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.
Why is this the only change caught by apiDump
?
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.
Thanks for doing this!
When WorkflowActionFactory
is deprecated, we'll want to call out that currentState
(in the receiver) is replaced with the oldState
lambda parameter
@afollestad I actually called it |
027d916
to
3f9f98a
Compare
@afollestad obsessively tweaked the kdoc samples to say |
bf049f8
to
49abd90
Compare
workflow-core/src/commonMain/kotlin/com/squareup/workflow1/StatefulWorkflow.kt
Show resolved
Hide resolved
14d5ea3
to
cc76acf
Compare
People are confused by the fact that a `WorkflowAction` can't assume that a sealed class / interface `StateT` is the same subtype that it was at render time when the action fires. And those that do understand it resent this boilerplate: ```kotlin action { (state as? SpecificState)?.let { currentState -> // whatever } } ``` So we introduce `StatefulWorkflow.safeAction` and `StatefulWorkflow.RenderContext.safeEventHandler` as conveniences to do that cast for you.
cc76acf
to
10f5ef7
Compare
@@ -130,6 +130,45 @@ public interface BaseRenderContext<out PropsT, StateT, in OutputT> { | |||
* given [update] function, and immediately passes it to [actionSink]. Handy for | |||
* attaching event handlers to renderings. | |||
* | |||
* It is important to understand that the [update] lambda you provide here |
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.
@jeffhashfield PTAL
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.
I like it!
* Like [eventHandler], but no-ops if [state][WorkflowAction.Updater.state] has | ||
* changed to a different type than [CurrentStateT] by the time [update] fires. | ||
* | ||
* It is also important to understand that **even if [update] is called, there is |
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.
@jeffhashfield Also added a callout here to that doc.
Signed-off-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
* This is not an uncommon case. Consider accidental rapid taps on | ||
* a button, where the first tap event moves the receiving [StatefulWorkflow] | ||
* to a new state. There is no reason to expect that the later taps will not | ||
* fire the (now stale) event handler a few more times. No promise can be |
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.
the (now stale) event handler
Is it confusing to use the term event handler
here? The WorkflowAction created by safeAction
is probably being used to handle an event, but it might be better to use a different term that isn't easily confused with the eventHandler
method. How about the (now stale) [update] lambda
? That change would also work well in the safeEventHandler
documentation.
@@ -130,6 +130,45 @@ public interface BaseRenderContext<out PropsT, StateT, in OutputT> { | |||
* given [update] function, and immediately passes it to [actionSink]. Handy for | |||
* attaching event handlers to renderings. | |||
* | |||
* It is important to understand that the [update] lambda you provide here |
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.
I like it!
People are confused by the fact that a
WorkflowAction
can't assume that a sealed class / interfaceStateT
is the same subtype that it was at render time when the action fires. And those that do understand it resent this boilerplate:So we introduce
StatefulWorkflow.safeAction
andStatefulWorkflow.RenderContext.safeEventHandler
as conveniences to do that cast for you.