Skip to content

Commit

Permalink
Throttle the container.tsx pipeline processing function
Browse files Browse the repository at this point in the history
  • Loading branch information
afshinm committed Dec 30, 2023
1 parent 6275e01 commit 451a912
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 19 deletions.
3 changes: 3 additions & 0 deletions src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ export interface Config {
/** to parse a HTML table and load the data */
from: HTMLElement;
storage: Storage<any>;

Check warning on line 38 in src/config.ts

View workflow job for this annotation

GitHub Actions / build (14.x)

Unexpected any. Specify a different type

Check warning on line 38 in src/config.ts

View workflow job for this annotation

GitHub Actions / build (16.x)

Unexpected any. Specify a different type
/** Pipeline process throttle timeout in milliseconds */
processingThrottleMs: number;
pipeline: Pipeline<Tabular>;
/** to automatically calculate the columns width */
autoWidth: boolean;
Expand Down Expand Up @@ -128,6 +130,7 @@ export class Config {
tableRef: createRef(),
width: '100%',
height: 'auto',
processingThrottleMs: 50,
autoWidth: true,
style: {},
className: {},
Expand Down
13 changes: 11 additions & 2 deletions src/util/throttle.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,20 @@
export const throttle = (fn: (...args) => void, wait = 100) => {
/**
* Throttle a given function
* @param fn Function to be called
* @param wait Throttle timeout in milliseconds
* @param leading whether or not to call "fn" immediately
* @returns Throttled function
*/
export const throttle = (fn: (...args) => void, wait = 100, leading = true) => {
let inThrottle: boolean;
let lastFn: ReturnType<typeof setTimeout>;
let lastTime: number;

return (...args) => {
if (!inThrottle) {
fn(...args);
if (leading) {
fn(...args);
}
lastTime = Date.now();
inThrottle = true;
} else {
Expand Down
35 changes: 18 additions & 17 deletions src/view/container.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import * as actions from './actions';
import { useStore } from '../hooks/useStore';
import useSelector from '../../src/hooks/useSelector';
import { useConfig } from '../../src/hooks/useConfig';
import { throttle } from 'src/util/throttle';

export function Container() {
const config = useConfig();
Expand All @@ -19,6 +20,23 @@ export function Container() {
const tableRef = useSelector((state) => state.tableRef);
const tempRef = createRef();

const processPipeline = throttle(async () => {
dispatch(actions.SetLoadingData());

try {
const data = await config.pipeline.process();
dispatch(actions.SetData(data));

// TODO: do we need this setTimemout?
setTimeout(() => {
dispatch(actions.SetStatusToRendered());
}, 0);
} catch (e) {
log.error(e);
dispatch(actions.SetDataErrored());
}
}, config.processingThrottleMs, false);

useEffect(() => {
// set the initial header object
// we update the header width later when "data"
Expand All @@ -41,23 +59,6 @@ export function Container() {
}
}, [data, config, tempRef]);

const processPipeline = async () => {
dispatch(actions.SetLoadingData());

try {
const data = await config.pipeline.process();
dispatch(actions.SetData(data));

// TODO: do we need this setTimemout?
setTimeout(() => {
dispatch(actions.SetStatusToRendered());
}, 0);
} catch (e) {
log.error(e);
dispatch(actions.SetDataErrored());
}
};

return (
<div
role="complementary"
Expand Down

0 comments on commit 451a912

Please sign in to comment.