Skip to content

Commit

Permalink
feat(esl-event-listener): ESLEventUtils.unsubscribe implementation …
Browse files Browse the repository at this point in the history
…moved to internal `ESLEventListener.unsubscribe`
  • Loading branch information
ala-n committed Aug 29, 2023
1 parent 3d7b4af commit eaa4204
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 11 deletions.
11 changes: 2 additions & 9 deletions src/modules/esl-event-listener/core/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import {getAutoDescriptors, isEventDescriptor, initDescriptor} from './descripto

import type {
ESLListenerHandler,
ESLListenerCriteria,
ESLListenerDescriptor,
ESLListenerDescriptorFn
} from './types';
Expand Down Expand Up @@ -41,9 +40,7 @@ export class ESLEventUtils {
* @param host - host object (listeners context) to associate subscription
* @param criteria - optional set of criteria {@link ESLListenerCriteria} to filter listeners list
*/
public static listeners(host: object, ...criteria: ESLListenerCriteria[]): ESLEventListener[] {
return ESLEventListener.get(host, ...criteria);
}
public static listeners = ESLEventListener.get;

/**
* Subscribes all auto descriptors of the host
Expand Down Expand Up @@ -105,11 +102,7 @@ export class ESLEventUtils {
* @param host - host element that stores subscriptions (listeners context)
* @param criteria - optional set of criteria {@link ESLListenerCriteria} to filter listeners to remove
*/
public static unsubscribe(host: object, ...criteria: ESLListenerCriteria[]): ESLEventListener[] {
const listeners = ESLEventListener.get(host, ...criteria);
listeners.forEach((listener) => listener.unsubscribe());
return listeners;
}
public static unsubscribe = ESLEventListener.unsubscribe;
}

/** @deprecated alias for {@link ESLEventUtils} */
Expand Down
15 changes: 13 additions & 2 deletions src/modules/esl-event-listener/core/listener.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ export class ESLEventListener implements ESLListenerDefinition, EventListenerObj
: handlerFull;
}

/** Executes handler if the passed event is accepted by the selector */
/** Executes a handler if the passed event is accepted by the selector */
protected handleDelegation(e: Event, handler: EventListener): void {
const {delegate} = this;
const target = e.target;
Expand Down Expand Up @@ -153,7 +153,7 @@ export class ESLEventListener implements ESLListenerDefinition, EventListenerObj
if (!criteria.length) return listeners;
return listeners.filter((listener) => criteria.every(listener.matches, listener));
}
/** Adds listener to the listener store of the host object */
/** Adds a listener to the listener store of the host object */
protected static add(host: object, instance: ESLEventListener): void {
if (!isObject(host)) return;
if (!Object.hasOwnProperty.call(host, LISTENERS)) (host as any)[LISTENERS] = [];
Expand Down Expand Up @@ -183,6 +183,17 @@ export class ESLEventListener implements ESLListenerDefinition, EventListenerObj
return listeners.filter((listener) => listener.subscribe());
}

/**
* Unsubscribes {@link ESLEventListener}(s) from the object
* @param host - host element that stores subscriptions (listeners context)
* @param criteria - optional set of criteria {@link ESLListenerCriteria} to filter listeners to remove
*/
public static unsubscribe(host: object, ...criteria: ESLListenerCriteria[]): ESLEventListener[] {
const listeners = ESLEventListener.get(host, ...criteria);
listeners.forEach((listener) => listener.unsubscribe());
return listeners;
}

/** Creates or resolves existing event listeners by handler and descriptors */
public static createOrResolve(
host: object,
Expand Down

0 comments on commit eaa4204

Please sign in to comment.