@elementor/utils 0.4.0 → 3.32.0-20
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/CHANGELOG.md +7 -0
- package/dist/index.d.mts +19 -2
- package/dist/index.d.ts +19 -2
- package/dist/index.js +56 -2
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +52 -1
- package/dist/index.mjs.map +1 -1
- package/package.json +6 -3
- package/src/encoding.ts +11 -0
- package/src/index.ts +2 -1
- package/src/use-debounce-state.ts +54 -0
package/CHANGELOG.md
CHANGED
package/dist/index.d.mts
CHANGED
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
import * as React from 'react';
|
|
2
|
+
|
|
1
3
|
type ElementorErrorOptions = {
|
|
2
4
|
cause?: Error['cause'];
|
|
3
5
|
context?: Record<string, unknown> | null;
|
|
@@ -26,12 +28,24 @@ declare const createError: <T extends ElementorErrorOptions["context"]>({ code,
|
|
|
26
28
|
cause?: unknown;
|
|
27
29
|
};
|
|
28
30
|
captureStackTrace(targetObject: object, constructorOpt?: Function): void;
|
|
29
|
-
prepareStackTrace
|
|
31
|
+
prepareStackTrace(err: Error, stackTraces: NodeJS.CallSite[]): any;
|
|
30
32
|
stackTraceLimit: number;
|
|
31
33
|
};
|
|
32
34
|
|
|
33
35
|
declare const ensureError: (error: unknown) => Error;
|
|
34
36
|
|
|
37
|
+
type UseDebounceStateOptions = {
|
|
38
|
+
delay?: number;
|
|
39
|
+
initialValue?: string;
|
|
40
|
+
};
|
|
41
|
+
type UseDebounceStateResult = {
|
|
42
|
+
debouncedValue: string;
|
|
43
|
+
inputValue: string;
|
|
44
|
+
handleChange: (val: string) => void;
|
|
45
|
+
setInputValue: React.Dispatch<React.SetStateAction<string>>;
|
|
46
|
+
};
|
|
47
|
+
declare function useDebounceState(options?: UseDebounceStateOptions): UseDebounceStateResult;
|
|
48
|
+
|
|
35
49
|
declare function debounce<TArgs extends any[]>(fn: (...args: TArgs) => void, wait: number): {
|
|
36
50
|
(...args: TArgs): void;
|
|
37
51
|
flush: (...args: TArgs) => void;
|
|
@@ -39,4 +53,7 @@ declare function debounce<TArgs extends any[]>(fn: (...args: TArgs) => void, wai
|
|
|
39
53
|
pending: () => boolean;
|
|
40
54
|
};
|
|
41
55
|
|
|
42
|
-
|
|
56
|
+
declare const encodeString: (value: string) => string;
|
|
57
|
+
declare const decodeString: (value: string, fallback?: string) => string;
|
|
58
|
+
|
|
59
|
+
export { type CreateErrorParams, ElementorError, type ElementorErrorOptions, type UseDebounceStateOptions, type UseDebounceStateResult, createError, debounce, decodeString, encodeString, ensureError, useDebounceState };
|
package/dist/index.d.ts
CHANGED
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
import * as React from 'react';
|
|
2
|
+
|
|
1
3
|
type ElementorErrorOptions = {
|
|
2
4
|
cause?: Error['cause'];
|
|
3
5
|
context?: Record<string, unknown> | null;
|
|
@@ -26,12 +28,24 @@ declare const createError: <T extends ElementorErrorOptions["context"]>({ code,
|
|
|
26
28
|
cause?: unknown;
|
|
27
29
|
};
|
|
28
30
|
captureStackTrace(targetObject: object, constructorOpt?: Function): void;
|
|
29
|
-
prepareStackTrace
|
|
31
|
+
prepareStackTrace(err: Error, stackTraces: NodeJS.CallSite[]): any;
|
|
30
32
|
stackTraceLimit: number;
|
|
31
33
|
};
|
|
32
34
|
|
|
33
35
|
declare const ensureError: (error: unknown) => Error;
|
|
34
36
|
|
|
37
|
+
type UseDebounceStateOptions = {
|
|
38
|
+
delay?: number;
|
|
39
|
+
initialValue?: string;
|
|
40
|
+
};
|
|
41
|
+
type UseDebounceStateResult = {
|
|
42
|
+
debouncedValue: string;
|
|
43
|
+
inputValue: string;
|
|
44
|
+
handleChange: (val: string) => void;
|
|
45
|
+
setInputValue: React.Dispatch<React.SetStateAction<string>>;
|
|
46
|
+
};
|
|
47
|
+
declare function useDebounceState(options?: UseDebounceStateOptions): UseDebounceStateResult;
|
|
48
|
+
|
|
35
49
|
declare function debounce<TArgs extends any[]>(fn: (...args: TArgs) => void, wait: number): {
|
|
36
50
|
(...args: TArgs): void;
|
|
37
51
|
flush: (...args: TArgs) => void;
|
|
@@ -39,4 +53,7 @@ declare function debounce<TArgs extends any[]>(fn: (...args: TArgs) => void, wai
|
|
|
39
53
|
pending: () => boolean;
|
|
40
54
|
};
|
|
41
55
|
|
|
42
|
-
|
|
56
|
+
declare const encodeString: (value: string) => string;
|
|
57
|
+
declare const decodeString: (value: string, fallback?: string) => string;
|
|
58
|
+
|
|
59
|
+
export { type CreateErrorParams, ElementorError, type ElementorErrorOptions, type UseDebounceStateOptions, type UseDebounceStateResult, createError, debounce, decodeString, encodeString, ensureError, useDebounceState };
|
package/dist/index.js
CHANGED
|
@@ -23,7 +23,10 @@ __export(index_exports, {
|
|
|
23
23
|
ElementorError: () => ElementorError,
|
|
24
24
|
createError: () => createError,
|
|
25
25
|
debounce: () => debounce,
|
|
26
|
-
|
|
26
|
+
decodeString: () => decodeString,
|
|
27
|
+
encodeString: () => encodeString,
|
|
28
|
+
ensureError: () => ensureError,
|
|
29
|
+
useDebounceState: () => useDebounceState
|
|
27
30
|
});
|
|
28
31
|
module.exports = __toCommonJS(index_exports);
|
|
29
32
|
|
|
@@ -63,6 +66,9 @@ var ensureError = (error) => {
|
|
|
63
66
|
return new Error(`Unexpected non-error thrown: ${message}`, { cause });
|
|
64
67
|
};
|
|
65
68
|
|
|
69
|
+
// src/use-debounce-state.ts
|
|
70
|
+
var import_react = require("react");
|
|
71
|
+
|
|
66
72
|
// src/debounce.ts
|
|
67
73
|
function debounce(fn, wait) {
|
|
68
74
|
let timer = null;
|
|
@@ -90,11 +96,59 @@ function debounce(fn, wait) {
|
|
|
90
96
|
run.pending = pending;
|
|
91
97
|
return run;
|
|
92
98
|
}
|
|
99
|
+
|
|
100
|
+
// src/use-debounce-state.ts
|
|
101
|
+
function useDebounceState(options = {}) {
|
|
102
|
+
const { delay = 300, initialValue = "" } = options;
|
|
103
|
+
const [debouncedValue, setDebouncedValue] = (0, import_react.useState)(initialValue);
|
|
104
|
+
const [inputValue, setInputValue] = (0, import_react.useState)(initialValue);
|
|
105
|
+
const runRef = (0, import_react.useRef)(null);
|
|
106
|
+
(0, import_react.useEffect)(() => {
|
|
107
|
+
return () => {
|
|
108
|
+
runRef.current?.cancel?.();
|
|
109
|
+
};
|
|
110
|
+
}, []);
|
|
111
|
+
const debouncedSetValue = (0, import_react.useCallback)(
|
|
112
|
+
(val) => {
|
|
113
|
+
runRef.current?.cancel?.();
|
|
114
|
+
runRef.current = debounce(() => {
|
|
115
|
+
setDebouncedValue(val);
|
|
116
|
+
}, delay);
|
|
117
|
+
runRef.current();
|
|
118
|
+
},
|
|
119
|
+
[delay]
|
|
120
|
+
);
|
|
121
|
+
const handleChange = (val) => {
|
|
122
|
+
setInputValue(val);
|
|
123
|
+
debouncedSetValue(val);
|
|
124
|
+
};
|
|
125
|
+
return {
|
|
126
|
+
debouncedValue,
|
|
127
|
+
inputValue,
|
|
128
|
+
handleChange,
|
|
129
|
+
setInputValue
|
|
130
|
+
};
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
// src/encoding.ts
|
|
134
|
+
var encodeString = (value) => {
|
|
135
|
+
return btoa(value);
|
|
136
|
+
};
|
|
137
|
+
var decodeString = (value, fallback = "") => {
|
|
138
|
+
try {
|
|
139
|
+
return atob(value);
|
|
140
|
+
} catch {
|
|
141
|
+
return fallback;
|
|
142
|
+
}
|
|
143
|
+
};
|
|
93
144
|
// Annotate the CommonJS export names for ESM import in node:
|
|
94
145
|
0 && (module.exports = {
|
|
95
146
|
ElementorError,
|
|
96
147
|
createError,
|
|
97
148
|
debounce,
|
|
98
|
-
|
|
149
|
+
decodeString,
|
|
150
|
+
encodeString,
|
|
151
|
+
ensureError,
|
|
152
|
+
useDebounceState
|
|
99
153
|
});
|
|
100
154
|
//# sourceMappingURL=index.js.map
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/index.ts","../src/errors/elementor-error.ts","../src/errors/create-error.ts","../src/errors/ensure-error.ts","../src/debounce.ts"],"sourcesContent":["export { ElementorError, createError, ensureError } from './errors';\nexport type { ElementorErrorOptions, CreateErrorParams } from './errors';\
|
|
1
|
+
{"version":3,"sources":["../src/index.ts","../src/errors/elementor-error.ts","../src/errors/create-error.ts","../src/errors/ensure-error.ts","../src/use-debounce-state.ts","../src/debounce.ts","../src/encoding.ts"],"sourcesContent":["export { ElementorError, createError, ensureError } from './errors';\nexport type { ElementorErrorOptions, CreateErrorParams } from './errors';\nexport { useDebounceState, type UseDebounceStateOptions, type UseDebounceStateResult } from './use-debounce-state';\nexport { debounce } from './debounce';\nexport { encodeString, decodeString } from './encoding';\n","export type ElementorErrorOptions = {\n\tcause?: Error[ 'cause' ];\n\tcontext?: Record< string, unknown > | null;\n\tcode: string;\n};\n\nexport class ElementorError extends Error {\n\treadonly context: ElementorErrorOptions[ 'context' ];\n\treadonly code: ElementorErrorOptions[ 'code' ];\n\n\tconstructor( message: string, { code, context = null, cause = null }: ElementorErrorOptions ) {\n\t\tsuper( message, { cause } );\n\t\tthis.context = context;\n\t\tthis.code = code;\n\t}\n}\n","import { ElementorError, type ElementorErrorOptions } from './elementor-error';\n\nexport type CreateErrorParams = {\n\tcode: ElementorErrorOptions[ 'code' ];\n\tmessage: string;\n};\n\nexport const createError = < T extends ElementorErrorOptions[ 'context' ] >( { code, message }: CreateErrorParams ) => {\n\treturn class extends ElementorError {\n\t\tconstructor( { cause, context }: { cause?: ElementorErrorOptions[ 'cause' ]; context?: T } = {} ) {\n\t\t\tsuper( message, { cause, code, context } );\n\t\t}\n\t};\n};\n","export const ensureError = ( error: unknown ) => {\n\tif ( error instanceof Error ) {\n\t\treturn error;\n\t}\n\n\tlet message: string;\n\tlet cause: unknown = null;\n\n\ttry {\n\t\tmessage = JSON.stringify( error );\n\t} catch ( e ) {\n\t\tcause = e;\n\t\tmessage = 'Unable to stringify the thrown value';\n\t}\n\treturn new Error( `Unexpected non-error thrown: ${ message }`, { cause } );\n};\n","import { useCallback, useEffect, useRef, useState } from 'react';\nimport type * as React from 'react';\n\nimport { debounce } from './debounce';\n\nexport type UseDebounceStateOptions = {\n\tdelay?: number;\n\tinitialValue?: string;\n};\n\nexport type UseDebounceStateResult = {\n\tdebouncedValue: string;\n\tinputValue: string;\n\thandleChange: ( val: string ) => void;\n\tsetInputValue: React.Dispatch< React.SetStateAction< string > >;\n};\n\nexport function useDebounceState( options: UseDebounceStateOptions = {} ): UseDebounceStateResult {\n\tconst { delay = 300, initialValue = '' } = options;\n\n\tconst [ debouncedValue, setDebouncedValue ] = useState( initialValue );\n\tconst [ inputValue, setInputValue ] = useState( initialValue );\n\n\tconst runRef = useRef< ReturnType< typeof debounce > | null >( null );\n\n\tuseEffect( () => {\n\t\treturn () => {\n\t\t\trunRef.current?.cancel?.();\n\t\t};\n\t}, [] );\n\n\tconst debouncedSetValue = useCallback(\n\t\t( val: string ) => {\n\t\t\trunRef.current?.cancel?.();\n\t\t\trunRef.current = debounce( () => {\n\t\t\t\tsetDebouncedValue( val );\n\t\t\t}, delay );\n\t\t\trunRef.current();\n\t\t},\n\t\t[ delay ]\n\t);\n\n\tconst handleChange = ( val: string ) => {\n\t\tsetInputValue( val );\n\t\tdebouncedSetValue( val );\n\t};\n\n\treturn {\n\t\tdebouncedValue,\n\t\tinputValue,\n\t\thandleChange,\n\t\tsetInputValue,\n\t};\n}\n","// eslint-disable-next-line @typescript-eslint/no-explicit-any\nexport function debounce< TArgs extends any[] >( fn: ( ...args: TArgs ) => void, wait: number ) {\n\tlet timer: ReturnType< typeof setTimeout > | null = null;\n\n\tconst cancel = () => {\n\t\tif ( ! timer ) {\n\t\t\treturn;\n\t\t}\n\n\t\tclearTimeout( timer );\n\t\ttimer = null;\n\t};\n\n\tconst flush = ( ...args: TArgs ) => {\n\t\tcancel();\n\n\t\tfn( ...args );\n\t};\n\n\tconst run = ( ...args: TArgs ) => {\n\t\tcancel();\n\n\t\ttimer = setTimeout( () => {\n\t\t\tfn( ...args );\n\n\t\t\ttimer = null;\n\t\t}, wait );\n\t};\n\n\tconst pending = () => !! timer;\n\n\trun.flush = flush;\n\trun.cancel = cancel;\n\trun.pending = pending;\n\n\treturn run;\n}\n","export const encodeString = ( value: string ): string => {\n\treturn btoa( value );\n};\n\nexport const decodeString = ( value: string, fallback: string = '' ): string => {\n\ttry {\n\t\treturn atob( value );\n\t} catch {\n\t\treturn fallback;\n\t}\n};\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;;;ACMO,IAAM,iBAAN,cAA6B,MAAM;AAAA,EAChC;AAAA,EACA;AAAA,EAET,YAAa,SAAiB,EAAE,MAAM,UAAU,MAAM,QAAQ,KAAK,GAA2B;AAC7F,UAAO,SAAS,EAAE,MAAM,CAAE;AAC1B,SAAK,UAAU;AACf,SAAK,OAAO;AAAA,EACb;AACD;;;ACRO,IAAM,cAAc,CAAkD,EAAE,MAAM,QAAQ,MAA0B;AACtH,SAAO,cAAc,eAAe;AAAA,IACnC,YAAa,EAAE,OAAO,QAAQ,IAA+D,CAAC,GAAI;AACjG,YAAO,SAAS,EAAE,OAAO,MAAM,QAAQ,CAAE;AAAA,IAC1C;AAAA,EACD;AACD;;;ACbO,IAAM,cAAc,CAAE,UAAoB;AAChD,MAAK,iBAAiB,OAAQ;AAC7B,WAAO;AAAA,EACR;AAEA,MAAI;AACJ,MAAI,QAAiB;AAErB,MAAI;AACH,cAAU,KAAK,UAAW,KAAM;AAAA,EACjC,SAAU,GAAI;AACb,YAAQ;AACR,cAAU;AAAA,EACX;AACA,SAAO,IAAI,MAAO,gCAAiC,OAAQ,IAAI,EAAE,MAAM,CAAE;AAC1E;;;ACfA,mBAAyD;;;ACClD,SAAS,SAAiC,IAAgC,MAAe;AAC/F,MAAI,QAAgD;AAEpD,QAAM,SAAS,MAAM;AACpB,QAAK,CAAE,OAAQ;AACd;AAAA,IACD;AAEA,iBAAc,KAAM;AACpB,YAAQ;AAAA,EACT;AAEA,QAAM,QAAQ,IAAK,SAAiB;AACnC,WAAO;AAEP,OAAI,GAAG,IAAK;AAAA,EACb;AAEA,QAAM,MAAM,IAAK,SAAiB;AACjC,WAAO;AAEP,YAAQ,WAAY,MAAM;AACzB,SAAI,GAAG,IAAK;AAEZ,cAAQ;AAAA,IACT,GAAG,IAAK;AAAA,EACT;AAEA,QAAM,UAAU,MAAM,CAAC,CAAE;AAEzB,MAAI,QAAQ;AACZ,MAAI,SAAS;AACb,MAAI,UAAU;AAEd,SAAO;AACR;;;ADnBO,SAAS,iBAAkB,UAAmC,CAAC,GAA4B;AACjG,QAAM,EAAE,QAAQ,KAAK,eAAe,GAAG,IAAI;AAE3C,QAAM,CAAE,gBAAgB,iBAAkB,QAAI,uBAAU,YAAa;AACrE,QAAM,CAAE,YAAY,aAAc,QAAI,uBAAU,YAAa;AAE7D,QAAM,aAAS,qBAAgD,IAAK;AAEpE,8BAAW,MAAM;AAChB,WAAO,MAAM;AACZ,aAAO,SAAS,SAAS;AAAA,IAC1B;AAAA,EACD,GAAG,CAAC,CAAE;AAEN,QAAM,wBAAoB;AAAA,IACzB,CAAE,QAAiB;AAClB,aAAO,SAAS,SAAS;AACzB,aAAO,UAAU,SAAU,MAAM;AAChC,0BAAmB,GAAI;AAAA,MACxB,GAAG,KAAM;AACT,aAAO,QAAQ;AAAA,IAChB;AAAA,IACA,CAAE,KAAM;AAAA,EACT;AAEA,QAAM,eAAe,CAAE,QAAiB;AACvC,kBAAe,GAAI;AACnB,sBAAmB,GAAI;AAAA,EACxB;AAEA,SAAO;AAAA,IACN;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,EACD;AACD;;;AErDO,IAAM,eAAe,CAAE,UAA2B;AACxD,SAAO,KAAM,KAAM;AACpB;AAEO,IAAM,eAAe,CAAE,OAAe,WAAmB,OAAgB;AAC/E,MAAI;AACH,WAAO,KAAM,KAAM;AAAA,EACpB,QAAQ;AACP,WAAO;AAAA,EACR;AACD;","names":[]}
|
package/dist/index.mjs
CHANGED
|
@@ -34,6 +34,9 @@ var ensureError = (error) => {
|
|
|
34
34
|
return new Error(`Unexpected non-error thrown: ${message}`, { cause });
|
|
35
35
|
};
|
|
36
36
|
|
|
37
|
+
// src/use-debounce-state.ts
|
|
38
|
+
import { useCallback, useEffect, useRef, useState } from "react";
|
|
39
|
+
|
|
37
40
|
// src/debounce.ts
|
|
38
41
|
function debounce(fn, wait) {
|
|
39
42
|
let timer = null;
|
|
@@ -61,10 +64,58 @@ function debounce(fn, wait) {
|
|
|
61
64
|
run.pending = pending;
|
|
62
65
|
return run;
|
|
63
66
|
}
|
|
67
|
+
|
|
68
|
+
// src/use-debounce-state.ts
|
|
69
|
+
function useDebounceState(options = {}) {
|
|
70
|
+
const { delay = 300, initialValue = "" } = options;
|
|
71
|
+
const [debouncedValue, setDebouncedValue] = useState(initialValue);
|
|
72
|
+
const [inputValue, setInputValue] = useState(initialValue);
|
|
73
|
+
const runRef = useRef(null);
|
|
74
|
+
useEffect(() => {
|
|
75
|
+
return () => {
|
|
76
|
+
runRef.current?.cancel?.();
|
|
77
|
+
};
|
|
78
|
+
}, []);
|
|
79
|
+
const debouncedSetValue = useCallback(
|
|
80
|
+
(val) => {
|
|
81
|
+
runRef.current?.cancel?.();
|
|
82
|
+
runRef.current = debounce(() => {
|
|
83
|
+
setDebouncedValue(val);
|
|
84
|
+
}, delay);
|
|
85
|
+
runRef.current();
|
|
86
|
+
},
|
|
87
|
+
[delay]
|
|
88
|
+
);
|
|
89
|
+
const handleChange = (val) => {
|
|
90
|
+
setInputValue(val);
|
|
91
|
+
debouncedSetValue(val);
|
|
92
|
+
};
|
|
93
|
+
return {
|
|
94
|
+
debouncedValue,
|
|
95
|
+
inputValue,
|
|
96
|
+
handleChange,
|
|
97
|
+
setInputValue
|
|
98
|
+
};
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
// src/encoding.ts
|
|
102
|
+
var encodeString = (value) => {
|
|
103
|
+
return btoa(value);
|
|
104
|
+
};
|
|
105
|
+
var decodeString = (value, fallback = "") => {
|
|
106
|
+
try {
|
|
107
|
+
return atob(value);
|
|
108
|
+
} catch {
|
|
109
|
+
return fallback;
|
|
110
|
+
}
|
|
111
|
+
};
|
|
64
112
|
export {
|
|
65
113
|
ElementorError,
|
|
66
114
|
createError,
|
|
67
115
|
debounce,
|
|
68
|
-
|
|
116
|
+
decodeString,
|
|
117
|
+
encodeString,
|
|
118
|
+
ensureError,
|
|
119
|
+
useDebounceState
|
|
69
120
|
};
|
|
70
121
|
//# sourceMappingURL=index.mjs.map
|
package/dist/index.mjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/errors/elementor-error.ts","../src/errors/create-error.ts","../src/errors/ensure-error.ts","../src/debounce.ts"],"sourcesContent":["export type ElementorErrorOptions = {\n\tcause?: Error[ 'cause' ];\n\tcontext?: Record< string, unknown > | null;\n\tcode: string;\n};\n\nexport class ElementorError extends Error {\n\treadonly context: ElementorErrorOptions[ 'context' ];\n\treadonly code: ElementorErrorOptions[ 'code' ];\n\n\tconstructor( message: string, { code, context = null, cause = null }: ElementorErrorOptions ) {\n\t\tsuper( message, { cause } );\n\t\tthis.context = context;\n\t\tthis.code = code;\n\t}\n}\n","import { ElementorError, type ElementorErrorOptions } from './elementor-error';\n\nexport type CreateErrorParams = {\n\tcode: ElementorErrorOptions[ 'code' ];\n\tmessage: string;\n};\n\nexport const createError = < T extends ElementorErrorOptions[ 'context' ] >( { code, message }: CreateErrorParams ) => {\n\treturn class extends ElementorError {\n\t\tconstructor( { cause, context }: { cause?: ElementorErrorOptions[ 'cause' ]; context?: T } = {} ) {\n\t\t\tsuper( message, { cause, code, context } );\n\t\t}\n\t};\n};\n","export const ensureError = ( error: unknown ) => {\n\tif ( error instanceof Error ) {\n\t\treturn error;\n\t}\n\n\tlet message: string;\n\tlet cause: unknown = null;\n\n\ttry {\n\t\tmessage = JSON.stringify( error );\n\t} catch ( e ) {\n\t\tcause = e;\n\t\tmessage = 'Unable to stringify the thrown value';\n\t}\n\treturn new Error( `Unexpected non-error thrown: ${ message }`, { cause } );\n};\n","// eslint-disable-next-line @typescript-eslint/no-explicit-any\nexport function debounce< TArgs extends any[] >( fn: ( ...args: TArgs ) => void, wait: number ) {\n\tlet timer: ReturnType< typeof setTimeout > | null = null;\n\n\tconst cancel = () => {\n\t\tif ( ! timer ) {\n\t\t\treturn;\n\t\t}\n\n\t\tclearTimeout( timer );\n\t\ttimer = null;\n\t};\n\n\tconst flush = ( ...args: TArgs ) => {\n\t\tcancel();\n\n\t\tfn( ...args );\n\t};\n\n\tconst run = ( ...args: TArgs ) => {\n\t\tcancel();\n\n\t\ttimer = setTimeout( () => {\n\t\t\tfn( ...args );\n\n\t\t\ttimer = null;\n\t\t}, wait );\n\t};\n\n\tconst pending = () => !! timer;\n\n\trun.flush = flush;\n\trun.cancel = cancel;\n\trun.pending = pending;\n\n\treturn run;\n}\n"],"mappings":";AAMO,IAAM,iBAAN,cAA6B,MAAM;AAAA,EAChC;AAAA,EACA;AAAA,EAET,YAAa,SAAiB,EAAE,MAAM,UAAU,MAAM,QAAQ,KAAK,GAA2B;AAC7F,UAAO,SAAS,EAAE,MAAM,CAAE;AAC1B,SAAK,UAAU;AACf,SAAK,OAAO;AAAA,EACb;AACD;;;ACRO,IAAM,cAAc,CAAkD,EAAE,MAAM,QAAQ,MAA0B;AACtH,SAAO,cAAc,eAAe;AAAA,IACnC,YAAa,EAAE,OAAO,QAAQ,IAA+D,CAAC,GAAI;AACjG,YAAO,SAAS,EAAE,OAAO,MAAM,QAAQ,CAAE;AAAA,IAC1C;AAAA,EACD;AACD;;;ACbO,IAAM,cAAc,CAAE,UAAoB;AAChD,MAAK,iBAAiB,OAAQ;AAC7B,WAAO;AAAA,EACR;AAEA,MAAI;AACJ,MAAI,QAAiB;AAErB,MAAI;AACH,cAAU,KAAK,UAAW,KAAM;AAAA,EACjC,SAAU,GAAI;AACb,YAAQ;AACR,cAAU;AAAA,EACX;AACA,SAAO,IAAI,MAAO,gCAAiC,OAAQ,IAAI,EAAE,MAAM,CAAE;AAC1E;;;
|
|
1
|
+
{"version":3,"sources":["../src/errors/elementor-error.ts","../src/errors/create-error.ts","../src/errors/ensure-error.ts","../src/use-debounce-state.ts","../src/debounce.ts","../src/encoding.ts"],"sourcesContent":["export type ElementorErrorOptions = {\n\tcause?: Error[ 'cause' ];\n\tcontext?: Record< string, unknown > | null;\n\tcode: string;\n};\n\nexport class ElementorError extends Error {\n\treadonly context: ElementorErrorOptions[ 'context' ];\n\treadonly code: ElementorErrorOptions[ 'code' ];\n\n\tconstructor( message: string, { code, context = null, cause = null }: ElementorErrorOptions ) {\n\t\tsuper( message, { cause } );\n\t\tthis.context = context;\n\t\tthis.code = code;\n\t}\n}\n","import { ElementorError, type ElementorErrorOptions } from './elementor-error';\n\nexport type CreateErrorParams = {\n\tcode: ElementorErrorOptions[ 'code' ];\n\tmessage: string;\n};\n\nexport const createError = < T extends ElementorErrorOptions[ 'context' ] >( { code, message }: CreateErrorParams ) => {\n\treturn class extends ElementorError {\n\t\tconstructor( { cause, context }: { cause?: ElementorErrorOptions[ 'cause' ]; context?: T } = {} ) {\n\t\t\tsuper( message, { cause, code, context } );\n\t\t}\n\t};\n};\n","export const ensureError = ( error: unknown ) => {\n\tif ( error instanceof Error ) {\n\t\treturn error;\n\t}\n\n\tlet message: string;\n\tlet cause: unknown = null;\n\n\ttry {\n\t\tmessage = JSON.stringify( error );\n\t} catch ( e ) {\n\t\tcause = e;\n\t\tmessage = 'Unable to stringify the thrown value';\n\t}\n\treturn new Error( `Unexpected non-error thrown: ${ message }`, { cause } );\n};\n","import { useCallback, useEffect, useRef, useState } from 'react';\nimport type * as React from 'react';\n\nimport { debounce } from './debounce';\n\nexport type UseDebounceStateOptions = {\n\tdelay?: number;\n\tinitialValue?: string;\n};\n\nexport type UseDebounceStateResult = {\n\tdebouncedValue: string;\n\tinputValue: string;\n\thandleChange: ( val: string ) => void;\n\tsetInputValue: React.Dispatch< React.SetStateAction< string > >;\n};\n\nexport function useDebounceState( options: UseDebounceStateOptions = {} ): UseDebounceStateResult {\n\tconst { delay = 300, initialValue = '' } = options;\n\n\tconst [ debouncedValue, setDebouncedValue ] = useState( initialValue );\n\tconst [ inputValue, setInputValue ] = useState( initialValue );\n\n\tconst runRef = useRef< ReturnType< typeof debounce > | null >( null );\n\n\tuseEffect( () => {\n\t\treturn () => {\n\t\t\trunRef.current?.cancel?.();\n\t\t};\n\t}, [] );\n\n\tconst debouncedSetValue = useCallback(\n\t\t( val: string ) => {\n\t\t\trunRef.current?.cancel?.();\n\t\t\trunRef.current = debounce( () => {\n\t\t\t\tsetDebouncedValue( val );\n\t\t\t}, delay );\n\t\t\trunRef.current();\n\t\t},\n\t\t[ delay ]\n\t);\n\n\tconst handleChange = ( val: string ) => {\n\t\tsetInputValue( val );\n\t\tdebouncedSetValue( val );\n\t};\n\n\treturn {\n\t\tdebouncedValue,\n\t\tinputValue,\n\t\thandleChange,\n\t\tsetInputValue,\n\t};\n}\n","// eslint-disable-next-line @typescript-eslint/no-explicit-any\nexport function debounce< TArgs extends any[] >( fn: ( ...args: TArgs ) => void, wait: number ) {\n\tlet timer: ReturnType< typeof setTimeout > | null = null;\n\n\tconst cancel = () => {\n\t\tif ( ! timer ) {\n\t\t\treturn;\n\t\t}\n\n\t\tclearTimeout( timer );\n\t\ttimer = null;\n\t};\n\n\tconst flush = ( ...args: TArgs ) => {\n\t\tcancel();\n\n\t\tfn( ...args );\n\t};\n\n\tconst run = ( ...args: TArgs ) => {\n\t\tcancel();\n\n\t\ttimer = setTimeout( () => {\n\t\t\tfn( ...args );\n\n\t\t\ttimer = null;\n\t\t}, wait );\n\t};\n\n\tconst pending = () => !! timer;\n\n\trun.flush = flush;\n\trun.cancel = cancel;\n\trun.pending = pending;\n\n\treturn run;\n}\n","export const encodeString = ( value: string ): string => {\n\treturn btoa( value );\n};\n\nexport const decodeString = ( value: string, fallback: string = '' ): string => {\n\ttry {\n\t\treturn atob( value );\n\t} catch {\n\t\treturn fallback;\n\t}\n};\n"],"mappings":";AAMO,IAAM,iBAAN,cAA6B,MAAM;AAAA,EAChC;AAAA,EACA;AAAA,EAET,YAAa,SAAiB,EAAE,MAAM,UAAU,MAAM,QAAQ,KAAK,GAA2B;AAC7F,UAAO,SAAS,EAAE,MAAM,CAAE;AAC1B,SAAK,UAAU;AACf,SAAK,OAAO;AAAA,EACb;AACD;;;ACRO,IAAM,cAAc,CAAkD,EAAE,MAAM,QAAQ,MAA0B;AACtH,SAAO,cAAc,eAAe;AAAA,IACnC,YAAa,EAAE,OAAO,QAAQ,IAA+D,CAAC,GAAI;AACjG,YAAO,SAAS,EAAE,OAAO,MAAM,QAAQ,CAAE;AAAA,IAC1C;AAAA,EACD;AACD;;;ACbO,IAAM,cAAc,CAAE,UAAoB;AAChD,MAAK,iBAAiB,OAAQ;AAC7B,WAAO;AAAA,EACR;AAEA,MAAI;AACJ,MAAI,QAAiB;AAErB,MAAI;AACH,cAAU,KAAK,UAAW,KAAM;AAAA,EACjC,SAAU,GAAI;AACb,YAAQ;AACR,cAAU;AAAA,EACX;AACA,SAAO,IAAI,MAAO,gCAAiC,OAAQ,IAAI,EAAE,MAAM,CAAE;AAC1E;;;ACfA,SAAS,aAAa,WAAW,QAAQ,gBAAgB;;;ACClD,SAAS,SAAiC,IAAgC,MAAe;AAC/F,MAAI,QAAgD;AAEpD,QAAM,SAAS,MAAM;AACpB,QAAK,CAAE,OAAQ;AACd;AAAA,IACD;AAEA,iBAAc,KAAM;AACpB,YAAQ;AAAA,EACT;AAEA,QAAM,QAAQ,IAAK,SAAiB;AACnC,WAAO;AAEP,OAAI,GAAG,IAAK;AAAA,EACb;AAEA,QAAM,MAAM,IAAK,SAAiB;AACjC,WAAO;AAEP,YAAQ,WAAY,MAAM;AACzB,SAAI,GAAG,IAAK;AAEZ,cAAQ;AAAA,IACT,GAAG,IAAK;AAAA,EACT;AAEA,QAAM,UAAU,MAAM,CAAC,CAAE;AAEzB,MAAI,QAAQ;AACZ,MAAI,SAAS;AACb,MAAI,UAAU;AAEd,SAAO;AACR;;;ADnBO,SAAS,iBAAkB,UAAmC,CAAC,GAA4B;AACjG,QAAM,EAAE,QAAQ,KAAK,eAAe,GAAG,IAAI;AAE3C,QAAM,CAAE,gBAAgB,iBAAkB,IAAI,SAAU,YAAa;AACrE,QAAM,CAAE,YAAY,aAAc,IAAI,SAAU,YAAa;AAE7D,QAAM,SAAS,OAAgD,IAAK;AAEpE,YAAW,MAAM;AAChB,WAAO,MAAM;AACZ,aAAO,SAAS,SAAS;AAAA,IAC1B;AAAA,EACD,GAAG,CAAC,CAAE;AAEN,QAAM,oBAAoB;AAAA,IACzB,CAAE,QAAiB;AAClB,aAAO,SAAS,SAAS;AACzB,aAAO,UAAU,SAAU,MAAM;AAChC,0BAAmB,GAAI;AAAA,MACxB,GAAG,KAAM;AACT,aAAO,QAAQ;AAAA,IAChB;AAAA,IACA,CAAE,KAAM;AAAA,EACT;AAEA,QAAM,eAAe,CAAE,QAAiB;AACvC,kBAAe,GAAI;AACnB,sBAAmB,GAAI;AAAA,EACxB;AAEA,SAAO;AAAA,IACN;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,EACD;AACD;;;AErDO,IAAM,eAAe,CAAE,UAA2B;AACxD,SAAO,KAAM,KAAM;AACpB;AAEO,IAAM,eAAe,CAAE,OAAe,WAAmB,OAAgB;AAC/E,MAAI;AACH,WAAO,KAAM,KAAM;AAAA,EACpB,QAAQ;AACP,WAAO;AAAA,EACR;AACD;","names":[]}
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@elementor/utils",
|
|
3
3
|
"description": "This package contains utility functions that are being used across the Elementor packages",
|
|
4
|
-
"version": "
|
|
4
|
+
"version": "3.32.0-20",
|
|
5
5
|
"private": false,
|
|
6
6
|
"author": "Elementor Team",
|
|
7
7
|
"homepage": "https://elementor.com/",
|
|
@@ -19,11 +19,11 @@
|
|
|
19
19
|
},
|
|
20
20
|
"repository": {
|
|
21
21
|
"type": "git",
|
|
22
|
-
"url": "git+https://github.com/elementor/elementor
|
|
22
|
+
"url": "git+https://github.com/elementor/elementor.git",
|
|
23
23
|
"directory": "packages/libs/utils"
|
|
24
24
|
},
|
|
25
25
|
"bugs": {
|
|
26
|
-
"url": "https://github.com/elementor/elementor
|
|
26
|
+
"url": "https://github.com/elementor/elementor/issues"
|
|
27
27
|
},
|
|
28
28
|
"publishConfig": {
|
|
29
29
|
"access": "public"
|
|
@@ -39,6 +39,9 @@
|
|
|
39
39
|
"build": "tsup --config=../../tsup.build.ts",
|
|
40
40
|
"dev": "tsup --config=../../tsup.dev.ts"
|
|
41
41
|
},
|
|
42
|
+
"peerDependencies": {
|
|
43
|
+
"react": "^18.3.1"
|
|
44
|
+
},
|
|
42
45
|
"devDependencies": {
|
|
43
46
|
"tsup": "^8.3.5"
|
|
44
47
|
}
|
package/src/encoding.ts
ADDED
package/src/index.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
export { ElementorError, createError, ensureError } from './errors';
|
|
2
2
|
export type { ElementorErrorOptions, CreateErrorParams } from './errors';
|
|
3
|
-
|
|
3
|
+
export { useDebounceState, type UseDebounceStateOptions, type UseDebounceStateResult } from './use-debounce-state';
|
|
4
4
|
export { debounce } from './debounce';
|
|
5
|
+
export { encodeString, decodeString } from './encoding';
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
import { useCallback, useEffect, useRef, useState } from 'react';
|
|
2
|
+
import type * as React from 'react';
|
|
3
|
+
|
|
4
|
+
import { debounce } from './debounce';
|
|
5
|
+
|
|
6
|
+
export type UseDebounceStateOptions = {
|
|
7
|
+
delay?: number;
|
|
8
|
+
initialValue?: string;
|
|
9
|
+
};
|
|
10
|
+
|
|
11
|
+
export type UseDebounceStateResult = {
|
|
12
|
+
debouncedValue: string;
|
|
13
|
+
inputValue: string;
|
|
14
|
+
handleChange: ( val: string ) => void;
|
|
15
|
+
setInputValue: React.Dispatch< React.SetStateAction< string > >;
|
|
16
|
+
};
|
|
17
|
+
|
|
18
|
+
export function useDebounceState( options: UseDebounceStateOptions = {} ): UseDebounceStateResult {
|
|
19
|
+
const { delay = 300, initialValue = '' } = options;
|
|
20
|
+
|
|
21
|
+
const [ debouncedValue, setDebouncedValue ] = useState( initialValue );
|
|
22
|
+
const [ inputValue, setInputValue ] = useState( initialValue );
|
|
23
|
+
|
|
24
|
+
const runRef = useRef< ReturnType< typeof debounce > | null >( null );
|
|
25
|
+
|
|
26
|
+
useEffect( () => {
|
|
27
|
+
return () => {
|
|
28
|
+
runRef.current?.cancel?.();
|
|
29
|
+
};
|
|
30
|
+
}, [] );
|
|
31
|
+
|
|
32
|
+
const debouncedSetValue = useCallback(
|
|
33
|
+
( val: string ) => {
|
|
34
|
+
runRef.current?.cancel?.();
|
|
35
|
+
runRef.current = debounce( () => {
|
|
36
|
+
setDebouncedValue( val );
|
|
37
|
+
}, delay );
|
|
38
|
+
runRef.current();
|
|
39
|
+
},
|
|
40
|
+
[ delay ]
|
|
41
|
+
);
|
|
42
|
+
|
|
43
|
+
const handleChange = ( val: string ) => {
|
|
44
|
+
setInputValue( val );
|
|
45
|
+
debouncedSetValue( val );
|
|
46
|
+
};
|
|
47
|
+
|
|
48
|
+
return {
|
|
49
|
+
debouncedValue,
|
|
50
|
+
inputValue,
|
|
51
|
+
handleChange,
|
|
52
|
+
setInputValue,
|
|
53
|
+
};
|
|
54
|
+
}
|