dev mode errors for non-stores

pull/1880/head
Rich Harris 7 years ago
parent 8f56cfe310
commit ae4767a4dc

@ -287,6 +287,7 @@ export default function dom(
const reactive_store_subscriptions = reactive_stores.length > 0 && reactive_stores const reactive_store_subscriptions = reactive_stores.length > 0 && reactive_stores
.map(name => deindent` .map(name => deindent`
let ${name}; let ${name};
${component.options.dev && `@validate_store(${name.slice(1)}, '${name.slice(1)}');`}
$$self.$$.on_destroy.push(${name.slice(1)}.subscribe($$value => { ${name} = $$value; $$make_dirty('${name}'); })); $$self.$$.on_destroy.push(${name.slice(1)}.subscribe($$value => { ${name} = $$value; $$make_dirty('${name}'); }));
`) `)
.join('\n\n'); .join('\n\n');

@ -37,7 +37,13 @@ export default function ssr(
} }
const reactive_stores = Array.from(component.template_references).filter(n => n[0] === '$'); const reactive_stores = Array.from(component.template_references).filter(n => n[0] === '$');
const reactive_store_values = reactive_stores.map(name => `const ${name} = @get_store_value(${name.slice(1)});`) const reactive_store_values = reactive_stores.map(name => {
const assignment = `const ${name} = @get_store_value(${name.slice(1)});`;
return component.options.dev
? `@validate_store(${name.slice(1)}, '${name.slice(1)}'); ${assignment}`
: assignment;
});
// TODO only do this for props with a default value // TODO only do this for props with a default value
const parent_bindings = component.javascript const parent_bindings = component.javascript

@ -55,4 +55,10 @@ export function safe_not_equal(a, b) {
export function not_equal(a, b) { export function not_equal(a, b) {
return a != a ? b == b : a !== b; return a != a ? b == b : a !== b;
}
export function validate_store(store, name) {
if (!store || typeof store.subscribe !== 'function') {
throw new Error(`'${name}' is not a store with a 'subscribe' method`);
}
} }

@ -0,0 +1,11 @@
export default {
compileOptions: {
dev: true
},
props: {
count: 0
},
error: `'count' is not a store with a 'subscribe' method`
};

@ -0,0 +1,5 @@
<script>
export let count;
</script>
<button on:click="{() => count.update(n => n + 1)}">count {$count}</button>
Loading…
Cancel
Save