vaderjs 1.4.6 → 1.4.7

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/README.MD CHANGED
@@ -45,7 +45,7 @@ Keyword folders - all files are passed from these folders to the build folder
45
45
  # Define your config
46
46
 
47
47
  ```ts
48
- import defineConfig from "../config";
48
+ import defineConfig from "vaderjs/config";
49
49
 
50
50
  export default defineConfig({
51
51
  port: 3000,
package/document/index.ts CHANGED
@@ -21,7 +21,7 @@ export const document = (element: any) => {
21
21
  continue;
22
22
  }
23
23
  //@ts-ignore
24
- if (key.includes("on")) {
24
+ if (key.startsWith("on")){
25
25
  continue;
26
26
  }
27
27
  el += ` ${key}="${attributes[key]}"`;
package/index.ts CHANGED
@@ -14,6 +14,9 @@ declare global {
14
14
  state: any;
15
15
  }
16
16
  const genKey: any;
17
+ /**
18
+ * @description Allows you to check if current session is server or client
19
+ */
17
20
  let isServer: boolean;
18
21
  /**
19
22
  * @description - The params object is used to store the query parameters of the current URL
@@ -27,6 +30,7 @@ declare global {
27
30
  * console.log(params.age) // 20
28
31
  */
29
32
  let params: { [key: string]: string };
33
+ let localStorage : []
30
34
  }
31
35
  //@ts-ignore
32
36
  globalThis.isServer = typeof window === "undefined";
@@ -39,6 +43,7 @@ globalThis.params = {
39
43
  },
40
44
  };
41
45
 
46
+
42
47
 
43
48
  /**
44
49
  * @description useFetch allows you to make POST - GET - PUT - DELETE requests then returns the data, loading state and error
@@ -130,6 +135,8 @@ export class Component {
130
135
  this.effect = [];
131
136
  this.Mounted = false;
132
137
  this.element = null;
138
+
139
+
133
140
  }
134
141
  setState(newState: any, Element: string) {
135
142
  globalThis.window.state[this.key] = { ...this.state, ...newState };
@@ -149,19 +156,32 @@ export class Component {
149
156
  }
150
157
  }
151
158
  }
152
- useState <T> (key: string, defaultValue: T) {
153
- let value = sessionStorage.getItem("state_" + key) || defaultValue;
154
- typeof value === "string" ? value = JSON.parse(value) : value;
155
-
156
- window.onbeforeunload = function() {
157
- sessionStorage.removeItem("state_" + key);
158
- }
159
- const setValue = (newValue: T ,element: string) => {
160
- value = newValue;
161
- sessionStorage.setItem("state_" + key, JSON.stringify(value));
162
- this.state[key] = value;
163
- this.forceUpdate(element)
159
+ useState<T>(key: string, defaultValue: T) {
160
+ let value = sessionStorage.getItem("state_" + key) ? JSON.parse(sessionStorage.getItem("state_" + key)).value : defaultValue;
161
+
162
+ // Parse value if it's a stringified object or number
163
+ if (typeof value === 'string') {
164
+ try {
165
+ value = JSON.parse(value);
166
+ } catch (error) {
167
+ // Not a valid JSON, keep it as is
168
+ }
169
+ }
170
+
171
+ // Add listener for unload event to save state
172
+ if (!window['listener' + key]) {
173
+ window['listener' + key] = true;
174
+ window.addEventListener('beforeunload', () => {
175
+ sessionStorage.removeItem('state_' + key)
176
+ });
164
177
  }
178
+
179
+ const setValue = (newValue: T) => {
180
+ value = newValue;
181
+ sessionStorage.setItem("state_" + key, JSON.stringify({ type: typeof newValue, value: newValue }));
182
+ this.forceUpdate(this.key)
183
+ };
184
+
165
185
  return [value, setValue];
166
186
  }
167
187
 
@@ -206,38 +226,39 @@ export class Component {
206
226
  forceUpdate(key) {
207
227
  //@ts-ignore
208
228
  let el = Array.from(document.querySelectorAll("*")).filter((el2: any) =>{ return el2.key === key})[0];
209
- let newl = this.parseToElement(this.render());
229
+ let newl = this.toElement();
210
230
  if(newl.key !== key){
211
231
  //@ts-ignore
212
232
  newl = Array.from(newl.children).filter((el2) => el2.key === key)[0];
213
- }
214
- if (this.Reconciler.shouldUpdate(el, newl)) {
215
- el.replaceWith(newl);
216
- }
233
+ }
234
+ this.Reconciler.update(el, newl);
217
235
  }
218
236
  Reconciler = {
219
- shouldUpdate(oldElement, newElement) {
220
- if(oldElement.outerHTML === newElement.outerHTML){
221
- return false;
222
- }
223
- let attributes = oldElement.attributes;
224
- let newAttributes = newElement.attributes;
225
- for (let i = 0; i < attributes.length; i++) {
226
- let attribute = attributes[i];
227
- if (attribute.name === "key") {
228
- continue;
229
- }
230
- if (attribute.name === "class") {
231
- if (attribute.value !== newElement.className) {
232
- return true;
233
- }
234
- continue;
235
- }
236
- if (attribute.value !== newAttributes[attribute.name]) {
237
- return true;
237
+ update: (oldElement, newElement) => {
238
+ if(!oldElement || !newElement) return;
239
+ if (this.Reconciler.shouldUpdate(oldElement, newElement) && oldElement.tagName == newElement.tagName){
240
+ oldElement.replaceWith(newElement)
241
+ } else {
242
+ let children = oldElement.childNodes;
243
+ for (let i = 0; i < children.length; i++) {
244
+ this.Reconciler.update(children[i], newElement.childNodes[i]);
238
245
  }
239
246
  }
240
- return true;
247
+ },
248
+ shouldUpdate(oldElement, newElement) {
249
+ if (oldElement.nodeType !== newElement.nodeType) {
250
+ return true;
251
+ }
252
+ if (oldElement.nodeType === 3 && newElement.nodeType === 3) {
253
+ return oldElement.textContent !== newElement.textContent;
254
+ }
255
+ if (oldElement.nodeName !== newElement.nodeName) {
256
+ return true;
257
+ }
258
+ if (oldElement.childNodes.length !== newElement.childNodes.length) {
259
+ return true;
260
+ }
261
+ return false;
241
262
  }
242
263
  };
243
264
 
@@ -301,7 +322,12 @@ export class Component {
301
322
  }
302
323
  toElement() {
303
324
  let children = this.render();
325
+ //@ts-ignore
326
+ if(children.props['key']){
327
+ this.key = children.props['key'];
328
+ }
304
329
  let el = this.parseToElement(children);
330
+ el.key = this.key;
305
331
  return el;
306
332
  }
307
333
  render() {
@@ -335,7 +361,7 @@ export function render(element: any, container) {
335
361
  let memoizedInstance = memoizeClassComponent(Component);
336
362
  memoizedInstance.Mounted = true;
337
363
  memoizedInstance.render = element.bind(memoizedInstance);
338
- let el = memoizedInstance.toElement();
364
+ let el = memoizedInstance.toElement();
339
365
  container.innerHTML = "";
340
366
  container.replaceWith(el);
341
367
 
package/main.js CHANGED
@@ -277,6 +277,9 @@ function handleFiles(){
277
277
  for await (var i of glob.scan()){
278
278
  let file = i
279
279
  fs.mkdirSync(path.join(process.cwd() + '/dist', path.dirname(file)), { recursive: true })
280
+ if(fs.existsSync(path.join(process.cwd() + '/dist', file))){
281
+ fs.rmSync(path.join(process.cwd() + '/dist', file))
282
+ }
280
283
  fs.copyFileSync(file, path.join(process.cwd() + '/dist', file))
281
284
  }
282
285
  resolve()
@@ -292,7 +295,7 @@ if(mode === 'development'){
292
295
  const watcher = fs.watch(path.join(process.cwd() + '/app'), { recursive: true })
293
296
  const publicWatcher = fs.watch(path.join(process.cwd() + '/public'), { recursive: true })
294
297
  publicWatcher.on('change', async (event, filename) => {
295
- try {
298
+ try {
296
299
  await handleFiles()
297
300
  clients.forEach(c => {
298
301
  c.send('reload')
@@ -303,8 +306,7 @@ if(mode === 'development'){
303
306
  })
304
307
  watcher.on('change', async (event, filename) => {
305
308
  try {
306
- await generateApp()
307
-
309
+ await generateApp()
308
310
  handleFiles()
309
311
  clients.forEach(c => {
310
312
  c.send('reload')
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "vaderjs",
3
- "version": "1.4.6",
3
+ "version": "1.4.7",
4
4
  "description": "A simple and powerful JavaScript library for building modern web applications.",
5
5
  "main":"./index.js",
6
6
  "bin": {