pull/10481/head
navorite 3 years ago
parent da9bd2417f
commit 50679927a8

@ -362,6 +362,25 @@ function is_store_name(name) {
return name[0] === '$' && /[A-Za-z_]/.test(name[1]);
}
/**
*
* @param {Iterable<import('#compiler').Binding>} bindings
*/
function is_store_exists(bindings) {
for (const binding of bindings) {
if (binding.kind === 'store_sub') {
for (const reference of binding.references) {
const node = reference.path.at(-1);
// make sure it is not a sub in a directive e.g. use:$store as it is unneeded
if (node?.type !== 'RegularElement') {
return true;
}
}
}
}
}
/**
* @param {import('estree').AssignmentExpression} node
* @param {import('zimmerframe').Context<import('#compiler').SvelteNode, import('./types').ServerTransformState>} context
@ -2089,15 +2108,10 @@ export function server_component(analysis, options) {
];
}
if (
[...analysis.instance.scope.declarations.values()].some(
(binding) => binding.kind === 'store_sub'
)
) {
if (is_store_exists(analysis.instance.scope.declarations.values())) {
instance.body.unshift(b.const('$$store_subs', b.object([])));
template.body.push(b.stmt(b.call('$.unsubscribe_stores', b.id('$$store_subs'))));
}
// Propagate values of bound props upwards if they're undefined in the parent and have a value.
// Don't do this as part of the props retrieval because people could eagerly mutate the prop in the instance script.
/** @type {import('estree').Property[]} */

@ -6,10 +6,8 @@ import { writable } from 'svelte/store';
export default function Main($$payload, $$props) {
$.push(false);
const $$store_subs = {};
const animate = writable();
$$payload.out += `<div>Hello!</div> <div>Hello!</div>`;
$.unsubscribe_stores($$store_subs);
$.pop();
}
Loading…
Cancel
Save