vaderjs 1.3.3-alpha-21 → 1.3.3-alpha-23

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,11 +2,21 @@
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-21",
5
+ "version": "1.3.3-alpha-23",
6
6
  "bin": {
7
7
  "vader": "./vader.js"
8
8
  },
9
9
  "type": "module",
10
+ "license": "MIT",
11
+ "homepage": "https://vader-js.pages.dev/#/",
12
+ "repository": {
13
+ "type": "git",
14
+ "url": "https://github.com/Postr-Inc/Vader.js"
15
+ },
16
+ "author": {
17
+ "name": "Malik Whitten",
18
+ "email": "malikwhitterb@gmail.com"
19
+ },
10
20
  "dependencies": {
11
21
  "glob": "latest"
12
22
  }
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,13 +248,13 @@ 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
 
261
260
  this.components[props.key] = comp
@@ -274,7 +273,7 @@ export class Component {
274
273
  }
275
274
 
276
275
  let comp = this.components[component.key];
277
-
276
+ comp.props = component.props;
278
277
  let h = comp.render()
279
278
 
280
279
  if(h && h.split('>,').length > 1){
@@ -408,7 +407,7 @@ export class Component {
408
407
  */
409
408
  bind(funcData,jsx,ref, paramNames, ...params) {
410
409
 
411
-
410
+
412
411
  const name = `func_${crypto ? crypto.getRandomValues(new Uint32Array(1))[0] : Math.random()}`;
413
412
 
414
413
  var dynamicFunction = async (...params) => {
@@ -422,29 +421,22 @@ export class Component {
422
421
 
423
422
  paramNames = paramNames.replace(/,,/g, ',');
424
423
  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
- });
424
+
425
+
426
+ for(var i in params){
427
+ paramObject[newparamnames.split(',')[i]] = params[i]
428
+ }
429
+
430
+
438
431
 
439
432
  paramNames = paramNames.replace(',,', ',');
440
433
  let func = new Function(`${paramNames}`, `
441
434
  return (async (${paramNames}) => {
442
- ${funcData}
435
+ ${funcData}
443
436
  })(${Object.keys(paramObject).join(',')})
444
- `);
445
-
446
- // Bind and execute the function with the provided parameters
447
- await func.bind(this)(...Object.values(paramObject));
437
+ `);
438
+ await func.bind(this)(...Object.values(paramObject));
439
+
448
440
  };
449
441
 
450
442
 
@@ -452,7 +444,7 @@ export class Component {
452
444
  if (!this.functionMap.has(name)) {
453
445
  document.addEventListener(`call_${name}`, (e) => {
454
446
 
455
- dynamicFunction(params);
447
+ dynamicFunction(params)
456
448
  this.functionMap.set(e.detail.name, {
457
449
  lastUsed: Date.now(),
458
450
  });
@@ -472,17 +464,20 @@ export class Component {
472
464
  };
473
465
 
474
466
  // Return a valid inline js function call
475
- return jsx ? dynamicFunction : `
467
+ function jsxCall(){
468
+ document.dispatchEvent(
469
+ new CustomEvent(`call_${name}`, {
470
+ detail: { name: `call_${name}` },
471
+ })
472
+ );
473
+ }
474
+ return jsx ? jsxCall: `
476
475
  ((event) => {
477
476
  event.target.setAttribute('data-ref', '${ref}');
478
477
  let reference = event.target.getAttribute('data-ref');
479
478
  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}')
479
+ event.target.data = event
480
+ call('${name}', {event:event.target.data}, '${paramNames}')
486
481
  })(event)
487
482
  `;
488
483
  }
package/vader.js CHANGED
@@ -226,9 +226,8 @@ 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
+ let newatribute = `${attributeName}="\${this.bind(\`${newvalue}\`, ${isJSXComponent ? true : false}, '${ref}', "${paramString}", ${params || null})}",`
230
230
 
231
-
232
231
  attribute[attributeName] = {
233
232
  old: old,
234
233
  new: newatribute,
@@ -261,11 +260,9 @@ function Compiler(func) {
261
260
  otherdata["jsx"] = isJSXComponent;
262
261
  otherdata["ref"] = ref;
263
262
  // 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;
263
+ newvalue = newvalue.split('\n').map(line => line.trim() ? line.trim() + ';' : line).join('\n');
267
264
  let paramString = params ? params.split(' ').map(param => param + ',').join('') : "";
268
- let newattribute = `${attributeName}="\${this.bind(\`${newvalue}\`, ${isJSXComponent ? true : false}, '${ref}', "${paramString}", ${params || null})}"`
265
+ let newattribute = `${attributeName}="\${this.bind(\`${newvalue}\`, ${isJSXComponent ? true : false}, '${ref}', "${paramString}", ${params || null})}",`
269
266
  newattribute = newattribute.replace(/\s+/g, " ")
270
267
  string = string.replace(old, newattribute);
271
268
  }
@@ -324,6 +321,7 @@ function Compiler(func) {
324
321
  const { element, attributes } = attribute;
325
322
  if (Object.keys(attributes).length === 0) return;
326
323
 
324
+
327
325
  newAttributes.push(attribute);
328
326
  for (let key in attributes) {
329
327
 
@@ -333,6 +331,7 @@ function Compiler(func) {
333
331
  if (value && value.includes("={")) {
334
332
  value = value.replace("=", "");
335
333
  value == "undefined" ? (value = '"') : (value = value);
334
+ console.log(value)
336
335
  key == 'style' ? value = `{this.parseStyle({${value.split('{{')[1].split('}}')[0]}})}` : null
337
336
 
338
337
 
@@ -347,9 +346,7 @@ function Compiler(func) {
347
346
 
348
347
  }
349
348
  } else if (value && value.new) {
350
- let newvalue = value.new + ",";
351
- let old = value.old;
352
- string = string.replace(old, newvalue);
349
+ string = string.replace(value.old, value.new);
353
350
  }
354
351
  }
355
352
  });
@@ -545,11 +542,13 @@ function Compiler(func) {
545
542
 
546
543
 
547
544
 
548
-
549
- props = props
550
- .replaceAll("=", ":")
545
+ /**
546
+ * @prop {string} props
547
+ * @description replace any given possible value in props and parse the string to a valid JSON object
548
+ */
549
+ props = props
551
550
  .replaceAll('"', "'")
552
- //fix key:'value' to key:'value', only if value ="value"
551
+
553
552
 
554
553
  .replaceAll(",,", ',')
555
554
  .replaceAll("className", "class")
@@ -558,14 +557,27 @@ function Compiler(func) {
558
557
  .replaceAll("}'", "")
559
558
  .split("$:")
560
559
  .join("")
561
- .replace('/', '')
562
- // remove trailing ,
560
+ // replace / with '' at the end of the string
561
+ .replace(/\/\s*$/, "")
562
+
563
563
  .replace(/,\s*$/, "")
564
+ .replaceAll('="', ':"')
565
+ .replaceAll("='", ":'")
566
+ .replaceAll('=`', ':`')
567
+ .replaceAll(`={\``, ':`')
568
+ .replaceAll('`}', '`')
569
+
570
+ .replaceAll(/=([A-z])/g, ":$1")
571
+ props = props.replace(/:('[^']*'|"[^"]*")/g, ':$1,');
564
572
  props = props.replace(/:('[^']*'|"[^"]*")/g, ':$1,');
573
+ // ANY VALUE NUMBER BOOLEAN OR STRING
574
+ props = props.replace(/=(\d+)/g, ':$1,');
565
575
  props = props.replaceAll(',,', ',')
566
576
 
567
577
 
568
-
578
+ /**
579
+ * @memoize - memoize a component to be remembered on each render and replace the old jsx
580
+ */
569
581
 
570
582
  let replace = "";
571
583
  replace = isChild