Skip to content
This repository has been archived by the owner on Feb 23, 2024. It is now read-only.

Commit

Permalink
Replace lodash debounce with custom utiity
Browse files Browse the repository at this point in the history
  • Loading branch information
mikejolley committed Apr 21, 2023
1 parent f466acc commit 8a645a4
Show file tree
Hide file tree
Showing 8 changed files with 46 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import {
useStoreEvents,
} from '@woocommerce/base-context/hooks';
import { sanitizeHTML } from '@woocommerce/utils';
import { debounce } from 'lodash';
import { debounce } from '@woocommerce/base-utils';
import type { ReactElement } from 'react';

/**
Expand Down
34 changes: 34 additions & 0 deletions assets/js/base/utils/debounce.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
// eslint-disable-next-line @typescript-eslint/no-explicit-any
export type DebouncedFunction< T extends ( ...args: any[] ) => any > = ( (
...args: Parameters< T >
) => void ) & { flush: () => void };

// eslint-disable-next-line @typescript-eslint/no-explicit-any
export const debounce = < T extends ( ...args: any[] ) => any >(
func: T,
wait: number,
immediate?: boolean
): DebouncedFunction< T > => {
let timeout: ReturnType< typeof setTimeout > | null;
let latestArgs: Parameters< T > | null = null;

const debounced = ( ( ...args: Parameters< T > ) => {
latestArgs = args;
if ( timeout ) clearTimeout( timeout );
timeout = setTimeout( () => {
timeout = null;
if ( ! immediate && latestArgs ) func( ...latestArgs );
}, wait );
if ( immediate && ! timeout ) func( ...args );
} ) as DebouncedFunction< T >;

debounced.flush = () => {
if ( timeout && latestArgs ) {
func( ...latestArgs );
clearTimeout( timeout );
timeout = null;
}
};

return debounced;
};
1 change: 0 additions & 1 deletion assets/js/base/utils/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,3 @@ export * from './create-notice';
export * from './get-navigation-type';
export * from './camel-case-keys';
export * from './debounce';
export * from './keyby';
2 changes: 1 addition & 1 deletion assets/js/blocks/reviews/editor-container-block.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
*/
import { __ } from '@wordpress/i18n';
import PropTypes from 'prop-types';
import { debounce } from 'lodash';
import { debounce } from '@woocommerce/base-utils';
import { Placeholder } from '@wordpress/components';
import { useBlockProps } from '@wordpress/block-editor';

Expand Down
8 changes: 6 additions & 2 deletions assets/js/data/cart/push-changes.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
/**
* External dependencies
*/
import { debounce, pick } from 'lodash';
import { pick } from 'lodash';
import { select, dispatch } from '@wordpress/data';
import { pluckEmail, removeAllNotices } from '@woocommerce/base-utils';
import {
pluckEmail,
removeAllNotices,
debounce,
} from '@woocommerce/base-utils';
import {
CartBillingAddress,
CartShippingAddress,
Expand Down
4 changes: 2 additions & 2 deletions assets/js/data/cart/test/push-changes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,8 @@ jest.mock( '@wordpress/data', () => ( {
} ) );

// Mocking lodash here so we can just call the debounced function directly without waiting for debounce.
jest.mock( 'lodash', () => ( {
...jest.requireActual( 'lodash' ),
jest.mock( '@woocommerce/base-utils', () => ( {
...jest.requireActual( '@woocommerce/base-utils' ),
__esModule: true,
debounce: jest.fn( ( callback ) => callback ),
} ) );
Expand Down
2 changes: 1 addition & 1 deletion assets/js/data/cart/update-payment-methods.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* External dependencies
*/
import { dispatch, select } from '@wordpress/data';
import { debounce } from 'lodash';
import { debounce } from '@woocommerce/base-utils';

/**
* Internal dependencies
Expand Down
2 changes: 1 addition & 1 deletion assets/js/editor-components/product-tag-control/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
*/
import { __, _n, sprintf } from '@wordpress/i18n';
import { Component } from '@wordpress/element';
import { debounce } from 'lodash';
import { debounce } from '@woocommerce/base-utils';
import PropTypes from 'prop-types';
import {
SearchListControl,
Expand Down

0 comments on commit 8a645a4

Please sign in to comment.