Skip to content

feat: JsFunction.withParameter and named captures for triggers#24465

Open
Artur- wants to merge 1 commit into
mainfrom
js-initializer-params
Open

feat: JsFunction.withParameter and named captures for triggers#24465
Artur- wants to merge 1 commit into
mainfrom
js-initializer-params

Conversation

@Artur-
Copy link
Copy Markdown
Member

@Artur- Artur- commented May 28, 2026

Adds a JsFunction.withParameter(name, value) primitive that prepends let <name>=$<N>; and appends the value as a capture, so a function body can reference values by name instead of by positional $N:

element.addJsInitializer(JsFunction.of("setup(target)")
        .withParameter("target", someElement));

The new addJsInitializer(JsFunction) overload accepts pre-built JsFunctions; the existing addJsInitializer(String, Object...) form delegates to it, and ElementJsInitializerRegistration now takes the JsFunction directly instead of rebuilding it from a string/array.

Sweeps the trigger/action/input toJs and toPromiseJs bodies to use the new named form so each rendered fragment reads as its own JS:

DomEventTrigger addEventListener(eventName, action) / removeEventListener(...)
CallbackAction channel(source(event))
PromiseAction observer(inner(event), channel)
HandlerInput return event[propertyName]
PropertyInput return target[propertyName]
SetPropertyAction target[propertyName] = source(event)
SignalInput return target[propertyName]
RequestFullscreenAction return target.requestFullscreen()
WriteToClipboardAction writePayload(text(event), html(event))

The companion "// $N = ..." comments are gone — the names in the body now match the captures. Trigger.install's Javadoc example uses the same form so subclasses are pointed at the named API.

Duplicate parameter names, and names that collide with a previously declared withArguments name, are rejected.

Adds a JsFunction.withParameter(name, value) primitive that prepends
`let <name>=$<N>;` and appends the value as a capture, so a function
body can reference values by name instead of by positional `$N`:

    element.addJsInitializer(JsFunction.of("setup(target)")
            .withParameter("target", someElement));

The new addJsInitializer(JsFunction) overload accepts pre-built
JsFunctions; the existing addJsInitializer(String, Object...) form
delegates to it, and ElementJsInitializerRegistration now takes the
JsFunction directly instead of rebuilding it from a string/array.

Sweeps the trigger/action/input toJs and toPromiseJs bodies to use the
new named form so each rendered fragment reads as its own JS:

  DomEventTrigger        addEventListener(eventName, action) / removeEventListener(...)
  CallbackAction         channel(source(event))
  PromiseAction          observer(inner(event), channel)
  HandlerInput           return event[propertyName]
  PropertyInput          return target[propertyName]
  SetPropertyAction      target[propertyName] = source(event)
  SignalInput            return target[propertyName]
  RequestFullscreenAction return target.requestFullscreen()
  WriteToClipboardAction writePayload(text(event), html(event))

The companion "// \$N = ..." comments are gone — the names in the body
now match the captures. Trigger.install's Javadoc example uses the
same form so subclasses are pointed at the named API.

Duplicate parameter names, and names that collide with a previously
declared withArguments name, are rejected.
@sonarqubecloud
Copy link
Copy Markdown

@github-actions
Copy link
Copy Markdown

Test Results

 1 427 files  ±0   1 427 suites  ±0   1h 23m 2s ⏱️ -5s
10 041 tests +6   9 973 ✅ +6  68 💤 ±0  0 ❌ ±0 
10 513 runs  +6  10 444 ✅ +6  69 💤 ±0  0 ❌ ±0 

Results for commit ebb7b4c. ± Comparison against base commit 792dc9c.

@Legioth
Copy link
Copy Markdown
Member

Legioth commented Jun 1, 2026

I'm not sure this is a good idea after all. See
#24385 (comment)

@mshabarov mshabarov requested a review from platosha June 1, 2026 11:41
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants