Svelte stores are just variables

I use Svelte at my job. It has a thing called stores and they are nice.

In a nutshell, they help you with reactive state. But I don't come here to tell (sell) you Svelte or its stores and why you should use them; there are too many plenty of online articles and courses that do that already.

I just want to tell you that they are just variables with not much more on top.

If you are like me, you like to poke at the source code of your dependencies, even if it's a compiler[1]. I was surprised to find out how simple the implementation of a Svelte store was. They are just a variable (technically it is a lexically scoped variable or closures but that's not too important).

Below is a synopsized version of the implementation[2]:

function writable(value) {
  const subscribers = new Set();

  function set(new_value) {
    if (value !== new_value) {
      value = new_value;
      notify_subscribers(subscribers);
    }
  }

  function update(fn) {
    set(fn(value));
  }

  function subscribe(run) {
    const subscriber = [run];
    subscribers.add(subscriber);
    run(value);

    return () => {
      subscribers.delete(subscriber);
    }
  }

  return { set, update, subscribe };
}

I omitted some parts of the implementation related to some utility callbacks—but they're not used as often nor are the selling point of stores.

I think that—besides the simplicity—there are deeper lessons here.

For one, scrutinize what your tools are made of!

We live in the era of open-source. Stallman et al. fought hard for you to be able to do this. Take advantage of the times we are living in.

After all, many tools are written by people no more intelligent than you, but (probably) more diligent than you. But you also have way more agency than you think over that second trait.

In fact, minutes before writing this, I was reading the code for the Clojure compiler to (really) understand the differences between (standard) macros, reader macros, and special forms. At first, I was a bit scared. But no more than 15 minutes in and I already had code that corroborated my understanding.

The great thing about learning through scrutiny is that, by the end, I am confident that I learned well. 

It was my terminal running code against the source code of the compiler; me against nature. And I won.

I love this approach for two reasons:

  1. Source code is truth. It is in the name. If you read the source code of what you are using, you are reading the code that your machine (and others) will run. No gimmickies, no ulterior motives, no sugar. Rawdogging knowledge into your brain is the best way to learn.
  2. It's actually faster. Learning this way feels slow, but it's actually fast. You will struggle, but take consolation in the fact that you are looking at the piece of code that articles, talks, tutorials, and courses can only proxytalk about. It may take you a few minutes to grasp proxy material. But, if you sum the time of all the proxy material, it will exceed the time it would've taken you to just understand the “true” source.

Why don't most people do this? The answer is boring and simple: because it's painful. It's the same reason why people spend millions of hours and dollars looking for magic meals and a perfect routine instead of just eating less and moving more. Losing money and time is less painful than actually trying to accomplish your goals.

So, the feeling of pain should serve as a hint that you should do it this way. Reading axiomatic material feels painful but, if you time yourself, you'll realize you've actually not spent that much time. Doing things the “good way” always feels painful and slow. That's why we spend hours on platforms like YouTube, Twitter, or Instagram and yet it only feels great and fast—until it doesn't.

So anyways, Svelte stores are great for handling app state and can teach you a thing or two about life!

Plug: we use Svelte stores at KREA all the time. And if this kind of engineering mentality resonates with you, we are hiring technical talent. Send me a direct message over Twitter/X or send me an email to d at krea dot ai.


[1]: if you didn't know, Svelte is a compiler that outputs JavaScript as opposed to, say, bytecode or x86 instructions.

[2]: The actual implementation can be found here.