Skip to content

allow Library.require override #289

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

Merged
merged 1 commit into from
Jun 7, 2022
Merged

allow Library.require override #289

merged 1 commit into from
Jun 7, 2022

Conversation

mbostock
Copy link
Member

@mbostock mbostock commented Jun 2, 2022

This PR allows the user to change the require implementation by assigning to Library.require.

Originally, I intended this to be local to the Library instance (hence the resolve argument to the Library constructor). However, there are a number of places where we want to require stuff statically, such as within the AbstractFile class definition. Therefore it seems unavoidable to have only a single definition of require. (Eventually we want to move everything to dynamic ES module imports anyway…)

@mbostock mbostock requested a review from duaneatat June 2, 2022 23:12
Copy link
Contributor

@duaneatat duaneatat left a comment

Choose a reason for hiding this comment

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

Does this create the possibility of a race-condition if there are in-flight requires when setDefaultRequire is called?

@mbostock
Copy link
Member Author

mbostock commented Jun 3, 2022

Does this create the possibility of a race-condition if there are in-flight requires when setDefaultRequire is called?

You can’t really have multiple requires at the same time (because require is built around a single global function named define), so the expectation is that you set Library.require before you do anything else.

@duaneatat
Copy link
Contributor

You can’t really have multiple requires at the same time (because require is built around a single global function named define)

Yep, that limitation is the reason I asked -- the race-condition I was referring to is if the user changed require while there are in-flight requires, since both requires depend on the global define. But I acknowledge that this is a pretty contrived scenario so this lgtm!

@mbostock mbostock force-pushed the mbostock/require-override branch from 2d258a2 to 2b58b50 Compare June 7, 2022 02:43
@mbostock mbostock merged commit 971b140 into main Jun 7, 2022
@mbostock mbostock deleted the mbostock/require-override branch June 7, 2022 02:44
@mbostock mbostock mentioned this pull request Jun 7, 2022
@mbostock
Copy link
Member Author

mbostock commented Jun 7, 2022

This was totally broken… sorry for not testing before asking you to review. I fixed this in 6de65c3. It’d be nice to have a better way to test this locally via unit tests.

@mbostock
Copy link
Member Author

mbostock commented Jun 7, 2022

Also I needed to re-export requireFrom and resolveFrom in 784dcb3 so I could use them (without having to load d3-require twice, since d3-require is baked-in to this library).

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.

2 participants