Skip to content

Commit 9fa4ef9

Browse files
committed
refactor resource handling: replace read.loading with _loading to avoid variable conflicts and restructure resource handling logic in createResource
1 parent 0af2c2b commit 9fa4ef9

File tree

2 files changed

+31
-12
lines changed

2 files changed

+31
-12
lines changed

.changeset/moody-clowns-vanish.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"solid-js": patch
3+
---
4+
5+
replace `read.loading` with `_loading` to avoid variable conflicts and restructure resource handling logic in `createResource`

packages/solid/src/server/rendering.ts

Lines changed: 26 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -450,34 +450,48 @@ export function createResource<T, S>(
450450
return resource.ref;
451451
}
452452
}
453-
const read = () => {
453+
const prepareResource = () => {
454454
if (error) throw error;
455455
const resolved =
456456
options.ssrLoadFrom !== "initial" &&
457457
sharedConfig.context!.async &&
458458
"data" in sharedConfig.context!.resources[id];
459459
if (!resolved && resourceContext) resourceContext.push(id);
460-
if (!resolved && read.loading) {
460+
if (!resolved && _loading) {
461461
const ctx = useContext(SuspenseContext);
462462
if (ctx) {
463-
ctx.resources.set(id, read);
463+
ctx.resources.set(id, { loading: _loading, error: read.error });
464464
contexts.add(ctx);
465465
}
466466
}
467-
return resolved ? sharedConfig.context!.resources[id].data : value;
467+
return resolved;
468+
};
469+
const read = () => {
470+
return prepareResource() ? sharedConfig.context!.resources[id].data : value;
468471
};
469-
read.loading = false;
472+
const loading = () => {
473+
prepareResource();
474+
return _loading;
475+
};
476+
let _loading = false;
470477
read.error = undefined as any;
471478
read.state = "initialValue" in options ? "ready" : "unresolved";
472-
Object.defineProperty(read, "latest", {
473-
get() {
474-
return read();
479+
Object.defineProperties(read, {
480+
latest: {
481+
get() {
482+
return read();
483+
}
484+
},
485+
loading: {
486+
get() {
487+
return loading();
488+
}
475489
}
476490
});
477491
function load() {
478492
const ctx = sharedConfig.context!;
479493
if (!ctx.async)
480-
return (read.loading = !!(typeof source === "function" ? (source as () => S)() : source));
494+
return (_loading = !!(typeof source === "function" ? (source as () => S)() : source));
481495
if (ctx.resources && id in ctx.resources && "data" in ctx.resources[id]) {
482496
value = ctx.resources[id].data;
483497
return;
@@ -495,19 +509,19 @@ export function createResource<T, S>(
495509
p = (fetcher as ResourceFetcher<S, T>)(lookup, { value });
496510
}
497511
if (p != undefined && typeof p === "object" && "then" in p) {
498-
read.loading = true;
512+
_loading = true;
499513
read.state = "pending";
500514
p = p
501515
.then(res => {
502-
read.loading = false;
516+
_loading = false;
503517
read.state = "ready";
504518
ctx.resources[id].data = res;
505519
p = null;
506520
notifySuspense(contexts);
507521
return res;
508522
})
509523
.catch(err => {
510-
read.loading = false;
524+
_loading = false;
511525
read.state = "errored";
512526
read.error = error = castError(err);
513527
p = null;

0 commit comments

Comments
 (0)