From ccc5f78777d628339400eeaa81c968c5647f7be7 Mon Sep 17 00:00:00 2001 From: Juanfran Date: Fri, 6 Sep 2024 12:01:43 +0200 Subject: [PATCH] feat(runtime): remove deprecated method --- libs/plugin-types/index.d.ts | 17 --------- libs/plugins-runtime/src/lib/api/index.ts | 7 ++-- .../src/lib/models/plugin.model.ts | 5 --- .../plugins-runtime/src/lib/plugin-manager.ts | 35 ++++++------------- 4 files changed, 12 insertions(+), 52 deletions(-) diff --git a/libs/plugin-types/index.d.ts b/libs/plugin-types/index.d.ts index e515ed1..347441c 100644 --- a/libs/plugin-types/index.d.ts +++ b/libs/plugin-types/index.d.ts @@ -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( - type: T, - callback?: (event: EventsMap[T]) => void - ): void; - /** * Removes an event listener for the specified event type. * diff --git a/libs/plugins-runtime/src/lib/api/index.ts b/libs/plugins-runtime/src/lib/api/index.ts index b86d862..981b0f6 100644 --- a/libs/plugins-runtime/src/lib/api/index.ts +++ b/libs/plugins-runtime/src/lib/api/index.ts @@ -124,11 +124,8 @@ export function createApi( return plugin.registerListener(type, callback, props); }, - off( - idtype: symbol | T, - callback?: (event: EventsMap[T]) => void - ): void { - plugin.destroyListener(idtype, callback); + off(eventId: symbol): void { + plugin.destroyListener(eventId); }, // Penpot State API diff --git a/libs/plugins-runtime/src/lib/models/plugin.model.ts b/libs/plugins-runtime/src/lib/models/plugin.model.ts index 78d2e09..98c7fba 100644 --- a/libs/plugins-runtime/src/lib/models/plugin.model.ts +++ b/libs/plugins-runtime/src/lib/models/plugin.model.ts @@ -5,8 +5,3 @@ export type RegisterListener = ( event: (arg: EventsMap[K]) => void, props?: { [key: string]: unknown } ) => symbol; - -export type DestroyListener = ( - id: symbol | K, - event?: (arg: EventsMap[K]) => void -) => void; diff --git a/libs/plugins-runtime/src/lib/plugin-manager.ts b/libs/plugins-runtime/src/lib/plugin-manager.ts index fb42b0d..7015b17 100644 --- a/libs/plugins-runtime/src/lib/plugin-manager.ts +++ b/libs/plugins-runtime/src/lib/plugin-manager.ts @@ -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 } = {}; + 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(); - } - 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 {