mirror of
https://github.com/penpot/penpot-plugins.git
synced 2025-01-04 13:50:13 -05:00
feat(runtime): remove deprecated method
This commit is contained in:
parent
c8066becca
commit
ccc5f78777
4 changed files with 12 additions and 52 deletions
17
libs/plugin-types/index.d.ts
vendored
17
libs/plugin-types/index.d.ts
vendored
|
@ -92,23 +92,6 @@ export interface Penpot
|
|||
props?: { [key: string]: unknown }
|
||||
): 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.
|
||||
*
|
||||
|
|
|
@ -124,11 +124,8 @@ export function createApi(
|
|||
return plugin.registerListener(type, callback, props);
|
||||
},
|
||||
|
||||
off<T extends keyof EventsMap>(
|
||||
idtype: symbol | T,
|
||||
callback?: (event: EventsMap[T]) => void
|
||||
): void {
|
||||
plugin.destroyListener(idtype, callback);
|
||||
off(eventId: symbol): void {
|
||||
plugin.destroyListener(eventId);
|
||||
},
|
||||
|
||||
// Penpot State API
|
||||
|
|
|
@ -5,8 +5,3 @@ export type RegisterListener = <K extends keyof EventsMap>(
|
|||
event: (arg: EventsMap[K]) => void,
|
||||
props?: { [key: string]: unknown }
|
||||
) => symbol;
|
||||
|
||||
export type DestroyListener = <K extends keyof EventsMap>(
|
||||
id: symbol | K,
|
||||
event?: (arg: EventsMap[K]) => void
|
||||
) => void;
|
||||
|
|
|
@ -5,7 +5,7 @@ import { Manifest } from './models/manifest.model.js';
|
|||
import { PluginModalElement } from './modal/plugin-modal.js';
|
||||
import { openUIApi } from './api/openUI.api.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(
|
||||
context: Context,
|
||||
|
@ -31,20 +31,17 @@ export async function createPluginManager(
|
|||
context?.removeListener(listenerId);
|
||||
});
|
||||
|
||||
// TODO: Remove when deprecating method `off`
|
||||
let listeners: { [key: string]: Map<object, symbol> } = {};
|
||||
let listeners: symbol[] = [];
|
||||
|
||||
const removeAllEventListeners = () => {
|
||||
context.removeListener(themeChangeId);
|
||||
destroyListener(themeChangeId);
|
||||
|
||||
Object.entries(listeners).forEach(([, map]) => {
|
||||
map.forEach((id) => {
|
||||
destroyListener(id);
|
||||
});
|
||||
listeners.forEach((id) => {
|
||||
destroyListener(id);
|
||||
});
|
||||
|
||||
uiMessagesCallbacks = [];
|
||||
listeners = {};
|
||||
listeners = [];
|
||||
};
|
||||
|
||||
const closePlugin = () => {
|
||||
|
@ -115,25 +112,13 @@ export async function createPluginManager(
|
|||
props
|
||||
);
|
||||
|
||||
if (!listeners[type]) {
|
||||
listeners[type] = new Map<object, symbol>();
|
||||
}
|
||||
listeners[type].set(callback, id);
|
||||
listeners.push(id);
|
||||
|
||||
return id;
|
||||
};
|
||||
|
||||
const destroyListener: DestroyListener = (idtype, callback) => {
|
||||
let listenerId: symbol | undefined;
|
||||
|
||||
if (typeof idtype === 'symbol') {
|
||||
listenerId = idtype;
|
||||
} else if (callback) {
|
||||
listenerId = listeners[idtype].get(callback);
|
||||
}
|
||||
|
||||
if (listenerId) {
|
||||
context.removeListener(listenerId);
|
||||
}
|
||||
const destroyListener = (listenerId: symbol) => {
|
||||
context.removeListener(listenerId);
|
||||
};
|
||||
|
||||
return {
|
||||
|
|
Loading…
Reference in a new issue