@marimo-team/frontend 0.25.3-dev21 → 0.25.3-dev23
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/package.json +1 -1
- package/src/utils/once.ts +19 -19
package/package.json
CHANGED
package/src/utils/once.ts
CHANGED
|
@@ -2,36 +2,36 @@
|
|
|
2
2
|
|
|
3
3
|
import { arrayShallowEquals } from "./arrays";
|
|
4
4
|
|
|
5
|
-
|
|
6
|
-
export function once<
|
|
7
|
-
|
|
5
|
+
export function once<F extends (...args: never[]) => unknown>(fn: F): F;
|
|
6
|
+
export function once<Args extends unknown[], Result, This>(
|
|
7
|
+
fn: (this: This, ...args: Args) => Result,
|
|
8
|
+
): (this: This, ...args: Args) => Result {
|
|
9
|
+
let result: Result;
|
|
8
10
|
let called = false;
|
|
9
|
-
return function (
|
|
10
|
-
this: ThisParameterType<T>,
|
|
11
|
-
...args: Parameters<T>
|
|
12
|
-
): ReturnType<T> {
|
|
11
|
+
return function (this: This, ...args: Args): Result {
|
|
13
12
|
if (!called) {
|
|
14
13
|
called = true;
|
|
15
|
-
result = fn.apply(this, args)
|
|
14
|
+
result = fn.apply(this, args);
|
|
16
15
|
}
|
|
17
16
|
return result;
|
|
18
|
-
}
|
|
17
|
+
};
|
|
19
18
|
}
|
|
20
19
|
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
20
|
+
export function memoizeLastValue<F extends (...args: never[]) => unknown>(
|
|
21
|
+
fn: F,
|
|
22
|
+
): F;
|
|
23
|
+
export function memoizeLastValue<Args extends unknown[], Result, This>(
|
|
24
|
+
fn: (this: This, ...args: Args) => Result,
|
|
25
|
+
): (this: This, ...args: Args) => Result {
|
|
26
|
+
let result: Result;
|
|
27
|
+
let lastArgs: Args | undefined;
|
|
25
28
|
let lastError: unknown;
|
|
26
29
|
let hasError = false;
|
|
27
30
|
|
|
28
|
-
return function (
|
|
29
|
-
this: ThisParameterType<T>,
|
|
30
|
-
...args: Parameters<T>
|
|
31
|
-
): ReturnType<T> {
|
|
31
|
+
return function (this: This, ...args: Args): Result {
|
|
32
32
|
if (lastArgs === undefined || !arrayShallowEquals(args, lastArgs)) {
|
|
33
33
|
try {
|
|
34
|
-
result = fn.apply(this, args)
|
|
34
|
+
result = fn.apply(this, args);
|
|
35
35
|
hasError = false;
|
|
36
36
|
lastError = undefined;
|
|
37
37
|
} catch (error) {
|
|
@@ -45,5 +45,5 @@ export function memoizeLastValue<T extends (...args: any[]) => any>(fn: T): T {
|
|
|
45
45
|
throw lastError;
|
|
46
46
|
}
|
|
47
47
|
return result;
|
|
48
|
-
}
|
|
48
|
+
};
|
|
49
49
|
}
|