Skip to content

Commit

Permalink
Fix waste render
Browse files Browse the repository at this point in the history
  • Loading branch information
compulim committed Sep 22, 2019
1 parent 59ef438 commit 88052d5
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 13 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
- Fix [#2243](https://github.com/microsoft/BotFramework-WebChat/issues/2243). Fixed sagas to correctly mark activities with speaking attachments, by [@tdurnford](https://github.com/tdurnford) in PR [#2320](https://github.com/microsoft/BotFramework-WebChat/issues/2320)
- Fix [#2365](https://github.com/microsoft/BotFramework-WebChat/issues/2365). Fix Adaptive Card `pushButton` appearance on Chrome, by [@corinagum](https://github.com/corinagum) in PR [#2382](https://github.com/microsoft/BotFramework-WebChat/pull/2382)
- Fix [#2379](https://github.com/microsoft/BotFramework-WebChat/issues/2379). Speech synthesis can be configured off by passing `null`, by [@compulim](https://github.com/compulim) in PR [#2408](https://github.com/microsoft/BotFramework-WebChat/pull/2408)
- Fix [#2418](https://github.com/microsoft/BotFramework-WebChat/issues/2418). Connectivity status should not waste-render every 400 ms, by [@compulim](https://github.com/compulim) in PR [#2419](https://github.com/microsoft/BotFramework-WebChat/pull/2419)

### Added

Expand Down
41 changes: 28 additions & 13 deletions packages/component/src/SendBox/ConnectivityStatus.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,30 @@ import WarningNotificationIcon from '../Attachment/Assets/WarningNotificationIco

const CONNECTIVITY_STATUS_DEBOUNCE = 400;

function setTimeoutSync(fn, interval) {
if (interval > 0) {
return setTimeout(fn, interval);
}

fn();
}

const DebouncedConnectivityStatus = ({ interval, children: propsChildren }) => {
const [children, setChildren] = useState(propsChildren);
const [children, setChildren] = useState(() => propsChildren);
const [since, setSince] = useState(Date.now());

const intervalBeforeSwitch = Math.max(0, interval - Date.now() + since);

useEffect(() => {
const timeout = setTimeout(() => {
setChildren(propsChildren);
setSince(Date.now());
}, Math.max(0, interval - Date.now() + since));
if (children !== propsChildren) {
const timeout = setTimeout(() => {
setChildren(() => propsChildren);
setSince(Date.now());
}, intervalBeforeSwitch);

return () => clearTimeout(timeout);
}, [interval, propsChildren, setChildren, setSince, since]);
return () => clearTimeout(timeout);
}
}, [children, intervalBeforeSwitch, propsChildren]);

return typeof children === 'function' ? children() : false;
};
Expand Down Expand Up @@ -116,17 +128,18 @@ const ConnectivityStatus = ({ connectivityStatus, language, styleSet }) => {

const renderStatus = useCallback(() => {
if (connectivityStatus === 'connectingslow') {
return renderConnectingSlow;
return renderConnectingSlow();
} else if (connectivityStatus === 'error' || connectivityStatus === 'notconnected') {
return renderNotConnected;
return renderNotConnected();
} else if (connectivityStatus === 'uninitialized') {
return renderUninitialized;
return renderUninitialized();
} else if (connectivityStatus === 'reconnecting') {
return renderReconnecting;
return renderReconnecting();
} else if (connectivityStatus === 'sagaerror') {
return renderSagaError;
return renderSagaError();
}
return renderEmptyStatus;

return renderEmptyStatus();
}, [
connectivityStatus,
renderConnectingSlow,
Expand All @@ -137,6 +150,8 @@ const ConnectivityStatus = ({ connectivityStatus, language, styleSet }) => {
renderUninitialized
]);

console.log('render');

return (
<div aria-atomic="false" aria-live="polite" role="status">
<DebouncedConnectivityStatus
Expand Down

0 comments on commit 88052d5

Please sign in to comment.