Skip to content

Commit

Permalink
memoize behavior method lists for fasting runtime calling.
Browse files Browse the repository at this point in the history
  • Loading branch information
Steven Orvell committed Oct 31, 2018
1 parent 09a6d1a commit 7251a3a
Showing 1 changed file with 75 additions and 73 deletions.
148 changes: 75 additions & 73 deletions lib/legacy/class.html
Original file line number Diff line number Diff line change
Expand Up @@ -14,32 +14,38 @@

'use strict';

let metaProps = {
const metaProps = {
attached: true,
detached: true,
ready: true,
created: true,
beforeRegister: true,
registered: true,
attributeChanged: true,
// meta objects
behaviors: true
};

const noBehaviorCopyProps = Object.assign({
behaviors: true
}, metaProps);

const memoizedProps = Object.assign({
listeners: true,
hostAttributes: true
}, metaProps);

function copyProperties(source, target) {
for (let p in source) {
// NOTE: cannot copy `metaProps` methods onto prototype at least because
// NOTE: cannot copy `noBehaviorCopyProps` methods onto prototype at least because
// `super.ready` must be called and is not included in the user fn.
if (!(p in metaProps)) {
if (!(p in noBehaviorCopyProps)) {
let pd = Object.getOwnPropertyDescriptor(source, p);
if (pd) {
Object.defineProperty(target, p, pd);
}
}
}
}

// TODO(sorvell): this breaks `Polymer.mixinBehaviors`; should fix via factoring

/**
* Applies a "legacy" behavior or array of behaviors to the provided class.
*
Expand Down Expand Up @@ -78,12 +84,10 @@
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 = _applyBehaviors(behaviors, klass);
if (superBehaviors) {
behaviors = superBehaviors.concat(behaviors);
}
// Set behaviors on prototype for BC...
// Set behaviors on prototype
klass.prototype.behaviors = behaviors;
return klass;
}
Expand Down Expand Up @@ -118,16 +122,24 @@
// 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 _applyBehaviors(behaviors, klass) {
function copyBehaviorProperties(behaviors, klass) {
const meta = {};
if (behaviors) {
klass.prototype.__behaviorMetaProps = meta;
for (let i=0; i<behaviors.length; i++) {
let b = behaviors[i];
// if (b) {
// Array.isArray(b) ? _applyBehaviors(b, klass) :
copyProperties(b, klass.prototype);
// }
copyProperties(behaviors[i], klass.prototype);
memoizeBehaviorMetaProps(meta, behaviors[i]);
}
}
klass.prototype.__behaviorMetaProps = meta;
}

function memoizeBehaviorMetaProps(meta, behavior) {
for (let p in memoizedProps) {
if (behavior[p]) {
meta[p] = meta[p] || [];
meta[p].push(behavior[p]);
}
return klass;
}
}

Expand Down Expand Up @@ -195,7 +207,7 @@
}
}
}

function mergeProperties(a, b) {
for (let p in b) {
a[p] = a[p] || {};
Expand Down Expand Up @@ -263,12 +275,10 @@
* @return {void}
*/
created() {
if (this.behaviors) {
for (let i=0, b; i < this.behaviors.length; i++) {
b = this.behaviors[i];
if (b.created) {
b.created.call(this);
}
const list = this.__behaviorMetaProps.created;
if (list) {
for (let i=0; i < list.length; i++) {
list[i].call(this);
}
}
if (info.created) {
Expand All @@ -288,17 +298,19 @@
`is` in `beforeRegister` as you could in 1.x.
*/
const proto = Object.getPrototypeOf(this);
_applyBehaviors(proto.behaviors, proto.constructor);
copyBehaviorProperties(proto.behaviors, proto.constructor);
copyProperties(info, proto);
if (proto.behaviors) {
for (let i=0, b; i < proto.behaviors.length; i++) {
b = proto.behaviors[i];
if (b.beforeRegister) {
b.beforeRegister.call(proto);
}
if (b.registered) {
b.registered.call(proto);
}
// Note, previously these were interleaved.
let list = this.__behaviorMetaProps.beforeRegister;
if (list) {
for (let i=0; i < list.length; i++) {
list[i].call(proto);
}
}
list = this.__behaviorMetaProps.registered;
if (list) {
for (let i=0; i < list.length; i++) {
list[i].call(proto);
}
}
if (info.beforeRegister) {
Expand All @@ -313,12 +325,13 @@
* @return {void}
*/
_applyListeners() {
if (this.behaviors) {
for (let i=0, b; i < this.behaviors.length; i++) {
b = this.behaviors[i];
if (b.listeners) {
for (let l in b.listeners) {
this._addMethodEventListenerToNode(this, l, b.listeners[l]);
const list = this.__behaviorMetaProps.listeners;
if (list) {
for (let i=0; i < list.length; i++) {
const listeners = list[i];
if (listeners) {
for (let l in listeners) {
this._addMethodEventListenerToNode(this, l, listeners[l]);
}
}
}
Expand All @@ -342,14 +355,13 @@
this._ensureAttribute(a, info.hostAttributes[a]);
}
}
if (this.behaviors) {
for (let i=this.behaviors.length-1, b; i >= 0; i--) {
b = this.behaviors[i];
if (b.hostAttributes) {
for (let a in b.hostAttributes) {
this._ensureAttribute(a, b.hostAttributes[a]);
const list = this.__behaviorMetaProps.hostAttributes;
if (list) {
for (let i=list.length-1; i >= 0; i--) {
const hostAttributes = list[i];
for (let a in hostAttributes) {
this._ensureAttribute(a, hostAttributes[a]);
}
}
}
}
}
Expand All @@ -359,12 +371,10 @@
*/
ready() {
super.ready();
if (this.behaviors) {
for (let i=0, b; i < this.behaviors.length; i++) {
b = this.behaviors[i];
if (b.ready) {
b.ready.call(this);
}
let list = this.__behaviorMetaProps.ready;
if (list) {
for (let i=0; i < list.length; i++) {
list[i].call(this);
}
}
if (info.ready) {
Expand All @@ -376,12 +386,10 @@
* @return {void}
*/
attached() {
if (this.behaviors) {
for (let i=0, b; i < this.behaviors.length; i++) {
b = this.behaviors[i];
if (b.attached) {
b.attached.call(this);
}
let list = this.__behaviorMetaProps.attached;
if (list) {
for (let i=0; i < list.length; i++) {
list[i].call(this);
}
}
if (info.attached) {
Expand All @@ -393,12 +401,10 @@
* @return {void}
*/
detached() {
if (this.behaviors) {
for (let i=0, b; i < this.behaviors.length; i++) {
b = this.behaviors[i];
if (b.detached) {
b.detached.call(this);
}
let list = this.__behaviorMetaProps.detached;
if (list) {
for (let i=0; i < list.length; i++) {
list[i].call(this);
}
}
if (info.detached) {
Expand All @@ -416,12 +422,10 @@
* @return {void}
*/
attributeChanged(name, old, value) {
if (this.behaviors) {
for (let i=0, b; i < this.behaviors.length; i++) {
b = this.behaviors[i];
if (b.attributeChanged) {
b.attributeChanged.call(this, name, old, value);
}
let list = this.__behaviorMetaProps.attributeChanged;
if (list) {
for (let i=0; i < list.length; i++) {
list[i].call(this, name, old, value);
}
}
if (info.attributeChanged) {
Expand All @@ -432,8 +436,6 @@

PolymerGenerated.generatedFrom = info;

// copyProperties(info, PolymerGenerated.prototype);

return PolymerGenerated;
}

Expand Down

0 comments on commit 7251a3a

Please sign in to comment.