Skip to content

Commit

Permalink
simplify
Browse files Browse the repository at this point in the history
  • Loading branch information
yisar committed Nov 30, 2022
1 parent cc6681d commit a1a2c29
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 42 deletions.
15 changes: 7 additions & 8 deletions src/commit.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { IFiber, IRef } from './type'
import { updateElement } from './dom'
import { isFn, LANE } from './reconcile'
import { isFn, TAG } from './reconcile'

export const commit = (fiber: IFiber): void => {
let current = fiber.next
Expand All @@ -11,28 +11,27 @@ export const commit = (fiber: IFiber): void => {
}

const op = (fiber: any) => {
if (fiber.lane & LANE.NOWORK) {
if (fiber.lane & TAG.NOWORK) {
return
}
if (fiber.lane === LANE.REMOVE) {
if (fiber.lane === TAG.REMOVE) {
remove(fiber)
return
}
if (fiber.lane & LANE.INSERT) {
if (fiber.lane & TAG.INSERT) {
if (fiber.isComp) {
fiber.child.lane = fiber.lane
op(fiber.child)
fiber.child.lane |= LANE.NOWORK
fiber.child.lane |= TAG.NOWORK
} else {
fiber.parentNode.insertBefore(fiber.node, fiber.after || null)
}
//
}
if (fiber.lane & LANE.UPDATE) {
if (fiber.lane & TAG.UPDATE) {
if (fiber.isComp) {
fiber.child.lane = fiber.lane
op(fiber.child)
fiber.child.lane |= LANE.NOWORK
fiber.child.lane |= TAG.NOWORK
} else {
updateElement(fiber.node, fiber.oldProps || {}, fiber.props)
}
Expand Down
4 changes: 2 additions & 2 deletions src/dom.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Attributes, DOM, IFiber } from './type'
import { isStr, LANE } from './reconcile'
import { isStr, TAG } from './reconcile'

const defaultObj = {} as const

Expand Down Expand Up @@ -45,7 +45,7 @@ export const createElement = <P = Attributes>(fiber: IFiber) => {
const dom =
fiber.type === '#text'
? document.createTextNode('')
: fiber.lane & LANE.SVG
: fiber.lane & TAG.SVG
? document.createElementNS(
'http://www.w3.org/2000/svg',
fiber.type as string
Expand Down
52 changes: 26 additions & 26 deletions src/reconcile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import { commit } from './commit'
let currentFiber: IFiber
let effectlist: any = {}

export const enum LANE {
export const enum TAG {
UPDATE = 1 << 1,
INSERT = 1 << 2,
REMOVE = 1 << 3,
Expand All @@ -34,8 +34,8 @@ export const render = (vnode: FreElement, node: Node): void => {
}

export const update = (fiber?: IFiber) => {
if (fiber && !(fiber.lane & LANE.DIRTY)) {
fiber.lane = LANE.UPDATE | LANE.DIRTY
if (fiber && !(fiber.lane & TAG.DIRTY)) {
fiber.lane = TAG.UPDATE | TAG.DIRTY
schedule(() => {
effectlist = fiber
return reconcile(fiber)
Expand Down Expand Up @@ -78,8 +78,8 @@ const capture = (wip: IFiber): IFiber | undefined => {
const getSibling = (wip) => {
while (wip) {
bubble(wip)
if (wip.lane & LANE.DIRTY) {
wip.lane &= ~LANE.DIRTY
if (wip.lane & TAG.DIRTY) {
wip.lane &= ~TAG.DIRTY
commit(wip)
return null
}
Expand Down Expand Up @@ -118,7 +118,7 @@ const updateHook = <P = Attributes>(wip: IFiber): any => {
const updateHost = (wip: IFiber): void => {
wip.parentNode = (getParentNode(wip) as any) || {}
if (!wip.node) {
if (wip.type === 'svg') wip.lane |= LANE.SVG
if (wip.type === 'svg') wip.lane |= TAG.SVG
wip.node = createElement(wip) as HTMLElementEx
}
wip.childNodes = Array.from(wip.node.childNodes || [])
Expand All @@ -145,12 +145,12 @@ const diffKids = (wip: any, children: FreNode): void => {

while (aHead <= aTail && bHead <= bTail) {
if (!same(aCh[aHead], bCh[bHead])) break
clone(aCh[aHead++], bCh[bHead++], LANE.UPDATE)
clone(aCh[aHead++], bCh[bHead++], TAG.UPDATE)
}

while (aHead <= aTail && bHead <= bTail) {
if (!same(aCh[aTail], bCh[bTail])) break
clone(aCh[aTail--], bCh[bTail--], LANE.UPDATE)
clone(aCh[aTail--], bCh[bTail--], TAG.UPDATE)
}

const { diff, keymap } = LCSdiff(bCh, aCh, bHead, bTail, aHead, aTail)
Expand All @@ -159,44 +159,44 @@ const diffKids = (wip: any, children: FreNode): void => {
for (let i = 0, aIndex = aHead, bIndex = bHead, mIndex; i < diff.length; i++) {
const op = diff[i]
const after = wip.node?.childNodes[aIndex]
if (op === LANE.UPDATE) {
if (op === TAG.UPDATE) {
if (!same(aCh[aIndex], bCh[bIndex])) {
bCh[bIndex].lane = LANE.INSERT
bCh[bIndex].lane = TAG.INSERT
bCh[bIndex].after = after
aCh[aIndex].lane = LANE.REMOVE
aCh[aIndex].lane = TAG.REMOVE
append(aCh[aIndex])
append(bCh[bIndex])
} else {
clone(aCh[aIndex], bCh[bIndex], LANE.UPDATE)
clone(aCh[aIndex], bCh[bIndex], TAG.UPDATE)
}
aIndex++
bIndex++
} else if (op === LANE.INSERT) {
} else if (op === TAG.INSERT) {
let c = bCh[bIndex]
mIndex = c.key != null ? keymap[c.key] : null
if (mIndex != null) {
c.after = after
clone(aCh[mIndex], c, LANE.INSERT)
clone(aCh[mIndex], c, TAG.INSERT)
aCh[mIndex] = undefined
} else {
c.after = isMount ? null : after
c.lane = LANE.INSERT
c.lane = TAG.INSERT
append(c)
}
bIndex++
} else if (op === LANE.REMOVE) {
} else if (op === TAG.REMOVE) {
aIndex++
}
}

for (let i = 0, aIndex = aHead; i < diff.length; i++) {
let op = diff[i]
if (op === LANE.UPDATE) {
if (op === TAG.UPDATE) {
aIndex++
} else if (op === LANE.REMOVE) {
} else if (op === TAG.REMOVE) {
let c = aCh[aIndex]
if (c !== undefined) {
c.lane = LANE.REMOVE
c.lane = TAG.REMOVE
append(c)
}
aIndex++
Expand All @@ -205,8 +205,8 @@ const diffKids = (wip: any, children: FreNode): void => {

for (let i = 0, prev = null, len = bCh.length; i < len; i++) {
const child = bCh[i]
if (wip.lane & LANE.SVG) {
child.lane |= LANE.SVG
if (wip.lane & TAG.SVG) {
child.lane |= TAG.SVG
}
child.parent = wip
if (i > 0) {
Expand Down Expand Up @@ -301,24 +301,24 @@ function LCSdiff(
while (ptr) {
const { newi, oldi } = ptr
while (curNewi > newi) {
diff[d--] = LANE.INSERT
diff[d--] = TAG.INSERT
curNewi--
}
while (curOldi > oldi) {
diff[d--] = LANE.REMOVE
diff[d--] = TAG.REMOVE
curOldi--
}
diff[d--] = LANE.UPDATE
diff[d--] = TAG.UPDATE
curNewi--
curOldi--
ptr = ptr.prev
}
while (curNewi >= bHead) {
diff[d--] = LANE.INSERT
diff[d--] = TAG.INSERT
curNewi--
}
while (curOldi >= aHead) {
diff[d--] = LANE.REMOVE
diff[d--] = TAG.REMOVE
curOldi--
}
return {
Expand Down
2 changes: 1 addition & 1 deletion src/schedule.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { IFiber, ITask, ITaskCallback } from './type'
import { ITask } from './type'

const queue: ITask[] = []
const threshold: number = 5
Expand Down
5 changes: 0 additions & 5 deletions src/type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ export interface Attributes extends Record<string, any> {
export interface FC<P extends Attributes = {}> {
(props: P): FreElement<P> | null
fiber?: IFiber
tag?: number
type?: string
memo?: boolean
shouldUpdate?: (newProps: P, oldProps: P) => boolean
Expand Down Expand Up @@ -60,12 +59,8 @@ export interface IFiber<P extends Attributes = any> {
lane: number
time: number
next: IFiber
prev: IFiber
d: IFiber
laziness: any[]
dirty: boolean
isComp: boolean
walker: any
}

export type HTMLElementEx = HTMLElement & { last: IFiber | null }
Expand Down

0 comments on commit a1a2c29

Please sign in to comment.