Skip to content
This repository has been archived by the owner on Sep 11, 2024. It is now read-only.

Commit

Permalink
Add GHA for Dead Code Analysis (#9052)
Browse files Browse the repository at this point in the history
  • Loading branch information
SimonBrandner authored Aug 4, 2022
1 parent ed8ccb5 commit a866005
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 52 deletions.
18 changes: 18 additions & 0 deletions .github/workflows/static_analysis.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -74,3 +74,21 @@ jobs:

- name: Run Linter
run: "yarn run lint:style"

analyse_dead_code:
name: "Analyse Dead Code"
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2

- uses: actions/setup-node@v3
with:
cache: 'yarn'

- name: Install Deps
run: "scripts/ci/layered.sh"

- name: Dead Code Analysis
run: |
cd element-web
yarn run analyse:unused-exports
52 changes: 0 additions & 52 deletions src/hooks/useEventEmitter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,43 +68,6 @@ export function useEventEmitter(
);
}

/**
* Hook to wrap an EventTarget addEventListener and removeEventListener in hook
* lifecycle
*/
export function useEventTarget(
emitter: EventTarget | undefined,
eventName: string,
handler: Handler,
): void {
// Create a ref that stores handler
const savedHandler = useRef(handler);

// Update ref.current value if handler changes.
useEffect(() => {
savedHandler.current = handler;
}, [handler]);

useEffect(
() => {
// allow disabling this hook by passing a falsy emitter
if (!emitter) return;

// Create event listener that calls handler function stored in ref
const eventListener = (...args) => savedHandler.current(...args);

// Add event listener
emitter.addEventListener(eventName, eventListener);

// Remove event listener on cleanup
return () => {
emitter.removeEventListener(eventName, eventListener);
};
},
[eventName, emitter], // Re-run if eventName or emitter changes
);
}

type Mapper<T> = (...args: any[]) => T;

export function useTypedEventEmitterState<
Expand Down Expand Up @@ -133,18 +96,3 @@ export function useEventEmitterState<T>(
useEventEmitter(emitter, eventName, handler);
return value;
}

export function useEventTargetState<T>(
target: EventTarget | undefined,
eventName: string,
fn: Mapper<T>,
): T {
const [value, setValue] = useState<T>(fn());
const handler = useCallback((...args: any[]) => {
setValue(fn(...args));
}, [fn]);
// re-run when the emitter changes
useEffect(handler, [target]); // eslint-disable-line react-hooks/exhaustive-deps
useEventTarget(target, eventName, handler);
return value;
}

0 comments on commit a866005

Please sign in to comment.