Svelte: Auto-subscribe to stores

Created on 16 Dec 2018  路  2Comments  路  Source: sveltejs/svelte

Referencing a variable with a $ prefix should cause code similar to the following to be added:

import { onDestroy } from 'svelte';

let $foo;
onDestroy(foo.subscribe($$value => {
  $foo = $$value;
});

(See RFC 2 for an explanation.)

In dev mode, it should probably check that foo has a subscribe method first, and print a more helpful error.

Questions:

  • If foo is not defined, what do we do? Assume $foo isn't the unwrapped value of a store?
  • Do we do the same thing for reactive declarations?
  • If $foo is already defined by the user, do we still add this code?

Most helpful comment

If foo is not defined, what do we do?

I vote for "throw a noisy error".

If $foo is already defined by the user, do we still add this code?

I vote for "throw a noisy error".

All 2 comments

If foo is not defined, what do we do?

I vote for "throw a noisy error".

If $foo is already defined by the user, do we still add this code?

I vote for "throw a noisy error".

closed via #1880

Was this page helpful?
0 / 5 - 0 ratings

Related issues

mmjmanders picture mmjmanders  路  3Comments

thoughtspile picture thoughtspile  路  3Comments

robnagler picture robnagler  路  3Comments

davidcallanan picture davidcallanan  路  3Comments

bestguy picture bestguy  路  3Comments