Skip to content

Commit

Permalink
Drop lodash.throttle, use own implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
arnautov-anton committed Jul 26, 2024
1 parent 2e591b9 commit b72535f
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 22 deletions.
2 changes: 0 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@
"form-data": "^4.0.0",
"isomorphic-ws": "^4.0.1",
"jsonwebtoken": "~9.0.0",
"lodash.throttle": "^4.1.1",
"ws": "^7.4.4"
},
"devDependencies": {
Expand Down Expand Up @@ -72,7 +71,6 @@
"@types/chai-as-promised": "^7.1.4",
"@types/chai-like": "^1.1.1",
"@types/eslint": "7.2.7",
"@types/lodash.throttle": "^4.1.9",
"@types/mocha": "^9.0.0",
"@types/node": "^16.11.11",
"@types/prettier": "^2.2.2",
Expand Down
4 changes: 1 addition & 3 deletions src/thread.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import throttle from 'lodash.throttle';

import { StreamChat } from './client';
import { Channel } from './channel';
import type {
Expand All @@ -14,7 +12,7 @@ import type {
MessagePaginationOptions,
AscDesc,
} from './types';
import { addToMessageList, findInsertionIndex, formatMessage, transformReadArrayToDictionary } from './utils';
import { addToMessageList, findInsertionIndex, formatMessage, transformReadArrayToDictionary, throttle } from './utils';
import { Handler, SimpleStateStore } from './store/SimpleStateStore';

type ThreadReadStatus<StreamChatGenerics extends ExtendableGenerics = DefaultGenerics> = {
Expand Down
33 changes: 33 additions & 0 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -420,3 +420,36 @@ export const transformReadArrayToDictionary = <T extends { last_read: string; us
};
return accumulator;
}, {});

// works exactly the same as lodash.throttle
export const throttle = <T extends (...args: unknown[]) => unknown>(
fn: T,
timeout = 200,
{ leading = true, trailing = false }: { leading?: boolean; trailing?: boolean } = {},
) => {
let runningTimeout: null | NodeJS.Timeout = null;
let storedArgs: Parameters<T> | null = null;

return (...args: Parameters<T>) => {
if (runningTimeout) {
if (trailing) storedArgs = args;
return;
}

if (leading) fn(...args);

const timeoutHandler = () => {
if (storedArgs) {
fn(...storedArgs);
storedArgs = null;
runningTimeout = setTimeout(timeoutHandler, timeout);

return;
}

runningTimeout = null;
};

runningTimeout = setTimeout(timeoutHandler, timeout);
};
};
17 changes: 0 additions & 17 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1797,18 +1797,6 @@
dependencies:
"@types/node" "*"

"@types/lodash.throttle@^4.1.9":
version "4.1.9"
resolved "https://registry.yarnpkg.com/@types/lodash.throttle/-/lodash.throttle-4.1.9.tgz#f17a6ae084f7c0117bd7df145b379537bc9615c5"
integrity sha512-PCPVfpfueguWZQB7pJQK890F2scYKoDUL3iM522AptHWn7d5NQmeS/LTEHIcLr5PaTzl3dK2Z0xSUHHTHwaL5g==
dependencies:
"@types/lodash" "*"

"@types/lodash@*":
version "4.17.7"
resolved "https://registry.yarnpkg.com/@types/lodash/-/lodash-4.17.7.tgz#2f776bcb53adc9e13b2c0dfd493dfcbd7de43612"
integrity sha512-8wTvZawATi/lsmNu10/j2hk1KEP0IvjubqPE3cu1Xz7xfXXt5oCq3SNUz4fMIP4XGF9Ky+Ue2tBA3hcS7LSBlA==

"@types/minimist@^1.2.0":
version "1.2.2"
resolved "https://registry.yarnpkg.com/@types/minimist/-/minimist-1.2.2.tgz#ee771e2ba4b3dc5b372935d549fd9617bf345b8c"
Expand Down Expand Up @@ -4345,11 +4333,6 @@ lodash.ismatch@^4.4.0:
resolved "https://registry.yarnpkg.com/lodash.ismatch/-/lodash.ismatch-4.4.0.tgz#756cb5150ca3ba6f11085a78849645f188f85f37"
integrity sha1-dWy1FQyjum8RCFp4hJZF8Yj4Xzc=

lodash.throttle@^4.1.1:
version "4.1.1"
resolved "https://registry.yarnpkg.com/lodash.throttle/-/lodash.throttle-4.1.1.tgz#c23e91b710242ac70c37f1e1cda9274cc39bf2f4"
integrity sha512-wIkUCfVKpVsWo3JSZlc+8MB5it+2AN5W8J7YVMST30UrvcQNZ1Okbj+rbVniijTWE6FGYy4XJq/rHkas8qJMLQ==

lodash@^4.17.15, lodash@^4.17.19, lodash@^4.17.20, lodash@^4.17.21:
version "4.17.21"
resolved "https://registry.yarnpkg.com/lodash/-/lodash-4.17.21.tgz#679591c564c3bffaae8454cf0b3df370c3d6911c"
Expand Down

0 comments on commit b72535f

Please sign in to comment.