Skip to content
This repository has been archived by the owner on Feb 29, 2020. It is now read-only.

Bug 1540765 - PersistentCache should do JSON parsing off of the main thread #4879

Merged
merged 4 commits into from
Apr 3, 2019

Conversation

rlr
Copy link
Contributor

@rlr rlr commented Apr 2, 2019

@@ -59,9 +59,8 @@ this.PersistentCache = class PersistentCache {
const filepath = OS.Path.join(OS.Constants.Path.localProfileDir, this._filename);
const fileExists = await OS.File.exists(filepath);
Copy link
Contributor

Choose a reason for hiding this comment

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

Out of curiosity, is it still necessary to do the stat here? Can we do the fetch, and handle the failure status in the event that the file does not exist?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

good point, will fix 👍

const json = gTextDecoder.decode(binaryData);
data = JSON.parse(json);
const file = await fetch(`file://${filepath}`);
data = await file.json();
Copy link
Contributor

Choose a reason for hiding this comment

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

I think these two calls can be joined together

await (await fetch(...)).json();

Copy link
Contributor

@piatra piatra left a comment

Choose a reason for hiding this comment

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

It’s cool we were able to fix this pretty easily. Just a small nit about keeping the side effects in tests to a minimum and adding back some of the error logging. Thanks.

@@ -385,7 +385,7 @@ describe("DiscoveryStreamFeed", () => {

await feed.loadSpocs(feed.store.dispatch);

assert.notCalled(global.fetch);
assert.calledOnce(global.fetch); // Persistent cache attempts a fetch
Copy link
Contributor

Choose a reason for hiding this comment

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

This is kinda weird and might make test debugging confusing since the test description mentions should not fetch.
Can you instead stub out PersistentCache so that call doesn’t happen? It should prevent having to make all the other changes as well.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

yeah, good call

await cache.get("foo");
assert.notCalled(fakeOS.File.read);
});
it("should catch and report errors", async () => {
Copy link
Contributor

Choose a reason for hiding this comment

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

I think it would be useful to keep the Cu.reportError Call if fetch or json parsing fails.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

the problem was fetch() throws if the cache file doesn't exist yet. So, it would be spammy to report that an an error since it is expected on a cold cache. I'll break up the fetch from the json and only report json parsing errors.

@piatra piatra assigned rlr and unassigned piatra Apr 2, 2019
@rlr rlr assigned piatra and unassigned rlr Apr 3, 2019
@piatra piatra self-requested a review April 3, 2019 07:58
Copy link
Contributor

@piatra piatra left a comment

Choose a reason for hiding this comment

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

👍

@piatra piatra removed their assignment Apr 3, 2019
@rlr rlr merged commit 08b93a3 into mozilla:master Apr 3, 2019
@rlr rlr deleted the bug1540765/cache-fetch branch April 5, 2019 18:34
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants