pawajs 2.0.10 → 2.2.0

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/index.js CHANGED
@@ -782,6 +782,11 @@ export const $state = (initialValue, section = null) => {
782
782
  console.warn('error while trying to use localStorage')
783
783
  }
784
784
  }
785
+ if (stateContext._stateMap) {
786
+ if (!stateContext._stateMap.has(id)) {
787
+ stateContext._stateMap.set(id, initialValue)
788
+ }
789
+ }
785
790
  let timeOut
786
791
  const main = createDeepProxy(states, (target, property) => {
787
792
  if (typeof section === 'string') {
@@ -890,7 +895,7 @@ export const restoreContext = (state_context) => {
890
895
  }
891
896
  export const HmrComponentMap=new Map()
892
897
  const renderedComponentsRusumed=new Set()
893
- export const globalRerender=(el,formercontext,context={})=>{
898
+ export const globalRerender=(el,formercontext,context={},oldStateContext)=>{
894
899
  if(!window?.hmr)return
895
900
  let element
896
901
  if (typeof el === 'string') {
@@ -899,7 +904,7 @@ export const globalRerender=(el,formercontext,context={})=>{
899
904
  element=dom.firstElementChild
900
905
  }else element=el
901
906
  formerStateContext=formercontext
902
- normal_component(element,formercontext,setStateContext,mapsPlugins,formercontext,pawaContext,stateWatch,context)
907
+ normal_component(element,formercontext,setStateContext,mapsPlugins,formercontext,pawaContext,stateWatch,context,oldStateContext)
903
908
  }
904
909
  /**
905
910
  *
@@ -10,30 +10,56 @@ function isPawaState(obj) {
10
10
  typeof obj.id === 'string'
11
11
  )
12
12
  }
13
- function restorePawaStateFromContext(oldCtx, newCtx) {
14
-
13
+
14
+ function snapshotInsert(ctx) {
15
+ const snapshot = {}
16
+ for (const key of Object.keys(ctx)) {
17
+ const val = ctx[key]
18
+ const raw = isPawaState(val) ? val.value : val
19
+ try {
20
+ snapshot[key] = structuredClone(raw)
21
+ } catch {
22
+ snapshot[key] = undefined
23
+ }
24
+ }
25
+ return snapshot
26
+ }
27
+
28
+ function sameInitial(a, b) {
29
+ if (a === undefined || b === undefined) return false
30
+ try {
31
+ return JSON.stringify(a) === JSON.stringify(b)
32
+ } catch {
33
+ return false
34
+ }
35
+ }
36
+
37
+ function restorePawaStateFromContext(oldCtx, newCtx,stateContext,oldStateContext) {
15
38
  if (!oldCtx || !newCtx) return
16
39
 
40
+ const prevInitials = oldStateContext._hmrinitial
41
+ const freshInitials = snapshotInsert(newCtx)
17
42
  for (const key of Object.keys(newCtx)) {
18
- console.log(key,'inside preserverState');
19
-
20
43
  const oldVal = oldCtx[key]
21
44
  const newVal = newCtx[key]
22
- if (isPawaState(oldVal) && isPawaState(newVal)) {
45
+ if (!isPawaState(oldVal) || !isPawaState(newVal)) continue
23
46
 
24
- const newOne=newVal.value
25
- const oldOne=oldVal.value
26
- if (typeof newOne === typeof oldOne) {
27
- newVal.value = oldVal.value
28
- }else if (newVal === null) {
29
- newVal.value = oldVal.value
30
- }
31
- console.log('is updating',key, newVal,'from',oldVal);
32
-
47
+ const initializerChanged =
48
+ prevInitials !== undefined &&
49
+ Object.prototype.hasOwnProperty.call(prevInitials, key) &&
50
+ !sameInitial(prevInitials[key], freshInitials[key])
51
+
52
+ if (initializerChanged) {
53
+ continue
54
+ }
55
+
56
+ if (typeof newVal.value === typeof oldVal.value ) {
57
+ newVal.value = oldVal.value
33
58
  }
34
59
  }
60
+ statecontext._hmrInitial = freshInitials
35
61
  }
36
- export const normal_component=(el,stateContext,setStateContext,mapsPlugin,formerStateContext,pawaContext,stateWatch,hmrPreserverContext)=>{
62
+ export const normal_component=(el,stateContext,setStateContext,mapsPlugin,formerStateContext,pawaContext,stateWatch,hmrPreserverContext,hmrOldStateContext)=>{
37
63
  // Checks if the content is an SVG fragment (graphic elements) without a root <svg> tag.
38
64
  // We exclude 'svg' from the match because a root <svg> tag can be parsed safely inside a <div>.
39
65
  const isSvgFragment = (str) => /^\s*<(path|circle|rect|line|polyline|polygon|ellipse|g|defs|symbol|use|image|text|animate|mask|pattern|clipPath|linearGradient|radialGradient|filter)/i.test(str);
@@ -107,23 +133,37 @@ export const normal_component=(el,stateContext,setStateContext,mapsPlugin,former
107
133
  stateContexts._name=el._componentName
108
134
  stateContexts._reactiveProps=reactiveProps
109
135
  stateContexts._template=el._template
110
- stateContexts._recallEffect=()=>{
111
-
112
- }
136
+ stateContexts._recallEffect=()=>{}
137
+ stateContexts._hmrinitial={}
113
138
  const storeContext=stateContexts
114
139
  let compo
115
140
  let awaits=false
116
141
  let suspense=''
117
142
  let elementContext=el._context
118
143
  if (__pawaDev.tool) {
119
- const id=Date.now() + Math.random()
144
+ const id=Date.now() + Math.random()
145
+ const removeFromHmrMap = () => {
146
+ const array = HmrComponentMap.get(componentName)
147
+ if (!array) return
148
+
149
+ const index = array.findIndex((item) => item.id === id)
150
+ if (index !== -1) {
151
+ array.splice(index, 1)
152
+ }
153
+
154
+ if (array.length === 0) {
155
+ HmrComponentMap.delete(componentName)
156
+ }
157
+ }
120
158
  if (HmrComponentMap.has(el._componentName)) {
121
159
  HmrComponentMap.get(el._componentName).push({id:id,template:el._template,el:el,stateContext:stateContexts,remove:()=>{
160
+ removeFromHmrMap()
122
161
  return pawaWayRemover(comment,endComment)
123
162
  },context:elementContext,comment:comment
124
163
  })
125
164
  }else{
126
165
  HmrComponentMap.set(el._componentName,[{id:id,template:el._template,el:el,stateContext:stateContexts,remove:()=>{
166
+ removeFromHmrMap()
127
167
  return pawaWayRemover(comment,endComment)
128
168
  },context:elementContext,comment:comment}])
129
169
  }
@@ -195,8 +235,9 @@ export const normal_component=(el,stateContext,setStateContext,mapsPlugin,former
195
235
 
196
236
  div.innerHTML = compo;
197
237
  if (component?._insert) {
198
- if (hmrPreserverContext) {
199
- restorePawaStateFromContext(hmrPreserverContext,component._insert)
238
+
239
+ if (hmrPreserverContext && hmrOldStateContext) {
240
+ restorePawaStateFromContext(hmrPreserverContext,component._insert,stateContexts,hmrOldStateContext)
200
241
  }
201
242
  Object.assign(el._context,component._insert)
202
243
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "pawajs",
3
- "version": "2.0.10",
3
+ "version": "2.2.0",
4
4
  "type":"module",
5
5
  "description": "pawajs library (reactive web runtime) for easily building web ui, enhancing html element, micro frontend, spa etc ",
6
6
  "main": "index.js",