Skip to content

Commit d3f5257

Browse files
committed
replace loading with _loading to avoid variable conflicts and restructure resource handling logic
1 parent 0af2c2b commit d3f5257

File tree

2 files changed

+36
-17
lines changed

2 files changed

+36
-17
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+
rename `loading` to `_loading`, refactor resource preparation logic and simplify promise handling

packages/solid/src/server/rendering.ts

Lines changed: 31 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -367,13 +367,13 @@ export function ErrorBoundary(props: {
367367
export interface Resource<T> {
368368
(): T | undefined;
369369
state: "unresolved" | "pending" | "ready" | "refreshing" | "errored";
370-
loading: boolean;
370+
_loading: boolean;
371371
error: any;
372372
latest: T | undefined;
373373
}
374374

375375
type SuspenseContextType = {
376-
resources: Map<string, { loading: boolean; error: any }>;
376+
resources: Map<string, { _loading: boolean; error: any }>;
377377
completed: () => void;
378378
};
379379

@@ -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 && read._loading) {
461461
const ctx = useContext(SuspenseContext);
462462
if (ctx) {
463463
ctx.resources.set(id, read);
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 read._loading;
475+
};
476+
read._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 (read._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+
read._loading = true;
499513
read.state = "pending";
500514
p = p
501515
.then(res => {
502-
read.loading = false;
516+
read._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+
read._loading = false;
511525
read.state = "errored";
512526
read.error = error = castError(err);
513527
p = null;
@@ -550,15 +564,15 @@ export function lazy<T extends Component<any>>(
550564
else load(id);
551565
if (p.resolved) return p.resolved(props);
552566
const ctx = useContext(SuspenseContext);
553-
const track = { loading: true, error: undefined };
567+
const track = { _loading: true, error: undefined };
554568
if (ctx) {
555569
ctx.resources.set(id, track);
556570
contexts.add(ctx);
557571
}
558572
if (sharedConfig.context!.async) {
559573
sharedConfig.context!.block(
560574
p.then(() => {
561-
track.loading = false;
575+
track._loading = false;
562576
notifySuspense(contexts);
563577
})
564578
);
@@ -571,7 +585,7 @@ export function lazy<T extends Component<any>>(
571585

572586
function suspenseComplete(c: SuspenseContextType) {
573587
for (const r of c.resources.values()) {
574-
if (r.loading) return false;
588+
if (r._loading) return false;
575589
}
576590
return true;
577591
}
@@ -642,7 +656,7 @@ export function Suspense(props: { fallback?: string; children: string }) {
642656
const value: SuspenseContextType =
643657
ctx.suspense[id] ||
644658
(ctx.suspense[id] = {
645-
resources: new Map<string, { loading: boolean; error: any }>(),
659+
resources: new Map<string, { _loading: boolean; error: any }>(),
646660
completed: () => {
647661
const res = runSuspense();
648662
if (suspenseComplete(value)) {

0 commit comments

Comments
 (0)