Skip to content

Commit 2b343c2

Browse files
committed
main 🧊 rework test add unmount, add use broadcast channel
1 parent d03e19e commit 2b343c2

File tree

54 files changed

+962
-113
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

54 files changed

+962
-113
lines changed

‎packages/core/src/bundle/hooks/index.js‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ export * from './useBattery/useBattery';
55
export * from './useBluetooth/useBluetooth';
66
export * from './useBoolean/useBoolean';
77
export * from './useBreakpoints/useBreakpoints';
8+
export * from './useBroadcastChannel/useBroadcastChannel';
89
export * from './useBrowserLanguage/useBrowserLanguage';
910
export * from './useClickOutside/useClickOutside';
1011
export * from './useClipboard/useClipboard';

‎packages/core/src/bundle/hooks/useBattery/useBattery.js‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,14 @@ export const useBattery = () => {
1717
'getBattery' in navigator &&
1818
typeof navigator.getBattery === 'function';
1919
const [value, setValue] = useState({
20-
loading: true,
20+
loading: supported,
2121
level: 0,
2222
charging: false,
2323
chargingTime: 0,
2424
dischargingTime: 0
2525
});
2626
useEffect(() => {
27-
if (!supported) return setValue({ ...value, loading: false });
27+
if (!supported) return;
2828
let battery;
2929
const onChange = () =>
3030
setValue({
@@ -44,6 +44,7 @@ export const useBattery = () => {
4444
});
4545
return () => {
4646
if (!battery) return;
47+
console.log('unmount', battery);
4748
battery.removeEventListener('levelchange', onChange);
4849
battery.removeEventListener('chargingchange', onChange);
4950
battery.removeEventListener('chargingtimechange', onChange);
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
import { useEffect, useRef, useState } from 'react';
2+
/**
3+
* @name useBroadcastChannel
4+
* @description Hook that provides cross-tab/window communication
5+
* @category Browser
6+
*
7+
* @browserapi BroadcastChannel https://developer.mozilla.org/en-US/docs/Web/API/BroadcastChannel
8+
*
9+
* @param {string} name The name of the channel
10+
* @param {Function} callback A callback function that will be called when a message is received
11+
* @returns {UseBroadcastChannelReturn} An object containing the channel state and controls
12+
*
13+
* @example
14+
* const { supported, data, post, error } = useBroadcastChannel('channel');
15+
*/
16+
export const useBroadcastChannel = (name, callback) => {
17+
const supported = typeof window !== 'undefined' && 'BroadcastChannel' in window;
18+
const [closed, setClosed] = useState(false);
19+
const [data, setData] = useState();
20+
const [error, setError] = useState();
21+
const channelRef = useRef(undefined);
22+
useEffect(() => {
23+
if (!supported) return;
24+
channelRef.current = new BroadcastChannel(name);
25+
const onMessage = (event) => {
26+
setData(event.data);
27+
callback?.(event.data);
28+
};
29+
const onMessageError = (event) => setError(event);
30+
const onClose = () => setClosed(true);
31+
channelRef.current.addEventListener('message', onMessage);
32+
channelRef.current.addEventListener('messageerror', onMessageError);
33+
channelRef.current.addEventListener('close', onClose);
34+
return () => {
35+
if (channelRef.current) {
36+
channelRef.current.removeEventListener('message', onMessage);
37+
channelRef.current.removeEventListener('messageerror', onMessageError);
38+
channelRef.current.removeEventListener('close', onClose);
39+
channelRef.current.close();
40+
}
41+
};
42+
}, [name]);
43+
const post = (data) => {
44+
console.log('post', data, channelRef.current);
45+
if (!channelRef.current) return;
46+
channelRef.current.postMessage(data);
47+
};
48+
const close = () => {
49+
if (!channelRef.current) return;
50+
channelRef.current.close();
51+
setClosed(true);
52+
};
53+
return {
54+
supported,
55+
channel: channelRef.current,
56+
data,
57+
post,
58+
close,
59+
error,
60+
closed
61+
};
62+
};

‎packages/core/src/bundle/hooks/useClickOutside/useClickOutside.js‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ export const useClickOutside = (...params) => {
3131
const internalCallbackRef = useRef(callback);
3232
internalCallbackRef.current = callback;
3333
useEffect(() => {
34+
console.log('target', target);
3435
if (!target && !internalRef.state) return;
3536
const onClick = (event) => {
3637
const element = target ? getElement(target) : internalRef.current;

‎packages/core/src/bundle/hooks/useDisplayMedia/useDisplayMedia.js‎

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -38,36 +38,34 @@ export const useDisplayMedia = (...params) => {
3838
const options = params[1] ? params[1] : params[0];
3939
const immediately = options?.immediately ?? false;
4040
const [sharing, setSharing] = useState(false);
41+
const elementRef = useRef(null);
4142
const streamRef = useRef(null);
4243
const internalRef = useRefState();
4344
const stop = () => {
44-
if (!streamRef.current || !supported) return;
45-
const element = target ? getElement(target) : internalRef.current;
46-
if (!element) return;
45+
if (!streamRef.current || !supported || !elementRef.current) return;
4746
setSharing(false);
48-
element.srcObject = null;
47+
elementRef.current.srcObject = null;
4948
streamRef.current.getTracks().forEach((track) => track.stop());
5049
streamRef.current = null;
5150
};
5251
const start = async () => {
53-
if (!supported) return;
54-
const element = target ? getElement(target) : internalRef.current;
55-
if (!element) return;
52+
if (!supported || !elementRef.current) return;
5653
const displayMedia = await navigator.mediaDevices.getDisplayMedia({
5754
video: options?.video,
5855
audio: options?.audio
5956
});
6057
setSharing(true);
6158
streamRef.current = displayMedia;
62-
element.srcObject = displayMedia;
59+
elementRef.current.srcObject = displayMedia;
6360
displayMedia.getTracks().forEach((track) => (track.onended = stop));
6461
return displayMedia;
6562
};
6663
useEffect(() => {
67-
if (!supported || !immediately) return;
68-
if (!target && !internalRef.state) return;
64+
if (!supported || (!target && !internalRef.state)) return;
6965
const element = target ? getElement(target) : internalRef.current;
7066
if (!element) return;
67+
elementRef.current = element;
68+
if (!immediately) return;
7169
start();
7270
return () => {
7371
stop();

‎packages/core/src/bundle/hooks/useMemory/useMemory.js‎

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
import { useState } from 'react';
2-
import { useInterval } from '../useInterval/useInterval';
1+
import { useEffect, useState } from 'react';
32
/**
43
* @name useMemory
54
* @description - Hook that gives you current memory usage
@@ -21,8 +20,10 @@ export const useMemory = () => {
2120
usedJSHeapSize: 0
2221
}
2322
);
24-
useInterval(() => setValue(performance.memory), 1000, {
25-
immediately: supported
26-
});
23+
useEffect(() => {
24+
if (!supported) return;
25+
const intervalId = setInterval(() => setValue(performance.memory), 1000);
26+
return () => clearInterval(intervalId);
27+
}, []);
2728
return { supported, value };
2829
};

‎packages/core/src/bundle/hooks/useRerender/useRerender.js‎

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,4 @@ import { useReducer } from 'react';
99
* @example
1010
* const rerender = useRerender();
1111
*/
12-
export const useRerender = () => {
13-
const rerender = useReducer(() => ({}), {})[1];
14-
return rerender;
15-
};
12+
export const useRerender = () => useReducer(() => ({}), {})[1];

‎packages/core/src/bundle/hooks/useTextDirection/useTextDirection.js‎

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
1-
import { useState } from 'react';
1+
import { useEffect, useState } from 'react';
22
import { getElement, isTarget } from '@/utils/helpers';
3-
import { useIsomorphicLayoutEffect } from '../useIsomorphicLayoutEffect/useIsomorphicLayoutEffect';
43
import { useRefState } from '../useRefState/useRefState';
54
/**
65
* @name useTextDirection
@@ -43,7 +42,7 @@ export const useTextDirection = (...params) => {
4342
setValue(value);
4443
element.setAttribute('dir', value);
4544
};
46-
useIsomorphicLayoutEffect(() => {
45+
useEffect(() => {
4746
if (!target && !internalRef.state) return;
4847
const element = target ? getElement(target) : internalRef.current;
4948
if (!element) return;

‎packages/core/src/bundle/hooks/useThrottleCallback/useThrottleCallback.js‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,6 @@ export const useThrottleCallback = (callback, delay) => {
4343
timeoutRef.current = setTimeout(timer, delayRef.current);
4444
};
4545
throttledCallback.cancel = cancel;
46-
console.log('cancel', timeoutRef.current);
4746
cancel();
4847
return throttledCallback;
4948
}, [delay]);

‎packages/core/src/bundle/hooks/useTime/useTime.js‎

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
1-
import { useState } from 'react';
1+
import { useEffect, useState } from 'react';
22
import { getDate } from '@/utils/helpers';
3-
import { useInterval } from '../useInterval/useInterval';
43
/**
54
* @name useTime
65
* @description - Hook that gives you current time in different values
@@ -13,6 +12,11 @@ import { useInterval } from '../useInterval/useInterval';
1312
*/
1413
export const useTime = () => {
1514
const [time, setTime] = useState(getDate());
16-
useInterval(() => setTime(getDate()), 1000);
15+
useEffect(() => {
16+
const timerId = setInterval(() => setTime(getDate()), 1000);
17+
return () => {
18+
clearInterval(timerId);
19+
};
20+
}, []);
1721
return time;
1822
};

0 commit comments

Comments
 (0)