On decoupled loaders

January 21, 2022 on Eric Bower's blog

It’s pretty common on the FE to track the status of data being loaded, which I often refer to them as loaders.

This provides the ability to display loading indicators when data is being fetched as well as return other meta data like errors or other relevant information about the data that was fetched. It can also help assist in flow control when there is some hairy business logic that needs to wait for an operation to complete before proceeding to the next step.

More times than not, it’s common to colocate the data with the status.

{
  posts: {
    status: 'loading', // or 'error' or 'success'
    error: '',
    meta: {},
    data: []
  }
}

On the surface this seems like a great way to ensure the data and the loading state stays in sync. When inspecting your state, it’s easy to see at-a-glance the current status of your data. However, there are a couple issues with this pattern.

Data is now nested deeper in your state object

I keep quoting the zen of python over and over again because it often guides how I think about code design and architecture:

$ python
>>> import this
The Zen of Python, by Tim Peters

Beautiful is better than ugly.
Explicit is better than implicit.
Simple is better than complex.
Complex is better than complicated.
**Flat is better than nested.**
Sparse is better than dense.
Readability counts.
Special cases aren't special enough to break the rules.
Although practicality beats purity.
Errors should never pass silently.
Unless explicitly silenced.
In the face of ambiguity, refuse the temptation to guess.
There should be one-- and preferably only one --obvious way to do it.
Although that way may not be obvious at first unless you're Dutch.
Now is better than never.
Although never is often better than *right* now.
If the implementation is hard to explain, it's a bad idea.
If the implementation is easy to explain, it may be a good idea.
Namespaces are one honking great idea -- let's do more of those!

Flat is better than nested

In redux, the kernel of this rule is embedded in the following recommendation:

When I’m debugging some code that relates to my state data, I want to be able to inspect the data quickly. I want the data to be plain JSON so it’s easy to read in the console, and I would prefer to not traverse a bunch of nested objects to find the relevant information.

By colocating a loader with the data that is being loaded, it adds unnecessary nesting. Also, now that the data is nested, every query or debug session involves traversing the object tree. This also requires a slew of existential checks to ensure the objects even exist.

Aside: I wrote an article on preventing existential checks, check it out!

Loading state logic requires a data object

The logic for loading states is now tightly coupled to the data that is being loaded. Like I said previously, on the surface this seems great, but what if you want loading states that are not a 1:1 to a single piece of data?

What if you want a loading state for multiple pieces of data being loaded? To solve that problem with a couple of loaders, you have to create custom logic to wait for all loaders to be completed before proceeding. What happens when of the loaders fail or never finish? This is all ad-hoc logic and complexity stapled on top of a design for something that it was never intended to solve.

Enter decoupled loaders

Instead, what I do in my projects with global state, like redux: I create a separate data store for all my loaders:

{
  posts: [],
  loaders: {
    posts: {
      status: 'loading',
      error: '',
      meta: {},
    }
  },
}

In this example, the posts data is at the top of the state data and all of my loaders are in a separate slice.

Here is an example of how I would manage the loading state:

async fetchPosts() {
  startLoader({ id: 'posts' });

  const result = await fetch('/posts');

  if (!result.ok) {
    errorLoader({ id: 'posts', error: 'failed' });
    return;
  }

  const post = await result.json();
  successLoader({ id: 'posts', meta: { id: post.id } });
}

A react component might look something like this:

import { fetchPosts } from './api';
import { useLoader } from './hooks';

const Page = () => {
  const loader = useLoader('posts'); // find loader by id

  useEffect(() => {
    fetchPosts();
  }, []);

  if (loader.loading) return <Spinner />;

  return (
    <div>
      {posts.map((p) => (
        <div key={p.id}>{p.title}</div>
      ))}
    </div>
  );
};

The logic is exactly the same, but the beauty of decoupling the logic of the loading state is you can now use the loaders for any asynchronous actions in our app.

For example, what if we want to have an initial loader to fetch a bunch of data:

async fetchInitialData() {
  startLoader({ id: 'initial-data' });

  await fetchUser();
  await fetchPosts();
  await fetchComments();

  successLoader({ id: 'initial-data' });
}

Or what if we have to do some CPU intensive task

async intensity() {
  startLoader({ id: 'intensity' });

  while (notFinished) {
    // create the universe
  }

  successLoader({ id: 'intensity' });
}

You can now reuse the same component logic as you do for loading single pieces of data!

const loader = useLoader('intensity');
// or useLoader('initial-data');
// or useLoader('posts');
// it's all the same!

In this way, our loaders become generic status trackers for any operation the user or app might trigger. This provides way more flexibility than a loader that is tightly coupled to fetching single pieces of data.

For all my react/redux projects, I generally use robodux which has a slice helper called createLoaderTable which encapsulates this decoupled loading logic.

As a result, every project can reuse the same decoupled loaders. It works well for small projects as well as large enterprise ones.


Articles from blogs I read

Generated by openring

Moxie Marlinspike on Decentralization

Moxie Marlinspike, founder and former CEO of Signal Messenger LLC, on the topic of decentralization and why centralized systems are supposedly the way to go.

via マリウス August 10, 2022

How I wish I could organize my thoughts

I keep a pen & notebook on my desk, which I make liberal use of to jot down my thoughts. It works pretty well: ad-hoc todo lists, notes on problems I’m working on, tables, flowcharts, etc. It has some limitations, though. Sharing anything out of my noteb…

via Drew DeVault's blog August 10, 2022

How to look up a Nix package's Nix store path from flake inputs

Image generated by MidJourney -- The fall of the Archons, colored pencil drawing, fireball spell, bright sky, digital art, lake of fire Sometimes God is dead and you need to figure out what the version of a package in your Nix flake's inputs is. With fl…

via Xe's Blog August 6, 2022