Skip to content
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

allow running xstate in a web-worker #1407

Merged
merged 3 commits into from
Aug 26, 2020
Merged

Conversation

tomenden
Copy link
Contributor

Currently, the code of the interpreter assumes that the code runs either in the browser's main thread (window is defined) or in node. This PR allows it to run in a web-worker, in which the global is self

@changeset-bot
Copy link

changeset-bot bot commented Aug 24, 2020

🦋 Changeset is good to go

Latest commit: c02ccbd

We got this.

This PR includes changesets to release 1 package
Name Type
xstate Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@tomenden
Copy link
Contributor Author

tomenden commented Aug 24, 2020

This PR includes changesets to release 7 packages

I think this means I probably messed up the changeset, right? I did not mean to create any major releases.

@codesandbox-ci
Copy link

codesandbox-ci bot commented Aug 24, 2020

This pull request is automatically built and testable in CodeSandbox.

To see build info of the built libraries, click here or the icon next to each commit SHA.

Latest deployment of this branch, based on commit c02ccbd:

Sandbox Source
XState Example Template Configuration
XState React Template Configuration

@tomenden
Copy link
Contributor Author

I could not find anywhere in xstate docs what Node.js version it is aimed for.
Assuming you are fine with supporting Node.js 12+, global/window/self can be replaced by globalThis
(However, this would make it a breaking change, as usage of xstate in earlier versions of node would break)

@@ -124,7 +124,13 @@ export class Interpreter<
},
logger: global.console.log.bind(console),
devTools: false
}))(typeof window === 'undefined' ? global : window);
}))(
typeof window !== 'undefined'
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We could just use self over window as window.self is supported in all popular browsers

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

@Andarist
Copy link
Member

Assuming you are fine with supporting Node.js 12+, global/window/self can be replaced by globalThis

Can't do that right now due to the fact that (like you have mentioned) this would be a breaking change.

Copy link
Member

@davidkpiano davidkpiano left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great change; see @Andarist's suggestions; other than that, LGTM!

tomenden and others added 2 commits August 26, 2020 13:12
Co-authored-by: Mateusz Burzyński <mateuszburzynski@gmail.com>
@tomenden
Copy link
Contributor Author

@Andarist Updated the PR.

@Andarist Andarist merged commit ad3026d into statelyai:master Aug 26, 2020
@github-actions github-actions bot mentioned this pull request Aug 26, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants