@x-oasis/throttle 0.0.14
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +19 -0
- package/dist/index.d.ts +3 -0
- package/dist/index.js +8 -0
- package/dist/throttle.cjs.development.js +47 -0
- package/dist/throttle.cjs.development.js.map +1 -0
- package/dist/throttle.cjs.production.min.js +2 -0
- package/dist/throttle.cjs.production.min.js.map +1 -0
- package/dist/throttle.esm.js +43 -0
- package/dist/throttle.esm.js.map +1 -0
- package/package.json +19 -0
- package/src/index.ts +61 -0
- package/test/test.spec.ts +5 -0
- package/tsconfig.build.json +11 -0
- package/tsconfig.json +7 -0
- package/vitest.config.ts +26 -0
package/README.md
ADDED
package/dist/index.d.ts
ADDED
package/dist/index.js
ADDED
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, '__esModule', { value: true });
|
|
4
|
+
|
|
5
|
+
function throttle(fn, threshold, fallbackOptions) {
|
|
6
|
+
if (threshold === void 0) {
|
|
7
|
+
threshold = 250;
|
|
8
|
+
}
|
|
9
|
+
var last = 0;
|
|
10
|
+
var timeoutHandler = null;
|
|
11
|
+
var fallback = !!fallbackOptions;
|
|
12
|
+
var queue = [];
|
|
13
|
+
return function throttled() {
|
|
14
|
+
var now = Date.now();
|
|
15
|
+
for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
|
|
16
|
+
args[_key] = arguments[_key];
|
|
17
|
+
}
|
|
18
|
+
if (now - last > threshold) {
|
|
19
|
+
last = now;
|
|
20
|
+
if (timeoutHandler) {
|
|
21
|
+
clearTimeout(timeoutHandler);
|
|
22
|
+
timeoutHandler = null;
|
|
23
|
+
queue = [];
|
|
24
|
+
}
|
|
25
|
+
fn.apply(null, args);
|
|
26
|
+
return;
|
|
27
|
+
}
|
|
28
|
+
if (!fallback) return;
|
|
29
|
+
timeoutHandler = setTimeout(function () {
|
|
30
|
+
var len = queue.length;
|
|
31
|
+
if (len) {
|
|
32
|
+
var currentArgs = queue[len - 1];
|
|
33
|
+
queue = [];
|
|
34
|
+
fn.apply(null, currentArgs);
|
|
35
|
+
}
|
|
36
|
+
}, threshold);
|
|
37
|
+
var nextArgs = args;
|
|
38
|
+
var persistArgs = typeof fallbackOptions === 'object' ? fallbackOptions.persistArgs : null;
|
|
39
|
+
if (typeof persistArgs === 'function') {
|
|
40
|
+
nextArgs = persistArgs(args);
|
|
41
|
+
}
|
|
42
|
+
queue.push(nextArgs);
|
|
43
|
+
};
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
exports.default = throttle;
|
|
47
|
+
//# sourceMappingURL=throttle.cjs.development.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"throttle.cjs.development.js","sources":["../src/index.ts"],"sourcesContent":["/**\n *\n * @param fn\n * @param threshold\n * @returns\n *\n * trigger first, then trigger on the last\n */\n\nexport default function throttle(\n fn: Function,\n threshold = 250,\n fallbackOptions?:\n | {\n persistArgs?: (...args: any[]) => any;\n }\n | boolean\n) {\n let last = 0;\n let timeoutHandler = null;\n const fallback = !!fallbackOptions;\n\n let queue = [];\n\n return function throttled(...args: any[]) {\n const now = Date.now();\n\n if (now - last > threshold) {\n last = now;\n if (timeoutHandler) {\n clearTimeout(timeoutHandler);\n timeoutHandler = null;\n queue = [];\n }\n\n fn.apply(null, args);\n return;\n }\n\n if (!fallback) return;\n\n timeoutHandler = setTimeout(() => {\n const len = queue.length;\n if (len) {\n const currentArgs = queue[len - 1];\n queue = [];\n fn.apply(null, currentArgs);\n }\n }, threshold);\n\n let nextArgs = args;\n const persistArgs =\n typeof fallbackOptions === 'object' ? fallbackOptions.persistArgs : null;\n\n if (typeof persistArgs === 'function') {\n nextArgs = persistArgs(args);\n }\n\n queue.push(nextArgs);\n };\n}\n"],"names":["throttle","fn","threshold","fallbackOptions","last","timeoutHandler","fallback","queue","throttled","now","Date","_len","arguments","length","args","Array","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W,GAAG,IAAI;IAE1E,IAAI,OAAOA,WAAW,KAAK,UAAU,EAAE;MACrCD,QAAQ,GAAGC,WAAW,CAACT,IAAI,CAAC;;IAG9BP,KAAK,CAACiB,IAAI,CAACF,QAAQ,CAAC;GACrB;AACH;;;;"}
|
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
"use strict";Object.defineProperty(exports,"__esModule",{value:!0}),exports.default=function(e,t,n){void 0===t&&(t=250);var r=0,l=null,u=!!n,o=[];return function(){for(var i=Date.now(),a=arguments.length,f=new Array(a),p=0;p<a;p++)f[p]=arguments[p];if(i-r>t)return r=i,l&&(clearTimeout(l),l=null,o=[]),void e.apply(null,f);if(u){l=setTimeout((function(){var t=o.length;if(t){var n=o[t-1];o=[],e.apply(null,n)}}),t);var s=f,c="object"==typeof n?n.persistArgs:null;"function"==typeof c&&(s=c(f)),o.push(s)}}};
|
|
2
|
+
//# sourceMappingURL=throttle.cjs.production.min.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"throttle.cjs.production.min.js","sources":["../src/index.ts"],"sourcesContent":["/**\n *\n * @param fn\n * @param threshold\n * @returns\n *\n * trigger first, then trigger on the last\n */\n\nexport default function throttle(\n fn: Function,\n threshold = 250,\n fallbackOptions?:\n | {\n persistArgs?: (...args: any[]) => any;\n }\n | boolean\n) {\n let last = 0;\n let timeoutHandler = null;\n const fallback = !!fallbackOptions;\n\n let queue = [];\n\n return function throttled(...args: any[]) {\n const now = Date.now();\n\n if (now - last > threshold) {\n last = now;\n if (timeoutHandler) {\n clearTimeout(timeoutHandler);\n timeoutHandler = null;\n queue = [];\n }\n\n fn.apply(null, args);\n return;\n }\n\n if (!fallback) return;\n\n timeoutHandler = setTimeout(() => {\n const len = queue.length;\n if (len) {\n const currentArgs = queue[len - 1];\n queue = [];\n fn.apply(null, currentArgs);\n }\n }, threshold);\n\n let nextArgs = args;\n const persistArgs =\n typeof fallbackOptions === 'object' ? fallbackOptions.persistArgs : null;\n\n if (typeof persistArgs === 'function') {\n nextArgs = persistArgs(args);\n }\n\n queue.push(nextArgs);\n };\n}\n"],"names":["fn","threshold","fallbackOptions","last","timeoutHandler","fallback","queue","now","Date","_len","arguments","length","args","Array","_key","clearTimeout","apply","setTimeout","len","currentArgs","nextArgs","persistArgs","push"],"mappings":"6FAUEA,EACAC,EACAC,YADAD,IAAAA,EAAY,KAOZ,IAAIE,EAAO,EACPC,EAAiB,KACfC,IAAaH,EAEfI,EAAQ,GAEZ,OAAO,WACkB,IAAvB,IAAMC,EAAMC,KAAKD,MAAME,EAAAC,UAAAC,OADIC,MAAWC,MAAAJ,GAAAK,IAAAA,EAAAL,EAAAK,IAAXF,EAAWE,GAAAJ,UAAAI,GAGtC,GAAIP,EAAMJ,EAAOF,EASf,OARAE,EAAOI,EACHH,IACFW,aAAaX,GACbA,EAAiB,KACjBE,EAAQ,SAGVN,EAAGgB,MAAM,KAAMJ,GAIjB,GAAKP,EAAL,CAEAD,EAAiBa,YAAW,WAC1B,IAAMC,EAAMZ,EAAMK,OAClB,GAAIO,EAAK,CACP,IAAMC,EAAcb,EAAMY,EAAM,GAChCZ,EAAQ,GACRN,EAAGgB,MAAM,KAAMG,MAEhBlB,GAEH,IAAImB,EAAWR,EACTS,EACuB,iBAApBnB,EAA+BA,EAAgBmB,YAAc,KAE3C,mBAAhBA,IACTD,EAAWC,EAAYT,IAGzBN,EAAMgB,KAAKF"}
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
function throttle(fn, threshold, fallbackOptions) {
|
|
2
|
+
if (threshold === void 0) {
|
|
3
|
+
threshold = 250;
|
|
4
|
+
}
|
|
5
|
+
var last = 0;
|
|
6
|
+
var timeoutHandler = null;
|
|
7
|
+
var fallback = !!fallbackOptions;
|
|
8
|
+
var queue = [];
|
|
9
|
+
return function throttled() {
|
|
10
|
+
var now = Date.now();
|
|
11
|
+
for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
|
|
12
|
+
args[_key] = arguments[_key];
|
|
13
|
+
}
|
|
14
|
+
if (now - last > threshold) {
|
|
15
|
+
last = now;
|
|
16
|
+
if (timeoutHandler) {
|
|
17
|
+
clearTimeout(timeoutHandler);
|
|
18
|
+
timeoutHandler = null;
|
|
19
|
+
queue = [];
|
|
20
|
+
}
|
|
21
|
+
fn.apply(null, args);
|
|
22
|
+
return;
|
|
23
|
+
}
|
|
24
|
+
if (!fallback) return;
|
|
25
|
+
timeoutHandler = setTimeout(function () {
|
|
26
|
+
var len = queue.length;
|
|
27
|
+
if (len) {
|
|
28
|
+
var currentArgs = queue[len - 1];
|
|
29
|
+
queue = [];
|
|
30
|
+
fn.apply(null, currentArgs);
|
|
31
|
+
}
|
|
32
|
+
}, threshold);
|
|
33
|
+
var nextArgs = args;
|
|
34
|
+
var persistArgs = typeof fallbackOptions === 'object' ? fallbackOptions.persistArgs : null;
|
|
35
|
+
if (typeof persistArgs === 'function') {
|
|
36
|
+
nextArgs = persistArgs(args);
|
|
37
|
+
}
|
|
38
|
+
queue.push(nextArgs);
|
|
39
|
+
};
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
export default throttle;
|
|
43
|
+
//# sourceMappingURL=throttle.esm.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"throttle.esm.js","sources":["../src/index.ts"],"sourcesContent":["/**\n *\n * @param fn\n * @param threshold\n * @returns\n *\n * trigger first, then trigger on the last\n */\n\nexport default function throttle(\n fn: Function,\n threshold = 250,\n fallbackOptions?:\n | {\n persistArgs?: (...args: any[]) => any;\n }\n | boolean\n) {\n let last = 0;\n let timeoutHandler = null;\n const fallback = !!fallbackOptions;\n\n let queue = [];\n\n return function throttled(...args: any[]) {\n const now = Date.now();\n\n if (now - last > threshold) {\n last = now;\n if (timeoutHandler) {\n clearTimeout(timeoutHandler);\n timeoutHandler = null;\n queue = [];\n }\n\n fn.apply(null, args);\n return;\n }\n\n if (!fallback) return;\n\n timeoutHandler = setTimeout(() => {\n const len = queue.length;\n if (len) {\n const currentArgs = queue[len - 1];\n queue = [];\n fn.apply(null, currentArgs);\n }\n }, threshold);\n\n let nextArgs = args;\n const persistArgs =\n typeof fallbackOptions === 'object' ? fallbackOptions.persistArgs : null;\n\n if (typeof persistArgs === 'function') {\n nextArgs = persistArgs(args);\n }\n\n queue.push(nextArgs);\n };\n}\n"],"names":["throttle","fn","threshold","fallbackOptions","last","timeoutHandler","fallback","queue","throttled","now","Date","_len","arguments","length","args","Array","_key","clearTimeout","apply","setTimeout","len","currentArgs","nextArgs","persistArgs","push"],"mappings":"SASwBA,QAAQA,CAC9BC,EAAY,EACZC,SAAS,EACTC,eAIW;MALXD,SAAS;IAATA,SAAS,GAAG,GAAG;;EAOf,IAAIE,IAAI,GAAG,CAAC;EACZ,IAAIC,cAAc,GAAG,IAAI;EACzB,IAAMC,QAAQ,GAAG,CAAC,CAACH,eAAe;EAElC,IAAII,KAAK,GAAG,EAAE;EAEd,OAAO,SAASC,SAASA;IACvB,IAAMC,GAAG,GAAGC,IAAI,CAACD,GAAG,EAAE;IAAC,SAAAE,IAAA,GAAAC,SAAA,CAAAC,MAAA,EADIC,IAAW,OAAAC,KAAA,CAAAJ,IAAA,GAAAK,IAAA,MAAAA,IAAA,GAAAL,IAAA,EAAAK,IAAA;MAAXF,IAAW,CAAAE,IAAA,IAAAJ,SAAA,CAAAI,IAAA;;IAGtC,IAAIP,GAAG,GAAGL,IAAI,GAAGF,SAAS,EAAE;MAC1BE,IAAI,GAAGK,GAAG;MACV,IAAIJ,cAAc,EAAE;QAClBY,YAAY,CAACZ,cAAc,CAAC;QAC5BA,cAAc,GAAG,IAAI;QACrBE,KAAK,GAAG,EAAE;;MAGZN,EAAE,CAACiB,KAAK,CAAC,IAAI,EAAEJ,IAAI,CAAC;MACpB;;IAGF,IAAI,CAACR,QAAQ,EAAE;IAEfD,cAAc,GAAGc,UAAU,CAAC;MAC1B,IAAMC,GAAG,GAAGb,KAAK,CAACM,MAAM;MACxB,IAAIO,GAAG,EAAE;QACP,IAAMC,WAAW,GAAGd,KAAK,CAACa,GAAG,GAAG,CAAC,CAAC;QAClCb,KAAK,GAAG,EAAE;QACVN,EAAE,CAACiB,KAAK,CAAC,IAAI,EAAEG,WAAW,CAAC;;KAE9B,EAAEnB,SAAS,CAAC;IAEb,IAAIoB,QAAQ,GAAGR,IAAI;IACnB,IAAMS,WAAW,GACf,OAAOpB,eAAe,KAAK,QAAQ,GAAGA,eAAe,CAACoB,WAAW,GAAG,IAAI;IAE1E,IAAI,OAAOA,WAAW,KAAK,UAAU,EAAE;MACrCD,QAAQ,GAAGC,WAAW,CAACT,IAAI,CAAC;;IAG9BP,KAAK,CAACiB,IAAI,CAACF,QAAQ,CAAC;GACrB;AACH;;;;"}
|
package/package.json
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@x-oasis/throttle",
|
|
3
|
+
"version": "0.0.14",
|
|
4
|
+
"description": "throttle function",
|
|
5
|
+
"main": "dist/index.js",
|
|
6
|
+
"typings": "dist/index.d.ts",
|
|
7
|
+
"module": "dist/throttle.esm.js",
|
|
8
|
+
"author": "",
|
|
9
|
+
"license": "ISC",
|
|
10
|
+
"devDependencies": {
|
|
11
|
+
"tsdx": "^0.14.1"
|
|
12
|
+
},
|
|
13
|
+
"scripts": {
|
|
14
|
+
"build": "tsdx build --tsconfig tsconfig.build.json",
|
|
15
|
+
"clean": "rimraf ./dist",
|
|
16
|
+
"test": "vitest",
|
|
17
|
+
"compile": "tsc -p tsconfig.build.json"
|
|
18
|
+
}
|
|
19
|
+
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
/**
|
|
2
|
+
*
|
|
3
|
+
* @param fn
|
|
4
|
+
* @param threshold
|
|
5
|
+
* @returns
|
|
6
|
+
*
|
|
7
|
+
* trigger first, then trigger on the last
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
export default function throttle(
|
|
11
|
+
fn: Function,
|
|
12
|
+
threshold = 250,
|
|
13
|
+
fallbackOptions?:
|
|
14
|
+
| {
|
|
15
|
+
persistArgs?: (...args: any[]) => any;
|
|
16
|
+
}
|
|
17
|
+
| boolean
|
|
18
|
+
) {
|
|
19
|
+
let last = 0;
|
|
20
|
+
let timeoutHandler = null;
|
|
21
|
+
const fallback = !!fallbackOptions;
|
|
22
|
+
|
|
23
|
+
let queue = [];
|
|
24
|
+
|
|
25
|
+
return function throttled(...args: any[]) {
|
|
26
|
+
const now = Date.now();
|
|
27
|
+
|
|
28
|
+
if (now - last > threshold) {
|
|
29
|
+
last = now;
|
|
30
|
+
if (timeoutHandler) {
|
|
31
|
+
clearTimeout(timeoutHandler);
|
|
32
|
+
timeoutHandler = null;
|
|
33
|
+
queue = [];
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
fn.apply(null, args);
|
|
37
|
+
return;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
if (!fallback) return;
|
|
41
|
+
|
|
42
|
+
timeoutHandler = setTimeout(() => {
|
|
43
|
+
const len = queue.length;
|
|
44
|
+
if (len) {
|
|
45
|
+
const currentArgs = queue[len - 1];
|
|
46
|
+
queue = [];
|
|
47
|
+
fn.apply(null, currentArgs);
|
|
48
|
+
}
|
|
49
|
+
}, threshold);
|
|
50
|
+
|
|
51
|
+
let nextArgs = args;
|
|
52
|
+
const persistArgs =
|
|
53
|
+
typeof fallbackOptions === 'object' ? fallbackOptions.persistArgs : null;
|
|
54
|
+
|
|
55
|
+
if (typeof persistArgs === 'function') {
|
|
56
|
+
nextArgs = persistArgs(args);
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
queue.push(nextArgs);
|
|
60
|
+
};
|
|
61
|
+
}
|
package/tsconfig.json
ADDED
package/vitest.config.ts
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
// import path from 'path';
|
|
2
|
+
// import tsPath from 'vite-tsconfig-paths';
|
|
3
|
+
import { defineConfig } from 'vitest/config';
|
|
4
|
+
|
|
5
|
+
// const __dirname = import.meta.url.slice(7, import.meta.url.lastIndexOf('/'));
|
|
6
|
+
|
|
7
|
+
export default defineConfig({
|
|
8
|
+
test: {
|
|
9
|
+
globals: true,
|
|
10
|
+
include: ['test/**/*.(spec|test).ts'],
|
|
11
|
+
exclude: ['node_modules/**'],
|
|
12
|
+
threads: false,
|
|
13
|
+
|
|
14
|
+
coverage: {
|
|
15
|
+
provider: 'istanbul', // or 'c8'
|
|
16
|
+
},
|
|
17
|
+
},
|
|
18
|
+
|
|
19
|
+
// plugins: [tsPath()],
|
|
20
|
+
resolve: {
|
|
21
|
+
alias: {},
|
|
22
|
+
},
|
|
23
|
+
define: {
|
|
24
|
+
__DEV__: false,
|
|
25
|
+
},
|
|
26
|
+
});
|