Skip to content

Commit 784882f

Browse files
committed
fix(vm): ensure custom properties can be set and read
1 parent caef933 commit 784882f

File tree

4 files changed

+37
-10
lines changed

4 files changed

+37
-10
lines changed

Diff for: src/__tests__/lifecylce.spec.ts

+25-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { defineMixin } from '../defineMixin'
44
import { wrapComposable } from './helpers'
55

66
describe('Lifecycle hooks', async () => {
7-
test('normal lifecycle hooks are called with proper `this`', async () => {
7+
test('are called with proper `this`', async () => {
88
const spy = vi.fn()
99
function testHandler() {
1010
// @ts-expect-error - doesn't like this for some reason
@@ -44,4 +44,28 @@ describe('Lifecycle hooks', async () => {
4444

4545
expect(spy).toHaveBeenCalledTimes(8)
4646
})
47+
48+
test('can read and set custom properties on `this`', async () => {
49+
const mixin = defineMixin({
50+
props: {},
51+
beforeCreate() {
52+
;(this as any).custom = 'A'
53+
},
54+
created() {
55+
expect((this as any).custom).toBe('A')
56+
},
57+
})
58+
59+
const composable = createComposableFromMixin(mixin)
60+
61+
const wrapper = wrapComposable(
62+
composable,
63+
{},
64+
{
65+
template: `<div/>`,
66+
}
67+
)
68+
69+
expect((wrapper.vm as any).custom).toBe('A')
70+
})
4771
})

Diff for: src/createComposable.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ export /* @__PURE__ */ function createComposableFromMixin<
140140
const reactiveContext = reactive(context)
141141
const vmContextProxy = createContextProxy(vm, reactiveContext) as VM
142142

143-
beforeCreate && callHook(beforeCreate, instance, 'bc')
143+
beforeCreate && callHook(beforeCreate, instance, vm, 'bc')
144144

145145
// methods
146146
if (methods) {
@@ -204,7 +204,7 @@ export /* @__PURE__ */ function createComposableFromMixin<
204204
}
205205

206206
// Lifecycle
207-
created && callHook(created, instance, 'c')
207+
created && callHook(created, instance, vm, 'c')
208208
beforeMount && onBeforeMount(beforeMount.bind(vm))
209209
mounted && onMounted(mounted.bind(vm))
210210
beforeUpdate && onBeforeUpdate(beforeUpdate.bind(vm))

Diff for: src/utils.ts

+7-4
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
1-
import { callWithAsyncErrorHandling, type ComponentInternalInstance } from 'vue'
1+
import {
2+
callWithAsyncErrorHandling,
3+
type ComponentInternalInstance,
4+
type ComponentPublicInstance,
5+
} from 'vue'
26

37
export const isFunction = (val: unknown): val is Function =>
48
typeof val === 'function'
@@ -15,12 +19,11 @@ export const isString = (val: unknown): val is string => typeof val === 'string'
1519
export function callHook(
1620
hook: Function,
1721
instance: ComponentInternalInstance,
22+
vm: ComponentPublicInstance,
1823
type: 'c' | 'bc'
1924
) {
2025
callWithAsyncErrorHandling(
21-
isArray(hook)
22-
? hook.map((h) => h.bind(instance.proxy!))
23-
: hook.bind(instance.proxy!),
26+
isArray(hook) ? hook.map((h) => h.bind(vm)) : hook.bind(vm),
2427
instance,
2528
type as any
2629
)

Diff for: src/vmContextProxy.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
11
import type { ComponentPublicInstance } from 'vue'
22

33
export /* #__PURE__ */ function createContextProxy(
4-
vm: ComponentPublicInstance,
4+
_vm: ComponentPublicInstance,
55
context: Record<string, any>
66
) {
7-
return new Proxy(vm, {
7+
return new Proxy(_vm, {
88
get(vm, key, receiver) {
99
if (key in context) {
1010
return Reflect.get(context, key, receiver)
1111
} else {
1212
return (vm as any)[key]
1313
}
1414
},
15-
set(m, key, value, receiver) {
15+
set(vm, key, value, receiver) {
1616
if (key in context) {
1717
return Reflect.set(context, key, value, receiver)
1818
} else {

0 commit comments

Comments
 (0)