Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[feature] Add optimistic update on create #153

Merged
merged 1 commit into from
Oct 9, 2019
Merged

Conversation

ntucker
Copy link
Collaborator

@ntucker ntucker commented Oct 8, 2019

Fixes #96

Motivation

Provide both the mechanism for common case optimistic updates, as well as well designed extension points for unusual cases like #124

Solution

Update the API of the function returned from useFetcher to take in an array of updaters:

const createArticle = useFetcher(ArticleResource.createShape());

createArticle({ id: 1 }, {}, [
  [ArticleResource.listShape(), {}, (newArticle, articles) => [...articles, newArticle]]
])

Additionally, this supports the same case for GETs, in the case of pagination:

class PaginatedArticleResource extends Resource {
  readonly id: number | null = null;
  readonly title: string = '';
  readonly content: string = '';
  readonly author: number | null = null;
  readonly tags: string[] = [];

  pk() {
    return this.id;
  }

  static urlRoot = 'http://test.com/article/';

  static listShape<T extends typeof Resource>(this: T) {
    return {
      ...super.listShape(),
      schema: { results: [this.getEntitySchema()] },
    };
  }
}

function mergeArticles(
  newPage: { results: string[] },
  articles: { results?: string[] },
): { results: string[] } {
  return [...(articles.results || []), ...newPage.results];
}

function useNextPageFetcher() {
  const getNextPage = useFetcher(ArticleResource.listShape());
  return useCallback(() => {
    return getNextPage({}, { cursor: 2 }, [
      [ArticleResource.listShape(), {}, mergeArticles],
    ]);
  }, [getNextPage]);
}

Credit

@zacharyfmarion wrote much of this code, but was rebased together so this attribution was lost in this PR

…pdates

checkpoint

Start scaffolding

Correctly pass results into updater

Add snap test for hooks.tsx

Add reducer tests

network manager tests

Update resource with correct url

Add types to createUpdater

getEntityPaths basic

Remove createUpdater

improve times for useFetcher

start of integration test

Update type of useFetcher

Working integration test, call on RPC

Add applyUpdatersToResult function

Don't purge anything

types cleanup

Commit changes

Working reducer tests

Finished test coverage

Remove DS_Store

type fixes

Test network manager

Add tests to applyUpdatersToResults

more reducer tests

Remove jest changes

type useFetcher correctly

Type applyUpdatersToResults

Fix types in NetworkManager

type fix

Add purgeMeta to netowrkManager type

more type fixes

typings

fix it

fix applyUpdaters types
@ntucker ntucker merged commit b33b8c2 into master Oct 9, 2019
@ntucker ntucker deleted the new-optimistic branch October 9, 2019 22:24
@ntucker ntucker mentioned this pull request Oct 23, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Optimistic query update on create
1 participant