Skip to content

Commit d311836

Browse files
committed
fix(hf): add promise memo and unwrapGroup
1 parent 19b09e4 commit d311836

39 files changed

+691
-39
lines changed

docs/en/v1/api/common/addWrappedProperties.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22
outline: [2, 3]
33
description: "The addWrappedProperties() function dynamically adds derived properties to a wrapped value while keeping the original type and extending the new type."
44
prev:
5-
text: "unwrap"
6-
link: "/en/v1/api/common/unwrap"
5+
text: "unwrapGroup"
6+
link: "/en/v1/api/common/unwrapGroup"
77
next:
88
text: "clone"
99
link: "/en/v1/api/common/clone"

docs/en/v1/api/common/index.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,9 @@ Async pause to wait for a certain time.
115115
### [memo](/en/v1/api/common/memo)
116116
Evaluates a function only once and reuses the result (lazy memoization).
117117

118+
### [memoPromise](/en/v1/api/common/memoPromise)
119+
Lazy memoization for functions returning a value or a promise.
120+
118121
### [toJSON](/en/v1/api/common/toJSON)
119122
Prepares a value for JSON serialization.
120123

@@ -154,6 +157,9 @@ Ensures a value is wrapped (idempotent if already wrapped).
154157
### [unwrap](/en/v1/api/common/unwrap)
155158
Retrieves the inner value of a wrapper.
156159

160+
### [unwrapGroup](/en/v1/api/common/unwrapGroup)
161+
Unwraps every value of an object.
162+
157163
### [addWrappedProperties](/en/v1/api/common/addWrappedProperties)
158164
Dynamically adds derived properties to a wrapped value.
159165

docs/en/v1/api/common/memo.md

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,13 @@ prev:
55
text: "sleep"
66
link: "/en/v1/api/common/sleep"
77
next:
8-
text: "toJSON"
9-
link: "/en/v1/api/common/toJSON"
8+
text: "memoPromise"
9+
link: "/en/v1/api/common/memoPromise"
1010
---
1111

1212
# memo
1313

14-
The **`memo()`** function evaluates a function only once then memorizes the result. Subsequent calls return the same value without recalculation.
14+
The **`memo()`** function evaluates a function only once then memorizes the result. The first access to `value` triggers the evaluation, and subsequent accesses return the same value without recalculation.
1515

1616
## Interactive example
1717

@@ -25,13 +25,13 @@ The **`memo()`** function evaluates a function only once then memorizes the resu
2525

2626
```typescript
2727
interface Memoized<
28-
GenericInput extends unknown
28+
GenericValue extends unknown
2929
> {
30-
readonly input: GenericInput;
30+
readonly value: GenericValue;
3131
}
3232

3333
function memo<
34-
GenericOutput extends AnyValue
34+
GenericOutput extends unknown
3535
>(
3636
theFunction: () => GenericOutput
3737
): Memoized<GenericOutput>;
@@ -43,7 +43,7 @@ function memo<
4343

4444
## Return value
4545

46-
A `Memoized` object with the `input` property containing the single result.
46+
A `Memoized` object with the `value` property containing the single result.
4747

4848
## See also
4949

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
---
2+
outline: [2, 3]
3+
description: "The memoPromise() function lazily evaluates a function that returns a value or a promise, then memoizes the resolved result."
4+
prev:
5+
text: "memo"
6+
link: "/en/v1/api/common/memo"
7+
next:
8+
text: "toJSON"
9+
link: "/en/v1/api/common/toJSON"
10+
---
11+
12+
# memoPromise
13+
14+
The **`memoPromise()`** function lazily evaluates a function that returns a value or a promise, then memoizes the resolved result.
15+
16+
## Interactive example
17+
18+
<MonacoTSEditor
19+
src="/examples/v1/api/common/memoPromise/tryout.doc.ts"
20+
majorVersion="v1"
21+
height="292px"
22+
/>
23+
24+
## Syntax
25+
26+
```typescript
27+
interface MemoizedPromise<
28+
GenericValue extends unknown
29+
> {
30+
readonly value: MaybePromise<GenericValue>;
31+
}
32+
33+
function memoPromise<
34+
GenericOutput extends unknown
35+
>(
36+
theFunction: () => MaybePromise<GenericOutput>
37+
): MemoizedPromise<GenericOutput>;
38+
```
39+
40+
## Parameters
41+
42+
- `theFunction` : Function evaluated once on first access, returning a value or a promise.
43+
44+
## Return value
45+
46+
A `MemoizedPromise` object with a lazy `value` getter. The first access returns a promise; once resolved, the property holds the resolved value.
47+
48+
## See also
49+
50+
- [`memo`](/en/v1/api/common/memo) - Lazy memoization for synchronous values
51+
- [`externalPromise`](/en/v1/api/common/externalPromise) - Controllable promise
52+
- [`promiseObject`](/en/v1/api/common/promiseObject) - Resolves objects of promises

docs/en/v1/api/common/toJSON.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22
outline: [2, 3]
33
description: "The toJSON() function prepares a value for JSON serialization while respecting objects' toJSON methods, traversing arrays/tuples, and excluding functions."
44
prev:
5-
text: "memo"
6-
link: "/en/v1/api/common/memo"
5+
text: "memoPromise"
6+
link: "/en/v1/api/common/memoPromise"
77
next:
88
text: "toTransform"
99
link: "/en/v1/api/common/toTransform"

docs/en/v1/api/common/unwrap.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ prev:
55
text: "toWrappedValue"
66
link: "/en/v1/api/common/toWrappedValue"
77
next:
8-
text: "addWrappedProperties"
9-
link: "/en/v1/api/common/addWrappedProperties"
8+
text: "unwrapGroup"
9+
link: "/en/v1/api/common/unwrapGroup"
1010
---
1111

1212
# unwrap
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
---
2+
outline: [2, 3]
3+
description: "The unwrapGroup() function unwraps every value of an object using unwrap(), while keeping the same keys."
4+
prev:
5+
text: "unwrap"
6+
link: "/en/v1/api/common/unwrap"
7+
next:
8+
text: "addWrappedProperties"
9+
link: "/en/v1/api/common/addWrappedProperties"
10+
---
11+
12+
# unwrapGroup
13+
14+
The **`unwrapGroup()`** function unwraps every value of an object using `unwrap()`, while keeping the same keys.
15+
16+
## Interactive example
17+
18+
<MonacoTSEditor
19+
src="/examples/v1/api/common/unwrapGroup/tryout.doc.ts"
20+
majorVersion="v1"
21+
height="292px"
22+
/>
23+
24+
## Syntax
25+
26+
```typescript
27+
function unwrapGroup<
28+
GenericGroup extends Record<string, unknown>
29+
>(
30+
group: GenericGroup
31+
): ComputeResult<GenericGroup>;
32+
```
33+
34+
## Parameters
35+
36+
- `group` : Object whose values can be wrapped or plain.
37+
38+
## Return value
39+
40+
A new object with the same keys and all values unwrapped. The input object is not mutated.
41+
42+
## See also
43+
44+
- [`unwrap`](/en/v1/api/common/unwrap) - Unwraps a single value
45+
- [`wrapValue`](/en/v1/api/common/wrapValue) - Wraps a value
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import { memoPromise } from "@duplojs/utils";
2+
3+
let calls = 0;
4+
const memoized = memoPromise(() => {
5+
calls += 1;
6+
return Promise.resolve("cached");
7+
});
8+
9+
const first = await memoized.value;
10+
// "cached", calls = 1
11+
const second = await memoized.value;
12+
// "cached", calls = 1 (no recompute)
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import { unwrapGroup, wrapValue } from "@duplojs/utils";
2+
3+
const group = {
4+
userId: wrapValue(42),
5+
role: "admin",
6+
};
7+
8+
const result = unwrapGroup(group);
9+
// { userId: 42, role: "admin" }
10+
11+
const stillWrapped = group.userId;
12+
// WrappedValue<number>

docs/fr/v1/api/common/addWrappedProperties.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22
outline: [2, 3]
33
description: "La fonction addWrappedProperties() ajoute dynamiquement des propriétés dérivées à une valeur wrappée, tout en conservant le type d'origine et en étendant le nouveau type."
44
prev:
5-
text: "unwrap"
6-
link: "/fr/v1/api/common/unwrap"
5+
text: "unwrapGroup"
6+
link: "/fr/v1/api/common/unwrapGroup"
77
next:
88
text: "clone"
99
link: "/fr/v1/api/common/clone"

0 commit comments

Comments
 (0)