Skip to content

Commit

Permalink
feat(runtime): remove deprecated method
Browse files Browse the repository at this point in the history
  • Loading branch information
juanfran committed Sep 6, 2024
1 parent c8066be commit ccc5f78
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 52 deletions.
17 changes: 0 additions & 17 deletions libs/plugin-types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down
7 changes: 2 additions & 5 deletions libs/plugins-runtime/src/lib/api/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 0 additions & 5 deletions libs/plugins-runtime/src/lib/models/plugin.model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
35 changes: 10 additions & 25 deletions libs/plugins-runtime/src/lib/plugin-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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 = () => {
Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit ccc5f78

Please sign in to comment.