vaderjs 1.3.3-alpha-45 → 1.3.3-alpha-46

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
@@ -1,11 +1,11 @@
1
1
  window.Vader = {
2
- version: "1.3.3",
2
+ version: "1.3.3",
3
3
  };
4
-
4
+ window.componentRegistry = {};
5
5
  let errors = {
6
6
  "SyntaxError: Unexpected token '<'": "You forgot to enclose tags in a fragment <></>",
7
- }
8
-
7
+ }
8
+
9
9
  let mounts = [];
10
10
  /**
11
11
  * @method strictMount
@@ -14,16 +14,11 @@ let mounts = [];
14
14
  * @param {*} callback
15
15
  */
16
16
  export const strictMount = (key, callback) => {
17
- let timer = setInterval(() => {
18
- if (mounts.find((m) => m.key === key)) {
19
- clearInterval(timer);
20
- callback();
21
- }
22
- }, 120);
17
+
23
18
  };
24
-
25
-
26
-
19
+
20
+
21
+
27
22
  /**
28
23
  * Represents a component in the Vader framework.
29
24
  */
@@ -33,52 +28,61 @@ export class Component {
33
28
  */
34
29
  constructor() {
35
30
  this.state = {};
31
+ /**
32
+ * @type {string}
33
+ * @description The key for the component. used to identify the component in the DOM
34
+ */
36
35
  this.key = null;
36
+ /**
37
+ * @private
38
+ */
37
39
  this.components = {};
38
- this.mounted = false;
40
+ this.mounted = false;
39
41
  this.checkIFMounted();
40
42
  this.memoizes = []
41
- this.functions = []
42
-
43
+ /**
44
+ * @private
45
+ */
46
+ this.functions = []
43
47
  this.children = []
44
-
45
-
48
+
49
+
46
50
  /**
47
51
  * Parent of the current component.
48
52
  * @type {Component}
49
53
  */
50
- this.parentNode = {}
51
-
52
- /**
53
- * Request object.
54
- */
55
- this.request = {
54
+ this.parentNode = {}
55
+
56
56
  /**
57
- * @type {string}
58
- * @description The headers for the current route
59
- */
60
- headers: {},
61
- /**
62
- * @type {string}
63
- * @description The method for the current route
64
- */
65
- method: "GET",
66
- /**
67
- * @type {string}
68
- * @description params for the given route /:id/:name etc
69
- */
70
- params: {},
71
- /**
72
- * @type {string}
73
- * @description path: current route path
57
+ * Request object.
74
58
  */
75
- path: "",
76
- /**
77
- * @type {string}
78
- * @description query: query object for the current route ?name=hello -> {name: 'hello'}
79
- */
80
- query: {},
81
- },
59
+ this.request = {
60
+ /**
61
+ * @type {string}
62
+ * @description The headers for the current route
63
+ */
64
+ headers:{},
65
+ /**
66
+ * @type {string}
67
+ * @description The method for the current route
68
+ */
69
+ method: "GET",
70
+ /**
71
+ * @type {string}
72
+ * @description params for the given route /:id/:name etc
73
+ */
74
+ params: {},
75
+ /**
76
+ * @type {string}
77
+ * @description path: current route path
78
+ */
79
+ path: "",
80
+ /**
81
+ * @type {string}
82
+ * @description query: query object for the current route ?name=hello -> {name: 'hello'}
83
+ */
84
+ query: {},
85
+ },
82
86
  /**
83
87
  * @type {string}
84
88
  * @description The response object for the current route
@@ -89,123 +93,90 @@ export class Component {
89
93
  * @description This method allows you to send json data to the client
90
94
  * @param {*} data
91
95
  */
92
- json: (data) => { },
96
+ json: (data) => {},
93
97
  /**
94
98
  * @method send
95
99
  * @description This method allows you to send text data to the client
96
100
  * @param {*} data
97
101
  */
98
- send: (data) => { },
102
+ send: (data) => {},
99
103
  /**
100
104
  * @method redirect
101
105
  * @description This method allows you to redirect the client to a new route
102
106
  * @param {*} path
103
107
  */
104
- redirect: (path) => { },
108
+ redirect: (path) => {},
105
109
  /**
106
110
  * @method render
107
111
  * @description render a new component to the client
108
112
  * @param {*} Component
109
113
  */
110
- render: async (Component) => { },
114
+ render: async (Component) => {},
111
115
  /**
112
116
  * @method log
113
117
  * @description This method is used to log the request and response
114
118
  * @param {String} type
115
119
  */
116
- log: (type) => { },
120
+ log: (type) => {},
117
121
  /**
118
122
  * @method setQuery
119
123
  * @description This method is used to set the query object for the current route
120
124
  */
121
- setQuery: (query) => { },
122
-
123
- }
124
- /**
125
- * @method router
126
- * @description use router methods directly from the parent component
127
- */
125
+ setQuery: (query) => {},
126
+
127
+ }
128
+ /**
129
+ * @method router
130
+ * @description use router methods directly from the parent component
131
+ */
128
132
 
129
- this.router = {
133
+ this.router = {
130
134
  /**
131
135
  * @method use
132
136
  * @description add a middleware to the current route
133
137
  * @param {Function} middleware
134
138
  * @returns {void}
135
139
  */
136
- use: (/**@type {Function} */ middleware) => { },
137
- }
138
- }
140
+ use: (/**@type {Function} */ middleware) => {},
141
+ }
142
+ }
139
143
 
144
+ /**
145
+ * @method createComponent
146
+ * @description This method allows you to create a component from a class or function
147
+ * @param {Component} component
148
+ * @param {Object} props
149
+ * @param {Array} children
150
+ */
140
151
  createComponent(/**@type {Component}**/component, props, children) {
141
-
142
- if (!component) {
143
- throw new Error("Component must be defined");
144
- }
145
- if (!props.key) {
146
- throw new Error('new components must have a key')
147
- }
148
- let comp = new component(props);
149
-
150
- comp['props'] = props;
151
- comp.children = children;
152
- comp.props.children = children.join('')
153
- comp.parentNode = this;
154
- comp.request = this.request;
155
- comp.response = this.response;
156
- comp.key = props.key || null;
157
-
158
- if (!this.components[props.key]) {
159
- this.components[props.key] = comp;
160
- }
161
- this.children.push(comp)
162
- return this.components[props.key]
152
+
163
153
  }
164
- reset() {
165
- console.log('reset')
166
- Object.keys(this.components).forEach((key) => {
167
- this.components[key].onUnmount()
168
- delete this.components[key]
169
- })
170
- this.state = {}
171
- this.children = []
154
+ /**
155
+ * @private
156
+ */
157
+ reset(){
158
+
172
159
  }
173
- memoize(/**@type {Component}**/component) {
174
- if (!component.key) {
175
- throw new Error('Component must have a static key')
176
- }
177
- switch (true) {
178
- case !this.memoizes.includes(component.key):
179
- this.memoizes.push(component.key)
180
- this.components[component.key] = component;
181
- break;
182
- }
183
-
184
- let comp = this.components[component.key];
185
- comp.bindMount();
186
- comp.parentNode = this;
187
- comp.props = component.props;
188
- comp.request = this.request;
189
- comp.response = this.response;
190
- let h = comp.render()
191
-
192
- if (h && h.split('>,').length > 1) {
193
- h = h.replaceAll('>,', '>')
194
- }
195
-
196
- return `<div key="${component.key}">${h}</div>`
160
+ /**
161
+ * @method memoize
162
+ * @description This method allows you to memoize a component which when rerendered will not be reinstantiated
163
+ * @param {Component} component
164
+ */
165
+ memoize(/**@type {Component}**/component){
166
+
197
167
  }
198
- parseStyle(styles) {
199
- let css = ''
200
- Object.keys(styles).forEach((key) => {
201
- let value = styles[key]
202
- key = key.replace(/([a-z0-9]|(?=[A-Z]))([A-Z])/g, '$1-$2').toLowerCase()
203
- css += `${key}:${value};`
204
- })
205
- return css
168
+ /**
169
+ * @method parseStyle
170
+ * @description This method allows you to parse a jsx style object to a string
171
+ * @param {object} styles
172
+ */
173
+ parseStyle(styles){
174
+
206
175
  }
207
- bindMount() {
208
- mounts.push(this)
176
+ /**
177
+ * @private
178
+ */
179
+ bindMount(){
209
180
  }
210
181
 
211
182
  /**
@@ -213,74 +184,44 @@ export class Component {
213
184
  * @private
214
185
  */
215
186
 
216
- domDifference(oldDom, newDom) {
217
- let diff = [];
218
-
219
- for (let i = 0; i < oldDom.length; i++) {
220
- let oldEl = oldDom[i];
221
- let newEl = newDom[i];
222
-
223
- if (oldEl && newEl && !oldEl.isEqualNode(newEl)) {
224
- diff.push({
225
- type: 'replace',
226
- old: oldEl,
227
- new: newEl.cloneNode(true),
228
- });
229
- }
230
- }
231
-
232
- return diff;
187
+ domDifference(oldDom, newDom) {
188
+
233
189
  }
234
-
235
- updateChangedElements(diff) {
236
- diff.forEach((change) => {
237
- switch (change.type) {
238
- case 'replace':
239
- change.old.parentNode.replaceChild(change.new, change.old);
240
- break;
241
- case 'remove':
242
- change.old.remove();
243
- break;
244
- case 'add':
245
- change.old.appendChild(change.new.cloneNode(true));
246
- break;
247
- default:
248
- break;
249
- }
250
- });
190
+ /**
191
+ * @private
192
+ * @param {*} diff
193
+ */
194
+
195
+ updateChangedElements(diff) {
196
+
251
197
  }
252
-
253
- hydrate(hook) {
254
- if (this.key) {
255
- if (hook) {
256
- let newDom = new DOMParser().parseFromString(this.render(), 'text/html').body.firstChild;
257
-
258
- let oldElements = document.body.querySelectorAll(`[ref="${hook}"]`);
259
- let newElements = newDom.querySelectorAll(`[ref="${hook}"]`);
260
- let diff = this.domDifference(oldElements, newElements);
261
- this.updateChangedElements(diff);
262
- } else {
263
- const targetElement = document.querySelector(`[key="${this.key}"]`);
264
- if (targetElement) {
265
- targetElement.outerHTML = this.render();
266
- } else {
267
- console.error('Target element not found.');
268
- }
269
- }
270
- }
198
+
199
+ /**
200
+ * @method hydrate
201
+ * @description This method allows you to hydrate a component
202
+ * @private
203
+ * @param {*} hook
204
+ */
205
+ hydrate(hook) {
206
+
271
207
  }
272
-
273
-
274
-
208
+
209
+
210
+ /**
211
+ * @method patch
212
+ * @description This method allows you to patch the dom
213
+ * @private
214
+ * @param {*} oldElements
215
+ * @param {*} newElements
216
+ */
217
+
275
218
  patch(oldElements, newElements) {
276
- const diff = this.domDifference(oldElements, newElements);
277
-
278
- this.updateChangedElements(diff);
219
+
279
220
  }
280
-
281
-
282
-
283
-
221
+
222
+
223
+
224
+
284
225
 
285
226
  /**
286
227
  * Handles an object by parsing it as JSON and evaluating it.
@@ -289,81 +230,25 @@ export class Component {
289
230
  * @prvate
290
231
  */
291
232
  handleObject(obj) {
292
- try {
293
- obj = JSON.parse(obj);
294
- } catch (error) {
295
- // Handle JSON parsing error if needed
296
- }
297
- return eval(obj);
233
+
298
234
  }
299
235
 
300
-
236
+
301
237
 
302
238
  /**
303
239
  * Binds a function to the component.
304
240
  * @param {string} funcData - The function data.
305
241
  * @param {string} p - The parameter.
306
242
  * @param {string} ref - The reference.
243
+ * @param {string} paramNames - The parameter names.
244
+ * @param {...*} params - The parameters.
307
245
  * @returns {string} - A valid inline JS function call.
308
246
  */
309
- bind(funcTion, isTerny, jsx, ref, paramNames, ...params) {
310
- ref = ref + this.key;
311
-
312
- let paramObject = {};
313
-
314
-
315
- paramNames = paramNames.replace(/,,/g, ',');
316
- let newparamnames = paramNames.replaceAll(',,', ',')
317
-
318
- for (var i in params) {
319
- let param = params[i]
320
- let paramname = newparamnames.split(',')[i]
321
- paramObject[paramname] = param
322
- }
323
-
324
-
325
-
326
- let ranfunctionanme = Math.random().toString(36).substring(7).slice(0, 5).replace(/[^a-z0-9]+/g, '')
327
- // no numbers
328
- ranfunctionanme = ranfunctionanme.replace(/[0-9]/g, '')
329
- let tfunc = funcTion.toString().replace('async', '').replace('function', `function ${ranfunctionanme}`)
330
-
331
- paramNames = paramNames.replace(',,', ',');
332
-
333
- let func = new Function(`${paramNames}`, `
334
- return (async (${paramNames}) => {
335
- ${tfunc}
336
- })(${Object.keys(paramObject).join(',')})
337
- `);
338
- func = func.bind(this)
339
-
340
- if (!this.functions.find((f) => f.ref === ref)) {
341
- document.addEventListener(`$dispatch_#id=${ref}`, (e) => {
342
- let { name, event } = e.detail;
343
- if (name === ref) {
344
- let params = this.functions.find((f) => f.ref === ref).params
345
- Object.keys(params).forEach((key) => {
346
- if (params[key] instanceof CustomEvent) {
347
- delete params[key]
348
- }
349
- params[key] === undefined ? delete params[key] : params[key]
350
- })
351
- isTerny ? funcTion(event, ...Object.values(params)) : func(...Object.values(params))
352
- }
353
- });
354
-
355
- }
356
-
357
- window.callFunction = (name, event) => {
358
- document.dispatchEvent(new CustomEvent(`$dispatch_#id=${name}`, { detail: { name: name, params: null, event: event } }));
359
- }
360
- !this.functions.find((f) => f.ref === ref) ? this.functions.push({ ref: ref, params: paramObject }) : !isTerny ? this.functions.find((f) => f.ref === ref).params = paramObject : null
361
-
362
-
363
- return jsx ? funcTion : `((event)=>{event.target.ev = event; callFunction('${ref}', event.target.ev)})(event)`;
247
+ bind(funcTion,isTerny, jsx,ref, paramNames, ...params) {
248
+
364
249
  }
365
-
366
-
250
+
251
+
367
252
 
368
253
  /**
369
254
  * useState hook.
@@ -373,71 +258,67 @@ export class Component {
373
258
  * @param {T} initialState - The initial state value.
374
259
  * @returns {[() => T, (newValue: T, hook: Function) => void]} - A tuple with getter and setter functions.
375
260
  */
376
- useState(key, initialState) {
377
- if (!this.state[key]) {
378
- this.state[key] = initialState;
379
- }
380
-
381
- /**
382
- * Get the current state value.
383
- *
384
- * @returns {T} The current state value.
385
- */
386
- let updatedValue = () => this.state[key];
387
-
388
- const getValue = updatedValue();
261
+ useState(key, initialState) {
262
+ if (!this.state[key]) {
263
+ this.state[key] = initialState;
264
+ }
389
265
 
390
- /**
391
- * Set a new value for the state.
392
- *
393
- * @param {T} newValue - The new value to set.
394
- * @param {Function} hook - The hook to hydrate after setting the value.
395
- */
396
- const set = (newValue, hook) => {
397
- this.state[key] = newValue;
398
- this.hydrate(hook);
399
- };
266
+ /**
267
+ * Get the current state value.
268
+ *
269
+ * @returns {T} The current state value.
270
+ */
271
+ let updatedValue = () => this.state[key];
400
272
 
273
+ const getValue = updatedValue();
401
274
 
275
+ /**
276
+ * Set a new value for the state.
277
+ *
278
+ * @param {T} newValue - The new value to set.
279
+ * @param {Function} hook - The hook to hydrate after setting the value.
280
+ */
281
+ const set = (newValue, hook) => {
282
+ this.state[key] = newValue;
283
+ this.hydrate(hook);
284
+ };
402
285
 
403
- return [getValue, set];
404
- }
286
+
405
287
 
288
+ return [getValue, set];
289
+ }
406
290
 
291
+
407
292
 
293
+ /**
294
+ * useRef hook.
295
+ * @param {string} key
296
+ * @param {any} initialState
297
+ * @returns {{ current: HTMLElement|any, bind: string }} - An object containing the current value and a bind string.
298
+ */
408
299
  useRef(key = null, initialState) {
409
- if (!this.state[key]) {
410
- this.state[key] = initialState;
411
- }
412
- const getValue = () => document.querySelector(`[ref="${key + this.key}"]`) || initialState;
413
- const set = (newValue) => {
414
- this.state[key] = newValue;
415
- this.hydrate();
416
- };
417
-
418
-
419
- return {
420
- bind: key + this.key,
421
- current: getValue(),
422
- }
300
+
423
301
  }
424
302
 
425
- useReducer(key = null, initialState, func = null) {
426
- const getValue = () => this.state[key];
427
- const value = getValue();
428
- const set = (newValue) => {
429
- const nextState = func ? func(this.state[key], newValue) : newValue;
430
- this.state[key] = nextState;
431
- };
432
- return [value, set];
303
+ /**
304
+ * useReducer hook.
305
+ * @param {string} key - The key for the state property.
306
+ * @param {*} initialState - The initial state value.
307
+ * @param {Function} func - The reducer function.
308
+ * @returns {[*, (newValue: *, hook: Function) => void]} - A tuple with getter and setter functions.
309
+ * **/
310
+
311
+ useReducer(key = null, initialState, func = null) {
312
+
433
313
  }
434
-
314
+
435
315
 
436
316
  /**
437
317
  * Placeholder for content to be rendered.
438
318
  * @method render
319
+ * @returns {string} - The rendered content.
439
320
  */
440
- render() { }
321
+ render() {}
441
322
 
442
323
  /**
443
324
  * Checks if the component is mounted and triggers the onMount method.
@@ -446,15 +327,15 @@ export class Component {
446
327
  checkIFMounted() {
447
328
  let observer = new MutationObserver((mutations) => {
448
329
  mutations.forEach((mutation) => {
449
-
330
+
450
331
  if (mutation.target.querySelector(`[key="${this.key}"]`) && !this.mounted) {
451
- this.onMount();
452
- this.mounted = true;
332
+ this.onMount();
333
+ this.mounted = true;
453
334
  }
454
-
455
- if (Array.from(mutation.removedNodes).find((node) => node.attributes && node.attributes.key && node.attributes.key.value === this.key)) {
335
+
336
+ if(Array.from(mutation.removedNodes).find((node) => node.attributes && node.attributes.key && node.attributes.key.value === this.key)){
456
337
  this.onUnmount();
457
- this.reset();
338
+ this.reset();
458
339
  }
459
340
  })
460
341
  })
@@ -468,16 +349,16 @@ export class Component {
468
349
  * Method that is called when the component is mounted.
469
350
  * @method onMount
470
351
  */
471
- onMount() { }
352
+ onMount() {}
472
353
  /**
473
354
  * Method that is called when the component is unmounted.
474
355
  * @method onUnmount
475
356
  */
476
- onUnmount() { }
357
+ onUnmount() {}
477
358
  }
478
359
 
479
-
480
-
360
+
361
+
481
362
  /**
482
363
  * useState hook.
483
364
  *
@@ -511,7 +392,7 @@ export const useState = (key, initialState) => {
511
392
  return [states[key], set];
512
393
  };
513
394
 
514
-
395
+
515
396
 
516
397
  /**
517
398
  * @method useReducer
@@ -520,16 +401,16 @@ export const useState = (key, initialState) => {
520
401
  * @returns {Array} [value, set]
521
402
  */
522
403
  export const useReducer = (/**@type {*}**/initialState, /**@type {function}**/reducer) => {
523
- return [initialState, (newValue) => { }];
404
+ return [initialState, (newValue) => {}];
524
405
  };
525
-
526
-
527
- /**
528
- * useRef hook.
529
- *
530
- * @param {*} initialState - The initial state value for the ref.
531
- * @returns {{ current: *, bind: string }} - An object containing the current value and a bind string.
532
- */
406
+
407
+
408
+ /**
409
+ * useRef hook.
410
+ * @param {string} key
411
+ * @param {any} initialState
412
+ * @returns {{ current: HTMLElement|any, bind: string }} - An object containing the current value and a bind string.
413
+ */
533
414
  export const useRef = (initialState) => {
534
415
  return {
535
416
  /**
@@ -546,9 +427,9 @@ export const useRef = (initialState) => {
546
427
  };
547
428
 
548
429
  export default {
549
- Component,
430
+ Component,
550
431
  useRef,
551
432
  useReducer,
552
433
  useState,
553
- strictMount,
434
+ strictMount,
554
435
  }