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

safeAction, safeEventHandler #1221

Merged
merged 2 commits into from
Sep 25, 2024
Merged

Conversation

rjrjr
Copy link
Contributor

@rjrjr rjrjr commented Sep 24, 2024

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:

action {
  (state as? SpecificState)?.let { currentState ->
    // whatever
  }
}

So we introduce StatefulWorkflow.safeAction and StatefulWorkflow.RenderContext.safeEventHandler as conveniences to do that cast for you.

@rjrjr rjrjr force-pushed the ray/give-me-convenience-or-give-me-death branch from 189207d to a5d53ea Compare September 24, 2024 20:11
@rjrjr rjrjr force-pushed the ray/give-me-convenience-or-give-me-death branch from 740efac to 6b001b0 Compare September 24, 2024 23:15
@rjrjr rjrjr changed the title wip: safeEventHandler safeAction, safeEventHandler Sep 24, 2024
@rjrjr rjrjr force-pushed the ray/give-me-convenience-or-give-me-death branch from ad8a4f9 to 1485fa0 Compare September 24, 2024 23:22
@rjrjr rjrjr marked this pull request as ready for review September 24, 2024 23:22
@rjrjr rjrjr requested a review from a team as a code owner September 24, 2024 23:22
@rjrjr rjrjr force-pushed the ray/give-me-convenience-or-give-me-death branch 3 times, most recently from 4c4f30a to 04c0440 Compare September 24, 2024 23:30
@@ -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
Copy link
Contributor Author

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?

Copy link
Member

@afollestad afollestad left a 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

@rjrjr
Copy link
Contributor Author

rjrjr commented Sep 24, 2024

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 currentState in the function definitions, so the IDE autocomplete will provide that name for us. And yes, we will need to call that out.

@rjrjr rjrjr force-pushed the ray/give-me-convenience-or-give-me-death branch from 027d916 to 3f9f98a Compare September 25, 2024 00:16
@rjrjr
Copy link
Contributor Author

rjrjr commented Sep 25, 2024

@afollestad obsessively tweaked the kdoc samples to say currentState instead of oldState.

@rjrjr rjrjr force-pushed the ray/give-me-convenience-or-give-me-death branch from bf049f8 to 49abd90 Compare September 25, 2024 00:24
@rjrjr rjrjr enabled auto-merge September 25, 2024 00:29
@rjrjr rjrjr disabled auto-merge September 25, 2024 20:59
@rjrjr rjrjr force-pushed the ray/give-me-convenience-or-give-me-death branch from 14d5ea3 to cc76acf Compare September 25, 2024 21:39
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.
@rjrjr rjrjr force-pushed the ray/give-me-convenience-or-give-me-death branch from cc76acf to 10f5ef7 Compare September 25, 2024 21:54
@@ -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
Copy link
Contributor Author

Choose a reason for hiding this comment

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

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
Copy link
Contributor Author

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>
@rjrjr rjrjr enabled auto-merge September 25, 2024 22:15
@rjrjr rjrjr disabled auto-merge September 25, 2024 22:23
@rjrjr rjrjr merged commit 6395d0b into main Sep 25, 2024
30 checks passed
@rjrjr rjrjr deleted the ray/give-me-convenience-or-give-me-death branch September 25, 2024 22:23
* 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

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

Choose a reason for hiding this comment

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

I like it!

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.

4 participants