-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreactFAKE.js
240 lines (219 loc) · 9.81 KB
/
reactFAKE.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
(() => {
const createElement = (type, props, ...children) => {
if (props === null) props = {};
return {type, props, children};
};
// a
const setAttribute = (dom, key, value) => {
if (typeof value == 'function' && key.startsWith('on')) {
const eventType = key.slice(2).toLowerCase();
dom._FakeReactHandlers = dom._FakeReactHandlers || {};
dom.removeEventListener(eventType, dom._FakeReactHandlers[eventType]);
console.dir(dom)
dom._FakeReactHandlers[eventType] = value;
dom.addEventListener(eventType, dom._FakeReactHandlers[eventType]);
} else if (key == 'checked' || key == 'value' || key == 'className') {
dom[key] = value;
} else if (key == 'style' && typeof value == 'object') {
Object.assign(dom.style, value);
} else if (key == 'ref' && typeof value == 'function') {
value(dom);
} else if (key == 'key') {
dom._FakeReactKey = value;
} else if (typeof value != 'object' && typeof value != 'function') {
dom.setAttribute(key, value);
}
};
const render = (vdom, parent=null) => {
const mount = parent ? (el => parent.appendChild(el)) : (el => el);
if (typeof vdom == 'string' || typeof vdom == 'number') {
return mount(document.createTextNode(vdom));
} else if (typeof vdom == 'boolean' || vdom === null) {
return mount(document.createTextNode(''));
} else if (typeof vdom == 'object' && typeof vdom.type == 'function') {
// Với Trường này thì sẽ phải đệ quy đến khi nào hết cái vdom.type == 'function ' thì thôi, vì nó đang ở trạng thái class,
// chúng ta phải mò vào bên trong hàm render của component , lấy dom nó ra là check cái dom của nó tiếp
console.log("check vdom.type function", vdom.type)
// if vdom.type is class
return Component.render(vdom, parent);
} else if (typeof vdom == 'object' && typeof vdom.type == 'string') {
// if vdom.type is string => return dom .. this case only return dom
const dom = mount(document.createElement(vdom.type));
console.log("vdom.type string" , vdom)
//backtrack algo
for (const child of [].concat(...vdom.children)) render(child, dom);
for (const prop in vdom.props) setAttribute(dom, prop, vdom.props[prop]);
console.log("fullll " , dom)
return dom;
} else {
throw new Error(`Invalid VDOM: ${vdom}.`);
}
};
// path x
const patch = (dom, vdom, parent=dom.parentNode) => {
console.log("tesst dom ", dom , "tesst vdom ", vdom , "parent" , parent)
const replace = parent ? el => (parent.replaceChild(el, dom) && el) : (el => el);
// case vdom.type is class , and vdom object
if (typeof vdom == 'object' && typeof vdom.type == 'function') {
return Component.patch(dom, vdom, parent);
}
else if (typeof vdom != 'object' && dom instanceof Text) {
return dom.textContent != vdom ? replace(render(vdom, parent)) : dom;
}
else if (typeof vdom == 'object' && dom instanceof Text) {
return replace(render(vdom, parent));
}
else if (typeof vdom == 'object' && dom.nodeName != vdom.type.toUpperCase()) {
return replace(render(vdom, parent));
}
else if (typeof vdom == 'object' && dom.nodeName == vdom.type.toUpperCase()) {
const pool = {};
const active = document.activeElement;
[].concat(...dom.childNodes).map((child, index) => {
const key = child._FakeReactKey || `index_${index}`;
pool[key] = child;
});
[].concat(...vdom.children).map((child, index) => {
const key = child.props && child.props.key || `__index_${index}`;
dom.appendChild(pool[key] ? patch(pool[key], child) : render(child, dom));
delete pool[key];
});
for (const key in pool) {
const instance = pool[key]._FakeReactInstance;
if (instance) instance.componentWillUnmount();
pool[key].remove();
}
//backtrack algo
for (const attr of dom.attributes) dom.removeAttribute(attr.name);
for (const prop in vdom.props) setAttribute(dom, prop, vdom.props[prop]);
active.focus();
return dom;
}
};
// hàm render sử dụng Component render
// render static là hàm sẽ check và lấy dom tử các hàm render trong các class ,
class Component {
constructor(props) {
this.props = props || {};
this.state = null;
}
static render(vdom, parent=null) {
//spead props , and vdom children => cũng là
const props = Object.assign({}, vdom.props, {children: vdom.children});
console.log("vdom . type " , vdom.type)
if (Component.isPrototypeOf(vdom.type)) {
const instance = new (vdom.type)(props); // instance is vdom
console.log("instance" ,instance)
// if(instance._FakeReactClass.getDerivedStateFromProps){
// instance._FakeReactClass.getDerivedStateFromProps(this.props, this.state)
// }
//save object base in instance, why ??
// save instance in base, when setState , we will use object base get instance
instance.base = render(instance.render(), parent);
// save instance and key
instance.base._FakeReactClass = vdom.type
instance.base._FakeReactInstance = instance;
instance.base._FakeReactKey = vdom.props.key;
instance.componentDidMount();
console.log("instance.base " , instance.base)
return instance.base;
} else {
// TH nó là function chẳng hạn mà ko phải là một class extends Component
return render(vdom.type(props), parent);
}
}
// static render(vdom , parent = null) {
// const props = Object.assign({} , vdom.props, {children : vdom.children})
// if(Component.is )
// }
// mỗi dom sẽ lưu các thông tin nha instance , key
static patch(dom, vdom, parent=dom.parentNode) {
const props = Object.assign({}, vdom.props, {children: vdom.children});
if (dom._FakeReactInstance && dom._FakeReactInstance.constructor == vdom.type) {
// Component.getDerivedStateFromProps(props, this.state);
dom._FakeReactInstance.props = props;
return patch(dom, dom._FakeReactInstance.render(), parent);
} else if (Component.isPrototypeOf(vdom.type)) {
const ndom = Component.render(vdom, parent);
return parent ? (parent.replaceChild(ndom, dom) && ndom) : (ndom);
} else if (!Component.isPrototypeOf(vdom.type)) {
return patch(dom, vdom.type(props), parent);
}
}
// static patch(dom , vdom , parent = dom.parentNode) {
// const props = Object.assign({}, vdom.props , { children : vdom.children})
// // if(dom._FakeReactInstance && dom._FakeReactInstance.con)
// }
setState(next) {
// check state is object and is para object ??
const compat = (a) => typeof this.state == 'object' && typeof a == 'object';
// i want getDerivedStateFromProps check update
if (this.base ) {
if(this.base._FakeReactClass.getDerivedStateFromProps && this.base._FakeReactClass.getDerivedStateFromProps(this.state, this.props) ){
const prevState = this.state;
this.state = compat(next) ? Object.assign({}, this.state, next) : next; //new state
this.shouldComponentUpdate(this.props ,this.state )
patch(this.base, this.render()); // process render
this.getSnapshotBeforeUpdate(this.props ,prevState )
this.componentDidUpdate(this.props, prevState);
}
else if(this.base._FakeReactClass.getDerivedStateFromProps && !this.base._FakeReactClass.getDerivedStateFromProps(this.state, this.props)){
}
else if(Component.getDerivedStateFromProps(this.props, this.state)){
const prevState = this.state;
this.state = compat(next) ? Object.assign({}, this.state, next) : next; //new state
this.shouldComponentUpdate(this.props ,this.state )
patch(this.base, this.render()); // process render
this.getSnapshotBeforeUpdate(this.props ,prevState )
this.componentDidUpdate(this.props, prevState);
}
// if(this.base._FakeReactClass.getDerivedStateFromProps){
// this.base._FakeReactClass.getDerivedStateFromProps(this.props, this.state)
// }
// else{
// Component.getSnapshotBeforeUpdate(nextProps , preState)
// }
// f
} else {
this.state = compat(next) ? Object.assign({}, this.state, next) : next;
}
}
getSnapshotBeforeUpdate(prevProps, prevState) {
return nextProps != this.props || nextState != this.state;
}
static getDerivedStateFromProps(nextProps , preState) {
return true;
}
shouldComponentUpdate(nextState, nextProps){
return undefined;
}
componentDidUpdate(prevProps, prevState) {
return undefined;
}
componentDidMount() {
return undefined;
}
componentWillUnmount() {
return undefined;
}
};
console.log(module)
module.exports = {createElement, render, Component};
// if (typeof module == 'undefined') window.Gooact = {createElement, render, Component};
})();
// class A extends Shape {
// constructor(a ,b) {
// super(a, b);
// this.AA = a
// this.BB = b
// }
// }
// function A ( AA ,BB){
// }
// A.prototype.AAA = function (x, y) {
// return "ABDVJB"
// }
// var B = function( C,b) {
// A.call(x,y)
// }
// B.prototype = Object.create(A.prototype)