sveaflet
Version:
Sveaflet = Svelte + Leaflet
29 lines (28 loc) • 821 B
JavaScript
import { isFunction } from './equal';
export class EventBridge {
instance;
eventMap = {};
constructor(instance) {
this.instance = instance;
}
addEvents(events = {}) {
try {
Object.entries(events).forEach(([key, value]) => {
if (key.startsWith('on') && key.length > 2 && isFunction(value)) {
const listenerType = key.slice(2);
this.eventMap[listenerType] = value;
}
});
this.instance?.on?.(this.eventMap);
}
catch (e) {
console.error('Bind event error:', e);
}
}
removeEvents() {
if (Object.keys(this.eventMap).length !== 0) {
this.instance?.off?.(this.eventMap);
this.eventMap = {};
}
}
}