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

fix: properly fallback in case of error/rejection of one Toot-ID getting method #72

Merged
merged 4 commits into from
Jun 18, 2022

Conversation

rugk
Copy link
Owner

@rugk rugk commented Jun 12, 2022

To implement such a fallback mechanism the extension races two promises against each other here, which have these two methods implemented:

return Promise.race([getFromApiQuery, scrapFromHtml]);

Promise.race just has the issue that it also rejects as soon as a promise rejects, i.e. errors.
That's actually not what we want. That works, as long as the success comes before the error, but if the error is instant (which is nearly always the case here because the JSON API is faster than website scraping), then it fails.

We want to ignore errors. I.e. I found something like this SO questions asks for, which points to the modern browser-implemented solution called Promise.any.

Also needs bumping the Firefox version, but that is not a problem, as that version is really old enough to not be supported anymore. :D


Also implemented a timeout for the API request, so it cannot be stale and hang around forever, which may be bad, too. (At some point we want to show the user an error, after all.)

Fixes #71

rugk added 3 commits June 12, 2022 19:11
…thod

To implement such a fallback mechanism the extension races two promises against each other here, which have these two methods implemented:
https://github.com/rugk/mastodon-simplified-federation/blob/e34050284bf2d614d694067ff32005eebd1f6427/src/background/modules/Detect/Mastodon.js#L164

[Promise.race](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/race) just has the [issue that it also rejects as soon as a promise rejects, i.e. errors](https://stackoverflow.com/questions/70747069/why-does-promise-race-fulfill-with-an-unfulfilled-promise).
That's actually not what we want. That works, as long as the success comes _before_ the error, but if the error is instant (which is nearly always the case here because the JSON API is faster than website scraping), then it fails.

We want to ignore errors. I.e. I found [something like this SO questions asks for](https://stackoverflow.com/questions/37234191/how-do-you-implement-a-racetosuccess-helper-given-a-list-of-promises), which [points to the modern browser-implemented solution](https://stackoverflow.com/a/60274215/5008962) called [`Promise.any`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/any).

Also needs bumping the Firefox version, but that is not a problem, as that version is really old enough to not be supported anymore. :D
@rugk rugk added this to the v1.4 milestone Jun 12, 2022
@rugk
Copy link
Owner Author

rugk commented Jun 12, 2022

@viviicat Feel free to try out the development version and report back whether it/everything works/you like it or what may still be adjusted. And/Or review the code if needed.

Otherwise I'd merge that soon. 🙂

Of course I tested it by myself, and for me it worked with your toot URL.

@rugk rugk changed the title fix: properly fallback in case of error/rejection of one detection method fix: properly fallback in case of error/rejection of one Toot-ID getting method Jun 12, 2022
@rugk rugk merged commit 62ddbf0 into master Jun 18, 2022
@rugk rugk deleted the mastodonfix branch June 18, 2022 12:49
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
1 participant