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

Allow for early returns in ForEach functions #390

Merged
merged 2 commits into from
Jul 5, 2018
Merged

Conversation

ggetz
Copy link
Contributor

@ggetz ggetz commented Jul 3, 2018

Restructure ForEach function to allow for early returns when defined values are returned from a handler function. This is often used in Cesium and I think the restructure is easier here than jumping through hoops there.

@ggetz ggetz requested a review from lilleyse July 3, 2018 17:48
Copy link
Contributor

@lilleyse lilleyse left a comment

Choose a reason for hiding this comment

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

Thanks @ggetz. I've definitely made mistakes before where I tried to return something from within a ForEach, so I'm glad that will work now.

lib/ForEach.js Outdated
return ForEach.mesh(gltf, function (mesh) {
return ForEach.meshPrimitive(mesh, function (primitive) {
var value;
value = ForEach.meshPrimitiveAttribute(primitive, function (accessorId, attributeSemantic) {
Copy link
Contributor

Choose a reason for hiding this comment

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

For all the value variables in here it should be fine to initialize them as var value = ... since they are all function scoped.

Same note for accessorContainingVertexAttributeData

lib/ForEach.js Outdated
@@ -20,12 +20,16 @@ function ForEach() {
* Fallback for glTF 1.0
* @private
*/
ForEach.objectLegacy = function(objects, handler) {
ForEach.objectLegacy = function (objects, handler) {
Copy link
Contributor

Choose a reason for hiding this comment

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

The style that Cesium uses (for the most part) is ForEach.objectLegacy = function(objects, handler) {

https://github.com/AnalyticalGraphicsInc/cesium/blob/master/Documentation/Contributors/CodingGuide/README.md#functions

var value = handler(object, objectId);

if (defined(value)) {
return value;
Copy link
Contributor

Choose a reason for hiding this comment

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

ForEachSpec should be updated to test early returns.

var value = handler(object, objectId);

if (defined(value)) {
return value;
Copy link
Contributor

Choose a reason for hiding this comment

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

Are there any area in gltf-pipeline that can take advantage of early returns now? https://github.com/AnalyticalGraphicsInc/gltf-pipeline/blob/2.0/lib/writeResources.js#L141-L152 comes to mind. If anything else comes up we can just fix as we notice them.

@ggetz
Copy link
Contributor Author

ggetz commented Jul 5, 2018

Thanks @lilleyse, updated!

@lilleyse
Copy link
Contributor

lilleyse commented Jul 5, 2018

Looks good, thanks!

@lilleyse lilleyse merged commit 7586da1 into 2.0 Jul 5, 2018
@lilleyse lilleyse deleted the for-each-return branch July 5, 2018 19:03
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants