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

feat: Add optimistic updates #246

Merged
merged 4 commits into from
Jan 19, 2020
Merged

feat: Add optimistic updates #246

merged 4 commits into from
Jan 19, 2020

Conversation

ntucker
Copy link
Collaborator

@ntucker ntucker commented Jan 18, 2020

Closes #191, closes #179.

Motivation

Make sites faster.

Solution

class ArticleResource extends Resource {
  static partialUpdateShape<T extends typeof Resource>(
    this: T,
  ): MutateShape<
    SchemaDetail<Readonly<AbstractInstanceType<T>>>,
    Readonly<object>,
    Partial<AbstractInstanceType<T>>
  > {
    return {
      ...super.partialUpdateShape(),
      options: {
        ...this.getFetchOptions(),
        optimisticUpdate: (params: any, body: any) => ({
          id: params.id,
          ...body,
        }),
      },
    };
  }
  static createShape<T extends typeof Resource>(this: T) {
    return {
      ...super.createShape(),
      options: {
        ...this.getFetchOptions(),
        optimisticUpdate: (
          params: Readonly<object>,
          body: Readonly<object | string> | void,
        ) => body,
      },
    };
  }
}

const appendUpdater = (
  newArticleID: string,
  articleIDs: string[] | undefined,
) => [...(articleIDs || []), newArticleID];

Partial updates

const update = useFetcher(ArticleResource.partialUpdateShape());

update({ id: '5' }, { isStaff: true });

Optimistic create with eager updates

This lets you eagerly insert into a list.

You'll need to make a fake id that won't collide so it can put it in the cache somewhere.

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

create({}, { id: 'made up temp id', content: 'fun times' }, [
  [ArticleResource.listShape(), {}, appendUpdater],
]);

Race conditions

Since optimistic updates are applied after all realized updates, all optimistic updates with a date equal to or before the currently realizing update should be removed. Note: this still potentially creates a problem when multiple endpoints interact on the same entity, but there is no clear way to realize that solution and this problem is far less likely. Mostly the expected issue is toggling a single control over quickly.

Update NetworkManager detection mechanism

Since we're now passing fetch actions through to the reducer to handle, we need a new mechanism to detect if NetworkManager isn't setup. This is a common error for those attempting to use redux.

We will add a flag to the meta named nm that is set to true when the action passes through NetworkManager.

@ntucker ntucker force-pushed the optimistic-updates branch 2 times, most recently from 1e72318 to f03b406 Compare January 19, 2020 04:04
@ntucker ntucker merged commit d448c55 into master Jan 19, 2020
@ntucker ntucker deleted the optimistic-updates branch January 19, 2020 04:34
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.

Ability to optimistically update detailShape Partial update optimism not working for singleton resource
1 participant