swiftwasm / WebAPIKit

Access the DOM and other Web APIs from Swift! (Very much a WIP)
MIT License
59 stars 7 forks source link

Support callback interfaces #34

Open j-f1 opened 2 years ago

j-f1 commented 2 years ago

These are a Cursed Object in the WebIDL spec (https://webidl.spec.whatwg.org/#idl-callback-interfaces), and they’re essentially shorthand for a union between a function/closure type and an object with a property matching that function type. They are unfortunately important because EventListener is a callback interface (although NodeFilter and XPathNSResolver are also a callback interface, they are less commonly used). This PR depends on #32, probably.

https://github.com/swiftwasm/DOMKit/blob/070f99efca195eaa38f46beeb4dc46ca1d6ce04b/Sources/WebIDLToSwift/IDLBuilder.swift#L82-L88

j-f1 commented 1 year ago

Update: Implemented EventListener in 61144ada0e1eba741d4416df7bc022e26aa35b29 by interpreting it as a simple closure (EventTarget) -> Void. This does technically mean you can’t pass an object as an event listener but I’ve never heard of anyone wanting to do that.

The other two callback interfaces are available as properties/method return values (EventListener is currently only used in the spec as a parameter), so it’s possible to get one of the object-y versions which could not be converted directly to a closure.