SolidJSS
SolidJSโ€ข12mo agoโ€ข
10 replies
rtzrtz.

SingleFlight does not work (See Example)

I'm trying to make a single flight request work, but I'm always getting two requests:

What am I missing here?

import {
  action,
  createAsync,
  query,
  revalidate,
  useAction,
} from "@solidjs/router";

const items: string[] = ["yo", "lo", "foo", "bar"];

const getItems = query(async () => {
  "use server";
  await new Promise((r) => setTimeout(r, 200));
  return items;
}, "items");

const addAction = action(async (item: string) => {
  "use server";
  items.push(item);
  return revalidate("items");
});

export default function Flight() {
  const items = createAsync(async () => getItems(), { // does always fire after clicking "Add"
    deferStream: true,
  });

  const addItem = useAction(addAction); // response never includes items-data

  return (
    <div>
      <ul>
        {items()?.map((item) => (
          <li>{item}</li>
        ))}
      </ul>
      <button onClick={() => addItem(Date.now().toString())}>Add</button>
    </div>
  );
}
Was this page helpful?