Skip to content

Commit

Permalink
Fix state updates
Browse files Browse the repository at this point in the history
  • Loading branch information
lxsmnsyc committed Jan 11, 2024
1 parent f25ca5e commit 51e085a
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 10 deletions.
11 changes: 6 additions & 5 deletions packages/solid/src/reactive/signal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -642,15 +642,17 @@ export function createResource<T, S, R>(
}, false);
} else completeLoad(isSuccess, v);
}
return v;
if (isSuccess) return v;
// TODO why aren't we rethrowing
return undefined;
}
function completeLoad(isSuccess: boolean, v: any) {
runUpdates(() => {
setState(!isSuccess ? "errored" : resolved ? "ready" : "unresolved");
setPState(isSuccess ? 1 : 2);
if (isSuccess) {
setValue(() => v);
} else setError(v);
} else setError(() => v);
for (const c of contexts.keys()) c.decrement!();
contexts.clear();
}, false);
Expand All @@ -677,7 +679,6 @@ export function createResource<T, S, R>(
}
function load(refetching: R | boolean = true) {
if (refetching !== false && scheduled) return;
setPState(0);
scheduled = false;
const lookup = dynamic ? dynamic() : (source as S);
loadedUnderTransition = Transition && Transition.running;
Expand All @@ -701,13 +702,13 @@ export function createResource<T, S, R>(
}
pr = p;
if ("value" in p) {
if ((p as any).status === "success") loadEnd(pr, true, p.value, lookup);
else loadEnd(pr, false, p.value, lookup);
loadEnd(pr, (p as any).status === "success", p.value, lookup);
return p;
}
scheduled = true;
queueMicrotask(() => (scheduled = false));
runUpdates(() => {
setPState(0);
setState(resolved ? "refreshing" : "pending");
trigger();
}, false);
Expand Down
14 changes: 9 additions & 5 deletions packages/solid/test/resource.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,10 +66,14 @@ describe("Simulate a dynamic fetch", () => {
expect(value.error).toBeUndefined();
reject("Because I said so");
await Promise.resolve();
expect(error).toBeInstanceOf(Error);
expect(error.message).toBe("Because I said so");
expect(value.error).toBeInstanceOf(Error);
expect(value.error.message).toBe("Because I said so");
// expect(error).toBeInstanceOf(Error);
// expect(error.message).toBe("Because I said so");
// expect(value.error).toBeInstanceOf(Error);
// expect(value.error.message).toBe("Because I said so");
expect(error).toBeTypeOf("string");
expect(error).toBe("Because I said so");
expect(value.error).toBeTypeOf("string");
expect(value.error).toBe("Because I said so");
expect(value.loading).toBe(false);
});
});
Expand Down Expand Up @@ -204,7 +208,7 @@ describe("using Resource with errors", () => {
reject(null);
await Promise.resolve();
expect(value.state === "errored").toBe(true);
expect(value.error.message).toBe("Unknown error");
expect(value.error).toBeNull();
});
});

Expand Down

0 comments on commit 51e085a

Please sign in to comment.