pawajs 2.1.0 → 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,25 +10,56 @@ function isPawaState(obj) {
10
10
  typeof obj.id === 'string'
11
11
  )
12
12
  }
13
- function restorePawaStateFromContext(oldCtx, newCtx) {
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) {
14
38
  if (!oldCtx || !newCtx) return
39
+
40
+ const prevInitials = oldStateContext._hmrinitial
41
+ const freshInitials = snapshotInsert(newCtx)
15
42
  for (const key of Object.keys(newCtx)) {
16
43
  const oldVal = oldCtx[key]
17
44
  const newVal = newCtx[key]
18
- if (isPawaState(oldVal) && isPawaState(newVal)) {
45
+ if (!isPawaState(oldVal) || !isPawaState(newVal)) continue
19
46
 
20
- const newOne=newVal.value
21
- const oldOne=oldVal.value
22
- if (typeof newOne === typeof oldOne) {
23
- newVal.value = oldVal.value
24
- }else if (newVal === null) {
25
- newVal.value = oldVal.value
26
- }
27
-
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
28
58
  }
29
59
  }
60
+ statecontext._hmrInitial = freshInitials
30
61
  }
31
- 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)=>{
32
63
  // Checks if the content is an SVG fragment (graphic elements) without a root <svg> tag.
33
64
  // We exclude 'svg' from the match because a root <svg> tag can be parsed safely inside a <div>.
34
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);
@@ -102,23 +133,37 @@ export const normal_component=(el,stateContext,setStateContext,mapsPlugin,former
102
133
  stateContexts._name=el._componentName
103
134
  stateContexts._reactiveProps=reactiveProps
104
135
  stateContexts._template=el._template
105
- stateContexts._recallEffect=()=>{
106
-
107
- }
136
+ stateContexts._recallEffect=()=>{}
137
+ stateContexts._hmrinitial={}
108
138
  const storeContext=stateContexts
109
139
  let compo
110
140
  let awaits=false
111
141
  let suspense=''
112
142
  let elementContext=el._context
113
143
  if (__pawaDev.tool) {
114
- 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
+ }
115
158
  if (HmrComponentMap.has(el._componentName)) {
116
159
  HmrComponentMap.get(el._componentName).push({id:id,template:el._template,el:el,stateContext:stateContexts,remove:()=>{
160
+ removeFromHmrMap()
117
161
  return pawaWayRemover(comment,endComment)
118
162
  },context:elementContext,comment:comment
119
163
  })
120
164
  }else{
121
165
  HmrComponentMap.set(el._componentName,[{id:id,template:el._template,el:el,stateContext:stateContexts,remove:()=>{
166
+ removeFromHmrMap()
122
167
  return pawaWayRemover(comment,endComment)
123
168
  },context:elementContext,comment:comment}])
124
169
  }
@@ -190,8 +235,9 @@ export const normal_component=(el,stateContext,setStateContext,mapsPlugin,former
190
235
 
191
236
  div.innerHTML = compo;
192
237
  if (component?._insert) {
193
- if (hmrPreserverContext) {
194
- restorePawaStateFromContext(hmrPreserverContext,component._insert)
238
+
239
+ if (hmrPreserverContext && hmrOldStateContext) {
240
+ restorePawaStateFromContext(hmrPreserverContext,component._insert,stateContexts,hmrOldStateContext)
195
241
  }
196
242
  Object.assign(el._context,component._insert)
197
243
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "pawajs",
3
- "version": "2.1.0",
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",