Skip to content

CustomHandler best practices #4119

Closed
Closed
@ToHold

Description

@ToHold

Recently I wanted to add a delay before displaying the text of CooperativeGesturesHandler on smartphones, so I decided to create my own Handler that extends of CooperativeGesturesHandler :

import * as MapLibreGL from 'maplibre-gl';

class CooperativeGesturesHandler extends MapLibreGL.CooperativeGesturesHandler {
	private _touchmoveDelayer: boolean = false;
	private _touchmoveDelayerTiemout: number = null;

	constructor(map: MapLibreGL.Map, active: boolean) {
		super(map, active);
	}

	touchmove(e: TouchEvent) {
		if (e.touches.length !== 1) {
			this._touchmoveDelayer = true;
			clearTimeout(this._touchmoveDelayerTiemout);
			this._touchmoveDelayerTiemout = null;
		} else if (this._touchmoveDelayerTiemout == null && this._touchmoveDelayer){
			this._touchmoveDelayerTiemout = setTimeout(() => {
				this._touchmoveDelayer = false;
				this._touchmoveDelayerTiemout = null;
			}, 500);
		}

		this._onCooperativeGesture((!this._touchmoveDelayer) && e.touches.length === 1);
	}
}

export default CooperativeGesturesHandler;

But here comes the problem : when adding a new Handler this way :

const cooperativeGestures = new CooperativeGesturesHandler(map, true);
cooperativeGestures.enable();
map.cooperativeGestures = cooperativeGestures;
map.handlers._add("cooperativeGestures", cooperativeGestures );

I can't find a way to specify the order inside the HandlerManager._handlers array, I need to specify an order because handlers "touchPan", and "touchZoom" are blocking the "touchMove" event. I can add an allowed list to the "_add" method this way : map.handlers._add("cooperativeGestures", cooperativeGestures , ["touchPan", "touchZoom"]); but now because my custom handler is added at the end of HandlerManager._handlers "touchZoom" and "touchPan" are not blocked by my custom handler.

Well it works, but when it comes to edit other handlers it becomes harder, that's why I would love to have some advices, or the best practices to create my custom handlers.

By the way I would avoid to use HandlerManager._handlers or HandlerManager._handlersById, because in my mind prefixed by _ variables can be changed in new realeases, and I even don't know if it's a great idea to use map.handlers._add too.

Any advices would be appreciate :)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions