Skip to content
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

[3.x] Add legacy-data-mixin as 1.x->2.x/3.x migration aide. Fixes #5262. #5339

Merged
merged 7 commits into from
Aug 28, 2018

Conversation

kevinpschaaf
Copy link
Member

Add legacy-data-mixin as 1.x->2.x/3.x migration aide.

Mixin to selectively add back Polymer 1.x's undefined rules governing when observers & computing functions run based on all arguments being defined (reference https://www.polymer-project.org/1.0/docs/devguide/observers#multi-property-observers).

When polymer/lib/legacy/legacy-data-mixin.js is loaded at the app level, all legacy elements (defined with Polymer({...})) will have the mixin applied. The mixin only restores legacy data handling if _legacyUndefinedCheck: true is set on the element's prototype.

This mixin is intended for use to help migration from Polymer 1.x to 2.x+ by allowing legacy code to work during the migration period, while identifying observers and computing functions that need undefined checks to work without the mixin in Polymer 2/3. It is generally not intended for use in production.

Reference Issue

Fixes #5262.
Supersedes and closes #5263

@kevinpschaaf kevinpschaaf changed the title Add legacy-data-mixin as 1.x->2.x/3.x migration aide. Fixes #5262. [3.x] Add legacy-data-mixin as 1.x->2.x/3.x migration aide. Fixes #5262. Aug 21, 2018
constructor(message) {
super(message);
this.name = this.constructor.name;
// Affordances for ensuring instanceof works after babel ES5 compilation
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Surprised this is needed. Verify?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The version of babel we use in CLI doesn't handle subclassing builtins correctly; the latest one does, so will add TODO to remove once CLI updates to latest babel.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note: should be fixed by Polymer/tools#670

* @private */
class LegacyDataMixin extends superClass {
/**
* Overrides `Polyer.PropertyEffects` to add `undefined` argument
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

s/Polyer/Polymer

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

*/
_marshalArgs(args, path, props) {
const vals = super._marshalArgs(args, path, props);
if (this._legacyUndefinedCheck && args.length > 1) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add comment briefly explaining the rules so it's clear that single argument observers are always called.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

if (vals[i] === undefined) {
// Break out of effect's control flow; will be caught in
// wrapped property effect function below
throw new UndefinedArgumentError(`argument '${args[i].name}' is undefined; ensure it has an undefined check`);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggest making the error use sentence casing and periods.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

_marshalArgs(args, path, props) {
const vals = super._marshalArgs(args, path, props);
if (this._legacyUndefinedCheck && args.length > 1) {
for (let i=0; i<args.length; i++) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems like it would be more clear to loop over vals?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

const fn = effect.fn;
effect.fn = function() {
try {
fn.apply(this, arguments);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we just check the arguments here and avoid the need to try catch the error? Then you don't even need to expose _marshalArgs.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, needs to also abort computed properties & inline computing functions. Will add tests for those.

},
_legacyUndefinedCheck: true,
observers: [
'staticObserver("staticObserver")',
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't we also test inline template expressions that have multiple arguments?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added.


});

Polymer.Class = (info, mixin) => Class(info,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not just put LegacyDataMixin on top of the user class?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, the answer is no because the mixin customizes how properties are created on the element prototype. Since a subclass only creates properties that it defines, it will not modify properties created by its superclass.

* @return {Array<*>} Array of argument values
* @private
*/
_marshalArgs(args, path, props) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Did you consider altering this method to get the info needed to present a good error? I'm concerned that since the intension is to warn, but the code throws here, we're depending on catching that where it's called.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, disregard that. It's clear that we need to abort the effect of marshaling the arguments and throwing let's us do this.

@kevinpschaaf kevinpschaaf merged commit 0d35437 into master Aug 28, 2018
@kevinpschaaf kevinpschaaf deleted the 5262-kschaaf-legacy-undefined-3.x branch August 28, 2018 18:02
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add 2.x option to treat complex observers as they were in Polymer1
4 participants