statelyai / xstate

Actor-based state management & orchestration for complex app logic.
https://stately.ai/docs
MIT License
27.23k stars 1.26k forks source link

Bug: clearTimeout being called with argument 'undefined' #5001

Closed arlyon closed 3 months ago

arlyon commented 4 months ago

XState version

XState version 5

Description

I am trying to integrate temporal and xstate 5 which I have working bar a few minor roadblocks. Temporal overrides clearTimeout which calls into their runtime however it crashes because xstate has a habit of calling clearTimeout with undefined. Temporal then tries to find a timer with id 0 (presumably coercing undefined across the FFI boundary) and erroring out because it doesn't exist. A workaround is to override this:

const clearTimeoutInner = global.clearTimeout;

/**
 * xstate passes undefined here, which temporal doesn't like
 * instead, we intercept and log it
 *
 * @param id
 */
global.clearTimeout = (id) => {
    if (id !== undefined) {
        clearTimeoutInner(id);
    } else {
        console.log("INVALID TIMEOUT");
    }
};

However, this seems like a bug as I don't think clearTimeout should ever be called with no arguments. From testing, xstate does still correctly cancel all the timers as it should. I do not know the cause of the extra invalid calls.

Expected result

Do not call clearTimeout with undefined.

Actual result

When timers elapse, clearTimeout is called with undefined.

Reproduction

https://codesandbox.io/p/live/c4801092-33a5-4d8e-9e9f-366078f138ab

Open up the sandbox, see the console print undefined.

Additional context

No response