Skip to content

Commit a1a2c29

Browse files
committed
simplify
1 parent cc6681d commit a1a2c29

File tree

5 files changed

+36
-42
lines changed

5 files changed

+36
-42
lines changed

src/commit.ts

+7-8
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { IFiber, IRef } from './type'
22
import { updateElement } from './dom'
3-
import { isFn, LANE } from './reconcile'
3+
import { isFn, TAG } from './reconcile'
44

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

1313
const op = (fiber: any) => {
14-
if (fiber.lane & LANE.NOWORK) {
14+
if (fiber.lane & TAG.NOWORK) {
1515
return
1616
}
17-
if (fiber.lane === LANE.REMOVE) {
17+
if (fiber.lane === TAG.REMOVE) {
1818
remove(fiber)
1919
return
2020
}
21-
if (fiber.lane & LANE.INSERT) {
21+
if (fiber.lane & TAG.INSERT) {
2222
if (fiber.isComp) {
2323
fiber.child.lane = fiber.lane
2424
op(fiber.child)
25-
fiber.child.lane |= LANE.NOWORK
25+
fiber.child.lane |= TAG.NOWORK
2626
} else {
2727
fiber.parentNode.insertBefore(fiber.node, fiber.after || null)
2828
}
29-
//
3029
}
31-
if (fiber.lane & LANE.UPDATE) {
30+
if (fiber.lane & TAG.UPDATE) {
3231
if (fiber.isComp) {
3332
fiber.child.lane = fiber.lane
3433
op(fiber.child)
35-
fiber.child.lane |= LANE.NOWORK
34+
fiber.child.lane |= TAG.NOWORK
3635
} else {
3736
updateElement(fiber.node, fiber.oldProps || {}, fiber.props)
3837
}

src/dom.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { Attributes, DOM, IFiber } from './type'
2-
import { isStr, LANE } from './reconcile'
2+
import { isStr, TAG } from './reconcile'
33

44
const defaultObj = {} as const
55

@@ -45,7 +45,7 @@ export const createElement = <P = Attributes>(fiber: IFiber) => {
4545
const dom =
4646
fiber.type === '#text'
4747
? document.createTextNode('')
48-
: fiber.lane & LANE.SVG
48+
: fiber.lane & TAG.SVG
4949
? document.createElementNS(
5050
'http://www.w3.org/2000/svg',
5151
fiber.type as string

src/reconcile.ts

+26-26
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ import { commit } from './commit'
1616
let currentFiber: IFiber
1717
let effectlist: any = {}
1818

19-
export const enum LANE {
19+
export const enum TAG {
2020
UPDATE = 1 << 1,
2121
INSERT = 1 << 2,
2222
REMOVE = 1 << 3,
@@ -34,8 +34,8 @@ export const render = (vnode: FreElement, node: Node): void => {
3434
}
3535

3636
export const update = (fiber?: IFiber) => {
37-
if (fiber && !(fiber.lane & LANE.DIRTY)) {
38-
fiber.lane = LANE.UPDATE | LANE.DIRTY
37+
if (fiber && !(fiber.lane & TAG.DIRTY)) {
38+
fiber.lane = TAG.UPDATE | TAG.DIRTY
3939
schedule(() => {
4040
effectlist = fiber
4141
return reconcile(fiber)
@@ -78,8 +78,8 @@ const capture = (wip: IFiber): IFiber | undefined => {
7878
const getSibling = (wip) => {
7979
while (wip) {
8080
bubble(wip)
81-
if (wip.lane & LANE.DIRTY) {
82-
wip.lane &= ~LANE.DIRTY
81+
if (wip.lane & TAG.DIRTY) {
82+
wip.lane &= ~TAG.DIRTY
8383
commit(wip)
8484
return null
8585
}
@@ -118,7 +118,7 @@ const updateHook = <P = Attributes>(wip: IFiber): any => {
118118
const updateHost = (wip: IFiber): void => {
119119
wip.parentNode = (getParentNode(wip) as any) || {}
120120
if (!wip.node) {
121-
if (wip.type === 'svg') wip.lane |= LANE.SVG
121+
if (wip.type === 'svg') wip.lane |= TAG.SVG
122122
wip.node = createElement(wip) as HTMLElementEx
123123
}
124124
wip.childNodes = Array.from(wip.node.childNodes || [])
@@ -145,12 +145,12 @@ const diffKids = (wip: any, children: FreNode): void => {
145145

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

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

156156
const { diff, keymap } = LCSdiff(bCh, aCh, bHead, bTail, aHead, aTail)
@@ -159,44 +159,44 @@ const diffKids = (wip: any, children: FreNode): void => {
159159
for (let i = 0, aIndex = aHead, bIndex = bHead, mIndex; i < diff.length; i++) {
160160
const op = diff[i]
161161
const after = wip.node?.childNodes[aIndex]
162-
if (op === LANE.UPDATE) {
162+
if (op === TAG.UPDATE) {
163163
if (!same(aCh[aIndex], bCh[bIndex])) {
164-
bCh[bIndex].lane = LANE.INSERT
164+
bCh[bIndex].lane = TAG.INSERT
165165
bCh[bIndex].after = after
166-
aCh[aIndex].lane = LANE.REMOVE
166+
aCh[aIndex].lane = TAG.REMOVE
167167
append(aCh[aIndex])
168168
append(bCh[bIndex])
169169
} else {
170-
clone(aCh[aIndex], bCh[bIndex], LANE.UPDATE)
170+
clone(aCh[aIndex], bCh[bIndex], TAG.UPDATE)
171171
}
172172
aIndex++
173173
bIndex++
174-
} else if (op === LANE.INSERT) {
174+
} else if (op === TAG.INSERT) {
175175
let c = bCh[bIndex]
176176
mIndex = c.key != null ? keymap[c.key] : null
177177
if (mIndex != null) {
178178
c.after = after
179-
clone(aCh[mIndex], c, LANE.INSERT)
179+
clone(aCh[mIndex], c, TAG.INSERT)
180180
aCh[mIndex] = undefined
181181
} else {
182182
c.after = isMount ? null : after
183-
c.lane = LANE.INSERT
183+
c.lane = TAG.INSERT
184184
append(c)
185185
}
186186
bIndex++
187-
} else if (op === LANE.REMOVE) {
187+
} else if (op === TAG.REMOVE) {
188188
aIndex++
189189
}
190190
}
191191

192192
for (let i = 0, aIndex = aHead; i < diff.length; i++) {
193193
let op = diff[i]
194-
if (op === LANE.UPDATE) {
194+
if (op === TAG.UPDATE) {
195195
aIndex++
196-
} else if (op === LANE.REMOVE) {
196+
} else if (op === TAG.REMOVE) {
197197
let c = aCh[aIndex]
198198
if (c !== undefined) {
199-
c.lane = LANE.REMOVE
199+
c.lane = TAG.REMOVE
200200
append(c)
201201
}
202202
aIndex++
@@ -205,8 +205,8 @@ const diffKids = (wip: any, children: FreNode): void => {
205205

206206
for (let i = 0, prev = null, len = bCh.length; i < len; i++) {
207207
const child = bCh[i]
208-
if (wip.lane & LANE.SVG) {
209-
child.lane |= LANE.SVG
208+
if (wip.lane & TAG.SVG) {
209+
child.lane |= TAG.SVG
210210
}
211211
child.parent = wip
212212
if (i > 0) {
@@ -301,24 +301,24 @@ function LCSdiff(
301301
while (ptr) {
302302
const { newi, oldi } = ptr
303303
while (curNewi > newi) {
304-
diff[d--] = LANE.INSERT
304+
diff[d--] = TAG.INSERT
305305
curNewi--
306306
}
307307
while (curOldi > oldi) {
308-
diff[d--] = LANE.REMOVE
308+
diff[d--] = TAG.REMOVE
309309
curOldi--
310310
}
311-
diff[d--] = LANE.UPDATE
311+
diff[d--] = TAG.UPDATE
312312
curNewi--
313313
curOldi--
314314
ptr = ptr.prev
315315
}
316316
while (curNewi >= bHead) {
317-
diff[d--] = LANE.INSERT
317+
diff[d--] = TAG.INSERT
318318
curNewi--
319319
}
320320
while (curOldi >= aHead) {
321-
diff[d--] = LANE.REMOVE
321+
diff[d--] = TAG.REMOVE
322322
curOldi--
323323
}
324324
return {

src/schedule.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { IFiber, ITask, ITaskCallback } from './type'
1+
import { ITask } from './type'
22

33
const queue: ITask[] = []
44
const threshold: number = 5

src/type.ts

-5
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ export interface Attributes extends Record<string, any> {
1717
export interface FC<P extends Attributes = {}> {
1818
(props: P): FreElement<P> | null
1919
fiber?: IFiber
20-
tag?: number
2120
type?: string
2221
memo?: boolean
2322
shouldUpdate?: (newProps: P, oldProps: P) => boolean
@@ -60,12 +59,8 @@ export interface IFiber<P extends Attributes = any> {
6059
lane: number
6160
time: number
6261
next: IFiber
63-
prev: IFiber
64-
d: IFiber
65-
laziness: any[]
6662
dirty: boolean
6763
isComp: boolean
68-
walker: any
6964
}
7065

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

0 commit comments

Comments
 (0)