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

Add findPathSync to find & calculate a path then return the result directly #62

Open
wants to merge 6 commits into
base: master
Choose a base branch
from

Conversation

mikedevelops
Copy link

The proposal is an implementation similar to (#54).

This change would not remove or modify from the existing public API or existing enableSync() method. It adds an additional method to the public API that allows users to find a path and calculate it's route truly synchronously with no callback.

Example usage...

var easyStar = new EasyStar.js();
var grid = [
    [0, 1, 0],
    [0, 0, 0],
    [0, 0, 0],
];
easyStar.setGrid(grid);
easyStar.setAcceptableTiles([0]);

const path = easyStar.findPathSync(0, 0, 1, 1); // your path array or null

I've done some semicolon and JSDoc housekeeping along the way, as well as added tests for the existing enableSync() behaviour.

});
}
};

// No acceptable tiles were set
Copy link
Author

Choose a reason for hiding this comment

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

This is likely the most controversial change ☝️ , I'm happy to discuss this, besides from deferring the callback onto the next execution stack, I'm not sure what is gained from wrapping this in a setTimeout for the asynchronous mode.

Copy link
Owner

Choose a reason for hiding this comment

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

This is important as without it we would be introducing zalgo bugs. The fact that the callback is asynchronous should be consistent.

See here: http://blog.izs.me/post/59142742143/designing-apis-for-asynchrony

Copy link
Author

@mikedevelops mikedevelops Apr 11, 2018

Choose a reason for hiding this comment

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

That is a really interesting read, not something that I've ever come across. I'm not certain I 100% understand it in our context, but I'm more than happy to revert this.

Copy link
Owner

Choose a reason for hiding this comment

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

In our context it is relevant because

If you have an API which takes a callback,
and sometimes that callback is called immediately,
and other times that callback is called at some point in the future,
then you will render any code using this API impossible to reason about, and cause the release of Zalgo.

If sync is not enabled, we want to consistently call the callback asynchronously. Removing the setTimeout means the callback could be called immediately, making the callback behavior inconsistent.

easyStar.setAcceptableTiles([0]);

easyStar.findPath(0, 0, 1, 1, function (path) {
result.push.apply(result, path);
Copy link
Owner

Choose a reason for hiding this comment

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

When you revert the Zalgo bug this test won't work anymore. I would recommend instead to set the iterationsPerCalculation value to be value low (like 1), a single calculate() call should still find the path.

Copy link
Author

@mikedevelops mikedevelops Apr 11, 2018

Choose a reason for hiding this comment

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

Interestingly this test does still work with the zalgo bug reverted. So I understand, when would you expect array referenced by result to be updated in this context?

This is the enableSync test, so our callback is immediately invoked on the (I assume) current execution stack. Therefore our result will have changed before calculate has returned as calculate is synchronous. We then run the assertion which will take place after calculate has returned.

Copy link
Owner

@prettymuchbryce prettymuchbryce Apr 14, 2018

Choose a reason for hiding this comment

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

My expectation would be that if you reverted the change to the setTimeout, then result would be an empty array until we enter the callback, because the callback is executed only after the assertions below calculate().

@prettymuchbryce
Copy link
Owner

Thanks very much for the change. This is looking great! Just a couple of small comments around the Zalgo stuff. Thanks for the additional cleanup as well!

Could you bump the minor version in package.json and change it throughout the code? It's kind of annoying right now, as you need to change it in a number of places, so alternatively you could simply remove the files changed in bin and I can create the built files when I go to bump the version and release.

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