Skip to content

Commit

Permalink
restores functionality of Polymer.mixinBehaviors
Browse files Browse the repository at this point in the history
  • Loading branch information
Steven Orvell committed Oct 17, 2018
1 parent 624513f commit 4af44c8
Showing 1 changed file with 15 additions and 7 deletions.
22 changes: 15 additions & 7 deletions lib/legacy/class.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,22 +53,31 @@ function copyProperties(source, target) {
* @suppress {invalidCasts, checkTypes}
*/
export function mixinBehaviors(behaviors, klass) {
if (behaviors) {
klass = applyBehaviors(behaviors, klass);
}
// provides behaviors functionality
return GenerateClassFromInfo({}, klass);
}


function applyBehaviors(behaviors, klass) {
if (!behaviors) {
klass = /** @type {HTMLElement} */(klass); // eslint-disable-line no-self-assign
return klass;
}
// NOTE: ensure the behavior is extending a class with
// legacy element api. This is necessary since behaviors expect to be able
// to access 1.x legacy api.
klass = LegacyElementMixin(klass);
klass = class extends LegacyElementMixin(klass) {};
if (!Array.isArray(behaviors)) {
behaviors = [behaviors];
}
let superBehaviors = klass.prototype.behaviors;
// get flattened, deduped list of behaviors *not* already on super class
behaviors = flattenBehaviors(behaviors, null, superBehaviors);
// mixin new behaviors
klass = _mixinBehaviors(behaviors, klass);
klass = _applyBehaviors(behaviors, klass);
if (superBehaviors) {
behaviors = superBehaviors.concat(behaviors);
}
Expand Down Expand Up @@ -107,11 +116,11 @@ export function mixinBehaviors(behaviors, klass) {
// If lifecycle is called (super then me), order is
// (1) C.created, (2) A.created, (3) B.created, (4) element.created
// (again same as 1.x)
function _mixinBehaviors(behaviors, klass) {
function _applyBehaviors(behaviors, klass) {
for (let i=0; i<behaviors.length; i++) {
let b = behaviors[i];
if (b) {
Array.isArray(b) ? _mixinBehaviors(b, klass) :
Array.isArray(b) ? _applyBehaviors(b, klass) :
copyProperties(b, klass.prototype);
}
}
Expand Down Expand Up @@ -479,11 +488,10 @@ export const Class = function(info, mixin) {
if (!info) {
console.warn(`Polymer's Class function requires \`info\` argument`);
}
const base = mixin ? mixin(LegacyElementMixin(HTMLElement)) :
let klass = mixin ? mixin(LegacyElementMixin(HTMLElement)) :
LegacyElementMixin(HTMLElement);
let klass = class extends base {};
if (info.behaviors) {
mixinBehaviors(info.behaviors, klass);
klass = applyBehaviors(info.behaviors, klass);
}
klass = GenerateClassFromInfo(info, klass);
// decorate klass with registration info
Expand Down

0 comments on commit 4af44c8

Please sign in to comment.