Skip to content

Commit

Permalink
3.1.1 (#381)
Browse files Browse the repository at this point in the history
  • Loading branch information
nmathew98 committed May 5, 2024
1 parent 84e1407 commit 8c66f33
Show file tree
Hide file tree
Showing 38 changed files with 591 additions and 301 deletions.
16 changes: 15 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,20 @@ misc:

## 🤝🏽 Contributions

- Contributions are welcome, just make a pull request
Contributions are welcome, just make a pull request.

Most of the core logic is within `shared`, have avoided creating an abstract core class which requires a `rerender` specific to each framework because the differences between each framework make this not worth the while I think because then effort shifts towards reconciliation, for example:

- with React we trigger a rerender by updating a counter
- with Solid (and other signal based frameworks) we trigger a rerender by updating the `qwery` signal

to extract out the core into a general purpose class would be introducing `data`, `version` and `dispatch` signals and then updating each of these individually.

Another one is that supporting both suspense and non suspense:

- in React we throw a Promise before proceeding
- in Solid, we `createResource`
- in Svelte, there is no suspense and we just return the async
- in Vue, we just make the component async

**_"Man's duality, life and its formalities"_**
2 changes: 1 addition & 1 deletion lerna.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"$schema": "node_modules/lerna/schemas/lerna-schema.json",
"version": "3.1.0",
"version": "3.1.1",
"packages": [
"packages/*"
],
Expand Down
2 changes: 1 addition & 1 deletion packages/example-react/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@b.s/qwery-example-react",
"private": true,
"version": "3.1.0",
"version": "3.1.1",
"type": "module",
"scripts": {
"dev": "vite",
Expand Down
2 changes: 1 addition & 1 deletion packages/example-solid/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "example-solid",
"private": true,
"version": "3.1.0",
"version": "3.1.1",
"type": "module",
"scripts": {
"dev": "vite",
Expand Down
2 changes: 1 addition & 1 deletion packages/example-svelte/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@b.s/qwery-example-svelte",
"private": true,
"version": "3.1.0",
"version": "3.1.1",
"type": "module",
"scripts": {
"dev": "vite",
Expand Down
2 changes: 1 addition & 1 deletion packages/example-vue/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@b.s/qwery-example-vue",
"private": true,
"version": "3.1.0",
"version": "3.1.1",
"type": "module",
"scripts": {
"dev": "vite",
Expand Down
2 changes: 1 addition & 1 deletion packages/gqwery/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@b.s/gqwery",
"version": "3.1.0",
"version": "3.1.1",
"type": "module",
"repository": {
"type": "git",
Expand Down
2 changes: 1 addition & 1 deletion packages/react/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@b.s/react-qwery",
"version": "3.1.0",
"version": "3.1.1",
"type": "module",
"repository": {
"type": "git",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,30 @@ describe("monitored fetch", () => {

await waitFor(() => {
expect(result.current.isFetching).toBeFalsy();
expect(result.current.isInitialFetch).toBeFalsy();
});

monitoredFirstFetch();
rerender();

await waitFor(() => {
expect(result.current.isFetching).toBeTruthy();
expect(result.current.isInitialFetch).toBeTruthy();
});

await vi.runAllTimersAsync();

await waitFor(() => {
expect(result.current.isFetching).toBeFalsy();
expect(result.current.isInitialFetch).toBeFalsy();
});

monitoredFirstFetch();
rerender();

await waitFor(() => {
expect(result.current.isFetching).toBeTruthy();
expect(result.current.isInitialFetch).toBeFalsy();
});
});
});
18 changes: 11 additions & 7 deletions packages/react/src/test/use-qwery/enable.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,11 @@ describe("enable", () => {
/>,
);

await waitFor(() => {
expect(() => screen.getByText("1")).toThrowError();
});
await expect(
waitFor(() => {
expect(screen.getByText("1")).toBeTruthy();
}),
).rejects.toThrowError();
});

it("enabled/disabled", async () => {
Expand Down Expand Up @@ -82,14 +84,16 @@ describe("enable", () => {
onChange={vitest.fn()}
initialValue={initialValue}
render={data => {
return data && 1;
return data && 2;
}}
enabled={false}
/>,
);

await waitFor(() => {
expect(() => screen.getByText("1")).toThrowError();
});
await expect(
waitFor(() => {
expect(screen.getByText("2")).toBeTruthy();
}),
).rejects.toThrowError();
});
});
15 changes: 13 additions & 2 deletions packages/react/src/use-monitored-fetch/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@ import { makeMonitor } from "@b.s/qwery-shared";

export const useMonitoredFetch = () => {
const [allFetchStatus, setAllFetchStatus] = React.useState<{
[key: string]: boolean;
[key: string]: {
isFetching: boolean;
count: number;
};
}>(Object.create(null));

const monitor = React.useCallback(
Expand All @@ -14,7 +17,15 @@ export const useMonitoredFetch = () => {
return {
isFetching:
Object.keys(allFetchStatus).length > 0
? Object.values(allFetchStatus).some(Boolean)
? Object.values(allFetchStatus).some(
({ isFetching }) => isFetching,
)
: false,
isInitialFetch:
Object.keys(allFetchStatus).length > 0
? Object.values(allFetchStatus).some(
({ isFetching, count }) => isFetching && count < 1,
)
: false,
monitor,
};
Expand Down
25 changes: 11 additions & 14 deletions packages/react/src/use-qwery/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ export const useQwery = <I extends InitialValue>({
enabled = true,
affects,
requestManager,
throwErrors = suspense,
}: UseQweryOptions<I>): UseQweryReturn<I> => {
const instanceId = React.useMemo(createId, []);

Expand Down Expand Up @@ -144,6 +145,7 @@ export const useQwery = <I extends InitialValue>({
abortController: abortControllerRef.current,
refetch: typeof initialValue === "function" ? initialValue : null,
affects,
throwErrors,
});

setQwery(_qwery);
Expand All @@ -158,6 +160,14 @@ export const useQwery = <I extends InitialValue>({
typeof createQwery
> | null>(null);

React.useEffect(() => {
const cancelInflightRequests = abortControllerRef.current.abort.bind(
abortControllerRef.current,
);

return cancelInflightRequests;
}, []);

React.useEffect(() => {
return () => {
SUSPENDED.delete(suspenseKey);
Expand All @@ -166,13 +176,7 @@ export const useQwery = <I extends InitialValue>({
}, [qwery, enabled, queryKey?.toString(), suspenseKey]);

React.useEffect(() => {
const cancelInflightRequests = abortControllerRef.current.abort.bind(
abortControllerRef.current,
);

create();

return cancelInflightRequests;
}, [
enabled,
queryKey?.toString(),
Expand Down Expand Up @@ -208,14 +212,7 @@ export const useQwery = <I extends InitialValue>({
return;
}

const observe = () => {
return subscribeQwery(qwery, {
rerender,
subscribe,
});
};

return observe();
return subscribeQwery(qwery, subscribe);
}, [qwery]);

React.useEffect(() => {
Expand Down
2 changes: 1 addition & 1 deletion packages/shared/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@b.s/qwery-shared",
"version": "3.1.0",
"version": "3.1.1",
"private": true,
"type": "module",
"devDependencies": {
Expand Down
5 changes: 4 additions & 1 deletion packages/shared/src/use-monitored-fetch/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,10 @@ export const makeMonitor =
onFetching: isFetching =>
update(allFetchStatus => ({
...allFetchStatus,
[id]: isFetching,
[id]: {
isFetching,
count: (allFetchStatus[id]?.count ?? 0) + 1 / 2,
},
})),
});
};
9 changes: 0 additions & 9 deletions packages/shared/src/use-qwery/create-broadcast-channel.ts

This file was deleted.

Loading

0 comments on commit 8c66f33

Please sign in to comment.