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

Rework createResource #28

Open
Olian04 opened this issue Feb 15, 2023 · 1 comment
Open

Rework createResource #28

Olian04 opened this issue Feb 15, 2023 · 1 comment
Assignees
Labels
enhancement New feature or request

Comments

@Olian04
Copy link
Owner

Olian04 commented Feb 15, 2023

Rework will include 3 major changes:

  1. ResourceStatus
const R1 = createResource({
  get: () => fetch(...),
});

R1.get(); // { status: ResourceStatus.PENDING }
await R1.get(); // { status: ResourceStatus.SUCCESS, value: Response }


const R2 = createResource({
  get: () => fetch(...),
});

R2.get(); // { status: ResourceStatus.PENDING }
await R2.get(); // { status: ResourceStatus.FAILED, error: Error }
  1. Requests re-use and requests abortion (previously Add requests re-use and requests abortion to resources #21)
const URL = createAtom({
  default: '/',
});

const A = createResource({
  get: signal => fetch(URL.get(), { signal }), 
});

A.get(); // promise 1
await A.get(); // promise 1

URL.set('/foo');
A.get(); // promise 2
URL.set('/bar');
A.get(); // promise 3 & aborted promise 2
  1. Response memoization (previously Add memoization to resources #3)
const A = createAtom({
  default: 42,
})

const C = createResource({
  get: () => fetch(`/${A.get()}`)
);

await C.get(); // network request
await C.get(); // cached

A.set(3);
await C.get(); // network request
await C.get(); // cached

A.set(42);
await C.get(); // cached

A.invalidate();
await C.get(); // network request
await C.get(); // cached
@Olian04 Olian04 self-assigned this Feb 15, 2023
@Olian04 Olian04 added the enhancement New feature or request label Feb 15, 2023
@Olian04
Copy link
Owner Author

Olian04 commented Feb 15, 2023

WIP: ResourceStatus
PromiseLike Resource

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

1 participant