-
Notifications
You must be signed in to change notification settings - Fork 391
/
resolveInitialArrayValues.ts
64 lines (61 loc) · 2.39 KB
/
resolveInitialArrayValues.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
import {ArraySchemaType, Path, SchemaType} from '@sanity/types'
import {concat, defer, EMPTY, from, Observable, of} from 'rxjs'
import {catchError, filter, map, mergeMap} from 'rxjs/operators'
import {resolveTypeName} from '@sanity/util/content'
import {FormPatch, set, unset} from '../../patch'
import {ObjectItem} from '../../types'
import {isEmptyItem} from '../../store/utils/isEmptyItem'
import {isNonNullable} from '../../../util'
const getMemberTypeOfItem = (schemaType: ArraySchemaType, item: any): SchemaType | undefined => {
const itemTypeName = resolveTypeName(item)
return schemaType.of.find((memberType) => memberType.name === itemTypeName)
}
/**
* Create patches that shallow merges keys from the given value
* Used by initial value resolver to retain any properties already added on the item when initial value is done resolving
* */
function assign(values: Record<string, unknown>, path: Path) {
return Object.entries(values).map(([key, value]) => set(value, [...path, key]))
}
export function resolveInitialArrayValues<T extends ObjectItem>(
items: T[],
schemaType: ArraySchemaType,
resolver: (type: SchemaType, params: Record<string, unknown>) => Promise<T>
): Observable<
| {type: 'patch'; patches: FormPatch[]}
| {type: 'error'; error: Error; item: T; schemaType: SchemaType}
> {
return from(items).pipe(
mergeMap((item) => {
const itemPathSegment = {_key: item._key}
return of(getMemberTypeOfItem(schemaType, item)).pipe(
mergeMap((memberType) => (memberType ? of(memberType) : EMPTY)),
mergeMap((memberType) => {
if (!isEmptyItem(item) || !resolver) {
return EMPTY
}
return concat(
of({
type: 'patch' as const,
patches: [set(true, [itemPathSegment, '_resolvingInitialValue'])],
}),
defer(() => resolver(memberType, item)).pipe(
filter(isNonNullable),
map((initial) => ({
type: 'patch' as const,
patches: assign(initial, [itemPathSegment]),
})),
catchError((error) =>
of({type: 'error' as const, error, item, schemaType: memberType})
)
),
of({
type: 'patch' as const,
patches: [unset([itemPathSegment, '_resolvingInitialValue'])],
})
)
})
)
})
)
}