Skip to content

Safe lazyInit for declMod #1584

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

Open
wants to merge 1 commit into
base: v4
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
81 changes: 74 additions & 7 deletions common.blocks/i-bem-dom/i-bem-dom.js
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ function initEntity(entityName, domElem, params, ignoreLazyInit, callback) {

entityCls._processInit();

if(ignoreLazyInit || params.lazyInit === false || !entityCls.lazyInit && !params.lazyInit) {
if(ignoreLazyInit || params.lazyInit === false || !entityCls._checkLazyInit(domElem[0]) && !params.lazyInit) {
ignoreLazyInit && domElem.addClass(BEM_CLASS_NAME); // add css class for preventing memory leaks in further destructing

entity = new entityCls(uniqIdToDomElems[uniqId], params, !!ignoreLazyInit);
Expand Down Expand Up @@ -341,6 +341,38 @@ function getEntityBase(baseCls, entityName, base) {
return base;
}

/**
* Extract lazyInit property from static props
* @param {Object} [staticProps]
* @returns {?Boolean}
*/
function extractLazyInitProp(staticProps) {
if(staticProps && staticProps.lazyInit !== undef) {
var lazyInit = staticProps.lazyInit;
delete staticProps.lazyInit;
return lazyInit;
}

return null;
}

/**
* Processing lazyInit rules for entity
* @param {Function} entity BemDomEntity
* @param {Object} mod mod declaration
* @param {Boolean} lazyInit lazyInit behavior
* @returns {?Boolean}
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ничего не возвращает. Походу опечатка.

*/
function processLazyInitRule(entity, mod, lazyInit) {
var rules = entity._lazyInitRules;
Copy link
Member Author

@belozer belozer Jul 26, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

В переменной нет смысла вообще. Нужно выбросить.


rules.push({
check : entity._buildModValRE(mod.modName, mod.modVal),
modName : mod.modName,
lazyInit : lazyInit
});
}

/**
* @class BemDomEntity
* @description Base mix for BEM entities that have DOM representation
Expand Down Expand Up @@ -770,11 +802,12 @@ var BemDomEntity = inherit(/** @lends BemDomEntity.prototype */{

/** @override */
declMod : function(mod, props, staticProps) {
if(staticProps && staticProps.lazyInit !== undef) {
throw Error('declMod with lazyInit prop not allowed. Your need use \'lazyInit\' in data-bem params');
}
var lazyInit = extractLazyInitProp(staticProps),
entity = this.__base.apply(this, arguments);

lazyInit !== null && processLazyInitRule(entity, mod, lazyInit);

return this.__base.apply(this, arguments);
return entity;
},

/** @override */
Expand Down Expand Up @@ -831,13 +864,16 @@ var BemDomEntity = inherit(/** @lends BemDomEntity.prototype */{
* Builds a regular expression for extracting modifier values from a DOM element of an entity
* @private
* @param {String} modName Modifier name
* @param {String} [modVal] Modifier value
* @returns {RegExp}
*/
_buildModValRE : function(modName) {
_buildModValRE : function(modName, modVal) {
modVal = (modVal === '*' || modVal === undef)? NAME_PATTERN : modVal;

return new RegExp(
'(\\s|^)' +
this._buildModClassNamePrefix(modName) +
'(?:' + MOD_DELIM + '(' + NAME_PATTERN + '))?(?=\\s|$)');
'(?:' + MOD_DELIM + '(' + modVal + '))?(?=\\s|$)');
},

/**
Expand All @@ -860,6 +896,33 @@ var BemDomEntity = inherit(/** @lends BemDomEntity.prototype */{
*/
_buildSelector : function(modName, modVal) {
return '.' + this._buildClassName(modName, modVal);
},

/**
* Check domNode for lazy initialization entity
* @protected
* @param {HTMLElement} domNode
* @returns {Boolean|undefined}
*/
_checkLazyInit : function(domNode) {
var rules = this._lazyInitRules,
len = rules.length,
rule,
lazyInit,
modName;

while(rule = rules[--len]) {
if(modName !== rule.modName && rule.check.test(domNode.className)) {
if(lazyInit === undef || lazyInit === true) {
modName = rule.modName;
lazyInit = rule.lazyInit;
}

if(lazyInit === false) return lazyInit;
}
}

return lazyInit !== undef? lazyInit : this.lazyInit;
}
});

Expand All @@ -874,6 +937,8 @@ var Block = inherit([bem.Block, BemDomEntity], /** @lends Block.prototype */{
_block : function() {
return this;
}
}, {
_lazyInitRules : []
});

/**
Expand All @@ -887,6 +952,8 @@ var Elem = inherit([bem.Elem, BemDomEntity], /** @lends Elem.prototype */{
_block : function() {
return this._blockInstance || (this._blockInstance = this.findParentBlock(getEntityCls(this.__self._blockName)));
}
}, {
_lazyInitRules : []
});

/**
Expand Down
95 changes: 85 additions & 10 deletions common.blocks/i-bem-dom/i-bem-dom.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -164,16 +164,6 @@ describe('i-bem-dom', function() {
block2.should.be.instanceOf(Block1);
elem2.should.be.instanceOf(Elem1);
});

it('should throw error if declMod contains lazyInit static property', function() {
var Block = bemDom.declBlock('block');

function mod() {
Block.declMod({ modName : 'mod' }, null, { lazyInit : true });
}

mod.should.throw(Error, 'declMod with lazyInit prop not allowed. Your need use \'lazyInit\' in data-bem params');
});
});

describe('getMod', function() {
Expand Down Expand Up @@ -1675,6 +1665,91 @@ describe('i-bem-dom', function() {
describe('lazy init', function() {
var spy;

['block', 'elem'].forEach(function(entityType) {
it('should have different lazyInit for base ' + entityType + ' and modifiers', function() {
var spy1 = sinon.spy(),
spy2 = sinon.spy(),
spy3 = sinon.spy(),
spy4 = sinon.spy(),
spy5 = sinon.spy(),

Entity = entityType === 'block'? bemDom.declBlock('block', {
onSetMod : { js : { inited : spy1 } }
}, {
lazyInit : true
}) : bemDom.declElem('block', 'elem', {
onSetMod : { js : { inited : spy1 } }
}, {
lazyInit : true
});

Entity.declMod({ modName : 'm1' }, {
onSetMod : { js : { inited : spy2 } }
}, {
lazyInit : false
});

Entity.declMod({ modName : 'm2', modVal : true }, {
onSetMod : { js : { inited : spy3 } }
});

Entity.declMod({ modName : 'm3', modVal : '*' }, {
onSetMod : { js : { inited : spy4 } }
}, {
lazyInit : false
});

Entity.declMod({ modName : 'm3', modVal : 'v2' }, {
onSetMod : { js : { inited : spy5 } }
}, {
lazyInit : true
});

rootNode = initDom([
{ },
{ m1 : true },
{ m2 : true },
{ m3 : 'v1' },
{ m3 : 'v2' }
].map(function(mods) {
var bemjson = entityType === 'block'? { mods : mods }
: { elem : 'elem', elemMods : mods };

bemjson.block = 'block';
bemjson.js = true;

return bemjson;
}));

spy1.should.have.not.been.called;
spy2.should.have.been.called;
spy3.should.have.not.been.called;
spy4.should.have.been.called;
spy5.should.have.not.been.called;
});
});

it('should use force init priority on domNode between multiple mods', function() {
spy = sinon.spy();

var Block = bemDom.declBlock('block', {
onSetMod : { js : { inited : spy } }
}, {
lazyInit : false
});

Block.declMod({ modName : 'm1' }, null, { lazyInit : false });
Block.declMod({ modName : 'm2' }, null, { lazyInit : true });

rootNode = initDom([
{ block : 'block', mods : { m1 : true, m2 : true }, js : true },
{ block : 'block', mods : { m1 : true }, js : true },
{ block : 'block', mods : { m2 : true }, js : true },
]);

spy.should.have.calledTwice;
});

it('should be possible to force initialization', function() {
spy = sinon.spy();

Expand Down