Code Monkey home page Code Monkey logo

daxus's People

Contributors

dependabot[bot] avatar fonger avatar jason89521 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

daxus's Issues

Manually set stale

Allow developer to set which accessors are stale. Like React Query's queryClient.invalidateQueries.

add devtool

Like Redux or React Query, we should also build a devtool to make debugging easier. This should be done after the api stable. However, I haven't built an extension, I should research it.

mutate auto model would not cause rerender

Currently the auto accessor only notifies the listener inside it instead of notifying the whole model. If developers mutate the model, it would not cause a rerender.

We have two way to fix it:

  1. notify the corresponding accessor when developers mutate the model
  2. remove the self listeners, the accessor directly notify the model's listeners

I prefer the first approach.

Option should match the first hook instead of the last one.

Consider the following situation in swr:

function Post({revalidateOnFocus}) {
 	const {data} = useSWR('post1', fetcher, { revalidateOnFocus});
	return <div>{data.title}</div>
}

function App() {
	return <div>
		<Post revalidateOnFocus={true} />
		<Post revalidateOnFocus={true} />
	</div>
}

revalidateOnFocus will be enabled because the first useSWR receive true. I think this is more friendly for the user.

SSR support

My current idea is to hydrate based on different accessors. The API might look like this:

const postAdapter = createPaginationAdapter({});
const postModel = createModel(initialModel);
const getPostById = postModel.defineAccessor('normal', {
    // configuration
});

function Page({ post }) {
    useHydrate(getPostById(post.id), () => {
        postModel.mutate(model => {
            postAdapter.upsertOne(model, post)
        })
    })
}

The useHydrate function will record the provided accessor and execute the function if the current accessor hasn't been recorded yet. When the accessor changes, it removes the previously recorded one and records the new one. In the example above, this ensures that when post changes, we can update the model in real-time.

However, this approach has a drawback. Developers need to place useHydrate at the top level of the component tree to manage accessors centrally and avoid duplicate accessors being passed. Additionally, attention needs to be paid to which models need to be hydrated on which pages to prevent data from being missed.

Conditional fetching

In swr, if the key is null, then the request will not be emitted. We should also implement this.

lazy accessor should only subscribe the data it holds

Currently the accessor will notify the whole model's listener. This may cause performance issue if the lazy model define many accessors. Since the accessors defined by the lazy model don't have dependencies, we don't need to notify the whole model after the fetching is done.

accessors should has `getIsStale` and `setIsStale` methods

setIsStale allow developers to determine whether the data is stale or not. getIsStale will be used in useAccessor in order to check whether the accessor should invoke revalidate.

The checkHasStale function from the options should be renamed to another one since it is not related to whether the data is stale or not.

provide a generic pagination adapter

In most case, developers don't need to specify transform or getId when creating pagination adapter. If we provide a generic pagination adapter, it may reduce the code that developers need to write.

should record the page number fetched by the infinite accessor

Since we would delete the idle accessor, if an infinite accessor is deleted, the pages number it has fetched will be delete also. If developers call accessor.revalidate(), the accessor would only fetch the first page.

We need to find a way to record the pages number.

add `useMutator`

Like React query's useMutation. This feature is not the most urgent and will likely be implemented after the accessor API stabilizes.

provide model's state in `fetchData`

It may be useful if some fetching need to read the model's state. Instead of requiring developers to call model.getState(), we can provide it directly in fetchData.

add `createLazyModel`

The model created by this function doesn't require developers to specify how to sync the state. It automatically handles it so that developers can use this package just like they use React Query.

A better package name

To be honest, I'm not very satisfied with the current name, but I can't think of a better one either. If anyone has better suggestions, please feel free to provide them to me.

More documents

  • List the difference of this project with Redux and SWR
    • Also, write the advantage and disadvantage of these two
  • Complete the api documentation
  • Write more details about the usage of this project

idle accessor should be deleted

To prevent too many unused accessor from being stored in memory, we need to delete the accessor if it is idle. An idle accessor means that it is not mounted by any hook and it is not used after a certain interval.

enhance pagination adapter

Since the core of RSM is the adapter, I should add more function to it.

getByXXX

This type of functions should throw error when the corresponding data is not existed.

tryGetByXXX

This type of functions we try to get the corresponding data from the model. If the data is not existed, then it would return undefined

More function about mutating pagination

  • prependPagination
  • appendPagination
  • replacePagination
  • removeFromPagination
  • and so on...

add `useModel`

Developer may need to watch a model if some value changes but they don't want to trigger a revalidation. Add useModel for this use case.

Resolve race condition

There are two case of the race condition.

  • The response returned from request is after the user mutating the model.
  • Suppose useInfiniteFetch fetch a post list with id from 1 to 10, and useFetch fetch the post with id 1 later. But the response from useFetch may be in front of the response from useInfiniteFetch. In this case, we will update the model with expired response.

Revalidate on focus would block fetch next

If we re-focus on the window by clicking a button which would fetch next page, then it will trigger the revalidation first instead of triggering fetchNextPage. This should be consider as a bug.

Add error retry interval

The error retry would be invoked immediately if the fetching failed. We should add an interval instead of invoking refetching immediately.

add `meta` for manual revalidation

Developers may need to specify some variables for how to update the state. For example, a refresh flag means that the whole pagination need to be refresh. However, refresh is not useful for api fetching. It is useful if we provide a meta to achieve this goal.

add eslint rule

Which function need a rule:

  • useHydrate: should use the serverStateKey when using model.mutate
  • useAccessor: the props or state in getSnapshot function should also appear in the accessor creator
  • useModel: the getSnapshot function should be memoized by useCallback

support `staleTime`

Like React Query, developers can specify how long the accessor will become stale.

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.