2
0
mirror of https://github.com/tenrok/vue-meta.git synced 2026-06-19 03:10:33 +03:00

fix: resolving arrays (collections still wip)

This commit is contained in:
pimlie
2020-11-16 00:07:54 +01:00
parent b61b44d5a8
commit 5c4ee7a547
4 changed files with 157 additions and 52 deletions
+24 -7
View File
@@ -21,7 +21,16 @@ export function createHandler (context: MetaContext, pathSegments: PathSegments
const keyPath: PathSegments = [...pathSegments, key]
const handler = /* #__PURE__ */ createHandler(context, keyPath)
value.__vm_proxy = createProxy(value, handler)
Object.defineProperty(
value,
'__vm_proxy',
{
configurable: false,
enumerable: false,
writable: false,
value: createProxy(value, handler)
}
)
}
return value.__vm_proxy
@@ -31,17 +40,25 @@ export function createHandler (context: MetaContext, pathSegments: PathSegments
key: string,
value: any
): boolean {
update(context, pathSegments, key, value)
// target[key] = value
return true
const success = Reflect.set(target, key, value)
if (success) {
update(context, pathSegments, key, value)
}
return success
},
deleteProperty (
target: { [key: string]: any },
prop: string
) {
remove(context, pathSegments, prop)
delete target[prop]
return true
const success = Reflect.deleteProperty(target, prop)
if (success) {
remove(context, pathSegments, prop)
}
return success
}
}
}
+23 -4
View File
@@ -1,4 +1,4 @@
import { hasOwn } from '@vue/shared'
import { hasOwn, isArray } from '@vue/shared'
import { clone } from '../utils'
import { ActiveNode, GetActiveNode, MetaContext, PathSegments, ShadowNode, GetShadowNodes } from '../types'
@@ -11,12 +11,19 @@ export function resolveActive (
) {
let value
const shadowLength = shadowParent[key] ? shadowParent[key].length : 0
const isUpdatingArrayKey = isArray(activeParent)
let shadowLength
if (isUpdatingArrayKey) {
shadowLength = shadowParent ? shadowParent.length : 0
} else {
shadowLength = shadowParent[key] ? shadowParent[key].length : 0
}
if (shadowLength > 1) {
// Is using freeze useful? Idea is to prevent the user from messing with these options by mistake
const getShadow: GetShadowNodes = () => Object.freeze(clone(shadowParent[key]))
const getActive: GetActiveNode = () => Object.freeze(clone(activeParent[key]))
const getShadow: GetShadowNodes = () => Object.freeze(clone(isUpdatingArrayKey ? shadowParent : shadowParent[key]))
const getActive: GetActiveNode = () => Object.freeze(clone(isUpdatingArrayKey ? activeParent : activeParent[key]))
value = context.resolve(
key,
@@ -30,6 +37,18 @@ export function resolveActive (
if (value === undefined) {
delete activeParent[key]
} else if (isUpdatingArrayKey) {
// set new values
for (const k in value) {
activeParent[k] = value[k]
}
// delete old values
for (const k in activeParent) {
if (!(k in value)) {
delete activeParent[k]
}
}
} else if (!hasOwn(activeParent, key) || activeParent[key] !== value) {
activeParent[key] = value
}
-3
View File
@@ -33,8 +33,6 @@ export function set (
activeParent[key],
pathSegments
)
} else if (isArray(value)) {
}
let idx = -1
@@ -83,7 +81,6 @@ export function set (
}
// Step 3: Update the active data
resolveActive(context, key, pathSegments, shadowParent, activeParent)
}