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

Make $$.on_hmr public (or a way to detect HMR update) #66

Closed
adiguba opened this issue Mar 4, 2023 · 6 comments
Closed

Make $$.on_hmr public (or a way to detect HMR update) #66

adiguba opened this issue Mar 4, 2023 · 6 comments

Comments

@adiguba
Copy link

adiguba commented Mar 4, 2023

Hello,

I'm working on some change on the way Svelte manage the event-handler via the on:event directive.
My changes work fine as long as the code is not reloaded via HMR...

In fact my code process the handlers added to the component, via .$on() (called by on:event={handler}), who were stored on cmp.$$.callbacks.
But when the code is refreshed via HMR, the component is fully recreated with props, and cmp.$$.callbacks is updated, but I have no way to detect that, so my code cannot (re)process the handlers correctly.

I need a way to know that the component was reloaded in order to reprocess cmp.$$.callback.

I see this comment about the undocumented hook cmp.$$.on_hmr : #57 (comment)
But it seem it didn't work on my case, because this field is initialized when svelte-hmr instrument the component (line 216 of svelte-hooks.js), and it will overwritte my own hook initialized when my component is created :

    targetCmp.$$.on_hmr = []

I edited this line of svelte-hooks.js in order to keep my hooks :

   targetCmp.$$.on_hmr = targetCmp.$$.on_hmr || [];

It's seem to work for me, but I don't know how Svelte-HMR works nor the impact of this change.

Or is there another way to detect code reload ?

Thanks

@adiguba
Copy link
Author

adiguba commented Mar 4, 2023

My bad I feel so stupid.
I just need to add the on_hmr hook AFTER the component was constructed, not inside the constructor.

So sometinh like that :

	let comp = new Comp(...);
	comp.$$.on_hmr && comp.$$.on_hmr.push( (_: any) => {
		return (c: SvelteComponent) => restart_all_callback(c);
	});

Just one question before closing : do $$.on_hmr is safe to use ?

@dominikg
Copy link
Member

dominikg commented Mar 5, 2023

Not sure which changes to svelte you are talking about. If you intend to send a PR to svelte itself, is there a feature request, open issue or PR you can link to?

In general $$ is not meant for public use or part of a stable api for public consumption. Please see vite docs for how to listen to update events: https://vitejs.dev/guide/api-hmr.html#hot-on-event-cb

That being said this is dev only, so you could do introspection like in your example and assume that if on_hmr exists it works the way it currently does. It may break the dev workflow but not production builds if svelte/svelte-hmr change in ways that make it incompatible.

@adiguba
Copy link
Author

adiguba commented Mar 5, 2023

Thanks for your response.
This code is only generated on dev mode.

The issue is here : sveltejs/svelte#2837
I will try to send a PR later...

@adiguba adiguba closed this as completed Mar 5, 2023
@rixo
Copy link
Collaborator

rixo commented Mar 5, 2023

@adiguba There's no immediate plan to make $$.on_hmr public, but there's no plan to remove or change it either.

Make sure you're subscribed to #57, as we'll issue some notice there if for some reason it comes to change.

In any case, if your usage of it is to support a feature that gets included in Svelte itself, you can be sure we won't change it without proper synchronisation to make sure everything keeps working.

@dominikg
Copy link
Member

dominikg commented Mar 5, 2023

also please note that svelte-hmr is currently a dev time addon over svelte. svelte itself is completely unaware of svelte-hmr and in its current major version (svelte3) is should stay that way.

A future major version of svelte may incorporate hmr features directly so svelte-hmr as a separate package will no longer be needed.
For svelte3, new features would have to be supported by svelte-hmr by improving it's proxy or other means.

Given this and the fact that svelte on:* support might need further discussions i encourage you to get in touch on svelte discord #contributing before investing a lot of work in a PR.

@adiguba
Copy link
Author

adiguba commented Mar 5, 2023

Thanks for your advices !

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

No branches or pull requests

3 participants