Reputation: 2967
As a follow-up to my question Dart JS Library, how to pass callback functions, I want to ensure that any functions passed through to D3 have the right parameters.
So I would like to do something like the following:
@JS('d3')
library d3;
import 'dart:js';
import "package:js/js.dart";
typedef num D3AccessorFunction(List<num> d, num i, List<List<num>> data);
@JS('line')
class Line {
external Line();
external String call (List<List<num>> data);
external Line x(D3AccessorFunction func);
external Line y(num func(List<num> d, num i, List<List<num>> data));
}
Then when I call the method:
Line line = new Line();
line.x(
allowInterop(
(List<num> d, num i, List<List<num>> data) {
return d[0]+10;
}
)
);
I would like it to complain if the parameters aren't matched properly, e.g.,
line.x(
allowInterop(
(List<num> d) {
return d[0]+10;
}
)
);
Unfortunately, the allowInterop swallows this complaint and you can't get rid of it. I've created a work-around (in the d3 definition):
Function d3Function(D3AccessorFunction function) {
return allowInterop(function);
}
Then I call it like this:
line.x(
d3Function(
(List<num> d, num i, List<List<num>> data) {
return d[0]+10;
}
)
);
Which will throw the right errors. But I'd like to know if there's a better way of doing this.
Upvotes: 1
Views: 697
Reputation: 2967
We no longer need to define our own JavaScript definition files, as Dart has a tool for doing so from TypeScript files. The JS Facade Generator. There's more information on using this on the official Dart Page about JS Interoperability.
Thanks to the DefinitelyTyped project, there's almost certainly a TypeScript file out there that will allow you to do this. If not, write one of those instead, it'll help both the Dart and TypeScript communities.
Upvotes: 1