Skip to content

Commit

Permalink
fix: fix bugs for debounce and eventlistener
Browse files Browse the repository at this point in the history
  • Loading branch information
ZL-Asica committed Nov 21, 2024
1 parent 924f35b commit 86d270a
Show file tree
Hide file tree
Showing 6 changed files with 15 additions and 8 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ jobs:

# Step 8: Publish to JSR
- name: 🌟 Publish to JSR
run: npx jsr publish --allow-dirty
run: pnpm dlx jsr publish --allow-dirty

deploy-docs:
runs-on: ubuntu-latest
Expand Down
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# @zl-asica/react

## 0.3.4

### Patch Changes

- Minor bugs fixed for debounce and eventlistener

## 0.3.3

### Patch Changes
Expand Down
2 changes: 1 addition & 1 deletion jsr.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@zl-asica/react",
"version": "0.3.3",
"version": "0.3.4",
"license": "MIT",
"exports": "./src/index.ts",
"importMap": "./import_map.json",
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@zl-asica/react",
"version": "0.3.3",
"version": "0.3.4",
"description": "A library of reusable React hooks, components, and utilities built by ZL Asica.",
"keywords": [
"react",
Expand Down
5 changes: 2 additions & 3 deletions src/hooks/dom/useEventListener.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,6 @@ import { useDebouncedCallback } from '@/hooks/state';
* };
* ```
*/

export const useEventListener = <T extends Event>(
event: string,
handler: (event: T) => void,
Expand All @@ -68,10 +67,10 @@ export const useEventListener = <T extends Event>(
}, [handler, debounce]);

useEffect(() => {
if (!element || !debouncedCallbackReference.current) return;
if (!element) return;

const eventHandler = (event_: Event) =>
debouncedCallbackReference.current!(event_ as T); // Type cast to match generic event type
debouncedCallbackReference.current?.(event_ as T); // Safe access with optional chaining
element.addEventListener(event, eventHandler);

return () => {
Expand Down
6 changes: 4 additions & 2 deletions src/hooks/state/useDebouncedCallback.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,10 @@ export const useDebouncedCallback = <TArguments extends unknown[]>(
const callbackReference = useRef(callback);
const timeoutReference = useRef<NodeJS.Timeout | null>(null);

// Always update the ref to the latest callback
callbackReference.current = callback;
useEffect(() => {
// Always update the ref to the latest callback
callbackReference.current = callback;
}, [callback]);

const debouncedCallback = useCallback(
(...arguments_: TArguments) => {
Expand Down

0 comments on commit 86d270a

Please sign in to comment.