Skip to content

fix: destructure attrs from context keep reactive, close #264 #594

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 7, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 38 additions & 17 deletions src/mixin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import {
asVmProperty,
} from './utils/instance'
import { getVueConstructor } from './runtimeContext'
import { createObserver } from './reactivity/reactive'
import { createObserver, reactive } from './reactivity/reactive'

export function mixin(Vue: VueConstructor) {
Vue.mixin({
Expand Down Expand Up @@ -190,38 +190,59 @@ export function mixin(Vue: VueConstructor) {
function createSetupContext(
vm: ComponentInstance & { [x: string]: any }
): SetupContext {
const ctx = {
slots: {},
} as SetupContext
const props: Array<string | [string, string]> = [
const ctx = { slots: {} } as SetupContext

const propsPlain = [
'root',
'parent',
'refs',
'attrs',
'listeners',
'isServer',
'ssrContext',
]
const propsReactiveProxy = ['attrs']
const methodReturnVoid = ['emit']
props.forEach((key) => {
let targetKey: string
let srcKey: string
if (Array.isArray(key)) {
;[targetKey, srcKey] = key
} else {
targetKey = srcKey = key
}
srcKey = `$${srcKey}`
proxy(ctx, targetKey, {

propsPlain.forEach((key) => {
let srcKey = `$${key}`
proxy(ctx, key, {
get: () => vm[srcKey],
set() {
warn(
`Cannot assign to '${targetKey}' because it is a read-only property`,
`Cannot assign to '${key}' because it is a read-only property`,
vm
)
},
})
})

propsReactiveProxy.forEach((key) => {
let srcKey = `$${key}`
proxy(ctx, key, {
get: () => {
const data = reactive({})
const source = vm[srcKey]

for (const attr of Object.keys(source)) {
proxy(data, attr, {
get: () => {
// to ensure it always return the latest value
return vm[srcKey][attr]
},
})
}

return data
},
set() {
warn(
`Cannot assign to '${key}' because it is a read-only property`,
vm
)
},
})
})

methodReturnVoid.forEach((key) => {
const srcKey = `$${key}`
proxy(ctx, key, {
Expand Down
135 changes: 0 additions & 135 deletions test/setupContext.spec.js

This file was deleted.

Loading