Skip to content

Commit

Permalink
feat: add cache to getRenderer and getChildComponentClass
Browse files Browse the repository at this point in the history
  • Loading branch information
meixg committed May 9, 2022
1 parent a757013 commit bf091b4
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 7 deletions.
10 changes: 5 additions & 5 deletions bin/debug-local.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ function compile (componentPath, externalPath, outputPath) {
const MyComponent = require(componentPath)
const sanProject = new SanProject()
const res = sanProject.compileToSource(MyComponent, 'js', {
// useProvidedComponentClass: true
useProvidedComponentClass: true
})

cancelMarkExternalComponent()
Expand All @@ -24,13 +24,13 @@ function compile (componentPath, externalPath, outputPath) {
}

compile('./sample/component', './component2', './dist/component.js')
// compile('./component2', './component', './dist/component2')
compile('./sample/component2', './component', './dist/component2.js')

// // online
// const Component = require('./component')
const Component = require('./sample/component')
const render = require('./dist/component')

const html = render({})
// const html = render({}, { ComponentClass: Component })
// const html = render({})
const html = render({}, { ComponentClass: Component })

console.log(html)
27 changes: 27 additions & 0 deletions src/runtime/resolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,17 @@ export interface Resolver {
type nodeRequire = typeof require;

export function createResolver (exports: {[key: string]: any}, require: nodeRequire): Resolver {
const renderCache = {} as {[key: string]: Function}
return {
getRenderer: function ({ id, specifier = '.' }, tagName, context) {
const customSSRFilePath = context && context.customSSRFilePath
const cacheKey = id + ' ' + specifier

// 没有自定义时,尝试缓存
if (!customSSRFilePath && renderCache[cacheKey]) {
return renderCache[cacheKey]
}

let mod: {[key: string]: any}
if (specifier === '.') {
mod = exports
Expand All @@ -50,10 +58,26 @@ export function createResolver (exports: {[key: string]: any}, require: nodeRequ
}
mod = require(path || specifier)
}

if (!customSSRFilePath) {
renderCache[cacheKey] = mod.sanSSRRenders[id]
}

return mod.sanSSRRenders[id]
},
getChildComponentClass: function ({ id, specifier = '.' }, instance: Component, tagName: string, context) {
const customComponentFilePath = context && context.customComponentFilePath
const pro = Object.getPrototypeOf(instance)
if (!pro.__componentClassCache) {
pro.__componentClassCache = {}
}
const componentClassCache = pro.__componentClassCache as {[key: string]: Component}
const cacheKey = tagName

// 没有自定义时,尝试缓存
if (!customComponentFilePath && componentClassCache[cacheKey]) {
return componentClassCache[cacheKey]
}

if (customComponentFilePath && specifier !== '.') {
const path = customComponentFilePath({ id, specifier, tagName })
Expand All @@ -69,13 +93,15 @@ export function createResolver (exports: {[key: string]: any}, require: nodeRequ
throw Error(`child component is not fount: ${tagName}${instance.prototype?.id || ''}`)
}
if (typeof ChildComponentClassOrInstance === 'string' && ChildComponentClassOrInstance === 'self') {
componentClassCache[cacheKey] = instance
return instance
}
// component loader
if (
Object.prototype.hasOwnProperty.call(ChildComponentClassOrInstance, 'load') &&
Object.prototype.hasOwnProperty.call(ChildComponentClassOrInstance, 'placeholder')
) {
componentClassCache[cacheKey] = ChildComponentClassOrInstance.placeholder
return ChildComponentClassOrInstance.placeholder
}
if (
Expand All @@ -84,6 +110,7 @@ export function createResolver (exports: {[key: string]: any}, require: nodeRequ
) {
throw Error(`external component is not provided: ${tagName}${instance.prototype?.id || ''}`)
}
componentClassCache[cacheKey] = ChildComponentClassOrInstance
return ChildComponentClassOrInstance
},
setRenderer: function (id: string, fn: Function) {
Expand Down
13 changes: 12 additions & 1 deletion test/cases/provide-class/component.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,24 @@ const List = san.defineComponent({
template: '<div>{{ text }}</div>'
})

const CompB = san.defineComponent({
initData () {
return {
text: 'comp b'
}
},
template: '<div>{{ text }}</div>'
})
const CompA = san.defineComponent({
components: {
'x-l': CompB
},
initData () {
return {
text: 'component a'
}
},
template: '<div>{{ text }}</div>'
template: '<div><div>{{ text }}</div><x-l/></div>'
})

const MyComponent = san.defineComponent({
Expand Down
2 changes: 1 addition & 1 deletion test/cases/provide-class/expected.html
Original file line number Diff line number Diff line change
@@ -1 +1 @@
<div><!--s-data:{"c": "x-l"}--><div>child</div><div>child</div><div>component a</div></div>
<div><!--s-data:{"c": "x-l"}--><div>child</div><div>child</div><div><div>component a</div><div>comp b</div></div></div>

0 comments on commit bf091b4

Please sign in to comment.