-
-
Notifications
You must be signed in to change notification settings - Fork 389
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
refactor(core/biblio-db): Move from indexedDB to idb #2228
Conversation
@sidvishnoi Could you have a look and tell me how to resolve Script error in tests (details in description)? |
Just tried fixing your error, it should now at least import the package properly. |
@saschanaz Thanks for your help, though the error still persist. But your changes pointed me to towards the problem: The test is trying to fetch So I need to check how to set the correct path for tests. Working on it. Thanks a lot. |
Finally fixed tests locally. Thank you @marcoscaceres and @saschanaz for help with importing |
I suspect you are using |
@saschanaz Yes I am using |
src/core/biblio-db.js
Outdated
return Array.from(aliasesAndRefs[type]).map(details => | ||
await this.add(type, details) | ||
return Array.from(aliasesAndRefs[type]).map( | ||
async details => await this.add(type, details) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This change is redundant because the return type is still Promise, and it's expected.
src/core/biblio-db.js
Outdated
@@ -222,7 +221,7 @@ export const biblioDB = { | |||
const storeNames = [...ALLOWED_TYPES]; | |||
const stores = await db.transaction(storeNames, "readwrite"); | |||
const clearStorePromises = storeNames.map(name => { | |||
return stores.objectStore(name).clear(); | |||
return await stores.objectStore(name).clear(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is redundant too. It still returns a Promise anyway.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Couple of quick suggestions... not sure all apply.
src/core/biblio-db.js
Outdated
}); | ||
const objectStore = db.transaction([type], "readonly").objectStore(type); | ||
const range = IDBKeyRange.only(id); | ||
return !!(await objectStore.openCursor(range)); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I know this was in the original, but it's still a bit ugly... maybe we can do?
return !!(await objectStore.openCursor(range)); | |
const result = await objectStore.openCursor(range); | |
return result ? true : false; |
Same with other one below...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
? true : false
? It doesn't look JavaScript-y for me... Maybe !!result
?
(Because result ?
part already coerces result
into a boolean.)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
!!result wfm
src/core/biblio-db.js
Outdated
.transaction(["alias"], "readonly") | ||
.objectStore("alias"); | ||
const range = IDBKeyRange.only(id); | ||
return !!(await objectStore.openCursor(range)); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As above...
src/core/biblio-db.js
Outdated
}); | ||
const store = db.transaction([type], "readwrite").objectStore(type); | ||
// update or add, depending of already having it in db | ||
const request = isInDB ? store.put(details) : store.add(details); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe we should do:
Maybe just:
const request = isInDB ? store.put(details) : store.add(details); | |
return isInDB ? await store.put(details) : await store.add(details); |
I have implemented the changes, but I am not able to run tests either by |
Investigation incoming... |
The problem is that currently the ES module system does not support resolving by package name. After #2241, karma wants to load We can replace |
Yeah, this is why copydeps.js exists :( and what’s nice about webpack is that it handles this. I’m still not sure how we overcome copydeps.js in a modules only world. We probably can’t as those files have to live somewhere. |
@saschanaz This breaks everything else except the karma tests. This is because |
Browsers do support imports, but yes, it will break requirejs compatibility... 🤔 |
Oh, I thought browsers not supporting imports was the reason |
Seems we are kind of locked. Maybe we should do #1975 first? |
Now that SpecRunner is gone, @Swapnilr1 what’s blocking this? |
@marcoscaceres SpecRunner wasn't really blocking this (it doesn't run on Travis anyway) - SpecRunner would not work after #2241 so I had just made a comment regarding that. Switching to [Extremely sorry for any technical inaccuracy; I am not very well versed with ES6 modules]. |
Hhmm... should we complain upstream? This doesn’t sound good and no other module exhibits this kind of problem? |
@marcoscaceres Yes, perhaps we can ask for RequireJS compatibility without needing shims - though I am not completely certain if this will fix the problem. |
This is because karma now consumes ES modules and this PR tries consuming a non-ES module (or ES module without an actual path) in an ES module. Webpack and requirejs somehow can deal with it but karma need import-maps support from browsers (to map "idb" to the actual path). |
Ok, but that seems like a bug upstream, no? The maintainer has shipped something non-conforming. We could ask them to ship an actual ES module, instead of whatever this is. |
No, |
That makes sense :)
This doesn't make sense to me (and probably where I'm getting confused). We should be testing the idb that is compiled into ReSpec. Additionally, shouldn't we just use use copydeps.js to copy idb over to deps? copydeps.js can be removed once we fully transition to some packager. |
We have tests that directly imports biblio-db as a module, which now depends on
Currently copydeps is only used for requirejs mapping, otherwise TypeScript type system breaks (as they only knows package name to type). It doesn't help here anyway, if we copy cjs then ES module breaks, or we copy esm then requirejs breaks, so 🤷♀️ |
Yay! module systems 💃. Ok, we hold I guess until we kill RequiresJS. |
Or until we get import maps on Firefox. I hope Mozillians get some push for this new feature. |
What's blocking this one? Why can't we use |
Because Karma is using ES module, some tests there want to import Using the actual path for ES module version of |
Okay, the tests now pass. Now time to review again! @marcoscaceres @sidvishnoi |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just a small nit.
src/core/biblio-db.js
Outdated
@@ -81,7 +45,8 @@ export const biblioDB = { | |||
if (await this.isAlias(id)) { | |||
id = await this.resolveAlias(id); | |||
} | |||
return this.get("reference", id); | |||
const result = this.get("reference", id); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We probably want to await this.get, then return the result.
@saschanaz, it shows you as having requested changes? I guess that’s from an old review? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍 Sorry for the loooong delay and thank you for this PR @Swapnilr1!
Fixes #1955
I started refactoring few days ago but ran into issues with tests and have not been able to resolve it.
When running the tests in
biblio-db-spec.js
, I getError: Script error for "idb", needed by: core/biblio-db
and the tests fail.I even tested after removing almost all code and keeping just
in
biblio-db.js
, and I still got the same errorError: Script error for "idb", needed by: core/biblio-db
.I even tried to use the IDB code from
xref.js
and I still got the same error. 😢I noticed that the tests for xref include the database creation code:
So I think I have to make changes to the test files to get idb to be "initialized".
Any guidance is much appreciated.