W3C home > Mailing lists > Public > public-script-coord@w3.org > October to December 2011

Re: [WebIDL] Simplify callbacks

From: Brendan Eich <brendan@mozilla.org>
Date: Thu, 10 Nov 2011 10:52:26 -0800
Cc: public-script-coord@w3.org
Message-Id: <97E33F29-69C4-4434-82AB-E55AED54629B@mozilla.org>
To: David Flanagan <dflanagan@mozilla.com>
On Nov 10, 2011, at 10:14 AM, David Flanagan wrote:

> My understanding of Anne's original proposal is that the method name part was secondary.  Primarily, it was a proposal to simplify WebIDL so that spec authors wouldn't have to define useless single-method interfaces like EventListener, and decorate them with [Callback].  And also decorate them with [NoInterfaceObject] or clutter up the global namespace like EventListener does. 
> 
> In order to achieve that simplification, Anne suggested standardizing handleEvent() as the method name. No one likes that, but the basic idea is still an interesting one, if a suitable WebIDL syntax can be devised.  Some sort of WebIDL function type that is parameterized by (or annotated with) an allowed method name and also argument types if those are necessary.  So then we could declare methods like addEventListener without an EventListener interface:
> 
> void addEventListener(DOMString type, [MethodName=handleEvent] function<Event>? listener, optional boolean capture);
> 
> I'm not convinced that a suitably non-ugly syntax can be devised, however, and I don't know what Cameron thinks about this sort of wholesale alteration to WebIDL syntax.

A JS function has a call method, of the form

  return_type call(this_type, arg1_type, ... argN_type)

What if we standardized on a "Delegate" interface that has a call method with a suitably generic (in terms of types) form of this signature? Then you could pass JS function objects or Delegate-implementing objects equally well.

Using call instead of callback and making the receiver the first parameter, shifting the event object over one, could be interfaced to the existing EventListener legacy by defining a suitable call method on a prototype (in a WebIDL interface), with the implementation just forwarding, e.g.

  EventListener.prototype.call = function (target, event) { return target.handleEvent(event); }

I may be missing something, but function objects in JS already have a "handleEvent" method: call. It does take uncurried |this| as first parameter.

/be
Received on Thursday, 10 November 2011 18:53:06 UTC

This archive was generated by hypermail 2.3.1 : Wednesday, 8 May 2013 19:30:04 UTC