Skip to content

Commit

Permalink
Bump maplibre-gl and add some extra interface methods
Browse files Browse the repository at this point in the history
  • Loading branch information
mlazar committed Nov 13, 2023
1 parent e4a4a0a commit 7e8c08c
Show file tree
Hide file tree
Showing 7 changed files with 300 additions and 212 deletions.
4 changes: 2 additions & 2 deletions libs/mapjet-core/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@inelo/mapjet-core",
"version": "0.5.1",
"version": "0.6.0",
"author": "Inelo <rwalus@inelo.pl>",
"license": "MIT",
"main": "dist/src/index",
Expand Down Expand Up @@ -31,6 +31,6 @@
"access": "public"
},
"peerDependencies": {
"maplibre-gl": ">=2.0.0 <= ^4.0.0"
"maplibre-gl": ">=2.0.0 <=^4.0.0"
}
}
12 changes: 8 additions & 4 deletions libs/mapjet-core/src/lib/controller/controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,21 +45,25 @@ export class MapJetController {
}, {});
}

private bindPlugin(plugin: MapJetPlugin & MapJetEventablePlugin) {
private bindPlugin(plugin: MapJetPlugin) {
if (this.pluginDefs[plugin.id]) {
Object.keys(this.pluginDefs[plugin.id]).forEach(eventName => {
this.pluginDefs[plugin.id][eventName].forEach(callback => {
plugin.on(eventName, callback);
if (isEventablePlugin(plugin)) {
plugin.on(eventName, callback);
}
});
});
}
}

private unbindPlugin(plugin: MapJetPlugin & MapJetEventablePlugin) {
private unbindPlugin(plugin: MapJetPlugin) {
if (this.pluginDefs[plugin.id]) {
Object.keys(this.pluginDefs[plugin.id]).forEach(eventName => {
this.pluginDefs[plugin.id][eventName].forEach(callback => {
plugin.off(eventName, callback);
if (isEventablePlugin(plugin)) {
plugin.off(eventName, callback);
}
});
});
}
Expand Down
48 changes: 48 additions & 0 deletions libs/mapjet-core/src/lib/mapjet-core.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,30 @@ describe('MapJet', () => {
});
});

describe('removePluginIfExists', () => {
it('should remove plugin and return true when plugin exists', () => {
const mapCore = createCore({ debug: true });
const plugin = new FakeResourcePlugin();
jest.spyOn(plugin.resourceLoader, 'destroy');

mapCore.addPlugin(plugin);
const result = mapCore.removePluginIfExists(plugin);

expect(result).toBeTruthy();
expect(plugin.resourceLoader.destroy).toHaveBeenCalledWith();
});

it('should return false when plugin not exists', () => {
const mapCore = createCore({ debug: true });
const plugin = new FakeResourcePlugin();
jest.spyOn(plugin.resourceLoader, 'destroy');

const result = mapCore.removePluginIfExists(plugin);

expect(result).toBeFalse();
});
});

describe('getPlugin()', () => {
let mapCore: MapJet;

Expand All @@ -253,6 +277,30 @@ describe('MapJet', () => {
});
});

describe('hasPlugin()', () => {
let mapCore: MapJet;

beforeEach(() => {
mapCore = createCore();
});

it('should return true when plugin is added', () => {
const plugin = { ...FakePlugin };
mapCore.addPlugin(plugin);

expect(mapCore.hasPlugin(plugin.id)).toBeTruthy();
expect(mapCore.hasPlugin(plugin)).toBeTruthy();
});

it('should return false when plugin is not added', () => {
const plugin = { ...FakePlugin };

expect(mapCore.hasPlugin(plugin.id)).toBeFalsy();
expect(mapCore.hasPlugin(plugin)).toBeFalsy();
});
});


describe('destroy', () => {
let mapCore: MapJet;

Expand Down
39 changes: 29 additions & 10 deletions libs/mapjet-core/src/lib/mapjet-core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ export class MapJet {
return;
}

if (this.plugins.has(plugin.id)) {
if (this.hasPlugin(plugin)) {
throw new Error(`Plugin with id ${plugin.id} already exists`);
}

Expand All @@ -56,27 +56,40 @@ export class MapJet {
Log.info('Added new plugin', plugin);
}

public removePlugin(plugin: MapJetPlugin) {
public removePlugin(plugin: string | MapJetPlugin) {
if (this.destroyed) {
Log.info(
'Mapjet was destroyed, all plugins were removed during this operation. This operation will have no effect.'
);
return;
}

if (!this.plugins.has(plugin.id)) {
throw new Error(`Plugin with id ${plugin.id} not exists`);
const pluginId: string = typeof plugin === 'object' ? plugin.id : plugin;
const pluginInstance: MapJetPlugin | undefined = this.plugins.get(pluginId);

if (!pluginInstance) {
throw new Error(`Plugin with id ${pluginId} not exists`);
}

plugin.onRemove(this);
pluginInstance.onRemove(this);

if (isResourcePlugin(plugin)) {
plugin.resourceLoader.destroy();
if (isResourcePlugin(pluginInstance)) {
pluginInstance.resourceLoader.destroy();
}

this.plugins.delete(plugin.id);
this.dispatch('pluginRemoved', plugin);
Log.info('Removed plugin', plugin);
this.plugins.delete(pluginId);
this.dispatch('pluginRemoved', pluginInstance);
Log.info('Removed plugin', pluginInstance);
}

public removePluginIfExists(plugin: string | MapJetPlugin): boolean {
try {
this.removePlugin(plugin);

return true;
} catch (_e) {
return false;
}
}

public getPlugin<T extends MapJetPlugin>(pluginId: string) {
Expand Down Expand Up @@ -107,6 +120,12 @@ export class MapJet {
this.dispatcher.fire(event, data);
}

public hasPlugin(plugin: string | MapJetPlugin): boolean {
const pluginId: string = typeof plugin === 'object' ? plugin.id : plugin;

return this.plugins.has(pluginId);
}

private performMap(options: MapJetOptions): MapLibre {
if (options.map) {
return options.map;
Expand Down
6 changes: 3 additions & 3 deletions libs/mapjet-core/src/lib/utils/layer-event-handler.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Feature, Map, MapEvent, MapGeoJSONFeature } from 'maplibre-gl';
import { Map, MapGeoJSONFeature } from 'maplibre-gl';

export type LayerEvent<T> = {
cancelBubble: boolean;
Expand All @@ -23,7 +23,7 @@ export class LayerEventHandler {
this._onMapEvent = this._onMapEvent.bind(this);
}

public on(eventName: MapEvent, layerId: string | null, callback: (...args: any[]) => any): void {
public on(eventName: string, layerId: string | null, callback: (...args: any[]) => any): void {
if (!this.defaultHandlers[eventName] && !this.handlers[eventName]) {
// Create new event name keys in our storage maps
this.defaultHandlers[eventName] = [];
Expand All @@ -42,7 +42,7 @@ export class LayerEventHandler {
}
}

public off(eventName: MapEvent, layerId: string | null, callbackRef: (...args: any[]) => any): void {
public off(eventName: string, layerId: string | null, callbackRef: (...args: any[]) => any): void {
if (layerId && this.handlers[eventName] && this.handlers[eventName][layerId]) {
this.handlers[eventName][layerId] = this.handlers[eventName][layerId].filter(cb => cb !== callbackRef);

Expand Down
3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@
"@angular/platform-browser-dynamic": "15.2.1",
"@angular/router": "15.2.1",
"core-js": "^3.26.1",
"libre-routing": "^0.3.0-alpha.62",
"parse-ms": "^3.0.0",
"regenerator-runtime": "0.13.10",
"rxjs": "~7.5.7",
Expand Down Expand Up @@ -78,7 +77,7 @@
"jest-preset-angular": "13.0.0",
"lerna": "^6.0.3",
"lodash": "^4.17.21",
"maplibre-gl": "^2.4.0",
"maplibre-gl": "^3.6.0",
"nx": "15.8.5",
"phosphor-icons": "^1.4.2",
"prettier": "2.7.1",
Expand Down

0 comments on commit 7e8c08c

Please sign in to comment.