Skip to content

Commit

Permalink
refactor: wrap the ctx when initializing the slots
Browse files Browse the repository at this point in the history
  • Loading branch information
LittleSound committed Apr 6, 2024
1 parent 2e39f3e commit 3aba5c9
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 57 deletions.
61 changes: 40 additions & 21 deletions packages/runtime-vapor/__tests__/componentSlots.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import {
nextTick,
ref,
template,
withCtx,
} from '../src'
import { makeRender } from './_utils'

Expand All @@ -35,21 +34,6 @@ function renderWithSlots(slots: any): any {
}

describe('component: slots', () => {
test('initSlots: instance.slots should be set correctly', () => {
const { slots } = renderWithSlots({ _: 1 })
expect(slots).toMatchObject({ _: 1 })
})

// NOTE: slot normalization is not supported
test.todo(
'initSlots: should normalize object slots (when value is null, string, array)',
() => {},
)
test.todo(
'initSlots: should normalize object slots (when value is function)',
() => {},
)

test('initSlots: instance.slots should be set correctly', () => {
let instance: any
const Comp = defineComponent({
Expand All @@ -74,6 +58,16 @@ describe('component: slots', () => {
)
})

// NOTE: slot normalization is not supported
test.todo(
'initSlots: should normalize object slots (when value is null, string, array)',
() => {},
)
test.todo(
'initSlots: should normalize object slots (when value is function)',
() => {},
)

// runtime-core's "initSlots: instance.slots should be set correctly (when vnode.shapeFlag is not SLOTS_CHILDREN)"
test('initSlots: instance.slots should be set correctly', () => {
const { slots } = renderWithSlots({
Expand Down Expand Up @@ -154,12 +148,16 @@ describe('component: slots', () => {
})

test('the current instance should be kept in the slot', async () => {
let instanceInSlot: any
let instanceInDefaultSlot: any
let instanceInVForSlot: any
let instanceInVIfSlot: any

const Comp = defineComponent({
render() {
const instance = getCurrentInstance()
instance!.slots.default!()
instance!.slots.inVFor!()
instance!.slots.inVIf!()
return template('<div></div>')()
},
})
Expand All @@ -170,16 +168,37 @@ describe('component: slots', () => {
Comp,
{},
{
default: withCtx(() => {
instanceInSlot = getCurrentInstance()
default: () => {
instanceInDefaultSlot = getCurrentInstance()
return template('content')()
}),
},
},
() => [
[
{
name: 'inVFor',
fn: () => {
instanceInVForSlot = getCurrentInstance()
return template('content')()
},
},
],
{
name: 'inVIf',
key: '1',
fn: () => {
instanceInVIfSlot = getCurrentInstance()
return template('content')()
},
},
],
)
},
}).render()

expect(instanceInSlot).toBe(instance)
expect(instanceInDefaultSlot).toBe(instance)
expect(instanceInVForSlot).toBe(instance)
expect(instanceInVIfSlot).toBe(instance)
})

test.todo('should respect $stable flag', async () => {
Expand Down
22 changes: 0 additions & 22 deletions packages/runtime-vapor/src/componentRenderContext.ts

This file was deleted.

46 changes: 33 additions & 13 deletions packages/runtime-vapor/src/componentSlots.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { type IfAny, extend, isArray } from '@vue/shared'
import { type IfAny, isArray } from '@vue/shared'
import { baseWatch } from '@vue/reactivity'
import type { ComponentInternalInstance } from './component'
import { type ComponentInternalInstance, setCurrentInstance } from './component'
import type { Block } from './apiRender'
import { createVaporPreScheduler } from './scheduler'

Expand Down Expand Up @@ -29,7 +29,14 @@ export const initSlots = (
rawSlots: InternalSlots | null = null,
dynamicSlots: DynamicSlots | null = null,
) => {
const slots: InternalSlots = extend({}, rawSlots)
const slots: InternalSlots = {}

for (const key in rawSlots) {
const slot = rawSlots[key]
if (slot) {
slots[key] = withCtx(slot)
}
}

if (dynamicSlots) {
const dynamicSlotKeys: Record<string, true> = {}
Expand All @@ -41,20 +48,22 @@ export const initSlots = (
// array of dynamic slot generated by <template v-for="..." #[...]>
if (isArray(slot)) {
for (let j = 0; j < slot.length; j++) {
slots[slot[j].name] = slot[j].fn
slots[slot[j].name] = withCtx(slot[j].fn)
dynamicSlotKeys[slot[j].name] = true
}
} else if (slot) {
// conditional single slot generated by <template v-if="..." #foo>
slots[slot.name] = slot.key
? (...args: any[]) => {
const res = slot.fn(...args)
// attach branch key so each conditional branch is considered a
// different fragment
if (res) (res as any).key = slot.key
return res
}
: slot.fn
slots[slot.name] = withCtx(
slot.key
? (...args: any[]) => {
const res = slot.fn(...args)
// attach branch key so each conditional branch is considered a
// different fragment
if (res) (res as any).key = slot.key
return res
}
: slot.fn,
)
dynamicSlotKeys[slot.name] = true
}
}
Expand All @@ -77,4 +86,15 @@ export const initSlots = (
}

instance.slots = slots

function withCtx(fn: Slot): Slot {
return (...args: any[]) => {
const reset = setCurrentInstance(instance.parent!)
try {
return fn(...args)
} finally {
reset()
}
}
}
}
1 change: 0 additions & 1 deletion packages/runtime-vapor/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@ export {
type FunctionalComponent,
type SetupFn,
} from './component'
export { withCtx } from './componentRenderContext'
export { renderEffect } from './renderEffect'
export {
watch,
Expand Down

0 comments on commit 3aba5c9

Please sign in to comment.