Skip to content

Commit e001d24

Browse files
authored
Merge pull request #76 from dyf19118/render-watcher
Render watcher
2 parents e83ba25 + 7367311 commit e001d24

16 files changed

Lines changed: 948 additions & 236 deletions

File tree

demo/src/index.tsx

Lines changed: 26 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.resolvedCount} />},
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,35 @@ 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!")
39-
// ...
45+
shouldComponentUpdate(propName, oldValue, newValue) {
46+
if (propName === 'innerCount') {
47+
return newValue <= 10;
48+
}
49+
50+
return true;
4051
}
4152

53+
componentDidUpdate() {
54+
console.log("parent dom updated!")
55+
}
56+
57+
// componentDidMount() {
58+
// console.log("parent dom loaded!")
59+
// }
60+
4261
render() {
4362
return (
4463
<>
@@ -56,7 +75,7 @@ class MyComponent extends QuarkElement {
5675
<h1>{this.text} Quarkc</h1>
5776

5877
<div className="card">
59-
<button onClick={this.add}>count is: {this.count}</button>
78+
<button onClick={this.add}>count is: {this.resolvedCount}</button>
6079
</div>
6180
</div>
6281
<ul>

demo/src/pages/child-second.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ class Child2 extends QuarkElement {
1616
])
1717

1818
componentDidMount() {
19+
console.log('dom loaded!', 'child-second')
1920
}
2021

2122
render() {

demo/src/pages/child.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ class Child1 extends QuarkElement {
1717

1818

1919
componentDidMount() {
20+
console.log('dom loaded!', 'child')
2021
}
2122

2223
goToLink() {

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!")
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
}

demo/src/pages/sub.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ class MyComponent extends QuarkElement {
1414

1515

1616
componentDidMount() {
17-
console.log("dom loaded!")
17+
console.log("dom loaded!", 'sub')
1818
// ...
1919
}
2020

demo4gluang/src/app-header/index.tsx

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { QuarkElement, customElement } from "quarkc"
1+
import { QuarkElement, customElement, state, watch, createRef } from "quarkc"
22
import style from "./index.less?inline"
33
import suntzu from "./suntzu.jpeg"
44

@@ -10,15 +10,39 @@ import { store } from '../store';
1010

1111
@customElement({ tag: "app-header", style })
1212
class MyComponent extends connectStore(QuarkElement) {
13+
@state()
14+
showAuthorName = false
1315

16+
@watch('showAuthorName')
17+
handleShowChange(newVal) {
18+
console.log('handleShowChange', newVal)
19+
store.author = newVal ? 'Sun Tzu' : 'Guess who?';
20+
this.$nextTick(() => {
21+
const { current: btn } = this.btn;
22+
if (btn) {
23+
console.log('nextTick, content of btn:', btn.textContent)
24+
}
25+
})
26+
}
27+
1428
handleSwitch = () => {
15-
store.author = store.author === 'Sun Tzu' ? 'Guess who?' : 'Sun Tzu';
29+
this.showAuthorName = !this.showAuthorName;
30+
}
31+
32+
componentDidUpdate(propName, oldVal, newVal) {
33+
console.log('componentDidUpdate', propName, oldVal, newVal)
1634
}
1735

36+
componentUpdated() {
37+
console.log('componentUpdated', this.showAuthorName, store.author)
38+
}
39+
40+
btn = createRef(null)
41+
1842
render() {
1943
return (
2044
<header>
21-
<h1 onClick={this.handleSwitch}>The Art of War - <span class="btn">{store.author}</span></h1>
45+
<h1 onClick={this.handleSwitch}>The Art of War - <span class="btn" ref={this.btn}>{store.author}</span></h1>
2246
{
2347
store.author === 'Sun Tzu' ?
2448
<img src={suntzu} alt="" /> : ''

demo4gluang/src/gluang.ts

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

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

2527
// Your framework need this function to init observe state
26-
update() {
28+
update(init = false) {
2729
stateRecorder.start();
28-
super.update();
30+
31+
if (!init) {
32+
super.update();
33+
} else {
34+
super.connectedCallback();
35+
}
36+
2937
this._initStateObservers();
3038
}
3139

packages/core/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "quarkc",
3-
"version": "1.0.58",
3+
"version": "2.0.0",
44
"description": "A Web Components framework",
55
"type": "module",
66
"main": "./lib/index.umd.js",

0 commit comments

Comments
 (0)