From b72535f83cd21da99a728568deead08294c3557d Mon Sep 17 00:00:00 2001 From: Anton Arnautov Date: Fri, 26 Jul 2024 20:50:22 +0200 Subject: [PATCH] Drop lodash.throttle, use own implementation --- package.json | 2 -- src/thread.ts | 4 +--- src/utils.ts | 33 +++++++++++++++++++++++++++++++++ yarn.lock | 17 ----------------- 4 files changed, 34 insertions(+), 22 deletions(-) diff --git a/package.json b/package.json index 4d2f91184..1ca6eee80 100644 --- a/package.json +++ b/package.json @@ -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": { @@ -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", diff --git a/src/thread.ts b/src/thread.ts index 5d3985a3f..827760bd2 100644 --- a/src/thread.ts +++ b/src/thread.ts @@ -1,5 +1,3 @@ -import throttle from 'lodash.throttle'; - import { StreamChat } from './client'; import { Channel } from './channel'; import type { @@ -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 = { diff --git a/src/utils.ts b/src/utils.ts index 528ec346d..40065f4d3 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -420,3 +420,36 @@ export const transformReadArrayToDictionary = unknown>( + fn: T, + timeout = 200, + { leading = true, trailing = false }: { leading?: boolean; trailing?: boolean } = {}, +) => { + let runningTimeout: null | NodeJS.Timeout = null; + let storedArgs: Parameters | null = null; + + return (...args: Parameters) => { + 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); + }; +}; diff --git a/yarn.lock b/yarn.lock index d337eea36..479e974c9 100644 --- a/yarn.lock +++ b/yarn.lock @@ -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" @@ -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"