Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bem-xjst: clean cached flags for wrap() and extend() (fix for #495) #496

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
# BEM-XJST Changelog

# 2018-04-16 [v8.9.2-rc.1](https://github.com/bem/bem-xjst/compare/v8.9.1...v8.9.2-rc.1), @miripiruni

Release candidate from https://github.com/bem/bem-xjst/pull/496 to check benchmarks on real project.


# 2018-03-05 [v8.9.1](https://github.com/bem/bem-xjst/compare/v8.9.0...v8.9.1), @miripiruni

Bug fixed: dot-delimited dependencies from global scope in object notation.
Expand Down
17 changes: 17 additions & 0 deletions lib/bemxjst/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ function BEMXJST(options) {
// Current match
this.match = null;

this._wraped = [];
this._extended = [];

// Create new Context constructor for overriding prototype
this.contextConstructor = function ContextChild(bemxjst) {
Context.call(this, bemxjst);
Expand Down Expand Up @@ -311,12 +314,26 @@ BEMXJST.prototype._run = function(context) {
return this.runOne(context);
};

BEMXJST.prototype.cleanWrapFlags = function(field) {
var cached = this[field];

if (cached.length === 0)
return;

for (var i = 0; i < cached.length; i++)
cached[i].wrap = null;
};

BEMXJST.prototype.run = function(json) {
var match = this.match;
var context = this.context;
var depth = this.depth;

this.match = null;

this.cleanWrapFlags('_extended');
this.cleanWrapFlags('_wraped');

this.context = new this.contextConstructor(this);
this.canFlush = this.context._flush !== null;
this.depth = 0;
Expand Down
10 changes: 8 additions & 2 deletions lib/bemxjst/match.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,9 @@ function MatchWrap(template) {
MatchWrap.prototype.exec = function(context) {
var res = this.wrap !== context.ctx;
this.wrap = context.ctx;

context._bemxjst._wraped.push(this);

return res;
};

Expand All @@ -54,8 +57,11 @@ function MatchExtend(template) {
}

MatchExtend.prototype.exec = function(context) {
var res = this.ext !== context.ctx;
this.ext = context.ctx;
var res = this.wrap !== context.ctx;
this.wrap = context.ctx;

context._bemxjst._extended.push(this);

return res;
};

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "bem-xjst",
"version": "8.9.1",
"version": "8.9.2-rc.1",
"description": "Declarative Template Engine for the browser and server",
"keywords": [
"template",
Expand Down
20 changes: 20 additions & 0 deletions test/modes-extend-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,4 +81,24 @@ describe('Modes extend', function() {
}, { block: 'b', foo: 'This is' },
'<div class="b">This is ContextChild</div>');
});

it('should work with several apply() calls', function() {
var bemjson = { block: 'b1' };
var expected = '<div class="b1">42</div>';
var tmpl = fixtures.compile(function() {
block('b1').extend()({ 'ctx.content': 42 });
});

assert.equal(
tmpl.apply(bemjson),
expected,
'first apply() call returns not expected value'
);

assert.equal(
tmpl.apply(bemjson),
expected,
'second apply() call returns not expected value'
);
});
});
24 changes: 24 additions & 0 deletions test/modes-wrap-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,30 @@ describe('Modes wrap', function() {
}, { block: 'b1' }, '<div class="b1"></div>');
});

it('should work with several apply() calls', function() {
var bemjson = { block: 'b1' };
var expected = '<div class="b2"><div class="b1"></div></div>';
var tmpl = fixtures.compile(function() {
block('b1').wrap()(function() {
return {
block: 'b2',
content: this.ctx
};
});
});

assert.equal(
tmpl.apply(bemjson),
expected,
'first apply() call returns not expected value'
);

assert.equal(
tmpl.apply(bemjson),
expected,
'second apply() call returns not expected value'
);
});

it('should use current context (with simple value)', function() {
test(function() {
Expand Down