vaderjs 1.3.3-alpha-22 → 1.3.3-alpha-24

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.
package/client/index.js CHANGED
@@ -170,19 +170,7 @@ export class Component {
170
170
  this.vdom = []
171
171
 
172
172
  this.children = []
173
- let dom = new DOMParser().parseFromString(this.render(), 'text/html').body.firstChild;
174
- dom.querySelectorAll('*').forEach((el) => {
175
- let obj = {
176
- el: el,
177
- tag: el.tagName,
178
- html: el.innerHTML,
179
- content: el.textContent,
180
- value: el.value,
181
- attributes: el.attributes,
182
- children: el.childNodes,
183
- }
184
- this.vdom.push(obj)
185
- });
173
+
186
174
 
187
175
  /**
188
176
  * Parent of the current component.
@@ -261,13 +249,13 @@ export class Component {
261
249
  throw new Error('new components must have a key')
262
250
  }
263
251
  let comp = new component(props);
264
- if(this.components[props.key]){
265
- return this.components[props.key]
266
- }
252
+
267
253
  comp['props'] = props;
268
254
  comp.children = children;
269
255
  comp.props.children = children.join('')
270
256
  comp.parentNode = this;
257
+ comp.request = this.request;
258
+ comp.response = this.response;
271
259
  comp.key = props.key || null;
272
260
 
273
261
  this.components[props.key] = comp
@@ -286,7 +274,8 @@ export class Component {
286
274
  }
287
275
 
288
276
  let comp = this.components[component.key];
289
-
277
+ console.log(component.props)
278
+ comp.props = component.props;
290
279
  let h = comp.render()
291
280
 
292
281
  if(h && h.split('>,').length > 1){
@@ -420,7 +409,7 @@ export class Component {
420
409
  */
421
410
  bind(funcData,jsx,ref, paramNames, ...params) {
422
411
 
423
-
412
+
424
413
  const name = `func_${crypto ? crypto.getRandomValues(new Uint32Array(1))[0] : Math.random()}`;
425
414
 
426
415
  var dynamicFunction = async (...params) => {
@@ -434,29 +423,22 @@ export class Component {
434
423
 
435
424
  paramNames = paramNames.replace(/,,/g, ',');
436
425
  let newparamnames = paramNames.replaceAll(',,', ',')
437
- params.forEach((param, index) => {
438
- if(param && Object.keys(param).includes('detail')){
439
- param = param['detail']['target']['event']
440
- params[index] = param
441
- }
442
- });
443
- // Remove trailing commas
444
- paramNames.endsWith(',') ? paramNames = paramNames.slice(0, -1) : null;
445
- console.log(paramNames)
446
-
447
- params.forEach((param, index) => {
448
- paramObject[newparamnames.split(',')[index]] = param;
449
- });
426
+
427
+
428
+ for(var i in params){
429
+ paramObject[newparamnames.split(',')[i]] = params[i]
430
+ }
431
+
432
+
450
433
 
451
434
  paramNames = paramNames.replace(',,', ',');
452
435
  let func = new Function(`${paramNames}`, `
453
436
  return (async (${paramNames}) => {
454
- ${funcData}
437
+ ${funcData}
455
438
  })(${Object.keys(paramObject).join(',')})
456
- `);
457
-
458
- // Bind and execute the function with the provided parameters
459
- await func.bind(this)(...Object.values(paramObject));
439
+ `);
440
+ await func.bind(this)(...Object.values(paramObject));
441
+
460
442
  };
461
443
 
462
444
 
@@ -464,7 +446,7 @@ export class Component {
464
446
  if (!this.functionMap.has(name)) {
465
447
  document.addEventListener(`call_${name}`, (e) => {
466
448
 
467
- dynamicFunction(params);
449
+ dynamicFunction(params)
468
450
  this.functionMap.set(e.detail.name, {
469
451
  lastUsed: Date.now(),
470
452
  });
@@ -489,12 +471,8 @@ export class Component {
489
471
  event.target.setAttribute('data-ref', '${ref}');
490
472
  let reference = event.target.getAttribute('data-ref');
491
473
  event.target.eventData = event;
492
-
493
- let domquery = queryRef(reference);
494
-
495
- domquery.eventData = event;
496
- domquery.eventData.target = domquery;
497
- call('${name}', {event:domquery.eventData}, '${paramNames}')
474
+ event.target.data = event
475
+ call('${name}', {event:event.target.data}, '${paramNames}')
498
476
  })(event)
499
477
  `;
500
478
  }
@@ -552,28 +530,45 @@ export class Component {
552
530
  }
553
531
 
554
532
  /**
555
- * Uses state to dynamically update the component.
556
- * @method useState
557
- * @param {string} [key=null] - The auto-generated key.
558
- * @param {*} initialState - The initial state.
559
- * @param {Component.render} [func=null] - The render function.
560
- * @returns {Array} - An array containing the state value and the setter function.
561
- */
562
- useState(key = null, initialState) {
563
- if (!this.state[key]) {
564
- this.state[key] = initialState;
565
- }
566
- let updatedValue = () => this.state[key];
567
- const getValue = updatedValue()
568
- const set = (newValue, hook) => {
569
- this.state[key] = newValue;
570
- this.hydrate(hook);
571
- };
572
- this[`set${key}`] = set;
573
- this[`get${key}`] = getValue;
574
- return [getValue, set];
533
+ * useState hook.
534
+ *
535
+ * @template T
536
+ * @param {string} key - The key for the state property.
537
+ * @param {T} initialState - The initial state value.
538
+ * @returns {[() => T, (newValue: T, hook: Function) => void]} - A tuple with getter and setter functions.
539
+ */
540
+ useState(key, initialState) {
541
+ if (!this.state[key]) {
542
+ this.state[key] = initialState;
575
543
  }
576
544
 
545
+ /**
546
+ * Get the current state value.
547
+ *
548
+ * @returns {T} The current state value.
549
+ */
550
+ let updatedValue = () => this.state[key];
551
+
552
+ const getValue = updatedValue();
553
+
554
+ /**
555
+ * Set a new value for the state.
556
+ *
557
+ * @param {T} newValue - The new value to set.
558
+ * @param {Function} hook - The hook to hydrate after setting the value.
559
+ */
560
+ const set = (newValue, hook) => {
561
+ this.state[key] = newValue;
562
+ this.hydrate(hook);
563
+ };
564
+
565
+
566
+
567
+ return [this.state[key], set];
568
+ }
569
+
570
+
571
+
577
572
  useRef(key = null, initialState) {
578
573
  if (!this.state[key]) {
579
574
  this.state[key] = initialState;
@@ -681,22 +676,41 @@ export const require = async (path, noresolve = false) => {
681
676
 
682
677
 
683
678
  window.require = require;
684
-
685
679
  /**
686
- * @method useState - type
687
- * @param {*} initialState
688
- * @returns {Array} [value, set]
689
- * @description Allows you to use state to dynamically update your component
680
+ * useState hook.
681
+ *
682
+ * @param {string} key - The key for the state property.
683
+ * @param {*} initialState - The initial state value.
684
+ * @returns {[*]} - A tuple with the current state value and a setter function.
690
685
  */
691
- export const useState = (initialState) => {
692
- let key = ''
693
- let value = initialState;
694
- if (key && !states[key]) {
695
- this.states[key] = initialState;
686
+ export const useState = (key, initialState) => {
687
+ if (!states[key]) {
688
+ states[key] = initialState;
696
689
  }
697
- return [value, (newValue) => {}];
690
+
691
+ /**
692
+ * Get the current state value.
693
+ *
694
+ * @returns {*} The current state value.
695
+ */
696
+ let updatedValue = () => states[key];
697
+
698
+ /**
699
+ * Set a new value for the state.
700
+ *
701
+ * @param {*} newValue - The new value to set.
702
+ * @param {Function} hook - The hook to hydrate after setting the value.
703
+ */
704
+ const set = (newValue, hook) => {
705
+ states[key] = newValue;
706
+ this.hydrate(hook);
707
+ };
708
+
709
+ return [states[key], set];
698
710
  };
699
711
 
712
+
713
+
700
714
  /**
701
715
  * @method useReducer
702
716
  * @param {*} initialState
@@ -717,19 +731,27 @@ export const constant = (value) => {
717
731
  }
718
732
  return constants[key];
719
733
  };
720
- /**
721
- * @method useRef
722
- * @description Allows you to use a reference to a DOM element
723
- * @param {*} initialState
724
- * @returns
725
- */
726
734
 
735
+ /**
736
+ * useRef hook.
737
+ *
738
+ * @param {*} initialState - The initial state value for the ref.
739
+ * @returns {{ current: *, bind: string }} - An object containing the current value and a bind string.
740
+ */
727
741
  export const useRef = (initialState) => {
728
742
  return {
743
+ /**
744
+ * @description The current value of the ref.
745
+
746
+ */
729
747
  current: initialState,
748
+ /**
749
+ * @description A unique string that can be used to bind the ref to an element.
750
+ */
730
751
  bind: '',
731
752
  };
732
753
  };
754
+
733
755
  export default {
734
756
  Component,
735
757
  require,
package/package.json CHANGED
@@ -2,7 +2,7 @@
2
2
  "name": "vaderjs",
3
3
  "description": "A Reactive library aimed to helping you build reactive applications inspired by react.js",
4
4
  "module": "vader.js",
5
- "version": "1.3.3-alpha-22",
5
+ "version": "1.3.3-alpha-24",
6
6
  "bin": {
7
7
  "vader": "./vader.js"
8
8
  },
package/runtime/router.js CHANGED
@@ -225,6 +225,9 @@ class VaderRouter{
225
225
  if(!document.querySelector('#root')){
226
226
  throw new Error('Root element not found, please add an element with id root');
227
227
  }
228
+ Component.state = {}
229
+ Component.reset();
230
+ Component.components = {}
228
231
  Component.request = req;
229
232
  Component.response = res;
230
233
  document.querySelector('#root').innerHTML = Component.render()
package/runtime/vader.js CHANGED
@@ -45,7 +45,6 @@ let mounts = [];
45
45
  export const strictMount = (key, callback) => {
46
46
  let timer = setInterval(() => {
47
47
  if (document.querySelector('[key="' + key + '"]')) {
48
- console.log('mounted')
49
48
  clearInterval(timer);
50
49
  callback();
51
50
  }
@@ -167,7 +166,7 @@ export class Component {
167
166
  this.checkIFMounted();
168
167
  this.memoizes = []
169
168
 
170
-
169
+ this.vdom = []
171
170
 
172
171
  this.children = []
173
172
 
@@ -249,19 +248,30 @@ export class Component {
249
248
  throw new Error('new components must have a key')
250
249
  }
251
250
  let comp = new component(props);
252
- if(this.components[props.key]){
253
- return this.components[props.key]
254
- }
251
+
255
252
  comp['props'] = props;
256
253
  comp.children = children;
257
254
  comp.props.children = children.join('')
258
255
  comp.parentNode = this;
256
+ comp.request = this.request;
257
+ comp.response = this.response;
259
258
  comp.key = props.key || null;
260
259
 
260
+ if(!this.components[props.key]){
261
+ states[props.key] = {}
262
+ }
261
263
  this.components[props.key] = comp
262
264
  this.children.push(comp)
263
265
  return this.components[props.key]
264
266
  }
267
+ reset(){
268
+ Object.keys(this.components).forEach((key) => {
269
+ states[key] = {}
270
+ })
271
+ states[this.key] = {}
272
+ this.components = {}
273
+ this.children = []
274
+ }
265
275
  memoize(/**@type {Component}**/component){
266
276
  if(!component.key){
267
277
  throw new Error('Component must have a static key')
@@ -274,7 +284,7 @@ export class Component {
274
284
  }
275
285
 
276
286
  let comp = this.components[component.key];
277
-
287
+ comp.props = component.props;
278
288
  let h = comp.render()
279
289
 
280
290
  if(h && h.split('>,').length > 1){
@@ -408,7 +418,7 @@ export class Component {
408
418
  */
409
419
  bind(funcData,jsx,ref, paramNames, ...params) {
410
420
 
411
-
421
+
412
422
  const name = `func_${crypto ? crypto.getRandomValues(new Uint32Array(1))[0] : Math.random()}`;
413
423
 
414
424
  var dynamicFunction = async (...params) => {
@@ -422,29 +432,22 @@ export class Component {
422
432
 
423
433
  paramNames = paramNames.replace(/,,/g, ',');
424
434
  let newparamnames = paramNames.replaceAll(',,', ',')
425
- params.forEach((param, index) => {
426
- if(param && Object.keys(param).includes('detail')){
427
- param = param['detail']['target']['event']
428
- params[index] = param
429
- }
430
- });
431
- // Remove trailing commas
432
- paramNames.endsWith(',') ? paramNames = paramNames.slice(0, -1) : null;
433
- console.log(paramNames)
434
-
435
- params.forEach((param, index) => {
436
- paramObject[newparamnames.split(',')[index]] = param;
437
- });
435
+
436
+
437
+ for(var i in params){
438
+ paramObject[newparamnames.split(',')[i]] = params[i]
439
+ }
440
+
441
+
438
442
 
439
443
  paramNames = paramNames.replace(',,', ',');
440
444
  let func = new Function(`${paramNames}`, `
441
445
  return (async (${paramNames}) => {
442
- ${funcData}
446
+ ${funcData}
443
447
  })(${Object.keys(paramObject).join(',')})
444
- `);
445
-
446
- // Bind and execute the function with the provided parameters
447
- await func.bind(this)(...Object.values(paramObject));
448
+ `);
449
+ await func.bind(this)(...Object.values(paramObject));
450
+
448
451
  };
449
452
 
450
453
 
@@ -452,7 +455,7 @@ export class Component {
452
455
  if (!this.functionMap.has(name)) {
453
456
  document.addEventListener(`call_${name}`, (e) => {
454
457
 
455
- dynamicFunction(params);
458
+ dynamicFunction(params)
456
459
  this.functionMap.set(e.detail.name, {
457
460
  lastUsed: Date.now(),
458
461
  });
@@ -472,17 +475,20 @@ export class Component {
472
475
  };
473
476
 
474
477
  // Return a valid inline js function call
475
- return jsx ? dynamicFunction : `
478
+ function jsxCall(){
479
+ document.dispatchEvent(
480
+ new CustomEvent(`call_${name}`, {
481
+ detail: { name: `call_${name}` },
482
+ })
483
+ );
484
+ }
485
+ return jsx ? jsxCall: `
476
486
  ((event) => {
477
487
  event.target.setAttribute('data-ref', '${ref}');
478
488
  let reference = event.target.getAttribute('data-ref');
479
489
  event.target.eventData = event;
480
-
481
- let domquery = queryRef(reference);
482
-
483
- domquery.eventData = event;
484
- domquery.eventData.target = domquery;
485
- call('${name}', {event:domquery.eventData}, '${paramNames}')
490
+ event.target.data = event
491
+ call('${name}', {event:event.target.data}, '${paramNames}')
486
492
  })(event)
487
493
  `;
488
494
  }
@@ -547,9 +553,9 @@ export class Component {
547
553
  * @param {T} initialState - The initial state value.
548
554
  * @returns {[() => T, (newValue: T, hook: Function) => void]} - A tuple with getter and setter functions.
549
555
  */
550
- useState(key, initialState) {
551
- if (!this.state[key]) {
552
- this.state[key] = initialState;
556
+ useState(key, initialState) {
557
+ if (!states[this.key][key]) {
558
+ states[this.key][key] = initialState;
553
559
  }
554
560
 
555
561
  /**
@@ -557,7 +563,7 @@ export class Component {
557
563
  *
558
564
  * @returns {T} The current state value.
559
565
  */
560
- let updatedValue = () => this.state[key];
566
+ let updatedValue = () => states[this.key][key];
561
567
 
562
568
  const getValue = updatedValue();
563
569
 
@@ -568,13 +574,13 @@ export class Component {
568
574
  * @param {Function} hook - The hook to hydrate after setting the value.
569
575
  */
570
576
  const set = (newValue, hook) => {
571
- this.state[key] = newValue;
577
+ states[this.key][key] = newValue;
572
578
  this.hydrate(hook);
573
579
  };
574
580
 
575
581
 
576
582
 
577
- return [this.state[key], set];
583
+ return [getValue, set];
578
584
  }
579
585
 
580
586
 
package/vader.js CHANGED
@@ -226,12 +226,13 @@ function Compiler(func) {
226
226
 
227
227
  let paramString = params ? params.split(' ').map(param => param + ',').join('') : "";
228
228
 
229
- let newatribute = `${attributeName}="\${this.bind(\`${newvalue}\`, ${isJSXComponent ? true : false}, '${ref}', "${paramString}", ${params || null})}"`
229
+ paramString = paramString.replaceAll(',,', ',')
230
+ let jsxAttribute = `${attributeName}=function(${paramString}){${newvalue}},`
231
+ let newatribute = `${attributeName}="\${this.bind(\`${newvalue}\`, ${isJSXComponent ? true : false}, '${ref}', "${paramString}", ${params || null})}",`
230
232
 
231
-
232
233
  attribute[attributeName] = {
233
234
  old: old,
234
- new: newatribute,
235
+ new: isJSXComponent ? jsxAttribute : newatribute,
235
236
  attribute: attributeName,
236
237
  };
237
238
  attributesList.push({
@@ -261,13 +262,13 @@ function Compiler(func) {
261
262
  otherdata["jsx"] = isJSXComponent;
262
263
  otherdata["ref"] = ref;
263
264
  // since js is all in one line split it
264
- newvalue = newvalue.split('\n').map(line => line.trim() ? line.trim() + ';' : line).join('\n');
265
- // param1, param2, param3
266
- let length = params ? params.split(' ').length : 0;
265
+ newvalue = newvalue.split('\n').map(line => line.trim() ? line.trim() + ';' : line).join('\n');
267
266
  let paramString = params ? params.split(' ').map(param => param + ',').join('') : "";
268
- let newattribute = `${attributeName}="\${this.bind(\`${newvalue}\`, ${isJSXComponent ? true : false}, '${ref}', "${paramString}", ${params || null})}"`
267
+ paramString = paramString.replaceAll(',,', ',')
268
+ let jsxAttribute = `${attributeName}=function(${paramString}){${newvalue}},`
269
+ let newattribute = `${attributeName}="\${this.bind(\`${newvalue}\`, ${isJSXComponent ? true : false}, '${ref}', "${paramString}", ${params || null})}",`
269
270
  newattribute = newattribute.replace(/\s+/g, " ")
270
- string = string.replace(old, newattribute);
271
+ string = string.replace(old, isJSXComponent ? jsxAttribute : newattribute);
271
272
  }
272
273
  }
273
274
  }
@@ -324,6 +325,7 @@ function Compiler(func) {
324
325
  const { element, attributes } = attribute;
325
326
  if (Object.keys(attributes).length === 0) return;
326
327
 
328
+
327
329
  newAttributes.push(attribute);
328
330
  for (let key in attributes) {
329
331
 
@@ -333,6 +335,7 @@ function Compiler(func) {
333
335
  if (value && value.includes("={")) {
334
336
  value = value.replace("=", "");
335
337
  value == "undefined" ? (value = '"') : (value = value);
338
+ console.log(value)
336
339
  key == 'style' ? value = `{this.parseStyle({${value.split('{{')[1].split('}}')[0]}})}` : null
337
340
 
338
341
 
@@ -347,9 +350,7 @@ function Compiler(func) {
347
350
 
348
351
  }
349
352
  } else if (value && value.new) {
350
- let newvalue = value.new + ",";
351
- let old = value.old;
352
- string = string.replace(old, newvalue);
353
+ string = string.replace(value.old, value.new);
353
354
  }
354
355
  }
355
356
  });
@@ -545,11 +546,13 @@ function Compiler(func) {
545
546
 
546
547
 
547
548
 
548
-
549
- props = props
550
- .replaceAll("=", ":")
549
+ /**
550
+ * @prop {string} props
551
+ * @description replace any given possible value in props and parse the string to a valid JSON object
552
+ */
553
+ props = props
551
554
  .replaceAll('"', "'")
552
- //fix key:'value' to key:'value', only if value ="value"
555
+
553
556
 
554
557
  .replaceAll(",,", ',')
555
558
  .replaceAll("className", "class")
@@ -558,14 +561,27 @@ function Compiler(func) {
558
561
  .replaceAll("}'", "")
559
562
  .split("$:")
560
563
  .join("")
561
- .replace('/', '')
562
- // remove trailing ,
564
+ // replace / with '' at the end of the string
565
+ .replace(/\/\s*$/, "")
566
+
563
567
  .replace(/,\s*$/, "")
564
- props = props.replace(/:('[^']*'|"[^"]*")/g, ':$1,');
568
+ .replaceAll('="', ':"')
569
+ .replaceAll("='", ":'")
570
+ .replaceAll('=`', ':`')
571
+ .replaceAll(`={\``, ':`')
572
+ .replaceAll('`}', '`')
573
+ .replaceAll(",,", ',')
574
+
575
+ .replaceAll(/=([A-z])/g, ":$1")
576
+ props = props.replace(/:('[^']*'|"[^"]*")/g, ':$1,');
577
+ // ANY VALUE NUMBER BOOLEAN OR STRING
578
+ props = props.replace(/=(\d+)/g, ':$1,');
565
579
  props = props.replaceAll(',,', ',')
566
580
 
567
581
 
568
-
582
+ /**
583
+ * @memoize - memoize a component to be remembered on each render and replace the old jsx
584
+ */
569
585
 
570
586
  let replace = "";
571
587
  replace = isChild