Skip to content

Commit 0223e3c

Browse files
committed
Refactor: Rename loading to _loading and enhance createResource logic for improved conflict prevention and resource management.
1 parent 0af2c2b commit 0223e3c

File tree

2 files changed

+40
-18
lines changed

2 files changed

+40
-18
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+
Refactor: Rename `loading` to `_loading` and enhance `createResource` logic for improved conflict prevention and resource management.

packages/solid/src/server/rendering.ts

Lines changed: 35 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -373,7 +373,7 @@ export interface Resource<T> {
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

@@ -445,39 +445,53 @@ export function createResource<T, S>(
445445
if (sharedConfig.context!.async && options.ssrLoadFrom !== "initial") {
446446
resource = sharedConfig.context!.resources[id] || (sharedConfig.context!.resources[id] = {});
447447
if (resource.ref) {
448-
if (!resource.data && !resource.ref[0].loading && !resource.ref[0].error)
448+
if (!resource.data && !resource.ref[0]._loading && !resource.ref[0].error)
449449
resource.ref[1].refetch();
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;
@@ -523,7 +537,10 @@ export function createResource<T, S>(
523537
return ctx.resources[id].data;
524538
}
525539
if (options.ssrLoadFrom !== "initial") load();
526-
const ref = [read, { refetch: load, mutate: (v: T) => (value = v) }] as ResourceReturn<T>;
540+
const ref = [
541+
read as unknown as Resource<T>,
542+
{ refetch: load, mutate: (v: T) => (value = v) }
543+
] as ResourceReturn<T>;
527544
if (p) resource.ref = ref;
528545
return ref;
529546
}
@@ -550,15 +567,15 @@ export function lazy<T extends Component<any>>(
550567
else load(id);
551568
if (p.resolved) return p.resolved(props);
552569
const ctx = useContext(SuspenseContext);
553-
const track = { loading: true, error: undefined };
570+
const track = { _loading: true, error: undefined };
554571
if (ctx) {
555572
ctx.resources.set(id, track);
556573
contexts.add(ctx);
557574
}
558575
if (sharedConfig.context!.async) {
559576
sharedConfig.context!.block(
560577
p.then(() => {
561-
track.loading = false;
578+
track._loading = false;
562579
notifySuspense(contexts);
563580
})
564581
);
@@ -571,7 +588,7 @@ export function lazy<T extends Component<any>>(
571588

572589
function suspenseComplete(c: SuspenseContextType) {
573590
for (const r of c.resources.values()) {
574-
if (r.loading) return false;
591+
if (r._loading) return false;
575592
}
576593
return true;
577594
}
@@ -642,7 +659,7 @@ export function Suspense(props: { fallback?: string; children: string }) {
642659
const value: SuspenseContextType =
643660
ctx.suspense[id] ||
644661
(ctx.suspense[id] = {
645-
resources: new Map<string, { loading: boolean; error: any }>(),
662+
resources: new Map<string, { _loading: boolean; error: any }>(),
646663
completed: () => {
647664
const res = runSuspense();
648665
if (suspenseComplete(value)) {

0 commit comments

Comments
 (0)