2
0
mirror of https://github.com/tenrok/vue-meta.git synced 2026-06-08 16:12:26 +03:00

feat: add deepest resolver (wip)

feat: improve resolver related typing
This commit is contained in:
pimlie
2020-08-10 02:06:53 +02:00
parent 5add8bf83f
commit bb04dc068d
12 changed files with 926 additions and 666 deletions
+3 -3
View File
@@ -1,6 +1,6 @@
import { hasOwn } from '@vue/shared'
import { clone } from '../utils'
import { ActiveNode, MetaContext, PathSegments, ShadowNode } from '../types'
import { ActiveNode, GetActiveNode, MetaContext, PathSegments, ShadowNode, GetShadowNodes } from '../types'
export function resolveActive (
context: MetaContext,
@@ -13,8 +13,8 @@ export function resolveActive (
if (shadowParent[key].length > 1) {
// Is using freeze useful? Idea is to prevent the user from messing with these options by mistake
const getShadow = () => Object.freeze(clone(shadowParent[key]))
const getActive = () => Object.freeze(clone(activeParent[key]))
const getShadow: GetShadowNodes = () => Object.freeze(clone(shadowParent[key]))
const getActive: GetActiveNode = () => Object.freeze(clone(activeParent[key]))
value = context.manager.resolver.resolve(
key,
+6 -5
View File
@@ -1,4 +1,4 @@
import { isPlainObject, hasOwn } from '@vue/shared'
import { isPlainObject, /**/ hasOwn } from '@vue/shared'
import { ActiveNode, MetaContext, PathSegments, ShadowNode } from '../types'
import { shadow, active } from './globals'
import { resolveActive } from './resolve'
@@ -15,7 +15,7 @@ export function set (
// shadow & active should always be in sync
// if not we have bigger fish to fry
if (!shadowParent[key]) {
shadowParent[key] = {}
shadowParent[key] = []
activeParent[key] = {}
}
@@ -26,7 +26,7 @@ export function set (
activeParent[key],
pathSegments
)
}
}/**/
let idx = -1
if (!shadowParent[key]) {
@@ -34,7 +34,7 @@ export function set (
} else {
// check if we already have a value listed for this element for this context
idx = shadowParent[key].findIndex(
({ context: $context }: { context: MetaContext }) => $context === context
({ context: shadowContext }: { context: MetaContext }) => shadowContext === context
)
}
@@ -68,12 +68,13 @@ export function setByObject (
}
if (isPlainObject(shadowParent[key])) {
console.log('HERERER')
setByObject(context, {}, shadowParent[key], activeParent[key], [
...pathSegments,
key
])
continue
}
} /**/
set(context, key, undefined, shadowParent, activeParent, [
...pathSegments,
+4 -4
View File
@@ -1,8 +1,8 @@
import { App } from 'vue'
import { isFunction } from '@vue/shared'
import { applyMetaPlugin } from './install'
import * as deepestResolver from './resolvers/deepest'
import { Config, ManagerResolverObject, ActiveResolverObject, MetaContext, PathSegments } from './types'
// import * as deepestResolver from './resolvers/deepest'
import { Config, ManagerResolverObject, GetActiveNode, ActiveResolverObject, MetaContext, PathSegments, GetShadowNodes } from './types'
export type Manager = {
readonly config: Config
@@ -11,7 +11,7 @@ export type Manager = {
install(app: App): void
}
export function createManager (config: Config, resolver: ActiveResolverObject = deepestResolver): Manager {
export function createManager (config: Config, resolver: ActiveResolverObject): Manager {
// TODO: validate resolver
const manager: Manager = {
resolver: {
@@ -22,7 +22,7 @@ export function createManager (config: Config, resolver: ActiveResolverObject =
resolver.setup(context)
},
resolve (key: string, pathSegments: PathSegments, getShadow, getActive) {
resolve (key: string, pathSegments: PathSegments, getShadow: GetShadowNodes, getActive: GetActiveNode) {
if (!resolver) {
return
}
+2 -8
View File
@@ -7,17 +7,11 @@ interface Target extends MetainfoInput {
__vm_proxy?: any // eslint-disable-line camelcase
}
export function createProxy (
target: Target,
handler: ProxyHandler<object>
): Target {
export function createProxy (target: Target, handler: ProxyHandler<object>): Target {
return markRaw(new Proxy(target, handler))
}
export function createHandler (
context: MetaContext,
pathSegments: PathSegments = []
): ProxyHandler<object> {
export function createHandler (context: MetaContext, pathSegments: PathSegments = []): ProxyHandler<object> {
return {
get (target: object, key: string, receiver: object) {
const value = Reflect.get(target, key, receiver)
+4 -4
View File
@@ -42,7 +42,7 @@ export function renderMeta (
data: TODO,
config: TODO
): void | RenderedMetainfo | RenderedMetainfoNode {
console.info('renderMeta', key, data, config)
// console.info('renderMeta', key, data, config)
if (config.attributesFor) {
return renderAttributes(context, key, data, config)
@@ -61,7 +61,7 @@ export function renderGroup (
data: TODO,
config: TODO
): RenderedMetainfo | RenderedMetainfoNode {
console.info('renderGroup', key, data, config)
// console.info('renderGroup', key, data, config)
if (isArray(data)) {
if (__DEV__) {
@@ -101,7 +101,7 @@ export function renderTag (
config: TODO = {},
groupConfig?: GroupConfig
): RenderedMetainfo | RenderedMetainfoNode {
console.info('renderTag', key, data, config, groupConfig)
// console.info('renderTag', key, data, config, groupConfig)
const contentAttributes = ['content', 'json', 'rawContent']
const getConfig = (key: string) => getConfigByKey([tag, config.tag], key, config)
@@ -234,7 +234,7 @@ export function renderAttributes (
data: TODO,
config: TODO = {}
): void {
console.info('renderAttributes', key, data, config)
// console.info('renderAttributes', key, data, config)
const { attributesFor } = config
+48 -7
View File
@@ -1,15 +1,56 @@
import {
ActiveNode,
// ActiveNode,
/* ActiveResolverSetup, ActiveResolverMethod, */ MetaContext,
PathSegments,
ShadowNode
ShadowNode,
GetActiveNode,
GetShadowNodes
} from '../types'
export function setup (context: MetaContext): void {}
interface DeepestResolverMetaContext extends MetaContext {
depth?: number
}
export function setup (context: DeepestResolverMetaContext): void {
let depth: number = 0
if (context.vm) {
let { vm } = context
do {
depth++
vm = vm.parent
} while (vm && vm !== vm.root)
}
context.depth = depth
}
export function resolve (
key: string,
pathSegments: PathSegments,
shadow: ShadowNode,
active: ActiveNode
): any {}
_pathSegments: PathSegments,
getOptions: GetShadowNodes,
getCurrentValue: GetActiveNode
): any {
let resolvedOption: ShadowNode | void
const options = getOptions()
for (const option of options) {
if (!resolvedOption || resolvedOption.context.depth < option.context.depth) {
resolvedOption = option
}
}
console.log(
'DEEPEST.RESOLVE',
key,
getCurrentValue(),
options.map(({ value }) => value)
)
if (resolvedOption) {
console.log(resolvedOption.value)
return resolvedOption.value
}
}
+11 -3
View File
@@ -34,7 +34,7 @@ export interface MetainfoActive {
export type MetaContext = {
id: string | symbol
vm?: ComponentInternalInstance | null
vm?: ComponentInternalInstance
manager: Manager
}
@@ -42,8 +42,8 @@ export type ActiveResolverSetup = (context: MetaContext) => void
export type ActiveResolverMethod = (
key: string,
pathSegments: PathSegments,
shadow: ShadowNode,
active: ActiveNode
shadow: GetShadowNodes,
active: GetActiveNode
) => any
export interface ActiveResolverObject {
@@ -63,3 +63,11 @@ export interface ShadowNode {
export interface ActiveNode {
[key: string]: TODO
}
export interface GetShadowNodes {
(): Array<ShadowNode>
}
export interface GetActiveNode {
(): ActiveNode
}
+1 -1
View File
@@ -20,7 +20,7 @@ export function useMeta (obj: MetainfoInput, manager?: Manager) {
const context: MetaContext = {
id: PolySymbol(`context ${contextCounter++}`),
vm,
vm: vm || undefined,
manager
}