Skip to content

Commit

Permalink
feat: add useResourceController hook (#19)
Browse files Browse the repository at this point in the history
  • Loading branch information
otakustay committed May 6, 2022
1 parent 6bcf832 commit b409a1e
Show file tree
Hide file tree
Showing 5 changed files with 1,403 additions and 1,363 deletions.
48 changes: 27 additions & 21 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,50 +30,56 @@
},
"dependencies": {
"fast-json-stable-stringify": "^2.1.0",
"use-sync-external-store": "^1.0.0"
"use-sync-external-store": "^1.1.0"
},
"devDependencies": {
"@emotion/css": "^11.7.1",
"@emotion/react": "^11.8.2",
"@emotion/css": "^11.9.0",
"@emotion/react": "^11.9.0",
"@emotion/styled": "^11.8.1",
"@faker-js/faker": "^6.1.1",
"@faker-js/faker": "^6.3.1",
"@reskript/cli": "5.2.1",
"@reskript/cli-build": "5.2.1",
"@reskript/cli-dev": "5.2.1",
"@reskript/cli-lint": "5.2.1",
"@reskript/config-lint": "5.2.1",
"@reskript/settings": "5.2.1",
"@testing-library/react-hooks": "^7.0.2",
"@types/jest": "^27.4.1",
"@types/react": "^17.0.43",
"@types/react-dom": "^17.0.14",
"@testing-library/react-hooks": "^8.0.0",
"@types/jest": "^27.5.0",
"@types/react": "^17.0.44",
"@types/react-dom": "^17.0.16",
"@types/react-router-dom": "^5.3.3",
"@types/react-test-renderer": "^18.0.0",
"@types/use-sync-external-store": "^0.0.3",
"@vitejs/plugin-react": "^1.3.0",
"antd": "^4.19.3",
"c8": "^7.11.0",
"core-js": "^3.21.1",
"@vitejs/plugin-react": "^1.3.2",
"antd": "^4.20.2",
"c8": "^7.11.2",
"core-js": "^3.22.4",
"echarts": "^5.3.2",
"eslint": "^8.12.0",
"eslint": "^8.14.0",
"history": "^5.3.0",
"husky": "^7.0.4",
"jsdom": "^19.0.0",
"prism-themes": "^1.9.0",
"react": "^18.0.0",
"react-dom": "^18.0.0",
"react": "^18.1.0",
"react-dom": "^18.1.0",
"react-icons": "^4.3.1",
"react-loading": "^2.0.3",
"react-refractor": "^2.1.6",
"react-refractor": "^2.1.7",
"react-router-dom": "^6.3.0",
"refractor": "^4.5.0",
"react-test-renderer": "^18.1.0",
"refractor": "^4.6.0",
"standard-version": "^9.3.2",
"stylelint": "^14.6.1",
"stylelint": "^14.8.2",
"typescript": "next",
"vite": "^2.9.1",
"vitest": "^0.8.1"
"vite": "^2.9.8",
"vitest": "^0.10.4"
},
"peerDependencies": {
"react": ">= 17"
},
"packageManager": "[email protected]"
"packageManager": "[email protected]",
"resolutions": {
"@types/react": "17.x",
"@types/react-dom": "17.x"
}
}
6 changes: 3 additions & 3 deletions src/__tests__/controller.test.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {test, expect} from 'vitest';
import {ReactNode} from 'react';
import {renderHook, act} from '@testing-library/react-hooks';
import {renderHook, act} from '@testing-library/react-hooks/dom';
import {createCacheProvider} from '../cache';

const {CacheProvider, useConstantResource} = createCacheProvider({contextDisplayName: 'CacheContext'});
Expand All @@ -20,11 +20,11 @@ function Provider({children}: ProviderProps) {
test('testable', async () => {
let value = 123;
const api = () => Promise.resolve(value++);
const {result, waitForNextUpdate} = renderHook(
const {result} = renderHook(
() => useConstantResource(api),
{wrapper: Provider}
);
await waitForNextUpdate();
await new Promise(r => setTimeout(r, 100));
await act(() => result.current[1].refresh());
expect(result.current[0]).toBe(124);
});
6 changes: 3 additions & 3 deletions src/__tests__/hook.test.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {test, expect} from 'vitest';
import {ReactNode} from 'react';
import {renderHook} from '@testing-library/react-hooks';
import {renderHook} from '@testing-library/react-hooks/dom';
import {createCacheProvider} from '../cache';

const {CacheProvider, useConstantResource} = createCacheProvider({contextDisplayName: 'CacheContext'});
Expand All @@ -20,10 +20,10 @@ function Provider({children}: ProviderProps) {
const api = () => Promise.resolve(123);

test('testable', async () => {
const {result, waitForNextUpdate} = renderHook(
const {result} = renderHook(
() => useConstantResource(api),
{wrapper: Provider}
);
await waitForNextUpdate();
await new Promise(r => setTimeout(r, 100));
expect(result.current[0]).toBe(123);
});
10 changes: 10 additions & 0 deletions src/cache.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,14 @@ export const createCacheProvider = ({contextDisplayName = 'BoundaryCacheContext'
return useResourceInternal(api);
}

function useResourceController<I, O>(api: Async<I, O>, params: I) {
return useResourceInternal(api, params)[1];
}

function useConstantResourceController<O>(api: ConstantAsync<O>) {
return useResourceInternal(api)[1];
}

function usePreloadResource<I, O>(api: Async<I, O>, params: I): void {
const {cache} = useContext(Context);
const key = stringifyKey(params);
Expand Down Expand Up @@ -200,6 +208,8 @@ export const createCacheProvider = ({contextDisplayName = 'BoundaryCacheContext'
useRefreshCache,
useResource,
useConstantResource,
useResourceController,
useConstantResourceController,
usePreloadResource,
usePreloadConstantResource,
usePreloadResourceCallback,
Expand Down
Loading

0 comments on commit b409a1e

Please sign in to comment.