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

fix: replace with getGlobal #3133

Merged
merged 5 commits into from
Mar 10, 2022
Merged

fix: replace with getGlobal #3133

merged 5 commits into from
Mar 10, 2022

Conversation

fw6
Copy link
Contributor

@fw6 fw6 commented Mar 8, 2022

In some runtime environments(the Mini Program unique to China), self or global may be empty.

@changeset-bot
Copy link

changeset-bot bot commented Mar 8, 2022

🦋 Changeset detected

Latest commit: f47d149

The changes in this PR will be included in the next version bump.

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

@ghost
Copy link

ghost commented Mar 8, 2022

CodeSee Review Map:

Review these changes using an interactive CodeSee Map

Review in an interactive map

View more CodeSee Maps

Legend

CodeSee Map Legend

@codesandbox-ci
Copy link

codesandbox-ci bot commented Mar 8, 2022

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 f47d149:

Sandbox Source
XState Example Template Configuration
XState React Template Configuration

@davidkpiano
Copy link
Member

@fw6
Copy link
Contributor Author

fw6 commented Mar 8, 2022

This seems to be missing the getGlobal() implementation?

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/globalThis#search_for_the_global_across_environments

🚧 the Mini Program development framework don't provide global/self, only window can get (framework app-service).

🚑️ It should be better to replace that line of code with function getGlobal.

iShot2022-03-08 21 04 36

@@ -142,9 +142,9 @@ export class Interpreter<
return clearTimeout(id);
}
} as Clock,
logger: global.console.log.bind(console),
logger: global?.console.log.bind(console),
Copy link
Member

Choose a reason for hiding this comment

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

Is this change required though? Are all standard~ global objects not available in this environment? Even self and globalThis?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

my fault😢 . this change is not required.
Only self and globalThis is undefined. window is available.

Copy link
Contributor Author

@fw6 fw6 Mar 9, 2022

Choose a reason for hiding this comment

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

the return value of getGlobal may be undefined

image

Copy link
Member

Choose a reason for hiding this comment

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

window is available.

This is somewhat confusing as on the screenshot shared here it states that window shouldn't be available in this environment 🤔

Copy link
Contributor Author

Choose a reason for hiding this comment

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

replacing global with window or globalThis may be the right way, but why not use getGlobal() directly.

image

Copy link
Contributor Author

Choose a reason for hiding this comment

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

😅 global don't have the console object
image

Copy link
Member

Choose a reason for hiding this comment

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

That is... some bizarre env there 😅 The especially surprising bit is this:

window "[object Object]"
globalThis "[object Window]"

I'm still surprised that window is available here as that contradicts the documentation - but, of course, I believe the screenshot that you have shared just now. Is globalThis.console available there?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Is globalThis.console available there?

Yep.

Copy link
Member

Choose a reason for hiding this comment

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

I've tweaked the fix and pushed it out to your branch. Could you check if this variant works for you?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Everything works perfectly. Thank you so much 🎉 🚀

@@ -142,9 +142,9 @@ export class Interpreter<
return clearTimeout(id);
}
} as Clock,
logger: global.console.log.bind(console),
Copy link
Member

Choose a reason for hiding this comment

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

my rationale here is that the argument passed to this bind call wasn't looked up on the global variable anyway. This means that all our users were already accessing this in their respective global scopes automatically and it worked. it had to work as otherwise they wouldn't be able to run XState at all as it would throw Uncaught ReferenceError: console is not defined at the initialization time.

So I think that we don't have to resolve the global object here manually at all and we can just rely on the standard JS scope lookup.

@Andarist Andarist merged commit 4feef9d into statelyai:main Mar 10, 2022
This was referenced Mar 10, 2022
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