Skip to content

feat(InputBinding): subscribe callback now supports event priority #4211

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

Open
wants to merge 4 commits into
base: main
Choose a base branch
from

Conversation

cpsievert
Copy link
Collaborator

@cpsievert cpsievert commented Apr 25, 2025

Until now, custom input bindings could only pass a boolean value to their subscribe callback. That is, they all do something similar to this to notify Shiny of a change:

exampleInputBinding.subscribe = function(el, callback) {
  $(el).on("keyup.exampleComponentName", function(event) {
    callback(true); // or false
  });
}

These boolean values effectively map to values of priority: "deferred" (true) and priority: "immediate" (false).

This means, if an input wants to resend their value immediately regardless of whether the value has changed (i.e., priority: "event"), that isn't possible (until now).

The motivator for adding this is for input_submit_textarea() in rstudio/bslib#1204, where it feels like the most desirable behavior is to always resend immediately.

@cpsievert cpsievert added this to the Next Release milestone Apr 26, 2025
@cpsievert cpsievert force-pushed the feat/subscribeCallbackEvent branch from 95f1c08 to b77ef5e Compare May 1, 2025 17:39
@cpsievert cpsievert force-pushed the feat/subscribeCallbackEvent branch from b77ef5e to 157d489 Compare May 1, 2025 17:40
@cpsievert cpsievert requested a review from Copilot May 1, 2025 17:41
Copilot

This comment was marked as outdated.

cpsievert and others added 2 commits May 1, 2025 12:51
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
@@ -27,7 +28,7 @@ function valueChangeCallback(
inputs: InputValidateDecorator,
binding: InputBinding,
el: HTMLElement,
allowDeferred: boolean
priority: InputPolicyOpts["priority"]
Copy link
Member

Choose a reason for hiding this comment

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

If I'm reading the source correctly, InputPolicyOpts["priority"] is the same as EventPriority, right? If so, I think the second option is clearer because it doesn't require redirecting through InputPolicyOpts

@gadenbuie
Copy link
Member

gadenbuie commented May 2, 2025

These boolean values effectively map to values of priority: "deferred" (true) and priority: "immediate" (false).

This explanation is the first time I've seen callback(false) or callback(true) written out in a way that is clear, concise and makes sense to me. JavaScript's positional variables are inherently opaque, but the naming here doesn't help.

If we're updating the API, I'd love to push it in a direction that supports a more readable interface. I.e. in addition to backcompat for callback(<boolean>), it'd be nice if the augmented API could support

callback(true)
callback({ priority: "deferred" })

callback(false)
callback({ priority: "immediate" })

// or now also
callback({priority: "event"})

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.

2 participants