Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
6 changes: 5 additions & 1 deletion src/image/loading_displaying.js
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,11 @@ function loadingDisplaying(p5, fn){
const blob = new Blob([data], { type: contentType });
const url = URL.createObjectURL(blob);

img.onerror = e => reject(e);
img.onerror = e => {
URL.revokeObjectURL(url);
reject(e);
};

img.onload = () => {
URL.revokeObjectURL(url);
resolve(img);
Expand Down
17 changes: 17 additions & 0 deletions test/unit/image/loading.js
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,23 @@ suite('loading images', function() {
assert.deepEqual(img.get(0, 0), GREEN);
assert.deepEqual(img.get(47, 47), GREEN);
});

test('revokes any object URLs created during loading', async () => {
const createSpy = vi.spyOn(URL, 'createObjectURL');
const revokeSpy = vi.spyOn(URL, 'revokeObjectURL');

// successful load
await mockP5Prototype.loadImage(svgImage);
expect(revokeSpy).toHaveBeenCalledTimes(createSpy.mock.calls.length);

// errored load
const INVALID_PATH = '';
try { await mockP5Prototype.loadImage(INVALID_PATH); } catch {}
expect(revokeSpy).toHaveBeenCalledTimes(createSpy.mock.calls.length);

createSpy.mockRestore();
revokeSpy.mockRestore();
});
});

suite.todo('displaying images', function() {
Expand Down
Loading