Skip to content

Commit d7bf489

Browse files
committed
feat: 修复shouldComponentUpdate引起的问题
1 parent 8bce893 commit d7bf489

6 files changed

Lines changed: 211 additions & 89 deletions

File tree

demo/src/index.tsx

Lines changed: 27 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { QuarkElement, property, customElement } from "quarkc"
1+
import { QuarkElement, property, customElement, state, computed } from "quarkc"
22
import { Router } from "quark-router"
33
import "./pages/home"
44
import "./pages/sub"
@@ -14,7 +14,7 @@ declare global {
1414
@customElement({ tag: "my-component", style })
1515
class MyComponent extends QuarkElement {
1616
private _routes = new Router(this, [
17-
{path: '/', render: () => <home-component/>},
17+
{path: '/', render: () => <home-component count={this.count} />},
1818
{path: '/sub/:id', render: ({id}) => <sub-component id={id}/>},
1919
{path: '/child/*', render: () => <child-component/>},
2020
{path: '/child', render: () => <child-component/>},
@@ -29,16 +29,36 @@ class MyComponent extends QuarkElement {
2929
@property({ type: String })
3030
text
3131

32+
@state()
33+
innerCount = 0;
34+
35+
@computed()
36+
get resolvedCount() {
37+
return this.count + this.innerCount;
38+
}
39+
3240
add = () => {
3341
// 内部事件
34-
this.count += 1
42+
this.innerCount += 1
3543
};
3644

37-
componentDidMount() {
38-
console.log("dom loaded!", 'parent')
39-
// ...
45+
shouldComponentUpdate(propName, oldValue, newValue) {
46+
if (propName === 'innerCount') {
47+
console.log(propName, oldValue, newValue)
48+
return newValue <= 10 || newValue >= 20;
49+
}
50+
51+
return true;
4052
}
4153

54+
componentDidUpdate() {
55+
console.log("parent dom updated!")
56+
}
57+
58+
// componentDidMount() {
59+
// console.log("parent dom loaded!")
60+
// }
61+
4262
render() {
4363
return (
4464
<>
@@ -56,7 +76,7 @@ class MyComponent extends QuarkElement {
5676
<h1>{this.text} Quarkc</h1>
5777

5878
<div className="card">
59-
<button onClick={this.add}>count is: {this.count}</button>
79+
<button onClick={this.add}>count is: {this.resolvedCount}</button>
6080
</div>
6181
</div>
6282
<ul>

demo/src/pages/home.tsx

Lines changed: 48 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { QuarkElement, property, customElement } from "quarkc"
1+
import { QuarkElement, property, customElement, watch, state, computed } from "quarkc"
22
import style from "./style.less?inline"
33

44
declare global {
@@ -12,16 +12,60 @@ class MyComponent extends QuarkElement {
1212
@property({ type: String })
1313
text = "hello world"
1414

15+
@property({ type: Number })
16+
count = 0
17+
18+
@state()
19+
loggerRunCount = 0
20+
21+
@state()
22+
counter = 0;
23+
24+
private _counterTimer = 0;
25+
26+
initCounter() {
27+
const runCounter = () => {
28+
this._counterTimer = window.setTimeout(() => {
29+
this.counter++;
30+
runCounter();
31+
}, 1000)
32+
};
33+
runCounter();
34+
}
1535

1636
componentDidMount() {
17-
console.log("dom loaded!", 'home')
18-
// ...
37+
console.log("home dom loaded!")
38+
this.initCounter();
39+
}
40+
41+
componentWillUnmount() {
42+
console.log('home dom will unmount');
43+
window.clearTimeout(this._counterTimer);
44+
}
45+
46+
@watch('count', { immediate: true })
47+
countLogger(newVal, oldVal) {
48+
console.log('home countLogger', newVal, oldVal);
49+
this.loggerRunCount++;
50+
}
51+
52+
@computed()
53+
get counterGreet() {
54+
console.log('home @computed counterGreet')
55+
return `${this.text} ${this.counter} times`;
56+
}
57+
58+
componentDidUpdate(propName, oldVal, newVal) {
59+
console.log("home dom updated!", propName, oldVal, newVal)
1960
}
2061

2162
render() {
2263
return (
2364
<div className="main">
24-
home {this.text}
65+
home
66+
<br/>passed down count: {this.count}
67+
<br />watcher run count: {this.loggerRunCount}
68+
<br/>{this.counterGreet}
2569
</div>
2670
);
2771
}

demo4gluang/src/app-header/index.tsx

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ import { store } from '../store';
1010

1111
@customElement({ tag: "app-header", style })
1212
class MyComponent extends connectStore(QuarkElement) {
13-
1413
handleSwitch = () => {
1514
store.author = store.author === 'Sun Tzu' ? 'Guess who?' : 'Sun Tzu';
1615
}

demo4gluang/src/gluang.ts

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,19 +15,25 @@ export const connectStore = superclass => {
1515
constructor() {
1616
super();
1717
this._observers = [];
18+
}
1819

19-
// 区分 lit(lit 中存在 performUpdate)
20-
if(!this.performUpdate) {
21-
console.log('before update')
22-
this.update(); // quarkc 中先去执行
23-
}
20+
connectedCallback() {
21+
// 区分 lit(lit 中存在 performUpdate)
22+
if(!this.performUpdate) {
23+
this.update(true); // quarkc 中先去执行
24+
}
2425
}
2526

2627
// Your framework need this function to init observe state
27-
update() {
28-
console.log('gluang update')
28+
update(init = false) {
2929
stateRecorder.start();
30-
super.update();
30+
31+
if (!init) {
32+
super.update();
33+
} else {
34+
super.connectedCallback();
35+
}
36+
3137
this._initStateObservers();
3238
}
3339

packages/core/src/computed.ts

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -25,21 +25,21 @@ const queueMicroTask = (callback: (...args: any[]) => any) => {
2525
}
2626
};
2727

28-
const flushUpdatedQueue = (watchers: Watcher[]) => {
29-
let i = watchers.length
30-
31-
while (i--) {
32-
const watcher = watchers[i]
33-
const {
34-
render,
35-
inst,
36-
} = watcher
37-
38-
if (render && inst) {
39-
inst.flushUpdatedQueue()
40-
}
41-
}
42-
}
28+
// const flushUpdatedQueue = (watchers: Watcher[]) => {
29+
// let i = watchers.length
30+
31+
// while (i--) {
32+
// const watcher = watchers[i]
33+
// const {
34+
// render,
35+
// inst,
36+
// } = watcher
37+
38+
// if (render && inst) {
39+
// inst.flushUpdatedQueue()
40+
// }
41+
// }
42+
// }
4343

4444
/** flush watcher queue */
4545
const flushWatcherQueue = () => {
@@ -49,7 +49,7 @@ const flushWatcherQueue = () => {
4949
watcher.run()
5050
}
5151

52-
flushUpdatedQueue(watchers)
52+
// flushUpdatedQueue(watchers)
5353

5454
// reset
5555
watchers.length = 0

0 commit comments

Comments
 (0)