Skip to content

Commit

Permalink
chore: remove throttleByAnimationFrameDecorator (ant-design#38693)
Browse files Browse the repository at this point in the history
* chore: remove throttleByAnimationFrameDecorator

* fix

* fix
  • Loading branch information
li-jia-nan authored Nov 19, 2022
1 parent cd52a3d commit 52cb37f
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 83 deletions.
21 changes: 1 addition & 20 deletions components/_util/__tests__/util.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,7 @@ import { waitFakeTimer, render, fireEvent } from '../../../tests/utils';
import getDataOrAriaProps from '../getDataOrAriaProps';
import delayRaf from '../raf';
import { isStyleSupport } from '../styleChecker';
import {
throttleByAnimationFrame,
throttleByAnimationFrameDecorator,
} from '../throttleByAnimationFrame';
import throttleByAnimationFrame from '../throttleByAnimationFrame';
import TransButton from '../transButton';

describe('Test utils function', () => {
Expand Down Expand Up @@ -49,22 +46,6 @@ describe('Test utils function', () => {

expect(callback).not.toHaveBeenCalled();
});

it('throttleByAnimationFrameDecorator should works', async () => {
const callbackFn = jest.fn();
class Test {
@throttleByAnimationFrameDecorator()
callback() {
callbackFn();
}
}
const test = new Test();
test.callback();
test.callback();
test.callback();
await waitFakeTimer();
expect(callbackFn).toHaveBeenCalledTimes(1);
});
});

describe('getDataOrAriaProps', () => {
Expand Down
41 changes: 7 additions & 34 deletions components/_util/throttleByAnimationFrame.tsx
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
import raf from 'rc-util/lib/raf';

export function throttleByAnimationFrame<T extends unknown[]>(fn: (...args: T) => void) {
type throttledFn = (...args: any[]) => void;

type throttledCancelFn = { cancel: () => void };

function throttleByAnimationFrame<T extends any[]>(fn: (...args: T) => void) {
let requestId: number | null;

const later = (args: T) => () => {
requestId = null;
fn(...args);
};

const throttled: {
(...args: T): void;
cancel: () => void;
} = (...args: T) => {
const throttled: throttledFn & throttledCancelFn = (...args: T) => {
if (requestId == null) {
requestId = raf(later(args));
}
Expand All @@ -25,32 +26,4 @@ export function throttleByAnimationFrame<T extends unknown[]>(fn: (...args: T) =
return throttled;
}

export function throttleByAnimationFrameDecorator() {
return function throttle(target: any, key: string, descriptor: any) {
const fn = descriptor.value;
let definingProperty = false;
return {
configurable: true,
get() {
// In IE11 calling Object.defineProperty has a side-effect of evaluating the
// getter for the property which is being replaced. This causes infinite
// recursion and an "Out of stack space" error.
// eslint-disable-next-line no-prototype-builtins
if (definingProperty || this === target.prototype || this.hasOwnProperty(key)) {
/* istanbul ignore next */
return fn;
}

const boundFn = throttleByAnimationFrame(fn.bind(this));
definingProperty = true;
Object.defineProperty(this, key, {
value: boundFn,
configurable: true,
writable: true,
});
definingProperty = false;
return boundFn;
},
};
};
}
export default throttleByAnimationFrame;
32 changes: 9 additions & 23 deletions components/affix/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import omit from 'rc-util/lib/omit';
import * as React from 'react';
import type { ConfigConsumerProps } from '../config-provider';
import { ConfigContext } from '../config-provider';
import { throttleByAnimationFrameDecorator } from '../_util/throttleByAnimationFrame';
import throttleByAnimationFrame from '../_util/throttleByAnimationFrame';

import useStyle from './style';
import {
Expand Down Expand Up @@ -126,9 +126,9 @@ class Affix extends React.Component<InternalAffixProps, AffixState> {
componentWillUnmount() {
clearTimeout(this.timeout);
removeObserveTarget(this);
(this.updatePosition as any).cancel();
this.updatePosition.cancel();
// https://github.com/ant-design/ant-design/issues/22683
(this.lazyUpdatePosition as any).cancel();
this.lazyUpdatePosition.cancel();
}

getOffsetTop = () => {
Expand Down Expand Up @@ -228,14 +228,11 @@ class Affix extends React.Component<InternalAffixProps, AffixState> {
}
};

// Handle realign logic
@throttleByAnimationFrameDecorator()
updatePosition() {
updatePosition = throttleByAnimationFrame(() => {
this.prepareMeasure();
}
});

@throttleByAnimationFrameDecorator()
lazyUpdatePosition() {
lazyUpdatePosition = throttleByAnimationFrame(() => {
const targetFunc = this.getTargetFunc();
const { affixStyle } = this.state;

Expand All @@ -262,7 +259,7 @@ class Affix extends React.Component<InternalAffixProps, AffixState> {

// Directly call prepare measure since it's already throttled.
this.prepareMeasure();
}
});

// =================== Render ===================
render() {
Expand All @@ -288,21 +285,11 @@ class Affix extends React.Component<InternalAffixProps, AffixState> {
}

return (
<ResizeObserver
onResize={() => {
this.updatePosition();
}}
>
<ResizeObserver onResize={this.updatePosition}>
<div {...props} ref={this.savePlaceholderNode}>
{affixStyle && <div style={placeholderStyle} aria-hidden="true" />}
<div className={className} ref={this.saveFixedNode} style={affixStyle}>
<ResizeObserver
onResize={() => {
this.updatePosition();
}}
>
{children}
</ResizeObserver>
<ResizeObserver onResize={this.updatePosition}>{children}</ResizeObserver>
</div>
</div>
</ResizeObserver>
Expand All @@ -321,7 +308,6 @@ const AffixFC = React.forwardRef<Affix, AffixProps>((props, ref) => {

const AffixProps: InternalAffixProps = {
...props,

affixPrefixCls,
rootClassName: hashId,
};
Expand Down
6 changes: 3 additions & 3 deletions components/back-top/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { ConfigContext } from '../config-provider';
import getScroll from '../_util/getScroll';
import { cloneElement } from '../_util/reactNode';
import scrollTo from '../_util/scrollTo';
import { throttleByAnimationFrame } from '../_util/throttleByAnimationFrame';
import throttleByAnimationFrame from '../_util/throttleByAnimationFrame';
import warning from '../_util/warning';
import useStyle from './style';

Expand All @@ -32,7 +32,7 @@ interface ChildrenProps {
visible?: boolean; // Only for test. Don't use it.
}

const BackTopContent: React.FC<ChildrenProps> = props => {
const BackTopContent: React.FC<ChildrenProps> = (props) => {
const { prefixCls, rootPrefixCls, children, visible } = props;
const defaultElement = (
<div className={`${prefixCls}-content`}>
Expand All @@ -52,7 +52,7 @@ const BackTopContent: React.FC<ChildrenProps> = props => {
);
};

const BackTop: React.FC<BackTopProps> = props => {
const BackTop: React.FC<BackTopProps> = (props) => {
const [visible, setVisible] = useMergedState(false, {
value: props.visible,
});
Expand Down
6 changes: 3 additions & 3 deletions components/float-button/BackTop.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@ import type { ConfigConsumerProps } from '../config-provider';
import { ConfigContext } from '../config-provider';
import getScroll from '../_util/getScroll';
import scrollTo from '../_util/scrollTo';
import { throttleByAnimationFrame } from '../_util/throttleByAnimationFrame';
import throttleByAnimationFrame from '../_util/throttleByAnimationFrame';
import FloatButtonGroupContext from './context';
import type { BackTopProps, FloatButtonShape } from './interface';
import useStyle from './style';

const BackTop: React.FC<BackTopProps> = props => {
const BackTop: React.FC<BackTopProps> = (props) => {
const {
prefixCls: customizePrefixCls,
className = '',
Expand Down Expand Up @@ -63,7 +63,7 @@ const BackTop: React.FC<BackTopProps> = props => {
};
}, [target]);

const scrollToTop: React.MouseEventHandler<HTMLDivElement> = e => {
const scrollToTop: React.MouseEventHandler<HTMLDivElement> = (e) => {
scrollTo(0, { getContainer: target || getDefaultTarget, duration });
if (typeof onClick === 'function') {
onClick(e);
Expand Down

0 comments on commit 52cb37f

Please sign in to comment.