Skip to content

Commit

Permalink
fix(esl-event-listener): fix support for any object-like host
Browse files Browse the repository at this point in the history
(cherry picked from commit 9ca6aa4)
  • Loading branch information
ala-n committed Apr 9, 2024
1 parent a96d5d7 commit ae4c98d
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 4 deletions.
8 changes: 4 additions & 4 deletions src/modules/esl-event-listener/core/listener.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import {wrap} from '../../esl-utils/misc/array';
import {isElement} from '../../esl-utils/dom/api';
import {isPassiveByDefault} from '../../esl-utils/dom/events/misc';
import {resolveProperty} from '../../esl-utils/misc/functions';
import {isObject, isSimilar} from '../../esl-utils/misc/object';
import {isObject, isObjectLike, isSimilar} from '../../esl-utils/misc/object';
import {resolveDomTarget} from '../../esl-utils/abstract/dom-target';
import {memoize} from '../../esl-utils/decorators/memoize';
import {ESLTraversingQuery} from '../../esl-traversing-query/core';
Expand Down Expand Up @@ -149,14 +149,14 @@ export class ESLEventListener implements ESLListenerDefinition, EventListenerObj
* Supports additional filtration criteria
*/
public static get(host?: object, ...criteria: ESLListenerCriteria[]): ESLEventListener[] {
if (!isObject(host)) return [];
if (!isObjectLike(host)) return [];
const listeners = ((host as any)[LISTENERS] || []) as ESLEventListener[];
if (!criteria.length) return listeners;
return listeners.filter((listener) => criteria.every(listener.matches, listener));
}
/** Adds a listener to the listener store of the host object */
protected static add(host: object, instance: ESLEventListener): void {
if (!isObject(host)) return;
if (!isObjectLike(host)) return;
if (!Object.hasOwnProperty.call(host, LISTENERS)) (host as any)[LISTENERS] = [];
(host as any)[LISTENERS].push(instance);
}
Expand Down Expand Up @@ -201,7 +201,7 @@ export class ESLEventListener implements ESLListenerDefinition, EventListenerObj
handler: ESLListenerHandler,
desc: ESLListenerDescriptor
): ESLEventListener[] {
if (!isObject(host)) return [];
if (!isObjectLike(host)) return [];
const eventString = resolveProperty(desc.event, host);
const listeners: ESLEventListener[] = [];
for (const event of splitEvents(eventString)) {
Expand Down
29 changes: 29 additions & 0 deletions src/modules/esl-event-listener/test/listener.subscribe.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,4 +73,33 @@ describe('ESLEventUtils:subscribe tests', () => {
ESLEventUtils.subscribe(host, {event: 'click'}, handle);
expect(ESLEventUtils.listeners(host).length).toBe(0);
});

describe('ESLEventListener subscribes correctly for any object-like host', () => {
test.each([
{},
Object.create(null),
function hostFunction() {},
class HostClass {},
new (class HostClassInst {})()
])('ESLEventListener subscribes correctly for %o host', (host) => {
const handle = jest.fn();
ESLEventUtils.subscribe(host, {event: 'click', target: document.body}, handle);
expect(ESLEventUtils.listeners(host).length).toBe(1);
ESLEventUtils.unsubscribe(host);
});

test('ESLEventListener can not subscribe for primitive host', () => {
jest.spyOn(console, 'warn').mockImplementationOnce(() => {});
const handle = jest.fn();
const count = ESLEventUtils.subscribe(1 as any, {event: 'click', target: document.body}, handle).length;
expect(count).toBe(0);
});

test('ESLEventListener can not subscribe for null host', () => {
jest.spyOn(console, 'warn').mockImplementationOnce(() => {});
const handle = jest.fn();
const count = ESLEventUtils.subscribe(null as any, {event: 'click', target: document.body}, handle).length;
expect(count).toBe(0);
});
});
});

0 comments on commit ae4c98d

Please sign in to comment.