Skip to content

Infinite Loop when Changing Mode in onStop with draw.create Event #1347

Open
@cdeclerck

Description

@cdeclerck

Title: Infinite Loop when Changing Mode in onStop with draw.create Event

Description:
I'm encountering an infinite loop issue when trying to switch to a custom mode after completing a drawing using mapbox-gl-draw.

When the drawing is finished, my onStop function is triggered, where I fire a draw.create event. After that, I attempt to change the mode to a custom mode using changeMode(). However, this results in a recursive loop with the following error:

mapbox-gl.js?v=40f4fdd7:1833 Uncaught RangeError: Maximum call stack size exceeded
    at Map.fire (mapbox-gl.js?v=40f4fdd7:1833:15)
    at CustomDrawRectangle.onStop (mono.ts:123:14)
    at Object.stop (@mapbox_mapbox-gl-dr…s?v=40f4fdd7:939:14)
    at Object.stop (@mapbox_mapbox-gl-dr…s?v=40f4fdd7:178:27)
    at Object.changeMode (@mapbox_mapbox-gl-dr…?v=40f4fdd7:1090:17)
    at api.changeMode (@mapbox_mapbox-gl-dr…?v=40f4fdd7:3425:16)
    at Map.<anonymous> (roof-zone.vue?t=1737448820365:79:16)
    at Map.fire (mapbox-gl.js?v=40f4fdd7:1838:39)
    at CustomDrawRectangle.onStop (mono.ts:123:14)
    at Object.stop (@mapbox_mapbox-gl-dr…s?v=40f4fdd7:939:14)

Steps to Reproduce:

  1. Start drawing using a custom mode (e.g., CustomDrawRectangle).
  2. When the drawing is completed, onStop is triggered.
  3. In onStop, I fire a draw.create event and immediately call changeMode() to switch to another mode.
  4. This results in an infinite recursive loop and the error above.

Expected Behavior:

I expect to be able to switch to another mode without triggering a recursive loop.

Actual Behavior:

Calling changeMode() inside onStop causes an infinite loop.

Code Snippet:

CustomDrawRectangle.onStop = function () {
    this.map.fire('draw.create', { features: [this.feature] });
    this.changeMode('custom-mode'); // Switching mode triggers an infinite loop
};

Environment:

  • mapbox-gl version: 3.9.3
  • mapbox-gl-draw version: 1.5.0

Possible Cause:

It seems like changeMode() internally calls stop(), which in turn calls onStop again, causing a loop. Perhaps an internal mechanism prevents mode changes inside onStop?

Suggested Fix:

Is there a recommended approach for switching modes after drawing is complete? Should there be a debounce or asynchronous delay before calling changeMode()?

Any insights or workarounds would be appreciated!

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