Skip to content

Commit 559dbbb

Browse files
committed
chore: support if channel not support
1 parent e3ef549 commit 559dbbb

File tree

2 files changed

+14
-4
lines changed

2 files changed

+14
-4
lines changed

src/hooks/channelUpdate.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import raf from 'rc-util/lib/raf';
2+
3+
export default function channelUpdate(callback: VoidFunction) {
4+
if (typeof MessageChannel === 'undefined') {
5+
raf(callback);
6+
} else {
7+
const channel = new MessageChannel();
8+
channel.port1.onmessage = () => callback();
9+
channel.port2.postMessage(undefined);
10+
}
11+
}

src/hooks/useEffectState.tsx

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import useEvent from 'rc-util/lib/hooks/useEvent';
22
import * as React from 'react';
33
import { unstable_batchedUpdates } from 'react-dom';
4+
import channelUpdate from './channelUpdate';
45

56
type Updater<T> = T | ((origin: T) => T);
67

@@ -20,16 +21,14 @@ export function useBatcher() {
2021
if (!updateFuncRef.current) {
2122
updateFuncRef.current = [];
2223

23-
const channel = new MessageChannel();
24-
channel.port1.onmessage = () => {
24+
channelUpdate(() => {
2525
unstable_batchedUpdates(() => {
2626
updateFuncRef.current.forEach(fn => {
2727
fn();
2828
});
2929
updateFuncRef.current = null;
3030
});
31-
};
32-
channel.port2.postMessage(undefined);
31+
});
3332
}
3433

3534
updateFuncRef.current.push(callback);

0 commit comments

Comments
 (0)