vaderjs 1.4.2-kml56 → 1.4.2-npiml56

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -2,6 +2,7 @@ window.vader = {
2
2
  version: '1.0.0'
3
3
  }
4
4
  globalThis.isServer = false
5
+ globalThis.preRender = false;
5
6
  window.hasRan = []
6
7
  globalThis.memoizedFunctions = []
7
8
  const memoizedRefs = []
@@ -11,40 +12,40 @@ const memoizedRefs = []
11
12
  * @param {HTMLElement} newNode
12
13
  * @returns
13
14
  */
14
- function calculateDiff(oldNode, newNode){
15
- if(oldNode === undefined || newNode === undefined){
15
+ function calculateDiff(oldNode, newNode) {
16
+ if (oldNode === undefined || newNode === undefined) {
16
17
  return []
17
18
  }
18
- let diff = []
19
- if(oldNode?.children.length > 0){
20
- for(var i = 0; i < oldNode.children.length; i++){
21
- diff.push(...calculateDiff(oldNode.children[i], newNode.children[i]))
22
-
23
- }
19
+ let diff = []
20
+ if (oldNode?.children.length > 0) {
21
+ for (var i = 0; i < oldNode.children.length; i++) {
22
+ diff.push(...calculateDiff(oldNode.children[i], newNode.children[i]))
23
+
24
+ }
24
25
  return diff
25
- }
26
- if(!oldNode?._isRoot){
27
- if(oldNode.nodeType === 3 && oldNode.nodeValue !== newNode.nodeValue){
28
- diff.push({type: 'REPLACE', oldNode, newNode})
26
+ }
27
+ if (!oldNode?._isRoot) {
28
+ if (oldNode.nodeType === 3 && oldNode.nodeValue !== newNode.nodeValue) {
29
+ diff.push({ type: 'REPLACE', oldNode, newNode })
29
30
  }
30
- else if(oldNode.nodeType === 1 && oldNode.innerHTML !== newNode.innerHTML
31
- ){
32
- diff.push({type: 'REPLACE', oldNode, newNode})
31
+ else if (oldNode.nodeType === 1 && oldNode.innerHTML !== newNode.innerHTML
32
+ ) {
33
+ diff.push({ type: 'REPLACE', oldNode, newNode })
33
34
  }
34
- else if(oldNode.nodeType === 1 && oldNode.tagName === newNode.tagName){
35
- for(var i = 0; i < oldNode.attributes.length; i++){
36
- if(oldNode.attributes[i].value !== newNode.attributes[i].value){
37
- diff.push({type: 'PROPS', oldNode, newNode})
35
+ else if (oldNode.nodeType === 1 && oldNode.tagName === newNode.tagName) {
36
+ for (var i = 0; i < oldNode.attributes.length; i++) {
37
+ if (oldNode.attributes[i].value !== newNode.attributes[i].value) {
38
+ diff.push({ type: 'PROPS', oldNode, newNode })
38
39
  }
39
40
  }
40
41
  }
41
- }
42
+ }
43
+
42
44
 
43
-
44
45
 
45
46
  return diff
46
47
  }
47
-
48
+
48
49
  /**
49
50
  * @description This function is used to generate functonal components
50
51
  * @param {Function} tag
@@ -52,38 +53,50 @@ function calculateDiff(oldNode, newNode){
52
53
  * @param {...any} children
53
54
  * @returns {Object} - The first child of the functional component
54
55
  */
55
- function generateJSX(tag, props, ...children){
56
- let node = {
57
- state: {},
58
- mainFunction: tag,
59
- _key: tag.name || Math.random().toString(36).substring(7),
60
- $$typeof: 'JSX_CHILD',
61
- firstChild:null,
62
- children: children,
63
- _name: tag.name,
64
- }
65
-
66
- node.firstChild = tag()
67
- node.firstChild.htmlNode._key = node._key
56
+ function generateJSX(tag, props, ...children) {
57
+ let node = {
58
+ state: {},
59
+ mainFunction: tag,
60
+ _key: tag.name || Math.random().toString(36).substring(7),
61
+ $$typeof: 'JSX_CHILD',
62
+ firstChild: null,
63
+ children: children,
64
+ _name: tag.name,
65
+ }
66
+
67
+ let comp = new Component({ $$key: node._key })
68
+ tag = tag.bind(comp)
69
+ comp.render = () => {
70
+ let childObj = children.map((child) => {
71
+ if (typeof child === 'function') {
72
+ return generateJSX(child, props, ...child.children)
73
+ }
74
+ return child
75
+ })
76
+ console.log(childObj)
77
+ return tag({...props, children:childObj[0] || childObj})
78
+ }
79
+ node.firstChild = comp.render()
80
+ node.firstChild.htmlNode._key = node._key
68
81
  node.firstChild.htmlRoot = node
69
- node.firstChild.htmlNode._isRoot = true
70
- node.firstChild.props = props || {}
82
+ node.firstChild.htmlNode._isRoot = true
83
+ node.firstChild.props = props || {}
71
84
  node.firstChild._isRoot = true
72
85
  node.firstChild._key = node._key
73
- node.firstChild.props['key'] = node._key
74
- return node.firstChild
75
-
86
+ node.firstChild.props['key'] = node._key
87
+ return node.firstChild
88
+
76
89
 
77
90
  }
78
- function handleStyles(styles, nodeEl) {
91
+ function handleStyles(styles, nodeEl) {
79
92
 
80
93
  for (let key in styles) {
81
- if(typeof styles[key] === 'object'){
94
+ if (typeof styles[key] === 'object') {
82
95
  handleStyles(styles[key], nodeEl)
83
96
  }
84
97
  nodeEl.style[key] = styles[key];
85
98
  }
86
-
99
+
87
100
  }
88
101
 
89
102
 
@@ -95,166 +108,167 @@ let hasBeenCalled = []
95
108
  * @param {...any} children
96
109
  * @returns {Object} - The virtual DOM element
97
110
  */
98
- function Element(tag, props, ...children){
111
+ function Element(tag, props, ...children) {
112
+ console.log(tag)
99
113
  !props ? props = {} : null
100
- if(!props?.['$$key']){
114
+ if (!props?.['$$key']) {
101
115
  props['$$key'] = tag.name || Math.random().toString(36).substring(7)
102
116
  }
103
117
 
104
- if(typeof tag === 'function'){
105
- return generateJSX(tag, props, children)
118
+ if (typeof tag === 'function') {
119
+ return generateJSX(tag, props, children)
106
120
  }
107
121
  let node = {
108
122
  tag: tag,
109
123
  props: props,
110
- children: children,
124
+ children: children,
111
125
  _key: props['$$key'],
112
126
  events: [],
113
127
  staticEl: document.createElement(tag),
114
128
  parentNode: null
115
129
  }
116
- for(var i = 0; i < children.length; i++){
117
- if(typeof children[i] === 'string' || typeof children[i] === 'number'){
118
- children[i] = {
119
- tag: 'TEXT_ELEMENT',
120
- props: {nodeValue: children[i]},
121
- _key: props['$$key'],
122
- parentNode: {tag: tag, props: props, children: children, _key: props['$$key']},
123
- children: []
130
+ for (var i = 0; i < children.length; i++) {
131
+ if (typeof children[i] === 'string' || typeof children[i] === 'number') {
132
+ children[i] = {
133
+ tag: 'TEXT_ELEMENT',
134
+ props: { nodeValue: children[i] },
135
+ _key: props['$$key'],
136
+ parentNode: { tag: tag, props: props, children: children, _key: props['$$key'] },
137
+ children: []
138
+ }
139
+ } else {
140
+ if (children[i]) {
141
+ children[i].parentNode = { tag: tag, props: props, children: children }
142
+ }
124
143
  }
125
- }else{
126
- if(children[i]){
127
- children[i].parentNode = {tag: tag, props: props, children: children}
128
- }
129
- }
130
- }
144
+ }
131
145
  let nodeEl = node.tag === 'TEXT_ELEMENT' ? document.createTextNode('') : document.createElement(node.tag)
132
146
  node.staticEl = nodeEl
133
-
134
- for(var key in props){
135
- if(key.toLowerCase().startsWith('on')){
136
- nodeEl.addEventListener(key.substring(2).toLowerCase(), props[key])
137
- node.events.push({type: key.substring(2).toLowerCase(), listener: props[key]})
138
- continue
139
- }
140
- if(key === '$$key' && !nodeEl._key && nodeEl.nodeType === 1
141
- ){
142
- Object.defineProperty(nodeEl, '_key', {
143
- value: props[key],
144
- writable: true
145
- })
146
- continue
147
- }
148
-
149
- if(nodeEl && nodeEl.nodeType === 1){
147
+
148
+ for (var key in props) {
149
+ if (key.toLowerCase().startsWith('on')) {
150
+ nodeEl.addEventListener(key.substring(2).toLowerCase(), props[key])
151
+ node.events.push({ type: key.substring(2).toLowerCase(), listener: props[key] })
152
+ continue
153
+ }
154
+ if (key === '$$key' && !nodeEl._key && nodeEl.nodeType === 1
155
+ ) {
156
+ Object.defineProperty(nodeEl, '_key', {
157
+ value: props[key],
158
+ writable: true
159
+ })
160
+ continue
161
+ }
162
+
163
+ if (nodeEl && nodeEl.nodeType === 1) {
150
164
  nodeEl.setAttribute(key, props[key])
151
165
  }
152
-
166
+
153
167
  }
154
- if(props.style){
168
+ if (props.style) {
155
169
  handleStyles(props.style, nodeEl)
156
170
  }
157
171
 
158
- if(props.id){
172
+ if (props.id) {
159
173
  nodeEl.id = props.id
160
174
  }
161
- if(props.ref){
162
- switch(true){
163
- case Array.isArray(props.ref.current):
164
- if(!props.ref.current.find((el) => el === nodeEl)){
165
- props.ref.current.push(nodeEl)
166
- }
167
- break;
168
- case props.ref.current === HTMLElement:
169
- props.ref.current = nodeEl
170
- break;
171
- case props.ref.current === null:
172
- props.ref.current = nodeEl
173
- break;
174
- case typeof props.ref === 'function' && !window.hasRan.includes(props.ref):
175
- window.hasRan.push(props.ref)
176
- props.ref(nodeEl)
177
- setTimeout(() => {
178
- window.hasRan.filter((el) => el !== props.ref)
179
- }, 0)
180
- break;
181
- default:
182
- props.ref.current = nodeEl
183
- break;
184
-
185
- }
175
+ if (props.ref) {
176
+ switch (true) {
177
+ case Array.isArray(props.ref.current):
178
+ if (!props.ref.current.find((el) => el === nodeEl)) {
179
+ props.ref.current.push(nodeEl)
180
+ }
181
+ break;
182
+ case props.ref.current === HTMLElement:
183
+ props.ref.current = nodeEl
184
+ break;
185
+ case props.ref.current === null:
186
+ props.ref.current = nodeEl
187
+ break;
188
+ case typeof props.ref === 'function' && !window.hasRan.includes(props.ref):
189
+ window.hasRan.push(props.ref)
190
+ props.ref(nodeEl)
191
+ setTimeout(() => {
192
+ window.hasRan.filter((el) => el !== props.ref)
193
+ }, 0)
194
+ break;
195
+ default:
196
+ props.ref.current = nodeEl
197
+ break;
198
+
199
+ }
186
200
  }
187
201
  node['htmlNode'] = nodeEl
188
-
189
-
190
- if(nodeEl.nodeType === 1){
191
- for(var i = 0; i < children.length; i++){
192
- if(children[i]){
193
- if(children[i].tag === 'TEXT_ELEMENT'){
194
- nodeEl.appendChild(document.createTextNode(children[i].props.nodeValue))
195
- }
196
- nodeEl.appendChild(Element(children[i].tag, children[i].props, ...children[i].children).htmlNode)
197
- }
198
- }
199
-
200
- }
201
-
202
+
203
+
204
+ if (nodeEl.nodeType === 1) {
205
+ for (var i = 0; i < children.length; i++) {
206
+ if (children[i]) {
207
+ if (children[i].tag === 'TEXT_ELEMENT') {
208
+ nodeEl.appendChild(document.createTextNode(children[i].props.nodeValue))
209
+ }
210
+ nodeEl.appendChild(Element(children[i].tag, children[i].props, ...children[i].children).htmlNode)
211
+ }
212
+ }
213
+
214
+ }
215
+
202
216
  return node;
203
217
  }
204
-
205
218
 
206
- function handleDiff(diff){
207
- for(var i = 0; i < diff.length; i++){
208
- switch(true){
209
- case diff[i].type === 'REPLACE' && !diff[i].oldNode._isRoot:
210
- let parent = diff[i].oldNode.parentNode
219
+
220
+ function handleDiff(diff) {
221
+ for (var i = 0; i < diff.length; i++) {
222
+ switch (true) {
223
+ case diff[i].type === 'REPLACE' && !diff[i].oldNode._isRoot:
224
+ let parent = diff[i].oldNode.parentNode
211
225
  diff[i].oldNode.parentNode.replaceChild(diff[i].newNode, diff[i].oldNode)
212
226
  break;
213
- case diff[i].type === 'PROPS':
227
+ case diff[i].type === 'PROPS':
214
228
  break;
215
- }
229
+ }
216
230
  }
217
231
 
218
232
  }
219
233
  let states = {}
220
- export const useState = (name, initialValue) => {}
221
- export function useRef(name, initialValue){
234
+ export const useState = (name, initialValue) => { }
235
+ export function useRef(name, initialValue) {
222
236
  let ref = initialValue
223
- if(!memoizedRefs.find((el) => el.name === name)){
224
- memoizedRefs.push({name, ref})
225
- }
226
- let getRef = () => memoizedRefs.find((el) => el.name === name).ref
227
- let setRef = (newValue) => {
228
- memoizedRefs.find((el) => el.name === name).ref = newValue
229
- }
230
- return {
237
+ if (!memoizedRefs.find((el) => el.name === name)) {
238
+ memoizedRefs.push({ name, ref })
239
+ }
240
+ let getRef = () => memoizedRefs.find((el) => el.name === name).ref
241
+ let setRef = (newValue) => {
242
+ memoizedRefs.find((el) => el.name === name).ref = newValue
243
+ }
244
+ return {
231
245
  current: getRef(),
232
246
  name,
233
247
  }
234
248
  }
235
- export function Mounted(fn, node){
236
- let el = Array.from(document.querySelectorAll('*')).find((el) => el._key === memoizedFunctions.find((el) => el.mainFunction === node)._key)
237
- if(el && !hasBeenCalled.find((el) => el === node)){
249
+ export function Mounted(fn, node) {
250
+ let el = Array.from(document.querySelectorAll('*')).find((el) => el._key === memoizedFunctions.find((el) => el.mainFunction === node)._key)
251
+ if (el && !hasBeenCalled.find((el) => el === node)) {
238
252
  fn()
239
253
  hasBeenCalled.push(node)
240
254
  }
241
- else{
255
+ else {
242
256
  setTimeout(() => {
243
257
  Mounted(fn, node)
244
258
  }, 0)
245
259
  }
246
260
  }
247
-
248
-
249
- let effects = [];
250
261
 
251
- export function useEffect(fn, deps){
252
- if(!effects.find((el) => el.fn.toString() === fn.toString())){
253
- effects.push({fn, deps})
262
+
263
+ let effects = [];
264
+
265
+ export function useEffect(fn, deps) {
266
+ if (!effects.find((el) => el.fn.toString() === fn.toString())) {
267
+ effects.push({ fn, deps })
254
268
  }
255
- else{
269
+ else {
256
270
  let effect = effects.find((el) => el.fn.toString() === fn.toString())
257
- if(effect.deps.toString() !== deps.toString()){
271
+ if (effect.deps.toString() !== deps.toString()) {
258
272
  effect.deps = deps
259
273
  effect.fn()
260
274
  }
@@ -264,154 +278,143 @@ export function useEffect(fn, deps){
264
278
  }
265
279
 
266
280
  }
267
- export function useReducer(name, reducer, vnode, initialState){
281
+ export function useReducer(name, reducer, vnode, initialState) {
268
282
  let [state, setState] = useState(name, vnode, initialState)
269
283
  let dispatch = (action) => {
270
284
  let newState = reducer(state, action)
271
285
  setState(newState)
272
286
  }
273
287
 
274
- return [state, dispatch]
288
+ return [state, dispatch]
275
289
 
276
290
  }
277
291
 
278
292
  class Component {
279
- constructor(props){
293
+ constructor(props) {
280
294
  this.props = props
281
295
  this._key = props['$$key'] || Math.random().toString(36).substring(7)
282
296
  this.state = {}
283
297
  this.htmlNode = null
284
298
  this.firstChild = null
285
299
  this.Element = Element
300
+ this.passiveProps = {}
286
301
  }
287
302
 
288
303
 
289
- setState(newState){
304
+ setState(newState) {
290
305
  this.state = newState
291
306
  }
292
307
 
293
- handleDiff(diff){
294
- for(var i = 0; i < diff.length; i++){
295
- switch(true){
296
- case diff[i].type === 'REPLACE' && !diff[i].oldNode._isRoot:
297
- let parent = diff[i].oldNode.parentNode
308
+ handleDiff(diff) {
309
+ for (var i = 0; i < diff.length; i++) {
310
+ switch (true) {
311
+ case diff[i].type === 'REPLACE' && !diff[i].oldNode._isRoot:
312
+ let parent = diff[i].oldNode.parentNode
298
313
  diff[i].oldNode.parentNode.replaceChild(diff[i].newNode, diff[i].oldNode)
299
314
  break;
300
- case diff[i].type === 'PROPS':
315
+ case diff[i].type === 'PROPS':
301
316
  break;
302
- }
317
+ }
303
318
  }
304
319
  }
305
- calculateDiff(oldNode, newNode){
306
- if(oldNode === undefined || newNode === undefined){
320
+ calculateDiff(oldNode, newNode) {
321
+ if (oldNode === undefined || newNode === undefined) {
307
322
  return []
308
323
  }
309
- let diff = []
310
- if(oldNode?.children.length > 0){
311
- for(var i = 0; i < oldNode.children.length; i++){
312
- diff.push(...this.calculateDiff(oldNode.children[i], newNode.children[i]))
313
-
314
- }
324
+ let diff = []
325
+ if (oldNode?.children.length > 0) {
326
+ for (var i = 0; i < oldNode.children.length; i++) {
327
+ diff.push(...this.calculateDiff(oldNode.children[i], newNode.children[i]))
328
+
329
+ }
315
330
  return diff
316
- }
317
- if(!oldNode?._isRoot){
318
- if(oldNode.nodeType === 3 && oldNode.nodeValue !== newNode.nodeValue){
319
- diff.push({type: 'REPLACE', oldNode, newNode})
331
+ }
332
+ if (!oldNode?._isRoot) {
333
+ if (oldNode.nodeType === 3 && oldNode.nodeValue !== newNode.nodeValue) {
334
+ diff.push({ type: 'REPLACE', oldNode, newNode })
320
335
  }
321
- else if(oldNode.nodeType === 1 && oldNode.innerHTML !== newNode.innerHTML
322
- ){
323
- diff.push({type: 'REPLACE', oldNode, newNode})
336
+ else if (oldNode.nodeType === 1 && oldNode.innerHTML !== newNode.innerHTML
337
+ ) {
338
+ diff.push({ type: 'REPLACE', oldNode, newNode })
324
339
  }
325
- else if(oldNode.nodeType === 1 && oldNode.tagName === newNode.tagName){
326
- for(var i = 0; i < oldNode.attributes.length; i++){
327
- if(oldNode.attributes[i].value !== newNode.attributes[i].value){
328
- diff.push({type: 'PROPS', oldNode, newNode})
340
+ else if (oldNode.nodeType === 1 && oldNode.tagName === newNode.tagName) {
341
+ for (var i = 0; i < oldNode.attributes.length; i++) {
342
+ if (oldNode.attributes[i].value !== newNode.attributes[i].value) {
343
+ diff.push({ type: 'PROPS', oldNode, newNode })
329
344
  }
330
345
  }
331
346
  }
332
- }
347
+ }
348
+
333
349
 
334
-
335
350
 
336
351
  return diff
337
352
  }
338
- useState(name, initialValue){
339
- if(!this.state[name]){
353
+ useState(name, initialValue) {
354
+ if (!this.state[name]) {
340
355
  this.state[name] = initialValue
341
- }
342
-
356
+ }
357
+
343
358
  let getState = () => this.state[name]
344
- let setState = (newValue) => {
345
- let dEl = this.firstChild.htmlNode
346
- if(dEl.tagName === 'HTML'){
347
- let firstChild = dEl.querySelector('body').firstChild
348
- dEl = firstChild
349
- }
350
- this.state[name] = newValue
351
- let el = Array.from(document.querySelectorAll('*')).find((el) =>{
359
+ let setState = (newValue) => {
360
+ let dEl = this.firstChild.htmlNode
361
+ if (dEl.tagName === 'HTML') {
362
+ dEl = dEl.querySelector('body')
363
+ dEl.firstChild._key = this._key
364
+ }
365
+ this.state[name] = newValue
366
+ let el = Array.from(document.querySelectorAll('*')).find((el) => {
352
367
  return el._key === dEl._key
353
- })
354
- let diff = calculateDiff(el, this.render().htmlNode.tagName === 'HTML' ? this.render().htmlNode.querySelector('body').firstChild : this.render().htmlNode)
368
+ })
369
+ let diff = calculateDiff(el, this.render(this.passiveProps).htmlNode.tagName === 'HTML' ? this.render(this.passiveProps).htmlNode.querySelector('body') : this.render(this.passiveProps).htmlNode)
355
370
  handleDiff(diff)
356
-
357
- }
358
- return [getState, setState]
371
+
372
+ }
373
+ return [getState, setState]
359
374
 
360
375
  }
361
376
  useEffect = useEffect
362
- useReducer(name, reducer, initialState){
377
+ useReducer(name, reducer, initialState) {
363
378
  let [state, setState] = this.useState(name, initialState)
364
379
  let dispatch = (action) => {
365
- let newState = reducer(state(), action)
380
+ let newState = reducer(state(), action)
366
381
  setState(newState)
367
382
  }
368
383
 
369
- return [state, dispatch]
384
+ return [state, dispatch]
370
385
  }
371
386
 
372
- render(){
387
+ render() {
373
388
  return null
374
389
  }
375
390
 
376
391
  }
377
-
378
- export async function render(vnode, container, ...passProps){
379
-
380
- if(!vnode){
392
+
393
+ export async function render(vnode, container, ...passProps) {
394
+
395
+ if (!vnode) {
381
396
  throw new Error('No vnode was provided')
382
397
  }
383
398
  // create an object for the node then bind to firstChild
384
- let comp = new Component({$$key: vnode.name || Math.random().toString(36).substring(7)})
399
+ let comp = new Component({ $$key: vnode.name || Math.random().toString(36).substring(7) })
385
400
  vnode = vnode.bind(comp)
386
- comp.render = () => {
387
- return vnode(...passProps)
401
+ if(passProps.length > 0){
402
+
403
+ passProps = {
404
+ req: passProps[0],
405
+ res: passProps[1]
406
+ }
407
+ comp.passiveProps = passProps
388
408
  }
389
-
409
+ comp.render = () => {
410
+ return vnode(comp.passiveProps)
411
+ }
412
+
390
413
  comp.firstChild = comp.render()
391
414
 
392
- if(comp.firstChild.htmlNode.tagName === 'HTML'){
393
- let hasHead = comp.firstChild.htmlNode.querySelector('head') ? true : false
394
- let hasBody = comp.firstChild.htmlNode.querySelector('body') ? true : false
395
-
396
- if(hasHead){
397
- document.head.innerHTML = comp.firstChild.htmlNode.querySelector('head').innerHTML
398
- comp.firstChild.children = comp.firstChild.children.filter((el) =>{
399
- return el.htmlNode.tagName !== 'HEAD'
400
- })
401
- }
402
- if(hasBody){
403
- comp.firstChild.children = comp.firstChild.children.filter((el) =>{
404
- if(el.htmlNode.tagName == 'BODY'){
405
- comp.firstChild = el.children[0]
406
- }
407
- })
408
- }
409
- }
410
- container.innerHTML = ''
411
-
412
- container.appendChild(comp.firstChild.htmlNode)
413
-
414
-
415
+ container.innerHTML = ''
416
+ container.replaceWith(comp.firstChild.htmlNode)
417
+
415
418
  }
416
-
419
+
417
420
  export default Element