Skip to content

Webimage setImage race condition #130

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

Merged
merged 3 commits into from
Feb 28, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Add tests for duplicate onload callbacks from calling setImage before…
… initial setImage finished resolving.
  • Loading branch information
anderoonies committed Feb 24, 2022
commit dcebb118f3fd353d2c33bf01a589af4e837943d2
4 changes: 1 addition & 3 deletions site/examples/graphics/webimage/setimage/setimage.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,4 @@ img.setAnchor({ vertical: 0.5, horizontal: 0.5 });
img.setPosition(getWidth() / 2, getHeight() / 2);
add(img);

mouseClickMethod(e => {
img.setImage('https://codehs.com/uploads/1e38851d76e7691d923a3d3f3f7eae1d');
});
img.setImage('https://codehs.com/uploads/1e38851d76e7691d923a3d3f3f7eae1d');
1 change: 0 additions & 1 deletion src/graphics/webimage.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,6 @@ class WebImage extends Thing {
this._hiddenCanvas = document.createElement('canvas');
this._hiddenCanvas.width = 1;
this._hiddenCanvas.height = 1;

if (this.image) {
// if this WebImage had an existing image, it may have an unresolved onload callback.
// dont allow original callback to resolve, since it might attempt to load pixel data
Expand Down
26 changes: 26 additions & 0 deletions test/webimage.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,32 @@ describe('WebImage', () => {
expect(topLeftPixel).toEqual([0, 0, 255, 255]);
});
});
describe('setImage', () => {
it('Allows replacing the image of the WebImage', () => {
const g = new Graphics({ shouldUpdate: false });
const img = new WebImage('www.codehs.com/doesnt-matter.gif');
img.setImage(RGBURL);
g.add(img);
img.loaded(() => {
g.redraw();
expect(g.getPixel(0, 0)).toEqual([255, 0, 0, 255]);
});
});
it('Cancels the original onload of the image', () => {
const g = new Graphics({ shouldUpdate: false });
const img = new WebImage('www.codehs.com/doesnt-matter.gif');
const firstLoadedSpy = jasmine.createSpy();
// we have to inspect here to get the actual onload event
img.image.onload = firstLoadedSpy;
img.setImage(RGBURL);
g.add(img);
img.loaded(() => {
g.redraw();
expect(g.getPixel(0, 0)).toEqual([255, 0, 0, 255]);
expect(firstLoadedSpy).not.toHaveBeenCalled();
});
});
});
describe('setRed/Blue/Green/Alpha', () => {
it('Updates the underlying data', () => {
const img = new WebImage(RGBURL);
Expand Down