Skip to content

Commit 555338b

Browse files
committed
refactor: resolve all slot children on component change
1 parent bb803af commit 555338b

File tree

3 files changed

+41
-20
lines changed

3 files changed

+41
-20
lines changed

packages/core/lib/for-each-slot.ts

+11-5
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
1-
import { ComponentData, Config, Content, RootData } from "../types";
1+
import { ComponentData, Content, RootData } from "../types";
22
import { isSlot } from "./is-slot";
33

4-
export const forEachSlot = <T extends ComponentData | RootData>(
4+
export const forEachSlot = async <T extends ComponentData | RootData>(
55
item: T,
6-
cb: (parentId: string, slotId: string, content: Content) => void,
6+
cb: (
7+
parentId: string,
8+
slotId: string,
9+
content: Content
10+
) => Promise<void> | void,
711
recursive: boolean = false
812
) => {
913
const props: Record<string, any> = item.props || {};
@@ -16,10 +20,12 @@ export const forEachSlot = <T extends ComponentData | RootData>(
1620
if (isSlot(props[propKey])) {
1721
const content = props[propKey] as Content;
1822

19-
cb(props.id, propKey, content);
23+
await cb(props.id, propKey, content);
2024

2125
if (recursive) {
22-
content.forEach((childItem) => forEachSlot(childItem, cb, true));
26+
content.forEach(
27+
async (childItem) => await forEachSlot(childItem, cb, true)
28+
);
2329
}
2430
}
2531
}

packages/core/lib/map-slots.ts

+10-12
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,20 @@
11
import { ComponentData, Content, RootData } from "../types";
22
import { forEachSlot } from "../lib/for-each-slot";
33

4-
export function mapSlots<T extends ComponentData | RootData>(
4+
export async function mapSlots<T extends ComponentData | RootData>(
55
item: T,
6-
map: (data: Content, path: string[]) => Content,
7-
path: string[] = []
8-
): T {
6+
map: (data: Content, propName: string) => Promise<Content>
7+
): Promise<T> {
98
const props: Record<string, any> = { ...item.props };
109

11-
forEachSlot(item, (_parentId, propName, content) => {
12-
const currentPath = [...path, `${_parentId}:${propName}`];
13-
14-
props[propName] = map(
15-
content.map((item) => {
16-
return mapSlots(item, map as any, currentPath);
17-
}),
18-
currentPath
10+
await forEachSlot(item, async (_parentId, propName, content) => {
11+
const mappedContent = await Promise.all(
12+
content.map(async (item) => {
13+
return await mapSlots(item, map as any);
14+
})
1915
);
16+
17+
props[propName] = await map(mappedContent, propName);
2018
});
2119

2220
return { ...item, props };

packages/core/lib/resolve-component-data.ts

+20-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import { ComponentData, Config, MappedItem, Metadata } from "../types";
22
import { mapSlots } from "./map-slots";
3-
import { forEachSlot } from "./for-each-slot";
43
import { getChanged } from "./get-changed";
54

65
export const cache: {
@@ -12,7 +11,8 @@ export const resolveComponentData = async (
1211
config: Config,
1312
metadata: Metadata = {},
1413
onResolveStart?: (item: MappedItem) => void,
15-
onResolveEnd?: (item: MappedItem) => void
14+
onResolveEnd?: (item: MappedItem) => void,
15+
recursive: boolean = true
1616
) => {
1717
const configForItem = config.components[item.type];
1818
if (configForItem.resolveData) {
@@ -36,14 +36,31 @@ export const resolveComponentData = async (
3636
metadata,
3737
});
3838

39-
const resolvedItem = {
39+
let resolvedItem = {
4040
...item,
4141
props: {
4242
...item.props,
4343
...resolvedProps,
4444
},
4545
};
4646

47+
if (recursive) {
48+
resolvedItem = await mapSlots(resolvedItem, async (content) => {
49+
return Promise.all(
50+
content.map(async (childItem) =>
51+
resolveComponentData(
52+
childItem,
53+
config,
54+
metadata,
55+
onResolveStart,
56+
onResolveEnd,
57+
false
58+
)
59+
)
60+
);
61+
});
62+
}
63+
4764
if (Object.keys(readOnly).length) {
4865
resolvedItem.readOnly = readOnly;
4966
}

0 commit comments

Comments
 (0)