Skip to content

Commit

Permalink
tree: do not use .map() too
Browse files Browse the repository at this point in the history
  • Loading branch information
indutny committed Apr 27, 2015
1 parent 2c1db76 commit 43b5596
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 33 deletions.
2 changes: 1 addition & 1 deletion lib/bemhtml/runtime/entity.js
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ Entity.prototype.initModes = function initModes(templates) {
// .def() default
if (this.def.count !== 0) {
var self = this;
this.def.push(new Template([], function() {
this.def.push(new Template([], function defaultBodyProxy() {
return self.defaultBody(this);
}));
}
Expand Down
2 changes: 1 addition & 1 deletion lib/bemhtml/runtime/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ BEMHTML.prototype.compile = function compile(templates) {
}

function localWrap(changes) {
return function(body) {
return function localBody(body) {
return self.local(changes, body);
};
}
Expand Down
68 changes: 38 additions & 30 deletions lib/bemhtml/runtime/tree.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,33 +87,41 @@ Tree.prototype.build = function build(templates, apply) {
};
};

Tree.prototype.methods = function methods(kind) {
return Tree.methods.map(function(name) {
var self = this;
var method = this[name];
var boundBody = this.boundBody;

if (kind !== 'body') {
return function wrapNotBody() {
method.apply(self, arguments);
return boundBody;
};
}

return function wrapBody() {
var res = method.apply(self, arguments);

// Insert body into last item
var child = self.queue.pop();
var last = self.queue[self.queue.length - 1];
last.conditions = last.conditions.concat(child.conditions);
last.children = last.children.concat(child.children);

if (name === 'replace' || name === 'extend')
return res;
function methodFactory(self, kind, name) {
var method = self[name];
var boundBody = self.boundBody;

if (kind !== 'body') {
return function wrapNotBody() {
method.apply(self, arguments);
return boundBody;
};
}, this);
}

return function wrapBody() {
var res = method.apply(self, arguments);

// Insert body into last item
var child = self.queue.pop();
var last = self.queue[self.queue.length - 1];
last.conditions = last.conditions.concat(child.conditions);
last.children = last.children.concat(child.children);

if (name === 'replace' || name === 'extend')
return res;
return boundBody;
};
}

Tree.prototype.methods = function methods(kind) {
var out = new Array(Tree.methods.length);

for (var i = 0; i < out.length; i++) {
var name = Tree.methods[i];
out[i] = methodFactory(this, kind, name);
}

return out;
};

// Called after all matches
Expand Down Expand Up @@ -203,8 +211,8 @@ Tree.prototype.content = function content() { return this.mode('content'); };
Tree.prototype.replace = function replace() {
var adaptor = this.def();
var applyCtx = this.refs.applyCtx;
return function (body) {
return adaptor(function() {
return function replaceBody(body) {
return adaptor(function replaceAdaptor() {
return applyCtx(body());
});
};
Expand All @@ -214,16 +222,16 @@ Tree.prototype.extend = function extend() {
var adaptor = this.def();
var local = this.refs.local;
var applyCtx = this.refs.applyCtx;
return function (body) {
return adaptor(function () {
return function localBody(body) {
return adaptor(function localAdaptor() {
var changes = {};

var obj = body();
var keys = Object.keys(obj);
for (var i = 0; i < keys.length; i++)
changes['ctx.' + keys[i]] = obj[keys[i]];

return local(changes)(function() {
return local(changes)(function preApplyCtx() {
return applyCtx(this.ctx);
});
});
Expand Down
2 changes: 1 addition & 1 deletion lib/bemhtml/runtime/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ function getUniq() {
}
exports.getUniq = getUniq;

exports.identify = function(obj, onlyGet) {
exports.identify = function identify(obj, onlyGet) {
if (!obj)
return getUniq();
if (onlyGet || obj[uniqExpando])
Expand Down

0 comments on commit 43b5596

Please sign in to comment.