Skip to content

Commit

Permalink
Try to use only hook useResponsiveMap
Browse files Browse the repository at this point in the history
  • Loading branch information
azro352 committed Dec 2, 2022
1 parent 2d9ca25 commit 515c028
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 8 deletions.
4 changes: 2 additions & 2 deletions components/_util/__tests__/responsiveObserve.test.ts
@@ -1,8 +1,8 @@
import ResponsiveObserve, { responsiveMap } from '../responsiveObserve';
import ResponsiveObserve, { useResponsiveMap } from '../responsiveObserve';

describe('Test ResponsiveObserve', () => {
it('test ResponsiveObserve subscribe and unsubscribe', () => {
const { xs } = responsiveMap;
const { xs } = useResponsiveMap();
const subscribeFunc = jest.fn();
const token = ResponsiveObserve.subscribe(subscribeFunc);
expect(ResponsiveObserve.matchHandlers[xs].mql.matches).toBeTruthy();
Expand Down
8 changes: 2 additions & 6 deletions components/_util/responsiveObserve.ts
Expand Up @@ -20,8 +20,6 @@ const useResponsiveMap = (): BreakpointMap => {
} as BreakpointMap;
};

export const responsiveMap: BreakpointMap = useResponsiveMap();

type SubscribeFunc = (screens: ScreenMap) => void;
const subscribers = new Map<Number, SubscribeFunc>();
let subUid = -1;
Expand Down Expand Up @@ -51,16 +49,14 @@ const responsiveObserve = {
if (!subscribers.size) this.unregister();
},
unregister() {
Object.keys(responsiveMap).forEach((screen: Breakpoint) => {
const matchMediaQuery = responsiveMap[screen];
Object.keys(useResponsiveMap()).forEach((screen: Breakpoint, matchMediaQuery: string) => {
const handler = this.matchHandlers[matchMediaQuery];
handler?.mql.removeListener(handler?.listener);
});
subscribers.clear();
},
register() {
Object.keys(responsiveMap).forEach((screen: Breakpoint) => {
const matchMediaQuery = responsiveMap[screen];
Object.keys(useResponsiveMap()).forEach((screen: Breakpoint, matchMediaQuery: string) => {
const listener = ({ matches }: { matches: boolean }) => {
this.dispatch({
...screens,
Expand Down

0 comments on commit 515c028

Please sign in to comment.