Skip to content

Commit 90dc4e2

Browse files
committed
wip: refactor
1 parent 11bcb21 commit 90dc4e2

File tree

4 files changed

+28
-26
lines changed

4 files changed

+28
-26
lines changed

packages/runtime-vapor/src/block.ts

Lines changed: 8 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ export interface VaporTransitionHooks extends TransitionHooks {
3535

3636
export type TransitionBlock = {
3737
key?: any
38-
transition?: VaporTransitionHooks
38+
$transition?: VaporTransitionHooks
3939
}
4040

4141
export type BlockFn = (...args: any[]) => Block
@@ -45,7 +45,7 @@ export class VaporFragment {
4545
anchor?: Node
4646
insert?: (parent: ParentNode, anchor: Node | null) => void
4747
remove?: (parent?: ParentNode) => void
48-
transitionChild?: TransitionBlock | undefined
48+
$transition?: VaporTransitionHooks | undefined
4949

5050
constructor(nodes: Block) {
5151
this.nodes = nodes
@@ -57,7 +57,6 @@ export class DynamicFragment extends VaporFragment {
5757
scope: EffectScope | undefined
5858
current?: BlockFn
5959
fallback?: BlockFn
60-
transitionChild?: Block
6160

6261
constructor(anchorLabel?: string) {
6362
super([])
@@ -74,16 +73,13 @@ export class DynamicFragment extends VaporFragment {
7473
pauseTracking()
7574
const parent = this.anchor.parentNode
7675

77-
const transition = this.transition
76+
const transition = this.$transition
7877
const renderBranch = () => {
7978
if (render) {
8079
this.scope = new EffectScope()
8180
this.nodes = this.scope.run(render) || []
8281
if (transition) {
83-
this.transitionChild = applyTransitionEnterHooks(
84-
this.nodes,
85-
transition,
86-
)
82+
this.$transition = applyTransitionEnterHooks(this.nodes, transition)
8783
}
8884
if (parent) insert(this.nodes, parent, this.anchor)
8985
} else {
@@ -120,10 +116,6 @@ export class DynamicFragment extends VaporFragment {
120116

121117
resetTracking()
122118
}
123-
124-
get transition(): VaporTransitionHooks | undefined {
125-
return this.transitionChild && this.transitionChild.transition
126-
}
127119
}
128120

129121
export function isFragment(val: NonNullable<unknown>): val is VaporFragment {
@@ -161,11 +153,11 @@ export function insert(
161153
anchor = anchor === 0 ? parent.firstChild : anchor
162154
if (block instanceof Node) {
163155
// don't apply transition on text or comment nodes
164-
if (block.transition && block instanceof Element) {
156+
if (block.$transition && block instanceof Element) {
165157
performTransitionEnter(
166158
block,
167159
// @ts-expect-error
168-
block.transition,
160+
block.$transition,
169161
() => parent.insertBefore(block, anchor),
170162
parentSuspense,
171163
)
@@ -196,11 +188,11 @@ export function prepend(parent: ParentNode, ...blocks: Block[]): void {
196188

197189
export function remove(block: Block, parent?: ParentNode): void {
198190
if (block instanceof Node) {
199-
if (block.transition && block instanceof Element) {
191+
if (block.$transition && block instanceof Element) {
200192
performTransitionLeave(
201193
block,
202194
// @ts-expect-error
203-
block.transition,
195+
block.$transition,
204196
() => parent && parent.removeChild(block),
205197
)
206198
} else {

packages/runtime-vapor/src/components/Transition.ts

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -111,18 +111,19 @@ function resolveTransitionHooks(
111111

112112
function setTransitionHooks(block: Block, hooks: VaporTransitionHooks) {
113113
if (!isFragment(block)) {
114-
block.transition = hooks
114+
block.$transition = hooks
115115
}
116116
}
117117

118118
export function applyTransitionEnterHooks(
119119
block: Block,
120120
hooks: VaporTransitionHooks,
121-
): Block | undefined {
121+
): VaporTransitionHooks | undefined {
122122
const child = findElementChild(block)
123+
let enterHooks
123124
if (child) {
124125
const { props, state, delayedLeave } = hooks
125-
let enterHooks = resolveTransitionHooks(
126+
enterHooks = resolveTransitionHooks(
126127
child,
127128
props,
128129
state,
@@ -132,10 +133,10 @@ export function applyTransitionEnterHooks(
132133
enterHooks.delayedLeave = delayedLeave
133134
setTransitionHooks(child, enterHooks)
134135
if (isFragment(block)) {
135-
block.transitionChild = child
136+
block.$transition = enterHooks
136137
}
137138
}
138-
return child
139+
return enterHooks
139140
}
140141

141142
export function applyTransitionLeaveHooks(
@@ -161,6 +162,7 @@ export function applyTransitionLeaveHooks(
161162
leavingHooks.afterLeave = () => {
162163
state.isLeaving = false
163164
afterLeaveCb()
165+
leavingBlock.$transition = undefined
164166
delete leavingHooks.afterLeave
165167
}
166168
} else if (mode === 'in-out') {
@@ -174,17 +176,24 @@ export function applyTransitionLeaveHooks(
174176
block[leaveCbKey] = () => {
175177
earlyRemove()
176178
block[leaveCbKey] = undefined
179+
leavingBlock.$transition = undefined
177180
delete enterHooks.delayedLeave
178181
}
179182
enterHooks.delayedLeave = () => {
180183
delayedLeave()
184+
leavingBlock.$transition = undefined
181185
delete enterHooks.delayedLeave
182186
}
183187
}
184188
}
185189
}
186190

191+
const transitionChildCache = new WeakMap<Block, Block>()
187192
export function findElementChild(block: Block): Block | undefined {
193+
if (transitionChildCache.has(block)) {
194+
return transitionChildCache.get(block)
195+
}
196+
188197
let child: Block | undefined
189198
if (block instanceof Node) {
190199
// transition can only be applied on Element child

packages/runtime-vapor/src/directives/vShow.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -39,20 +39,20 @@ function setDisplay(target: Block, value: unknown): void {
3939
if (target instanceof DynamicFragment) {
4040
return setDisplay(target.nodes, value)
4141
}
42-
const { transition } = target
42+
const { $transition } = target
4343
if (target instanceof Element) {
4444
const el = target as VShowElement
4545
if (!(vShowOriginalDisplay in el)) {
4646
el[vShowOriginalDisplay] =
4747
el.style.display === 'none' ? '' : el.style.display
4848
}
49-
if (transition) {
49+
if ($transition) {
5050
if (value) {
51-
transition.beforeEnter(target)
51+
$transition.beforeEnter(target)
5252
el.style.display = el[vShowOriginalDisplay]!
53-
transition.enter(target)
53+
$transition.enter(target)
5454
} else {
55-
transition.leave(target, () => {
55+
$transition.leave(target, () => {
5656
el.style.display = 'none'
5757
})
5858
}

packages/runtime-vapor/src/dom/prop.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -267,6 +267,7 @@ export function optimizePropertyLookup(): void {
267267
if (isOptimized) return
268268
isOptimized = true
269269
const proto = Element.prototype as any
270+
proto.$transition = undefined
270271
proto.$evtclick = undefined
271272
proto.$root = false
272273
proto.$html =

0 commit comments

Comments
 (0)