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

feat: WeakMap when environments are referenced #3014

Closed
wants to merge 4 commits into from
Closed

feat: WeakMap when environments are referenced #3014

wants to merge 4 commits into from

Conversation

maraisr
Copy link
Contributor

@maraisr maraisr commented Feb 6, 2020

With this PR I aim to address the situation where environments are removed, but never garbage collected as references are held onto in this Map.

A simple case could be:

const MyApp = () => {

	const [environment, setEnvironment] = useState(new Environment({ network, store }));

	useEffect(() => {
		setTimeout(() => {
			setEnvironment(new Environment({ network, store }));
		}, 1e3);
	}, []);

	return (<RelayEnvironmentProvider environment={environment}>
		<MyComponentTreeWith_A_useFragment/>
	</RelayEnvironmentProvider>);
};

as you'll find from that example, the dataResources map will be forever growing, with nothing clearing.

fixes: #3013

Copy link
Contributor

@sibelius sibelius left a comment

Choose a reason for hiding this comment

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

It would fix SSR use cases

@maraisr maraisr changed the title feat: FragmentResource dataResources to WeakMap instead feat: WeakMap when environments are referenced Feb 7, 2020
Copy link
Contributor

@alunyov alunyov left a comment

Choose a reason for hiding this comment

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

Thanks for this change!

packages/relay-experimental/FragmentResource.js Outdated Show resolved Hide resolved
packages/relay-experimental/QueryResource.js Outdated Show resolved Hide resolved
Copy link
Contributor

@facebook-github-bot facebook-github-bot left a comment

Choose a reason for hiding this comment

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

@jstejada has imported this pull request. If you are a Facebook employee, you can view this diff on Phabricator.

@maraisr
Copy link
Contributor Author

maraisr commented Feb 8, 2020

Thanks for that @alunyov, I have made those IMap changes... I'm not the best Flow writer, so please do tell me if there was a better way.

Also; should we try and implement something that mimic's the WeakMap behavior for those targets where WEAKMAP_SUPPORTED is false, or pull in core-js polyfill for that? On that note though, afaik WeakMap is supported for the same browser stack as React - so we could do away with the check? All except Internet Explorer 9 - which need polyfills anyway?

@alunyov
Copy link
Contributor

alunyov commented Feb 10, 2020

Thanks for the update!

Also; should we try and implement something that mimic's the WeakMap

IMO, we may just keep it as it is right now. They're just a few instances where we are using WeakMap, so - not a huge overhead. And also pulling in the polyfill will most likely conflict with our internal setup.

Copy link
Contributor

@facebook-github-bot facebook-github-bot left a comment

Choose a reason for hiding this comment

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

@alunyov has imported this pull request. If you are a Facebook employee, you can view this diff on Phabricator.

@maraisr
Copy link
Contributor Author

maraisr commented Feb 11, 2020

Oh sorry, I may have no articulated myself correctly. My bad. I'm not saying we include the polyfill within this repo. Im merely saying, that bundlers, runtimers etc.. That they include the polyfill, if they choose. Much like how react isnt including a polyfill for Map, but rather saying that if IE9 is your target, then you'll need to include a polyfill for Map.

What I'm really trying to say is, let this library articulate that we want the behavior of a WeakMap by using it - and simply say that for environments where that isn't directly available, that you have to use polyfill's, by whatever means your setup is like.. So Facebook might use rome, babel, whatever.

@facebook-github-bot
Copy link
Contributor

@alunyov merged this pull request in a727f8c.

jstejada pushed a commit that referenced this pull request Feb 13, 2020
Summary:
With this PR I aim to address the situation where environments are removed, but never garbage collected as references are held onto in this Map.

A simple case could be:

```js
const MyApp = () => {

	const [environment, setEnvironment] = useState(new Environment({ network, store }));

	useEffect(() => {
		setTimeout(() => {
			setEnvironment(new Environment({ network, store }));
		}, 1e3);
	}, []);

	return (<RelayEnvironmentProvider environment={environment}>
		<MyComponentTreeWith_A_useFragment/>
	</RelayEnvironmentProvider>);
};
```

as you'll find from that example, the `dataResources` map will be forever growing, with nothing clearing.

fixes: #3013
Pull Request resolved: #3014

Reviewed By: jstejada

Differential Revision: D19797050

Pulled By: alunyov

fbshipit-source-id: 4ef18e7634c7d50597944967e9ffc0a330ac3633
@maraisr maraisr mentioned this pull request Apr 14, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Environment's held on longer than they need too
4 participants