Skip to content

Commit

Permalink
move the mixing-in of behaviors so that it happens before register
Browse files Browse the repository at this point in the history
…behaviors are invoked
  • Loading branch information
Scott J Miles committed Aug 26, 2015
1 parent e00bd6a commit 637367c
Showing 1 changed file with 30 additions and 22 deletions.
52 changes: 30 additions & 22 deletions src/micro/behaviors.html
Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,23 @@

_desugarBehaviors: function() {
if (this.behaviors.length) {
this.behaviors = this._flattenBehaviorsList(this.behaviors);
this.behaviors = this._desugarSomeBehaviors(this.behaviors);
}
},

_desugarSomeBehaviors: function(behaviors) {
// iteration 1
behaviors = this._flattenBehaviorsList(behaviors);
// iteration 2
// traverse the behaviors in _reverse_ order (youngest first) because
// `_mixinBehavior` has _first property wins_ behavior, this is done
// to optimize # of calls to `_copyOwnProperty`
for (var i=behaviors.length-1; i>=0; i--) {
this._mixinBehavior(behaviors[i]);
}
return behaviors;
},

_flattenBehaviorsList: function(behaviors) {
var flat = [];
behaviors.forEach(function(b) {
Expand All @@ -80,28 +93,8 @@
return flat;
},

_prepBehaviors: function() {
this._prepFlattenedBehaviors(this.behaviors);
},

_prepFlattenedBehaviors: function(behaviors) {
// traverse the behaviors in _reverse_ order (youngest first) because
// `_mixinBehavior` has _first property wins_ behavior, this is done
// to optimize # of calls to `_copyOwnProperty`
for (var i=behaviors.length-1; i>=0; i--) {
this._mixinBehavior(behaviors[i]);
}
// we iterate a second time so that `_prepBehavior` goes in natural order
// otherwise, it's a tricky detail for implementors of `_prepBehavior`
for (var i=0, l=behaviors.length; i<l; i++) {
this._prepBehavior(behaviors[i]);
}
// prep our prototype-as-behavior
this._prepBehavior(this);
},

_mixinBehavior: function(b) {
Object.getOwnPropertyNames(b).forEach(function(n) {
Object.getOwnPropertyNames(b).forEach(function(n) {
switch (n) {
case 'hostAttributes':
case 'registered':
Expand All @@ -124,6 +117,21 @@
}, this);
},

_prepBehaviors: function() {
this._prepFlattenedBehaviors(this.behaviors);
},

_prepFlattenedBehaviors: function(behaviors) {
// iteration 3
// `_prepBehavior` goes in natural order
// otherwise, it's a tricky detail for implementors of `_prepBehavior`
for (var i=0, l=behaviors.length; i<l; i++) {
this._prepBehavior(behaviors[i]);
}
// prep our prototype-as-behavior
this._prepBehavior(this);
},

_doBehavior: function(name, args) {
this.behaviors.forEach(function(b) {
this._invokeBehavior(b, name, args);
Expand Down

0 comments on commit 637367c

Please sign in to comment.