-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Introduced
Controller
, useController()
and mergeQueries()
- Loading branch information
Showing
10 changed files
with
176 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
import { renderHook } from '@testing-library/react-hooks'; | ||
import { Controller } from 'rx-effects'; | ||
import { useController } from './useController'; | ||
|
||
describe('useController()', () => { | ||
it('should create a controller by the factory and destroy it on unmount', () => { | ||
const action = jest.fn(); | ||
const destroy = jest.fn(); | ||
|
||
function createController(): Controller<{ | ||
value: number; | ||
action: () => void; | ||
}> { | ||
return { value: 1, action, destroy }; | ||
} | ||
|
||
const { result, unmount } = renderHook(() => | ||
useController(createController), | ||
); | ||
|
||
expect(result.current.value).toBe(1); | ||
|
||
result.current.action(); | ||
expect(action).toBeCalledTimes(1); | ||
|
||
unmount(); | ||
expect(destroy).toBeCalledTimes(1); | ||
}); | ||
|
||
it('should recreate the controller if a dependency is changed', () => { | ||
const destroy = jest.fn(); | ||
|
||
const createController = (value: number) => ({ value, destroy }); | ||
|
||
const { result, rerender, unmount } = renderHook( | ||
({ value }) => useController(() => createController(value), [value]), | ||
{ initialProps: { value: 1 } }, | ||
); | ||
|
||
const controller1 = result.current; | ||
expect(controller1.value).toBe(1); | ||
|
||
rerender({ value: 1 }); | ||
const controller2 = result.current; | ||
expect(controller2).toBe(controller1); | ||
expect(controller2.value).toBe(1); | ||
|
||
rerender({ value: 2 }); | ||
const controller3 = result.current; | ||
expect(controller3).not.toBe(controller2); | ||
expect(controller3.value).toBe(2); | ||
|
||
unmount(); | ||
expect(destroy).toBeCalledTimes(2); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
/* eslint-disable @typescript-eslint/no-explicit-any */ | ||
/* eslint-disable react-hooks/exhaustive-deps */ | ||
|
||
import { useEffect, useMemo } from 'react'; | ||
import { Controller } from 'rx-effects'; | ||
|
||
export function useController<T extends Controller<Record<string, any>>>( | ||
factory: () => T, | ||
dependencies?: unknown[], | ||
): T { | ||
const controller = useMemo(factory, dependencies); | ||
useEffect(() => controller.destroy, [controller]); | ||
|
||
return controller; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
/* eslint-disable @typescript-eslint/no-explicit-any */ | ||
/* eslint-disable @typescript-eslint/ban-types */ | ||
|
||
/** | ||
* Business logic controller | ||
* | ||
* @example | ||
* ```ts | ||
* type LoggerController = Controller<{ | ||
* log: (message: string) => void; | ||
* }>; | ||
* ``` | ||
*/ | ||
export type Controller<ControllerProps extends {} = {}> = Readonly< | ||
{ | ||
/** Dispose the controller and clean its resources */ | ||
destroy: () => void; | ||
} & ControllerProps | ||
>; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters