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 cursor error in Safari #420

Merged
merged 4 commits into from
Jan 21, 2021
Merged

fix cursor error in Safari #420

merged 4 commits into from
Jan 21, 2021

Conversation

stutrek
Copy link
Contributor

@stutrek stutrek commented Dec 22, 2020

This change is one line and a spelling correction, I have created a comment in the files section. Please advise on the dist files that are significantly changed.

The app I work on has seven broadcast channels and a large application database. Some of my channels were consistently getting this error, then not working:

Error: DataError: Failed to execute 'openCursor' on 'IDBObjectStore': The parameter is not a valid key.

Interestingly, it was always the same databases.

I don't know the true root cause, but I did find a fix. For some reason, Safari was not happy with the key range, and it would always error trying to create the cursor. Fortunately, this library does not require the range and the cursor can be passed the last known index.

Comment on lines 105 to 106
const keyRangeValue = IDBKeyRange.bound(lastCursorId + 1, Infinity);
return new Promise(res => {
objectStore.openCursor(keyRangeValue).onsuccess = ev => {
objectStore.openCursor(lastCursorId + 1).onsuccess = ev => {
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 is the one change.

@pubkey
Copy link
Owner

pubkey commented Jan 3, 2021

I migrated the CI to github actions now.
Please merge the current master into this branch.
When I did this, the browser tests failed for me, please check.

@stutrek
Copy link
Contributor Author

stutrek commented Jan 5, 2021

I rebased and ran the tests. I couldn't get the browser tests to pass on my branch, but they failed the same way on master. I also had a ton of changes to the dist folder, so I suspect that the build runs differently on my system.

@stutrek
Copy link
Contributor Author

stutrek commented Jan 5, 2021

Kudos to your tests, there was a bug in my code. I have fixed it.

The localStorage tests still fail on my machine, but they work in CI 🤷

} else {
ret.push(cursor.value);
cursor.continue();
}
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 is the change. It opens a cursor on the store without a KeyRange, then advances the cursor. This is because creating the key range can sometimes error in Safari.

@pubkey
Copy link
Owner

pubkey commented Jan 16, 2021

Thanks for fixing.
Have you checked if the change affects the performance?
Because as far as I understand this change means we always start from the beginning and iterate over all entries in the db.
Isn't this much slower the the current code?
We might better catch the error and only do this when the normal openCursor fails.

@stutrek
Copy link
Contributor Author

stutrek commented Jan 16, 2021

Performance testing is always a good idea.

This avoids iterating over every message, but it does always pull the first item out. By advancing the cursor to the desired ID, it skips anything between the first message and the desired message.

I like the idea of only doing it when the KeyRange fails, that's very clever! Another solution that would improve speed is to add an empty message with an ID of 1, then never delete it. I can add the KeyRange idea on Tuesday.

@stutrek
Copy link
Contributor Author

stutrek commented Jan 19, 2021

Ok, I pushed the change.

@pubkey pubkey merged commit 8cb5c18 into pubkey:master Jan 21, 2021
pubkey added a commit that referenced this pull request Jan 21, 2021
@pubkey
Copy link
Owner

pubkey commented Jan 21, 2021

Thank you very much. Merged. Release will come next week.

@stutrek
Copy link
Contributor Author

stutrek commented Jan 21, 2021

No problem! Thanks for the great library!

@luryson
Copy link

luryson commented Apr 12, 2022

It seemed that it's not working in iframe in safari 13.0.5

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.

3 participants