Skip to content

Commit

Permalink
feat: add image.removeEventListener implementation (#500)
Browse files Browse the repository at this point in the history
Co-authored-by: Vladimir Panov <[email protected]>
  • Loading branch information
esjs and Vladimir Panov authored Jan 7, 2024
1 parent 1488e88 commit 606dc36
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 3 deletions.
13 changes: 12 additions & 1 deletion src/lib/web-worker/worker-image.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import { logWorker } from '../log';
import { resolveUrl } from './worker-exec';
import { webWorkerCtx } from './worker-constants';

type HTMLImageElementEvents = 'load' | 'error';

export const createImageConstructor = (env: WebWorkerEnvironment) =>
class HTMLImageElement {
s: string;
Expand Down Expand Up @@ -44,7 +46,7 @@ export const createImageConstructor = (env: WebWorkerEnvironment) =>
);
}

addEventListener(eventName: 'load' | 'error', cb: EventHandler) {
addEventListener(eventName: HTMLImageElementEvents, cb: EventHandler) {
if (eventName === 'load') {
this.l.push(cb);
}
Expand All @@ -53,6 +55,15 @@ export const createImageConstructor = (env: WebWorkerEnvironment) =>
}
}

removeEventListener(eventName: HTMLImageElementEvents, cb: EventHandler) {
if (eventName === 'load') {
this.l = this.l.filter((fn) => fn !== cb);
}
if (eventName === 'error') {
this.e = this.e.filter((fn) => fn !== cb);
}
}

get onload() {
return this.l[0];
}
Expand Down
10 changes: 8 additions & 2 deletions tests/platform/image/image.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@ import { test, expect } from '@playwright/test';
test('image', async ({ page }) => {
await page.goto('/tests/platform/image/');

const resolveUrlCalled = new Promise(resolve =>
page.on('request', request => request.url().includes('resolvedUrl') && resolve(true)));
const resolveUrlCalled = new Promise((resolve) =>
page.on('request', (request) => request.url().includes('resolvedUrl') && resolve(true))
);

await page.waitForSelector('.testImageOnLoad');
const testImageOnLoad = page.locator('#testImageOnLoad');
Expand Down Expand Up @@ -34,6 +35,11 @@ test('image', async ({ page }) => {
const testImgListenerLoad = page.locator('#testImgListenerLoad');
await expect(testImgListenerLoad).toHaveText('load');

await page.waitForSelector('.testImgListenerRemoved');
const testImgListenerRemoved = page.locator('#testImgListenerRemoved');
await page.waitForTimeout(100);
await expect(testImgListenerRemoved).toHaveText('onload');

await page.waitForSelector('.testImgListenerError');
const testImgListenerError = page.locator('#testImgListenerError');
await expect(testImgListenerError).toHaveText('error');
Expand Down
24 changes: 24 additions & 0 deletions tests/platform/image/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,30 @@ <h1>Image</h1>
</script>
</li>

<li>
<strong>&lt;img&gt; removeEventListener('load')</strong>
<code id="testImgListenerRemoved"></code>
<script type="text/partytown">
(function () {
const image = new Image();
const elm = document.getElementById('testImgListenerRemoved');

image.onload = function(){
elm.textContent = 'onload';
}

function handleImageLoad(ev) {
elm.textContent = ev.type;
}
image.addEventListener('load', handleImageLoad);
image.removeEventListener('load', handleImageLoad);
image.src = 'dot.gif?testImgListenerRemoved';

elm.className = 'testImgListenerRemoved';
})();
</script>
</li>

<li>
<strong>&lt;img&gt; addEventListener('error')</strong>
<code id="testImgListenerError"></code>
Expand Down

1 comment on commit 606dc36

@vercel
Copy link

@vercel vercel bot commented on 606dc36 Jan 7, 2024

Choose a reason for hiding this comment

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

Please sign in to comment.