Skip to content

Refactored code and added await to stop race conditions#507

Merged
abeverley merged 2 commits intoctrlo:devfrom
droberts-ctrlo:autosave-fix
Jan 22, 2025
Merged

Refactored code and added await to stop race conditions#507
abeverley merged 2 commits intoctrlo:devfrom
droberts-ctrlo:autosave-fix

Conversation

@droberts-ctrlo
Copy link
Contributor

No description provided.

Copy link
Contributor Author

@droberts-ctrlo droberts-ctrlo left a comment

Choose a reason for hiding this comment

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

This appears to behave "better" - looks like the setting of the recovering key wasn't always returning as expected (as it wasn't awaited) but I feel it may need checking further in a test environment just to be certain (local testing works fine).

e.preventDefault();
e.stopPropagation();

await this.storage.setItem('recovering', true);
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This was causing a race condition that was borken

let $list = $("<ul></ul>");
const $body = $modal.find(".modal-body");
$body.html("<p>Restoring values...</p>").append($list);
await Promise.all($form.find('.linkspace-field').map(async (_,field) => {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Don't need this to be awaited because the finally (after this) encapsulates what would happen at the end either way.

const $field = $(field);
await this.storage.getItem(this.columnKey($field)).then(json => {
try {
const json = await this.storage.getItem(this.columnKey($field))
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I was being a little overzealous with my usage of Promise objects here - IMO this is "cleaner" now; we still need the Promise.all because the field mappings need to run decryption asynchronously.

Copy link
Contributor

@abeverley abeverley left a comment

Choose a reason for hiding this comment

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

Thanks Dave, looks good, can you just add some comments inline to the code?

@abeverley abeverley merged commit 1d1fcb5 into ctrlo:dev Jan 22, 2025
7 checks passed
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