Comments (6)
@drcmda My pleasure! In that case I'll send a PR implementing 1.a
soon (2
is okay to be left as it is as I said)
@miguel-silva Your concern around key collisions is fair, but I think it can be solved easily userland, I opened an issue suggesting how
from suspend-react.
@miguel-silva key collisions are easy to avoid, ,[key1, key2, "pmndrs/suspend-react/functionName"])
or
const uid = new Symbol()
suspend(fn, [key1, key2, uid])
i wanted this library to be as simple as humanly possible. cache store already introduce things that aren't so straight forward.
from suspend-react.
added a small section in the docs https://github.com/pmndrs/suspend-react#making-cache-keys-unique
from suspend-react.
thanks for the input, super apreciated! im going with your instincts, they seem warranted. having this finally infer types is amazing (using use-asset for 1.5 years, but typing was a major pita). i renamed args to keys and documented it accordingly.
from suspend-react.
Similarly to what the OP mentioned in one of the approaches (1. b), I was also thinking that one of the better ways to improve type safety is to change the API in a way that enforces creating individual caches, each for a specific pair of Keys
and Promise result structures, instead of relying on a global one (which doesn't prevent Keys
collisions for different result structures)
@drcmda, I hope you don't mind, but to illustrate what that could look like I created an adapted version of suspend-react based on one of your demos.
Basically the adapted api there closes over the loader function and config from the args of the "suspending store" factory:
function createSuspendingStore<Keys extends any[], Value>(
fn: (...keys: Keys) => Promise<Value>, config?: Config
): {
suspend(keys: Keys): Value,
preload(keys: Keys): void,
peek(keys: Keys): Value,
clear(keys?: Keys): void
}
const postsStore = createSuspendingStore(fetchPost);
const post = postsStore.suspend([id]);
Alternatively the loader function and config could be moved back to the related methods, but consuming them involves a bit more boilerplate:
function createSuspendingStore<Keys extends any[], Value>(): {
suspend(fn: (...keys: Keys) => Promise<Value>, keys: Keys, config?: Config): Value,
preload(fn: (...keys: Keys) => Promise<Value>, keys: Keys, config?: Config): void,
peek(keys: Keys): Value,
clear(keys?: Keys): void
}
const postsStore = createSuspendingStore<[number], HNPost>();
const post = postsStore.suspend(fetchPost, [id]);
What do you think?
from suspend-react.
Blocked on microsoft/TypeScript#46724, waiting to see if it's a bug. If it is then will wait till it gets fixed, if it's not a bug then will see if we can do some workarounds. In worst case we can write declaration by hand instead of the making the compiler emit wrong ones.
from suspend-react.
Related Issues (14)
- Cancellation HOT 11
- Re-rendering on clear HOT 2
- Contribution Guide HOT 1
- Wrong file ending. HOT 1
- PeerDependencies complaint under React 16.14
- shallowEqualArrays falling short when using empty array as key
- How to clean ressources ? HOT 9
- Source of 0.0.10 HOT 1
- Assets are not cached correctly using array as useLoader input
- Discussion: Concerns about uncollectable references? HOT 2
- Suggestion: Document ways to avoid key collisions HOT 3
- Fallback to previous render HOT 1
- How would you use suspend-react when fetching through a hook? HOT 4
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from suspend-react.