-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Interpretter clock.setTimeout method does not work in Firefox WebExtension content scripts by default #1703
Comments
@blimmer Thanks for the investigation! I'll fix this soon. |
No problem at all! Thank you so much for the quick response, I really appreciate it. Does the |
Yes it does; I'd appreciate a PR! For tests, this might be hard to test since it's a Firefox-specific thing, so as long as the existing tests pass, that's sufficient. |
It probably should be enough to just remove |
I can't remember why, but there was a reason I used |
Yeah, I was wondering what the background was on why the
|
Description
When an XState machine with delayed events/transitions is used in a Firefox WebExtension content script, the default implementation of
clock.setTimeout
andclock.clearTimeout
throws this exception:TypeError: 'setTimeout' called on an object that does not implement interface Window.
Expected Result
I expected the machine, as defined, to work without throwing an exception.
Actual Result
An exception is thrown, and the state machine does not work properly.
Reproduction
Because this happens in a Firefox WebExtension, I couldn't use any of the recommended reproduction templates. Instead, I created a sample repository to show the issue: https://github.com/blimmer/xstate-webext-setTimeout-issue. There is information in the Quick Start section of the README that explains how to run the example.
Additional context
This issue seems to be caused by the default implementation of
setTimeout
/clearTimeout
defined here: https://github.com/davidkpiano/xstate/blob/844c2c30d6160763ab164dc0592e33a942746361/packages/core/src/interpreter.ts#L125-L132I believe the issue is with
thisArg
beingnull
. I locally changed the default implementation to look like this (note theglobal
thisArg
) and the state machine started working with the default implementation.I was looking at the history and noticed that @davidkpiano change the default
clock
behavior in this commit to use the.call(null, ...args)
pattern. This makes sense since you wantsetTimeout
bound to theglobal
instance passed in, but I think Firefox is doing some additional checks on thethisArg
which causes this error.The text was updated successfully, but these errors were encountered: