Skip to content

Commit 038a945

Browse files
committed
detach list
1 parent 800b5dd commit 038a945

File tree

6 files changed

+63
-23
lines changed

6 files changed

+63
-23
lines changed

demo/index.html

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,6 @@
88
</head>
99
<body>
1010
<div id="app"></div>
11-
<script type="module" src="/src/concurrent.tsx"></script>
11+
<script type="module" src="/src/keys.tsx"></script>
1212
</body>
1313
</html>

demo/src/keys.tsx

+1-1
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ export default function App() {
8282
<div>
8383
<button
8484
onClick={() => {
85-
setState([1,2,5]);
85+
setState([1]);
8686
}}
8787
>
8888
set

demo/src/mixin.tsx

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import { render, useState, createRoot,h } from "../../src/index"
2+
3+
function App() {
4+
console.log('父组件')
5+
const [count, setCount] = useState(0)
6+
return (
7+
<div>
8+
<B i={1} />
9+
<B i={2} />
10+
<h1>{count}</h1>
11+
<button onClick={() => setCount(count + 1)}>+</button>
12+
</div>
13+
)
14+
}
15+
16+
function B({ i }) {
17+
console.log('子组件', i)
18+
return 111
19+
}
20+
21+
const root = createRoot(document.getElementById("app"))
22+
root.mixin(function enableTimeSlicing() {
23+
let sync = true
24+
return {
25+
shouldYeild() {
26+
if (sync === true) {
27+
return false
28+
}
29+
}
30+
}
31+
})
32+
root.render(<App />)

src/commit.ts

+13-14
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,25 @@
11
import { IFiber, IRef, } from "./type"
22
import { updateElement } from "./dom"
3-
import { isFn, LANE, deletions } from './reconcile'
3+
import { isFn, LANE } from './reconcile'
44

55
export const commit = (fiber: IFiber): void => {
6-
let e = fiber.next
7-
fiber.next = null
6+
let d = fiber
7+
let e = d.e
8+
d.e = null
89
do {
910
const s = e.sibling
10-
if (s && isFn(s.type)) {
11-
e.sibling = s.child
12-
}
11+
if (s && isFn(s.type)) e.sibling = s.child
1312
paint(e)
14-
} while (e = e.next)
15-
deletions.forEach(e => {
16-
if (isFn(e.type)) {
17-
e.child.lane = LANE.REMOVE
18-
paint(e.child)
13+
} while (e = e.e)
14+
15+
while (d = d.d) {
16+
if (isFn(d.type)) {
17+
d.child.lane = LANE.REMOVE
18+
paint(d.child)
1919
} else {
20-
paint(e)
20+
paint(d)
2121
}
22-
})
23-
deletions.length = 0
22+
}
2423
}
2524

2625
const paint = (fiber: IFiber): void => {

src/reconcile.ts

+14-6
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ import { commit } from './commit'
1616
let currentFiber: IFiber
1717
let finish = null
1818
let effect = null
19-
export let deletions = []
19+
let detach = null
2020

2121
export const enum LANE {
2222
UPDATE = 1 << 1,
@@ -31,7 +31,8 @@ export const enum LANE {
3131

3232
export function createRoot(root) {
3333
return {
34-
render: (vnode) => render(vnode, root)
34+
render: (vnode) => render(vnode, root),
35+
mixin
3536
}
3637
}
3738

@@ -51,6 +52,7 @@ export const update = (fiber?: IFiber) => {
5152
fiber.lane = LANE.UPDATE | LANE.DIRTY
5253
fiber.sibling = null
5354
effect = fiber
55+
detach = fiber
5456
schedule(reconcile.bind(null, fiber) as any)
5557
}
5658
}
@@ -87,7 +89,7 @@ const bubble = (WIP) => {
8789
kid.lane |= WIP.lane
8890
invokeHooks(WIP)
8991
} else {
90-
effect.next = WIP
92+
effect.e = WIP
9193
effect = WIP
9294
}
9395
}
@@ -171,7 +173,8 @@ const diffKids = (WIP: any, children: FreNode): void => {
171173
while (aHead <= aTail) {
172174
let c = aCh[aTail--]
173175
c.lane = LANE.REMOVE
174-
deletions.push(c)
176+
detach.d = c
177+
detach = c
175178
}
176179
} else {
177180
if (!keyed) {
@@ -195,14 +198,15 @@ const diffKids = (WIP: any, children: FreNode): void => {
195198
for (const k in keyed) {
196199
let c = aCh[keyed[k]]
197200
c.lane = LANE.REMOVE
198-
deletions.push(c)
201+
detach.d = c
202+
detach = c
203+
199204
}
200205
}
201206

202207
while (bHead-- > 0) {
203208
clone(aCh[bHead], bCh[bHead], LANE.UPDATE, WIP, bHead)
204209
}
205-
206210
}
207211

208212
function linke(kid, WIP, i) {
@@ -246,6 +250,10 @@ function invokeHooks(fiber) {
246250
}
247251
}
248252

253+
function mixin(...mixins){
254+
console.log(mixins)
255+
}
256+
249257
const same = (a, b) => {
250258
const type = (c) => isFn(c.type) ? c.type.name : c.type
251259
return a && b && (a.key === b.key) && (type(a) === type(b))

src/type.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -54,10 +54,11 @@ export interface IFiber<P extends Attributes = any> {
5454
props: P
5555
lane: number
5656
time: number
57-
next: IFiber,
57+
e: IFiber,
5858
first:IFiber,
5959
last:IFiber,
6060
prev:IFiber,
61+
d:IFiber,
6162
laziness: any[]
6263
}
6364

0 commit comments

Comments
 (0)