Skip to content

Add methods to JS classes via Dart #30554

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
nex3 opened this issue Aug 25, 2017 · 2 comments
Open

Add methods to JS classes via Dart #30554

nex3 opened this issue Aug 25, 2017 · 2 comments
Labels
area-web-js Issues related to JavaScript support for Dart Web, including DDC, dart2js, and JS interop. type-enhancement A request for a change that isn't a bug web-js-interop Issues that impact all js interop

Comments

@nex3
Copy link
Member

nex3 commented Aug 25, 2017

There's currently no way for (non-SDK) Dart code to modify a class's prototype, which makes it difficult to expose Dart-like APIs for JS classes without resorting to inefficient wrapper objects. For example, we might want to expose Node.js's net.Server.close([callback]) method so it exposes a more Dart-y future API:

@JS()
class Server {
  Future close();
}

dart:html does this frequently, but the inability to do it outside the SDK means that user-defined JS wrappers can't provide APIs that are as usable as those in the SDK.

I propose we add an @OnPrototype([String jsName]) annotation to the js package that explicitly indicates that a method should be added to the class's prototype. For example:

@JS()
class Server {
  @JS("close")
  void _close([void callback([error])]);

  @OnPrototype("dart_close")
  Future close() {
    // ...
  }
}
@nex3 nex3 added web-js-interop Issues that impact all js interop type-enhancement A request for a change that isn't a bug labels Aug 25, 2017
@a14n
Copy link
Contributor

a14n commented Aug 26, 2017

Similar to #24779 ?

@aemino
Copy link

aemino commented Nov 27, 2017

This feature would be extremely useful. I'm not even sure if it's possible to create a workaround until this feature is implemented. Are there any plans for this?

Also, to achieve the requested functionality, it might not be necessary to modify the prototype of a class at all. Perhaps all methods not marked as external could be interpreted as non-JS methods?

@vsmenon vsmenon added the area-web-js Issues related to JavaScript support for Dart Web, including DDC, dart2js, and JS interop. label Jul 22, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-web-js Issues related to JavaScript support for Dart Web, including DDC, dart2js, and JS interop. type-enhancement A request for a change that isn't a bug web-js-interop Issues that impact all js interop
Projects
None yet
Development

No branches or pull requests

4 participants