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

component.get() can return undefined after component destroyed #1354

Closed
nolanlawson opened this issue Apr 19, 2018 · 5 comments · Fixed by #1365
Closed

component.get() can return undefined after component destroyed #1354

nolanlawson opened this issue Apr 19, 2018 · 5 comments · Fixed by #1365

Comments

@nolanlawson
Copy link
Contributor

Since this.get('foo') is deprecated (#1347), I've been working on migrating all usages to let { foo } = this.get().

However I've found that the two aren't exactly equivalent, since this.get() can actually return undefined if the component is destroyed (e.g. because it was called within a requestIdleCallback() or setTimeout() callback).

Throws an error:

<h1>Hello {name}!</h1>
<script>
	export default {
		oncreate() {
			setTimeout(() => this.destroy(), 1000)
			setTimeout(() => {
				let { name } = this.get() // throws
				console.log('name', name)
			}, 2000)
		}
	}
</script>

Doesn't throw an error:

<h1>Hello {name}!</h1>
<script>
	export default {
		oncreate() {
			setTimeout(() => this.destroy(), 1000)
			setTimeout(() => {
				let name = this.get('name') // does not throw
				console.log('name', name)
			}, 2000)
		}
	}
</script>

So far, my workaround has been to do let { foo } = this.get() || {} but it would be nice if it were just automatically wrapped in an empty object so that the two forms were equivalent.

@nolanlawson
Copy link
Contributor Author

Related: it would be useful to be able to know within an asynchronous callback whether the component was destroyed or not, without having to attach an on('destroy') listener. (I notice #948 was not merged.)

@Conduitry
Copy link
Member

Can we combine these two problems into a solution? Does component.get() always return undefined after the component has been destroyed?

@nolanlawson
Copy link
Contributor Author

I'm not sure. What I am sure about is that if the component is destroyed, then component.get() is undefined. I'm not sure where exactly in the lifecycle it gets set to undefined, i.e. if it's right before getting destroyed, right after, etc.

In any case, I would prefer not to have to think about every place in my code where the component might have been destroyed (i.e. to wrap component.get() in a component.destroyed check or similar), but maybe that's not practical.

@tevel
Copy link

tevel commented Apr 19, 2018

I also just fell into this.
It would be great to have the way to know if component was destroyed.
Getting undefined within component.get() may be a solution.
Let's keep it

@Rich-Harris
Copy link
Member

As of 2.1.1, component state reverts to an empty object on destroy, rather than null. We could revisit #948, though I sort of prefer the idea that in a state-driven system you shouldn't ever have to think about whether a component has been destroyed or not.

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 a pull request may close this issue.

5 participants