Evan You 09b4202a22 refactor(reactivity): adjust APIs
BREAKING CHANGE: Reactivity APIs adjustments:

- `readonly` is now non-tracking if called on plain objects.
  `lock` and `unlock` have been removed. A `readonly` proxy can no
  longer be directly mutated. However, it can still wrap an already
  reactive object and track changes to the source reactive object.

- `isReactive` now only returns true for proxies created by `reactive`,
   or a `readonly` proxy that wraps a `reactive` proxy.

- A new utility `isProxy` is introduced, which returns true for both
  reactive or readonly proxies.

- `markNonReactive` has been renamed to `markRaw`.
2020-04-15 16:45:20 -04:00

248 lines
4.9 KiB
TypeScript

import { markRaw } from '@vue/reactivity'
export const enum NodeTypes {
TEXT = 'text',
ELEMENT = 'element',
COMMENT = 'comment'
}
export const enum NodeOpTypes {
CREATE = 'create',
INSERT = 'insert',
REMOVE = 'remove',
SET_TEXT = 'setText',
SET_ELEMENT_TEXT = 'setElementText',
PATCH = 'patch'
}
export interface TestElement {
id: number
type: NodeTypes.ELEMENT
parentNode: TestElement | null
tag: string
children: TestNode[]
props: Record<string, any>
eventListeners: Record<string, Function | Function[]> | null
}
export interface TestText {
id: number
type: NodeTypes.TEXT
parentNode: TestElement | null
text: string
}
export interface TestComment {
id: number
type: NodeTypes.COMMENT
parentNode: TestElement | null
text: string
}
export type TestNode = TestElement | TestText | TestComment
export interface NodeOp {
type: NodeOpTypes
nodeType?: NodeTypes
tag?: string
text?: string
targetNode?: TestNode
parentNode?: TestElement
refNode?: TestNode | null
propKey?: string
propPrevValue?: any
propNextValue?: any
}
let nodeId: number = 0
let recordedNodeOps: NodeOp[] = []
export function logNodeOp(op: NodeOp) {
recordedNodeOps.push(op)
}
export function resetOps() {
recordedNodeOps = []
}
export function dumpOps(): NodeOp[] {
const ops = recordedNodeOps.slice()
resetOps()
return ops
}
function createElement(tag: string): TestElement {
const node: TestElement = {
id: nodeId++,
type: NodeTypes.ELEMENT,
tag,
children: [],
props: {},
parentNode: null,
eventListeners: null
}
logNodeOp({
type: NodeOpTypes.CREATE,
nodeType: NodeTypes.ELEMENT,
targetNode: node,
tag
})
// avoid test nodes from being observed
markRaw(node)
return node
}
function createText(text: string): TestText {
const node: TestText = {
id: nodeId++,
type: NodeTypes.TEXT,
text,
parentNode: null
}
logNodeOp({
type: NodeOpTypes.CREATE,
nodeType: NodeTypes.TEXT,
targetNode: node,
text
})
// avoid test nodes from being observed
markRaw(node)
return node
}
function createComment(text: string): TestComment {
const node: TestComment = {
id: nodeId++,
type: NodeTypes.COMMENT,
text,
parentNode: null
}
logNodeOp({
type: NodeOpTypes.CREATE,
nodeType: NodeTypes.COMMENT,
targetNode: node,
text
})
// avoid test nodes from being observed
markRaw(node)
return node
}
function setText(node: TestText, text: string) {
logNodeOp({
type: NodeOpTypes.SET_TEXT,
targetNode: node,
text
})
node.text = text
}
function insert(child: TestNode, parent: TestElement, ref?: TestNode | null) {
let refIndex
if (ref) {
refIndex = parent.children.indexOf(ref)
if (refIndex === -1) {
console.error('ref: ', ref)
console.error('parent: ', parent)
throw new Error('ref is not a child of parent')
}
}
logNodeOp({
type: NodeOpTypes.INSERT,
targetNode: child,
parentNode: parent,
refNode: ref
})
// remove the node first, but don't log it as a REMOVE op
remove(child, false)
// re-calculate the ref index because the child's removal may have affected it
refIndex = ref ? parent.children.indexOf(ref) : -1
if (refIndex === -1) {
parent.children.push(child)
child.parentNode = parent
} else {
parent.children.splice(refIndex, 0, child)
child.parentNode = parent
}
}
function remove(child: TestNode, logOp: boolean = true) {
const parent = child.parentNode
if (parent) {
if (logOp) {
logNodeOp({
type: NodeOpTypes.REMOVE,
targetNode: child,
parentNode: parent
})
}
const i = parent.children.indexOf(child)
if (i > -1) {
parent.children.splice(i, 1)
} else {
console.error('target: ', child)
console.error('parent: ', parent)
throw Error('target is not a childNode of parent')
}
child.parentNode = null
}
}
function setElementText(el: TestElement, text: string) {
logNodeOp({
type: NodeOpTypes.SET_ELEMENT_TEXT,
targetNode: el,
text
})
el.children.forEach(c => {
c.parentNode = null
})
if (!text) {
el.children = []
} else {
el.children = [
{
id: nodeId++,
type: NodeTypes.TEXT,
text,
parentNode: el
}
]
}
}
function parentNode(node: TestNode): TestElement | null {
return node.parentNode
}
function nextSibling(node: TestNode): TestNode | null {
const parent = node.parentNode
if (!parent) {
return null
}
const i = parent.children.indexOf(node)
return parent.children[i + 1] || null
}
function querySelector(): any {
throw new Error('querySelector not supported in test renderer.')
}
function setScopeId(el: TestElement, id: string) {
el.props[id] = ''
}
export const nodeOps = {
insert,
remove,
createElement,
createText,
createComment,
setText,
setElementText,
parentNode,
nextSibling,
querySelector,
setScopeId
}