Title: Looking for API ideas: Typed closure handle for BridgeJS · Issue #541 · swiftwasm/JavaScriptKit · GitHub
Open Graph Title: Looking for API ideas: Typed closure handle for BridgeJS · Issue #541 · swiftwasm/JavaScriptKit
X Title: Looking for API ideas: Typed closure handle for BridgeJS · Issue #541 · swiftwasm/JavaScriptKit
Description: Summary BridgeJS needs a typed closure handle type that preserves object identity across the Swift/JS bridge. We're looking for community input on naming. Background JavaScriptKit already has JSClosure for untyped closures: let closure =...
Open Graph Description: Summary BridgeJS needs a typed closure handle type that preserves object identity across the Swift/JS bridge. We're looking for community input on naming. Background JavaScriptKit already has JSClo...
X Description: Summary BridgeJS needs a typed closure handle type that preserves object identity across the Swift/JS bridge. We're looking for community input on naming. Background JavaScriptKit already has J...
Opengraph URL: https://github.com/swiftwasm/JavaScriptKit/issues/541
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Looking for API ideas: Typed closure handle for BridgeJS","articleBody":"## Summary\n\nBridgeJS needs a typed closure handle type that preserves object identity across the Swift/JS bridge. We're looking for community input on naming.\n\n## Background\n\nJavaScriptKit already has `JSClosure` for untyped closures:\n\n```swift\nlet closure = JSClosure { args -\u003e JSValue in\n // ...\n}\n```\n\nFor BridgeJS, we need a **typed** variant with a generic parameter for the function signature. This enables type-safe declarations like:\n\n```swift\n@JSFunction func addEventListener(_ type: String, _ listener: (MessageEvent) -\u003e Void)\n```\n\nThe typed closure handle is essential for APIs like `addEventListener`/`removeEventListener` that require stable object identity, passing the same closure reference to both calls.\n\n## Related issues\n\n- https://github.com/swiftwasm/JavaScriptKit/issues/536\n - We need the explicit handle to allow manually releasing JS-passed closures \n- https://github.com/swiftwasm/JavaScriptKit/issues/540\n\n## Constraints\n\n1. Must coexist with `JSClosure`: we can't simply make `JSClosure\u003cT\u003e` generic (breaking change)\n2. Explicit conversion required: Swift doesn't support implicit conversion from closure literals, so users will write the type name frequently\n\n## Candidates\n\n### 1. Nested type: `JSClosure.Typed\u003cT\u003e`\n\n```swift\n@JSClass struct JSHTMLElement {\n @JSFunction func addEventListener(_ type: String, _ listener: JSClosure.Typed\u003c(MessageEvent) -\u003e Void\u003e)\n @JSFunction func removeEventListener(_ type: String, _ listener: JSClosure.Typed\u003c(MessageEvent) -\u003e Void\u003e)\n}\n\nlet handler = JSClosure.Typed\u003c(MessageEvent) -\u003e Void\u003e { event in\n print(event.data)\n}\ndocument.addEventListener(\"load\", handler)\ndocument.removeEventListener(\"load\", handler)\n\n@JSFunction func requestAnimationFrame(_ callback: JSOneshotClosure.Typed\u003c(Double) -\u003e Void\u003e)\n\nrequestAnimationFrame(JSOneshotClosure.Typed { timestamp in\n print(timestamp)\n})\n```\n\n- Clear relationship to `JSClosure`\n- Discoverable via autocomplete\n- Verbose (15 characters)\n\n\n### 2. `JSTypedClosure\u003cT\u003e`\n\n\n```swift\n@JSClass struct JSHTMLElement {\n @JSFunction func addEventListener(_ type: String, _ listener: JSTypedClosure\u003c(MessageEvent) -\u003e Void\u003e)\n @JSFunction func removeEventListener(_ type: String, _ listener: JSTypedClosure\u003c(MessageEvent) -\u003e Void\u003e)\n}\n\nlet handler = JSTypedClosure\u003c(MessageEvent) -\u003e Void\u003e { event in\n print(event.data)\n}\ndocument.addEventListener(\"load\", handler)\ndocument.removeEventListener(\"load\", handler)\n\n@JSFunction func requestAnimationFrame(_ callback: JSTypedClosure\u003c(Double) -\u003e Void\u003e)\n\nrequestAnimationFrame(JSTypedClosure.oneshot { timestamp in\n print(timestamp)\n})\n```\n\n### 3. `JSCallback\u003cT\u003e`\n\n\n```swift\n@JSClass struct JSHTMLElement {\n @JSFunction func addEventListener(_ type: String, _ listener: JSCallback\u003c(MessageEvent) -\u003e Void\u003e)\n @JSFunction func removeEventListener(_ type: String, _ listener: JSCallback\u003c(MessageEvent) -\u003e Void\u003e)\n}\n\nlet handler = JSCallback\u003c(MessageEvent) -\u003e Void\u003e { event in\n print(event.data)\n}\ndocument.addEventListener(\"load\", handler)\ndocument.removeEventListener(\"load\", handler)\n\n@JSFunction func requestAnimationFrame(_ callback: JSCallback.Oneshot\u003c(Double) -\u003e Void\u003e)\n\nrequestAnimationFrame(JSCallback.Oneshot { timestamp in\n print(timestamp)\n})\n```\n\n### 3. [Need your input]\n","author":{"url":"https://github.com/kateinoigakukun","@type":"Person","name":"kateinoigakukun"},"datePublished":"2026-01-28T11:45:43.000Z","interactionStatistic":{"@type":"InteractionCounter","interactionType":"https://schema.org/CommentAction","userInteractionCount":0},"url":"https://github.com/541/JavaScriptKit/issues/541"}
| route-pattern | /_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format) |
| route-controller | voltron_issues_fragments |
| route-action | issue_layout |
| fetch-nonce | v2:57da3230-262f-71f2-6c9c-e22daef73293 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | D4A0:15B875:2BE193:37F542:69800922 |
| html-safe-nonce | dbfe37a6f9186e53a0367902af4a6ca0dcece18362296e87cfb7a089936a1f8c |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJENEEwOjE1Qjg3NToyQkUxOTM6MzdGNTQyOjY5ODAwOTIyIiwidmlzaXRvcl9pZCI6IjkwOTIwMTI1ODA0MzE2OTYyIiwicmVnaW9uX2VkZ2UiOiJpYWQiLCJyZWdpb25fcmVuZGVyIjoiaWFkIn0= |
| visitor-hmac | 728c837fb260fb86ebf0fb8f15780a6113366b46efc359953c29d990b3f0a116 |
| hovercard-subject-tag | issue:3865028795 |
| github-keyboard-shortcuts | repository,issues,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/swiftwasm/JavaScriptKit/541/issue_layout |
| twitter:image | https://opengraph.githubassets.com/99fa4002a5bce2c5d840ee8bfb130dfeb44d8b3c17774a7cc91cbf935365840a/swiftwasm/JavaScriptKit/issues/541 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/99fa4002a5bce2c5d840ee8bfb130dfeb44d8b3c17774a7cc91cbf935365840a/swiftwasm/JavaScriptKit/issues/541 |
| og:image:alt | Summary BridgeJS needs a typed closure handle type that preserves object identity across the Swift/JS bridge. We're looking for community input on naming. Background JavaScriptKit already has JSClo... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | kateinoigakukun |
| hostname | github.com |
| expected-hostname | github.com |
| None | 60279d4097367e16897439d16d6bbe4180663db828c666eeed2656988ffe59f6 |
| turbo-cache-control | no-preview |
| go-import | github.com/swiftwasm/JavaScriptKit git https://github.com/swiftwasm/JavaScriptKit.git |
| octolytics-dimension-user_id | 49500752 |
| octolytics-dimension-user_login | swiftwasm |
| octolytics-dimension-repository_id | 244832006 |
| octolytics-dimension-repository_nwo | swiftwasm/JavaScriptKit |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 244832006 |
| octolytics-dimension-repository_network_root_nwo | swiftwasm/JavaScriptKit |
| turbo-body-classes | logged-out env-production page-responsive |
| disable-turbo | false |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | 7c85641c598ad130c74f7bcc27f58575cac69551 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
Links:
Viewport: width=device-width