Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve resolving arguments in createResource #2353

Merged
merged 2 commits into from
Nov 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/twelve-comics-knock.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"solid-js": patch
---

Improve resolving arguments in createResource
11 changes: 6 additions & 5 deletions packages/solid/src/reactive/signal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -591,14 +591,15 @@ export function createResource<T, S, R>(
let source: ResourceSource<S>;
let fetcher: ResourceFetcher<S, T, R>;
let options: ResourceOptions<T, S>;
if ((arguments.length === 2 && typeof pFetcher === "object") || arguments.length === 1) {
source = true as ResourceSource<S>;
fetcher = pSource as ResourceFetcher<S, T, R>;
options = (pFetcher || {}) as ResourceOptions<T, S>;
} else {

if (typeof pFetcher === "function") {
source = pSource as ResourceSource<S>;
fetcher = pFetcher as ResourceFetcher<S, T, R>;
options = pOptions || ({} as ResourceOptions<T, S>);
} else {
source = true as ResourceSource<S>;
fetcher = pSource as ResourceFetcher<S, T, R>;
options = (pFetcher || {}) as ResourceOptions<T, S>;
}

let pr: Promise<T> | null = null,
Expand Down
13 changes: 5 additions & 8 deletions packages/solid/src/server/rendering.ts
Original file line number Diff line number Diff line change
Expand Up @@ -374,16 +374,13 @@ export function createResource<T, S>(
fetcher?: ResourceFetcher<S, T> | ResourceOptions<T> | ResourceOptions<undefined>,
options: ResourceOptions<T> | ResourceOptions<undefined> = {}
): ResourceReturn<T> | ResourceReturn<T | undefined> {
if (arguments.length === 2) {
if (typeof fetcher === "object") {
options = fetcher as ResourceOptions<T> | ResourceOptions<undefined>;
fetcher = source as ResourceFetcher<S, T>;
source = true as ResourceSource<S>;
}
} else if (arguments.length === 1) {
fetcher = source as ResourceFetcher<S, T>;

if (typeof fetcher !== "function") {
source = true as ResourceSource<S>;
fetcher = source as ResourceFetcher<S, T>;
options = (fetcher || {}) as ResourceOptions<T> | ResourceOptions<undefined>;
}

const contexts = new Set<SuspenseContextType>();
const id = sharedConfig.getNextContextId();
let resource: { ref?: any; data?: T } = {};
Expand Down
32 changes: 32 additions & 0 deletions packages/solid/test/resource.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -385,3 +385,35 @@ describe("using Resource with custom store", () => {
expect(street).toBe(2);
});
});

describe("createResource can be wrapped", () => {

const fns: [name: string, function: typeof createResource][] = [
["original createResource", createResource],
// @ts-ignore
["createResource(...args)", (...args) => createResource(...args)],
// @ts-ignore
["createResource(a, b, c)", (a, b, c) => createResource(a, b, c)],
]

for (const [name, fn] of fns) {

test(`only fetcher in ${name}`, () => {
const [[data], dispose] = createRoot(dispose => [fn(() => 123), dispose])
expect(data()).toBe(123)
dispose()
})

test(`fetcher and source in ${name}`, () => {
const [source, setSource] = createSignal(1)

const [[data], dispose] = createRoot(dispose => [fn(source, v => v), dispose])
expect(data()).toBe(1)

setSource(2)
expect(data()).toBe(2)

dispose()
})
}
})
Loading