Skip to content

Commit

Permalink
chore(stubs): refactor caching logic
Browse files Browse the repository at this point in the history
move caching logic from stub function to top level transformer
  • Loading branch information
xanf committed Oct 7, 2022
1 parent 77d1bbd commit 70f6737
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 44 deletions.
56 changes: 12 additions & 44 deletions src/vnodeTransformers/stubComponentsTransformer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -128,21 +128,6 @@ const resolveComponentStubByName = (componentName: string, stubs: Stubs) => {
}
}

function createStubOnceForType(
type: ConcreteComponent,
factoryFn: () => ConcreteComponent,
cache: WeakMap<ConcreteComponent, ConcreteComponent>
): ConcreteComponent {
const cachedStub = cache.get(type)
if (cachedStub) {
return cachedStub
}

const stub = factoryFn()
cache.set(type, stub)
return stub
}

interface CreateStubComponentsTransformerConfig {
stubs?: Stubs
shallow?: boolean
Expand All @@ -154,14 +139,6 @@ export function createStubComponentsTransformer({
shallow = false,
renderStubDefaultSlot = false
}: CreateStubComponentsTransformerConfig): VTUVNodeTypeTransformer {
const createdStubsMap: WeakMap<ConcreteComponent, ConcreteComponent> =
new WeakMap()

const createStubOnce = (
type: ConcreteComponent,
factoryFn: () => ConcreteComponent
) => createStubOnceForType(type, factoryFn, createdStubsMap)

return function componentsTransformer(type, instance) {
// stub teleport by default via config.global.stubs
if ((type as any) === Teleport && 'teleport' in stubs) {
Expand Down Expand Up @@ -234,18 +211,12 @@ export function createStubComponentsTransformer({
: { ...unwrappedStub }
specializedStubComponent.props = unwrappedStub.props

const specializedStub = createStubOnce(
type,
() => specializedStubComponent
)
specializedStub.props = unwrappedStub.props
registerStub({
source: type,
stub: specializedStub,
stub: specializedStubComponent,
originalStub: stub
})
// pass the props and children, for advanced stubbing
return specializedStub
return specializedStubComponent
}

if (stub === false) {
Expand All @@ -264,19 +235,16 @@ export function createStubComponentsTransformer({
throw new Error('Attempted to stub a non-component')
}

const newStub = createStubOnce(
type,
() =>
config.plugins.createStubs?.({
name: stubName,
component: type
}) ??
createStub({
name: stubName,
type,
renderStubDefaultSlot
})
)
const newStub =
config.plugins.createStubs?.({
name: stubName,
component: type
}) ??
createStub({
name: stubName,
type,
renderStubDefaultSlot
})
registerStub({ source: type, stub: newStub })
return newStub
}
Expand Down
12 changes: 12 additions & 0 deletions src/vnodeTransformers/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,19 +22,31 @@ export const createVNodeTransformer = ({
}: {
transformers: VTUVNodeTypeTransformer[]
}): VNodeArgsTransformerFn => {
const transformationCache: WeakMap<
VNodeTransformerInputComponentType,
VNodeTransformerInputComponentType
> = new WeakMap()

return (args: VNodeTransformerArgsType, instance: InstanceArgsType) => {
const [originalType, ...restVNodeArgs] = args

if (!isComponent(originalType)) {
return [originalType, ...restVNodeArgs]
}

const cachedTransformation = transformationCache.get(originalType)
if (cachedTransformation) {
return [cachedTransformation, ...restVNodeArgs]
}

const componentType: VNodeTransformerInputComponentType = originalType

const transformedType = transformers.reduce(
(type, transformer) => transformer(type, instance),
componentType
)
transformationCache.set(originalType, transformedType)

return [transformedType, ...restVNodeArgs]
}
}

0 comments on commit 70f6737

Please sign in to comment.