Skip to content

Commit

Permalink
Use AFRAME.utils.split in getComponentPropertyPath fixing caching beh…
Browse files Browse the repository at this point in the history
…aviour (#5467)

Co-authored-by: Noeri Huisman <mrxz@users.noreply.github.com>
  • Loading branch information
mrxz and mrxz committed Feb 17, 2024
1 parent afaa8d0 commit 32f5370
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 13 deletions.
16 changes: 7 additions & 9 deletions src/utils/entity.js
Original file line number Diff line number Diff line change
@@ -1,27 +1,25 @@
var split = require('./split').split;

/**
* Split a delimited component property string (e.g., `material.color`) to an object
* containing `component` name and `property` name. If there is no delimiter, just return the
* string back.
*
* Cache arrays from splitting strings via delimiter to save on memory.
* Uses the caching split implementation `AFRAME.utils.split`
*
* @param {string} str - e.g., `material.opacity`.
* @param {string} delimiter - e.g., `.`.
* @returns {array} e.g., `['material', 'opacity']`.
*/
var propertyPathCache = {};
function getComponentPropertyPath (str, delimiter) {
delimiter = delimiter || '.';
if (!propertyPathCache[delimiter]) { propertyPathCache[delimiter] = {}; }
if (str.indexOf(delimiter) !== -1) {
propertyPathCache[delimiter][str] = str.split(delimiter);
} else {
propertyPathCache[delimiter][str] = str;
var parts = split(str, delimiter);
if (parts.length === 1) {
return parts[0];
}
return propertyPathCache[delimiter][str];
return parts;
}
module.exports.getComponentPropertyPath = getComponentPropertyPath;
module.exports.propertyPathCache = propertyPathCache;

/**
* Get component property using encoded component name + component property name with a
Expand Down
4 changes: 0 additions & 4 deletions tests/utils/entity.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,16 +24,12 @@ suite('utils.entity', function () {
var el = this.el;
el.setAttribute('material', {color: 'red'});
assert.equal(getComponentProperty(el, 'material.color'), 'red');
assert.equal(entity.propertyPathCache['.']['material.color'][0], 'material');
assert.equal(entity.propertyPathCache['.']['material.color'][1], 'color');
});

test('can get custom-delimited attribute', function () {
var el = this.el;
el.setAttribute('material', {color: 'red'});
assert.equal(getComponentProperty(el, 'material|color', '|'), 'red');
assert.equal(entity.propertyPathCache['|']['material|color'][0], 'material');
assert.equal(entity.propertyPathCache['|']['material|color'][1], 'color');
});
});

Expand Down

0 comments on commit 32f5370

Please sign in to comment.