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
WIP: add registerController for controllers #7407
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for this!!
I'd also change core.controller
to use the registry. Look for the line const ControllerClass = controllers[meta.type];
BarController.id = 'bar'; | ||
BarController.defaults = defaults; | ||
BarController.preRegister = () => { | ||
BarController.prototype.dataElementType = Rectangle; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I can't find anyone else online suggesting that prototype
caused them an issue. Maybe babel is transforming it first which is causing issues? (e.g. Object.defineProperty(BarController.prototype, ...
or something). Otherwise I think I'd report an issue to Rollup and see what they say before we introduce this workaround because it's a bit weird to me that this would cause trouble but BarController.id = 'bar';
wouldn't
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
take a look at
and you can play around yourself. btw. webpack is not better, in this simple test it not even shaked out the .id
variant
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should we move id
inside preRegister
as well then to help webpack?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
then .preRegister
still remains which has the same effect.
I don't know how to have something like .registerController(LineController)
without having additional attributes like id, defaults, preRegister.
one could extract everything to a separate method: registerLineController
but this be not as handy
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good point. Thanks for that repl! It's very informative and helpful
I filed an issue with rollup to see if there's anything they can do on their side, but we should figure out what's possible just on our end in the meantime
I don't know how to have something like .registerController(LineController) without having additional attributes like id, defaults, preRegister.
What if we do something like:
export function line() {
const defaults = { ... };
registry.registerContoller('line', LineController, defaults);
}
Then users could do:
import { Chart, line, categoryScale, lineScale } from 'chart.js';
[ line, categoryScale, lineScale ].forEach(f => f());
I did change the import, so it should already use them. |
src/controllers/controller.bar.js
Outdated
]; | ||
BarController.id = 'bar'; | ||
BarController.defaults = defaults; | ||
BarController.preRegister = () => { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could use beforeRegister
and afterRegister
? That way we'd match the format of other lifecycle calls
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I agree tho those names and we could have those hooks, but I think we should not expect anything to happen in beforeRegister/afterRegister. Maybe we can come up with a better name for this method. initializePrototype
(just to get you thinking, I don't like that either)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The id
and defaults
changes seem obvious and good.
LineController.id = 'line'; | ||
LineController.defaults = defaults; | ||
LineController.preRegister = () => { | ||
LineController.prototype.datasetElementType = Line; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Somehow I don't like a function that modifies the prototype. But I don't really know why :)
Other option that comes to mind is return the things from a method or methods.
getDataElementType
etc.
src/controllers/controller.bar.js
Outdated
]; | ||
BarController.id = 'bar'; | ||
BarController.defaults = defaults; | ||
BarController.preRegister = () => { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I agree tho those names and we could have those hooks, but I think we should not expect anything to happen in beforeRegister/afterRegister. Maybe we can come up with a better name for this method. initializePrototype
(just to get you thinking, I don't like that either)
seems like there are three main open points how to deal with the
how to name the
should controllers auto registers scales / [elements] e.g., pro: simpler configuration without the need to know details about the controller. e..g con: in case the scale configuration is customized, e.g., using a time scale or logarithmic scale, the |
I got some tips from the rollup folks:
We should probably test in Webpack as well, but I do see mention in their docs about the pure annotations, so it seems likely that would work |
I'd vote no to this question for the reason you mentioned as the con. E.g. I most frequently use a |
Rollup has a PR pending now to fix the tree shaking on their end without us having to do anything extra: rollup/rollup#3598. Anyone want to file a request with Webpack? :-) |
closed in favor of #7435 |
implement one possible variant for registering controllers. whether the
registerController
method is wrapped with an genericregister
method is to be discussed.The optional
preRegister
method is used to avoid using the.prototype
statically which prevents tree shaking in my experiments. Similarly settings defaults by calling a method likemerge
will also prevent tree shaking.To sum from my experiments: