-
Notifications
You must be signed in to change notification settings - Fork 2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add legacy-data-mixin as 1.x->2.x migration aide. Fixes #5262.
- Loading branch information
1 parent
cbcf991
commit c46a38d
Showing
4 changed files
with
440 additions
and
54 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
<!-- | ||
@license | ||
Copyright (c) 2017 The Polymer Project Authors. All rights reserved. | ||
This code may only be used under the BSD style license found at http://polymer.github.io/LICENSE.txt | ||
The complete set of authors may be found at http://polymer.github.io/AUTHORS.txt | ||
The complete set of contributors may be found at http://polymer.github.io/CONTRIBUTORS.txt | ||
Code distributed by Google as part of the polymer project is also | ||
subject to an additional IP rights grant found at http://polymer.github.io/PATENTS.txt | ||
--> | ||
|
||
<link rel="import" href="../utils/mixin.html"> | ||
|
||
<script> | ||
(function() { | ||
'use strict'; | ||
|
||
const UndefinedArgumentError = class extends Error {}; | ||
|
||
/** | ||
* 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 loaded, 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 while identifying observers and | ||
* computing functions that need undefined checks to work without | ||
* the mixin in Polymer 2. | ||
*/ | ||
Polymer.LegacyDataMixin = Polymer.dedupingMixin(superClass => { | ||
|
||
/** | ||
* @polymer | ||
* @mixinClass | ||
* @implements {Polymer_LegacyDataMixin} | ||
*/ | ||
class LegacyDataMixin extends superClass { | ||
/** | ||
* Overrides `Polyer.PropertyEffects` to add `undefined` argument | ||
* checking to match Polymer 1.x style rules | ||
* | ||
* @param {!Array<!MethodArg>} args Array of argument metadata | ||
* @param {string} path Property/path name that triggered the method effect | ||
* @param {Object} props Bag of current property changes | ||
* @return {Array<*>} Array of argument values | ||
* @private | ||
*/ | ||
_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++) { | ||
if (vals[i] === undefined) { | ||
throw new UndefinedArgumentError(`argument '${args[i].name}' is undefined; ensure it has an undefined check`); | ||
} | ||
} | ||
} | ||
return vals; | ||
} | ||
|
||
/** | ||
* Overrides `Polyer.PropertyEffects` to wrap effect functions to | ||
* catch `UndefinedArgumentError`s and warn. | ||
* | ||
* @param {string} property Property that should trigger the effect | ||
* @param {string} type Effect type, from this.PROPERTY_EFFECT_TYPES | ||
* @param {Object=} effect Effect metadata object | ||
* @return {void} | ||
* @protected | ||
*/ | ||
_addPropertyEffect(property, type, effect) { | ||
if (effect && effect.fn) { | ||
const fn = effect.fn; | ||
effect.fn = function() { | ||
try { | ||
fn.apply(this, arguments); | ||
} catch (e) { | ||
if (e instanceof UndefinedArgumentError) { | ||
console.warn(e.message); | ||
} else { | ||
throw e; | ||
} | ||
} | ||
}; | ||
} | ||
return super._addPropertyEffect(property, type, effect); | ||
} | ||
|
||
} | ||
|
||
return LegacyDataMixin; | ||
|
||
}); | ||
|
||
const Class = Polymer.Class; | ||
Polymer.Class = info => Class(info, Polymer.LegacyDataMixin); | ||
|
||
console.info('LegacyDataMixin will be applied to all legacy elements.\n' + | ||
'Set `_legacyUndefinedCheck: true` to enable.'); | ||
|
||
})(); | ||
</script> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.