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

Simplify and optimize some GraphNode find functions #5850

Merged
merged 4 commits into from
Dec 4, 2023
Merged
Changes from 1 commit
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
103 changes: 34 additions & 69 deletions src/scene/graph-node.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,27 @@ const matrix = new Mat4();
const target = new Vec3();
const up = new Vec3();

/**
* Helper function that handles signature overloading to receive a test function.
*
* @param {FindNodeCallback|string} attr - Attribute or lambda.
* @param {*} [value] - Optional value in case of `attr` being a `string`
* @returns {FindNodeCallback} Test function that receives a GraphNode and returns a boolean.
* @ignore
*/
function createTest(attr, value) {
if (attr instanceof Function) {
return attr;
}
return (node) => {
let x = node[attr];
if (x instanceof Function) {
x = x();
}
return x === value;
};
}

/**
* Callback used by {@link GraphNode#find} and {@link GraphNode#findOne} to search through a graph
* node and all of its descendants.
Expand Down Expand Up @@ -534,40 +555,13 @@ class GraphNode extends EventHandler {
* const entities = parent.find('name', 'Test');
*/
find(attr, value) {
let result, results = [];
const len = this._children.length;

if (attr instanceof Function) {
const fn = attr;

result = fn(this);
if (result)
results.push(this);

for (let i = 0; i < len; i++) {
const descendants = this._children[i].find(fn);
if (descendants.length)
results = results.concat(descendants);
}
} else {
let testValue;

if (this[attr]) {
if (this[attr] instanceof Function) {
testValue = this[attr]();
} else {
testValue = this[attr];
}
if (testValue === value)
results.push(this);
}
const results = [];
const test = createTest(attr, value);

for (let i = 0; i < len; ++i) {
const descendants = this._children[i].find(attr, value);
if (descendants.length)
results = results.concat(descendants);
}
}
this.forEach((node) => {
if (test(node))
results.push(node);
slimbuck marked this conversation as resolved.
Show resolved Hide resolved
});

return results;
}
Expand Down Expand Up @@ -597,39 +591,16 @@ class GraphNode extends EventHandler {
* const node = parent.findOne('name', 'Test');
*/
findOne(attr, value) {
const test = createTest(attr, value);
const len = this._children.length;
let result = null;

if (attr instanceof Function) {
const fn = attr;
if (test(this))
return this;

result = fn(this);
for (let i = 0; i < len; ++i) {
const result = this._children[i].findOne(test);
slimbuck marked this conversation as resolved.
Show resolved Hide resolved
if (result)
return this;

for (let i = 0; i < len; i++) {
result = this._children[i].findOne(fn);
if (result)
return result;
}
} else {
let testValue;
if (this[attr]) {
if (this[attr] instanceof Function) {
testValue = this[attr]();
} else {
testValue = this[attr];
}
if (testValue === value) {
return this;
}
}

for (let i = 0; i < len; i++) {
result = this._children[i].findOne(attr, value);
if (result !== null)
return result;
}
return result;
}

return null;
Expand Down Expand Up @@ -683,13 +654,7 @@ class GraphNode extends EventHandler {
* null if no node is found.
*/
findByName(name) {
if (this.name === name) return this;

for (let i = 0; i < this._children.length; i++) {
const found = this._children[i].findByName(name);
if (found !== null) return found;
}
return null;
return this.findOne('name', name);
}

/**
Expand Down