-
-
Notifications
You must be signed in to change notification settings - Fork 95
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
107 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import type { CodegenContext } from '../generate' | ||
import type { OnceIRNode } from '../ir' | ||
import { genBlock } from './block' | ||
import { type CodeFragment, NEWLINE, buildCodeFragment, genCall } from './utils' | ||
|
||
export function genCreateOnce( | ||
oper: OnceIRNode, | ||
context: CodegenContext, | ||
): CodeFragment[] { | ||
const { vaporHelper } = context | ||
const { block } = oper | ||
const [frag, push] = buildCodeFragment() | ||
|
||
let arg = genBlock(block, context) | ||
|
||
push(NEWLINE, `const n${oper.id} = `) | ||
push(...genCall(vaporHelper('createOnce'), arg)) | ||
|
||
return frag | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,44 @@ | ||
import { NodeTypes, findDir } from '@vue/compiler-dom' | ||
import type { NodeTransform } from '../transform' | ||
import { type ElementNode, NodeTypes, findDir } from '@vue/compiler-dom' | ||
import { | ||
type TransformContext, | ||
createStructuralDirectiveTransform, | ||
} from '../transform' | ||
import { type BlockIRNode, DynamicFlag, IRNodeTypes } from '../ir' | ||
import { newBlock, wrapTemplate } from './utils' | ||
|
||
const seen = new WeakSet() | ||
export const transformOnce = createStructuralDirectiveTransform( | ||
['once'], | ||
(node, _, context) => { | ||
if ( | ||
node.type === NodeTypes.ELEMENT && | ||
findDir(node, 'once', true) && | ||
!context.inVOnce | ||
) { | ||
context.inVOnce = true | ||
|
||
export const transformOnce: NodeTransform = (node, context) => { | ||
if (node.type === NodeTypes.ELEMENT && findDir(node, 'once', true)) { | ||
if (seen.has(node) || context.inVOnce /* || context.inSSR */) { | ||
return | ||
const id = context.reference() | ||
context.dynamic.flags |= DynamicFlag.INSERT | ||
const [block, onExit] = createNewBlock(node, context) | ||
|
||
return () => { | ||
onExit() | ||
context.registerOperation({ | ||
type: IRNodeTypes.ONCE, | ||
id, | ||
block, | ||
}) | ||
} | ||
} | ||
seen.add(node) | ||
context.inVOnce = true | ||
} | ||
}, | ||
) | ||
|
||
function createNewBlock( | ||
node: ElementNode, | ||
context: TransformContext<ElementNode>, | ||
): [BlockIRNode, () => void] { | ||
context.node = node = wrapTemplate(node, ['once']) | ||
const block: BlockIRNode = newBlock(node) | ||
const exitBlock = context.enterBlock(block) | ||
context.reference() | ||
return [block, exitBlock] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import { type Block, type Fragment, fragmentKey } from './apiRender' | ||
import { effectScope } from '@vue/reactivity' | ||
import { createComment, createTextNode } from './dom/element' | ||
import { type ComponentInternalInstance, isVaporComponent } from './component' | ||
import { isArray } from '@vue/shared' | ||
import type { BlockFn } from './apiCreateIf' | ||
|
||
/*! #__NO_SIDE_EFFECTS__ */ | ||
export const createOnce = (blockFn: BlockFn): Fragment => { | ||
let scope = effectScope() | ||
const anchor = __DEV__ ? createComment('once') : createTextNode() | ||
const fragment: Fragment = { | ||
nodes: scope.run(() => { | ||
const block = blockFn() | ||
const components = getComponents(block) | ||
components.forEach(comp => comp.scope.stop()) | ||
return block | ||
})!, | ||
anchor, | ||
[fragmentKey]: true, | ||
} | ||
scope.stop() | ||
return fragment | ||
} | ||
|
||
function getComponents(block: Block | null) { | ||
let components: ComponentInternalInstance[] = [] | ||
if (isVaporComponent(block)) { | ||
components.push(block, ...getComponents(block.block)) | ||
} else if (isArray(block)) { | ||
block.forEach(child => components.push(...getComponents(child))) | ||
} | ||
return components | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters