mirror of
https://github.com/tenrok/vue-meta.git
synced 2026-06-24 19:30:35 +03:00
feat: add support for recomputing nested paths
This commit is contained in:
@@ -25,14 +25,27 @@ export const allKeys = <T>(source?: MergeSource<T>, ...sources: MergeSource<T>[]
|
|||||||
return keys
|
return keys
|
||||||
}
|
}
|
||||||
|
|
||||||
export const recompute = <T>(context: MergeContext<T>, sources?: MergeSource<T>[], target?: MergedObject, path: PathSegments = []): void => {
|
export const recompute = <T>(context: MergeContext<T>, path: PathSegments = [], target?: MergedObject, sources?: MergeSource<T>[]): void => {
|
||||||
if (!path.length) {
|
const setTargetAndSources = !target && !sources
|
||||||
if (!target) {
|
if (setTargetAndSources) {
|
||||||
target = context.active
|
({ active: target, sources } = context)
|
||||||
}
|
|
||||||
|
|
||||||
if (!sources) {
|
if (path.length) {
|
||||||
sources = context.sources
|
for (let i = 0; i < path.length; i++) {
|
||||||
|
const seg = path[i]
|
||||||
|
|
||||||
|
if (!target || !target[seg]) {
|
||||||
|
if (__DEV__) {
|
||||||
|
// eslint-disable-next-line no-console
|
||||||
|
console.error(`recompute: segment ${seg} not found on target`, path, target)
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
target = target[seg]
|
||||||
|
|
||||||
|
sources = sources.map(source => (source as Record<string, any>)[seg]).filter(Boolean)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -53,7 +66,17 @@ export const recompute = <T>(context: MergeContext<T>, sources?: MergeSource<T>[
|
|||||||
for (const key of keys) {
|
for (const key of keys) {
|
||||||
// This assumes consistent types usages for keys across sources
|
// This assumes consistent types usages for keys across sources
|
||||||
// @ts-ignore
|
// @ts-ignore
|
||||||
if (isPlainObject(sources[0][key])) {
|
let isObject = false
|
||||||
|
for (let i = 0; i < sources.length; i++) {
|
||||||
|
const source = sources[i] as Record<string, any>
|
||||||
|
|
||||||
|
if (source && key in source && source[key] !== undefined) {
|
||||||
|
isObject = isPlainObject(source[key])
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (isObject) {
|
||||||
if (!target[key]) {
|
if (!target[key]) {
|
||||||
target[key] = {}
|
target[key] = {}
|
||||||
}
|
}
|
||||||
@@ -66,7 +89,7 @@ export const recompute = <T>(context: MergeContext<T>, sources?: MergeSource<T>[
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
recompute(context, keySources, target[key], [...path, key])
|
recompute(context, [...path, key], target[key], keySources)
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -91,7 +114,6 @@ export const recompute = <T>(context: MergeContext<T>, sources?: MergeSource<T>[
|
|||||||
resolved = clone(resolved)
|
resolved = clone(resolved)
|
||||||
}
|
}
|
||||||
|
|
||||||
// console.log('RESOLVED', key, resolved, 'was', target[key])
|
|
||||||
target[key] = resolved
|
target[key] = resolved
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user