0
Fork 0
mirror of https://github.com/penpot/penpot-plugins.git synced 2025-01-06 14:50:21 -05:00

feat(runtime): remove deprecated method

This commit is contained in:
Juanfran 2024-09-06 12:01:43 +02:00
parent c8066becca
commit ccc5f78777
4 changed files with 12 additions and 52 deletions

View file

@ -92,23 +92,6 @@ export interface Penpot
props?: { [key: string]: unknown } props?: { [key: string]: unknown }
): symbol; ): symbol;
/**
* Removes an event listener for the specified event type.
*
* @param type The event type to stop listening for.
* @param callback The callback function to remove.
*
* @example
* ```js
* penpot.off('pagechange', () => {...do something}).
* ```
* @deprecated this method should not be used. Use instead off sending the `listenerId` (return value from `on` method)
*/
off<T extends keyof EventsMap>(
type: T,
callback?: (event: EventsMap[T]) => void
): void;
/** /**
* Removes an event listener for the specified event type. * Removes an event listener for the specified event type.
* *

View file

@ -124,11 +124,8 @@ export function createApi(
return plugin.registerListener(type, callback, props); return plugin.registerListener(type, callback, props);
}, },
off<T extends keyof EventsMap>( off(eventId: symbol): void {
idtype: symbol | T, plugin.destroyListener(eventId);
callback?: (event: EventsMap[T]) => void
): void {
plugin.destroyListener(idtype, callback);
}, },
// Penpot State API // Penpot State API

View file

@ -5,8 +5,3 @@ export type RegisterListener = <K extends keyof EventsMap>(
event: (arg: EventsMap[K]) => void, event: (arg: EventsMap[K]) => void,
props?: { [key: string]: unknown } props?: { [key: string]: unknown }
) => symbol; ) => symbol;
export type DestroyListener = <K extends keyof EventsMap>(
id: symbol | K,
event?: (arg: EventsMap[K]) => void
) => void;

View file

@ -5,7 +5,7 @@ import { Manifest } from './models/manifest.model.js';
import { PluginModalElement } from './modal/plugin-modal.js'; import { PluginModalElement } from './modal/plugin-modal.js';
import { openUIApi } from './api/openUI.api.js'; import { openUIApi } from './api/openUI.api.js';
import { OpenUIOptions } from './models/open-ui-options.model.js'; import { OpenUIOptions } from './models/open-ui-options.model.js';
import { RegisterListener, DestroyListener } from './models/plugin.model.js'; import { RegisterListener } from './models/plugin.model.js';
export async function createPluginManager( export async function createPluginManager(
context: Context, context: Context,
@ -31,20 +31,17 @@ export async function createPluginManager(
context?.removeListener(listenerId); context?.removeListener(listenerId);
}); });
// TODO: Remove when deprecating method `off` let listeners: symbol[] = [];
let listeners: { [key: string]: Map<object, symbol> } = {};
const removeAllEventListeners = () => { const removeAllEventListeners = () => {
context.removeListener(themeChangeId); destroyListener(themeChangeId);
Object.entries(listeners).forEach(([, map]) => { listeners.forEach((id) => {
map.forEach((id) => { destroyListener(id);
destroyListener(id);
});
}); });
uiMessagesCallbacks = []; uiMessagesCallbacks = [];
listeners = {}; listeners = [];
}; };
const closePlugin = () => { const closePlugin = () => {
@ -115,25 +112,13 @@ export async function createPluginManager(
props props
); );
if (!listeners[type]) { listeners.push(id);
listeners[type] = new Map<object, symbol>();
}
listeners[type].set(callback, id);
return id; return id;
}; };
const destroyListener: DestroyListener = (idtype, callback) => { const destroyListener = (listenerId: symbol) => {
let listenerId: symbol | undefined; context.removeListener(listenerId);
if (typeof idtype === 'symbol') {
listenerId = idtype;
} else if (callback) {
listenerId = listeners[idtype].get(callback);
}
if (listenerId) {
context.removeListener(listenerId);
}
}; };
return { return {