Add GHA for Dead Code Analysis (#9052)

This commit is contained in:
Šimon Brandner 2022-08-04 10:00:49 +02:00 committed by GitHub
parent ed8ccb5d80
commit a866005bea
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 18 additions and 52 deletions

View file

@ -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

View file

@ -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<
@ -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;
}