what is starfx?

on erock's blog

a modern approach to side-effect and state-management
#starfx #front-end #javascript

starfx is a library I've been working on for about a year. My plan is to make it the spiritual successor to redux-saga. However, in that journey I realized I also had to leave redux behind.

The idea is still percolating, but I wanted to slowly start talking about it as I finalize the API. This library is trying to accomplish a lot, but it will essentially provide the functionality we enjoy with redux, redux-saga, and rtk-query but with a modern spin on side-effect and state management.

My current idea about this library is this: The FE community has been so focused on state management to the point where side-effect management is an afterthought. I want to flip that on its head with starfx. Side-effect management should be the first-class citizen of FE apps.

With that idea in mind, we have a set of abstractions that make managing side-effects -- including state mutations -- easy to read, write, and maintain.

At the core, we are leveraging two foundational libraries that help us with structured concurrency, using generators:

# delimited continuations

I recently gave a talk about deliminited continuations where I also discuss this library:

Delminited continuations are all you need

# starfx/examples

Want to skip the exposition and jump into code?

Check out our starfx-examples to play with it.

# starfx

As we have seen with the success of redux-saga, what we really want is the ability to have a tree-like task structure to manage side-effects. Any I/O operations greatly benefit from having complete control over asynchronous tasks and redux-saga is awesome at that because of structured concurrency.

So with effection we get something that looks like redux-saga but doesn't need redux at all to function.

The example currently in our README demonstrates that level of control we have in starfx:

 1import { each, json, main, parallel, request } from "starfx";
 2
 3function* fetchChapter(title: string) {
 4  const response = yield* request(`/chapters/${title}`);
 5  const data = yield* json(response);
 6  return data;
 7}
 8
 9const task = main(function* () {
10  const chapters = ["01", "02", "03"];
11  const ops = chapters.map((title) => () => fetchChapter(title));
12
13  // parallel returns a list of `Result` type
14  const chapters = yield* parallel(ops);
15
16  // make http requests in parallel but process them in sequence (e.g. 01, 02, 03)
17  for (const result of yield* each(chapters.sequence)) {
18    if (result.ok) {
19      console.log(result.value);
20    } else {
21      console.error(result.error);
22    }
23    yield* each.next;
24  }
25});
26
27const results = await task;
28console.log(results);

So we have everything we need to express any async flow construct at the core of this library. At least for FE apps, the next thing we need is a way to intelligently let our view layer (e.g. react) know when our data store updates. This is where an immutable data store has been incredibly useful.

# starfx/store

I love redux and nothing comes close to its ability to scale with such a simple idea: reducers. I think it beats any other "data synchronization" libraries out there (including react-query), but there's a cost. The cost is a strict structure for updating your data store as well as boilerplate. rtk did a lot to reduce boilerplate and move the community forward, but under-the-hood, it's relying on immer to provide immutability.

So I created starfx/store to give us an immutable data store that doesn't require reducers.

1import { configureStore, updateStore } from "starfx/store";
2const store = configureStore({ initialState: { users: {} } });
3
4await store.run(function*() {
5  yield* updateStore((state) => {
6    state.users[1] = { id: "1", name: "bob" });
7  });
8});

Users are free to mutate state applying the same rules as immer -- because that's what we use. There's a koa-like middleware system as well for the store, to provide similar features to redux via middleware, with plans to inline major ones like redux-persist.

It's simpler than redux but I think it still captures the essence of what redux provides: a way to structure state mutations so react can be notified of updates.

With simplifying state management, we are trying to create an API that resonates with modern typescript. Instead of defining a reducer per slice in your store and making sure they don't interact with each other, we want something that looks more like a database schema. A single file where we can go and say "ah, this is our state structure." The reason why this is so important is because when you know what kind of data you have in your store, it makes understanding the codebase much simpler. The store is king and we want a schematic for it. Further, we took the idea of createSlice() from rtk and put some structure around it. I've been using createSlice() in production probably more than anyone on this planet -- because I helped create it. When building and structuring my data store for tons of web apps, there are very common data structures:

With these I can pretty much build any modern FE web app. So when you define your data store in starfx/store, you provide a schema of these types:

 1import { createSchema, slice } from "starfx/store";
 2
 3const emptyUser = { id: "", name: "" };
 4export const schema = createSchema({
 5  users: slice.table({ empty: emptyUser }),
 6  data: slice.table({ empty: {} }),
 7  loaders: slice.loader(),
 8  token: slice.str(),
 9});
10export type AppState = typeof schema.initialState;
11/*
12{
13  users: Record<string, { id: string; name: string }>;
14  data: Record<string, unknown>;
15  loaders: Record<string, LoaderItemState>;
16}
17*/

When creating this schema, you get: the entire state type, initial state, actions, and selectors. I won't dwell on this API too much but this is the area I'm the most excited about with starfx. I have to admit, the inspiration came from the idea of stapling zod on top of redux. I've only just started playing with it, but I can tell already it's going to be awesome. Pre-built data structures and a way to read and write them is nice.

Like redux, we leverage reselect to create selectors and encourage end-developers to use it as well, which is why we re-export createSelector.

Similar to rtk but philosophically different, we also have a way to manage data synchronization.

# createPipe

createPipe is a koa-like middleware system hooked into activating side-effects as well as our business logic. It a port of saga-query:

 1import { createPipe, run, sleep } from "starfx";
 2
 3const thunks = createPipe();
 4thunks.use(thunks.routes());
 5thunks.use(function* (ctx, next) {
 6  console.log("start");
 7  yield next();
 8  console.log("all done!");
 9});
10
11const increment = thunks.create("increment", function* (ctx, next) {
12  yield next();
13  console.log("waiting 1s");
14  yield sleep(1000);
15  console.log("incrementing!");
16});
17
18await run(increment());
19// start
20// waiting 1s
21// incrementing!
22// all done!

# createApi

createApi builds on top of createPipe but restructures it for making HTTP requests:

 1import { createApi, fetcher, requestMonitor } from "starfx";
 2import { configureStore, createSchema, slice, storeMdw } from "starfx/store";
 3
 4const schema = createSchema({
 5  loaders: slice.loader(),
 6  data: slice.table(),
 7});
 8const { db } = schema;
 9const store = configureStore(schema);
10
11export const api = createApi();
12api.use(requestMonitor());
13api.use(storeMdw(db));
14api.use(api.routes());
15api.use(fetcher({ baseUrl: "https://jsonplaceholder.typicode.com" }));
16
17export const fetchUsers = api.get<never, User[]>(
18  "/users",
19  { supervisor: takeEvery },
20  function* (ctx, next) {
21    yield* next();
22    if (!ctx.json.ok) {
23      return;
24    }
25
26    const users = ctx.json.data.reduce<Record<string, User>>((acc, user) => {
27      acc[user.id] = user;
28      return acc;
29    }, {});
30    yield* schema.update(db.users.add(users));
31  },
32);
33
34store.dispatch(fetchUsers());

# react

This will fetch and automatically store the result, to be used inside react:

 1import { useDispatch, useSelector } from "starfx/react";
 2
 3import { AppState, db, fetchUsers } from "./api.ts";
 4
 5function App({ id }: { id: string }) {
 6  const dispatch = useDispatch();
 7  const user = useSelector((s: AppState) => db.users.selectById(s, { id }));
 8  const userList = useSelector(db.users.selectTableAsList);
 9  return (
10    <div>
11      <div>hi there, {user.name}</div>
12      <button onClick={() => dispatch(fetchUsers())}>Fetch users</button>
13      {userList.map((u) => {
14        return <div key={u.id}>({u.id}) {u.name}</div>;
15      })}
16    </div>
17  );
18}

Have a comment? Start a discussion by sending an email to my public inbox.