Giter Site home page Giter Site logo

Feature request: Loaders about tyin HOT 2 OPEN

mausworks avatar mausworks commented on July 29, 2024
Feature request: Loaders

from tyin.

Comments (2)

mausworks avatar mausworks commented on July 29, 2024 1

Thanks for a great feature request! ❤️

Data fetching is a concern that I want Tyin to be able to cover (without external libraries such as react-query), and I am working on a new plugin which will be found under tyin/plugin-sync in version 2.0.0! I'm currently experimenting with it in dott.bio and it's nearing a feature-complete and stable API.

I wanted to use a pattern that most developers are familiar with and landed in pseudo-git mechanics: pushing, pulling, and deleting. The mentality is that, just as with git, you push and pull your changes upstream or downstream—syncing your changes to the repository. This translates exceptionally well into the idea of stores/state containers.

With it, you will have request-deduplication and caching built straight into Tyin, and the setup is dead simple. I'll share a simple example from dott.bio:

import { PrivateProfile } from "@/utils/profile";
import extend from "tyin/extend";
import storeHook from "tyin/hook";
import objectAPI from "tyin/api/object";
import sync from "tyin/sync";
import { databaseAccess } from "./useDatabase";

const useUserProfile = extend(storeHook<PrivateProfile | null>(null))
  .with(objectAPI())
  .with(databaseAccess())
  .with((store) =>
    sync({
      pullOptions: { cacheDuration: 5000 },
      pull: async (sub: string | undefined | null) => {
        if (!sub) return null;

        return await store.db
          .from("Profile")
          .select("*, Socials:Social(*), Links:Link(*)")
          .eq("sub", sub)
          .order("order", { referencedTable: "Link", ascending: true })
          .order("order", { referencedTable: "Social", ascending: true })
          .then(({ data }) => data?.at(0) ?? null);
      },
    })
  )
  .seal();

export default useUserProfile;

To automatically pull the state, you can use the usePull hook, provided by the same module:

import usePull from "tyin/sync/usePull";
import useUserProfile from "@/stores/useUserProfile";
import useCurrentUser from "@/stores/useCurrentUser";

export default function UserProfilePage() {
  const user = useCurrentUser();
  const { error, status, state: profile } = usePull(useUserProfile, user?.sub);

  return null;
}

You can also provide push and delete functions when setting up the sync API, though there is no hook that helps with it directly. I'll provide a PR later tonight, and I would love your feedback! ❤️

from tyin.

tengl avatar tengl commented on July 29, 2024

I looked at the PR #10 and it looked great. Just a few comments, but nothing big.

from tyin.

Related Issues (2)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.