pawajs 2.1.0 → 2.3.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 +7 -2
- package/normal/component.js +64 -17
- package/package.json +1 -1
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
|
*
|
package/normal/component.js
CHANGED
|
@@ -10,25 +10,56 @@ function isPawaState(obj) {
|
|
|
10
10
|
typeof obj.id === 'string'
|
|
11
11
|
)
|
|
12
12
|
}
|
|
13
|
-
|
|
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)
|
|
45
|
+
if (!isPawaState(oldVal) || !isPawaState(newVal)) continue
|
|
19
46
|
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
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,38 @@ 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={}
|
|
138
|
+
const componentName=el._componentName
|
|
108
139
|
const storeContext=stateContexts
|
|
109
140
|
let compo
|
|
110
141
|
let awaits=false
|
|
111
142
|
let suspense=''
|
|
112
143
|
let elementContext=el._context
|
|
113
144
|
if (__pawaDev.tool) {
|
|
114
|
-
|
|
145
|
+
const id=Date.now() + Math.random()
|
|
146
|
+
const removeFromHmrMap = () => {
|
|
147
|
+
const array = HmrComponentMap.get(componentName)
|
|
148
|
+
if (!array) return
|
|
149
|
+
|
|
150
|
+
const index = array.findIndex((item) => item.id === id)
|
|
151
|
+
if (index !== -1) {
|
|
152
|
+
array.splice(index, 1)
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
if (array.length === 0) {
|
|
156
|
+
HmrComponentMap.delete(componentName)
|
|
157
|
+
}
|
|
158
|
+
}
|
|
115
159
|
if (HmrComponentMap.has(el._componentName)) {
|
|
116
160
|
HmrComponentMap.get(el._componentName).push({id:id,template:el._template,el:el,stateContext:stateContexts,remove:()=>{
|
|
161
|
+
removeFromHmrMap()
|
|
117
162
|
return pawaWayRemover(comment,endComment)
|
|
118
163
|
},context:elementContext,comment:comment
|
|
119
164
|
})
|
|
120
165
|
}else{
|
|
121
166
|
HmrComponentMap.set(el._componentName,[{id:id,template:el._template,el:el,stateContext:stateContexts,remove:()=>{
|
|
167
|
+
removeFromHmrMap()
|
|
122
168
|
return pawaWayRemover(comment,endComment)
|
|
123
169
|
},context:elementContext,comment:comment}])
|
|
124
170
|
}
|
|
@@ -190,8 +236,9 @@ export const normal_component=(el,stateContext,setStateContext,mapsPlugin,former
|
|
|
190
236
|
|
|
191
237
|
div.innerHTML = compo;
|
|
192
238
|
if (component?._insert) {
|
|
193
|
-
|
|
194
|
-
|
|
239
|
+
|
|
240
|
+
if (hmrPreserverContext && hmrOldStateContext) {
|
|
241
|
+
restorePawaStateFromContext(hmrPreserverContext,component._insert,stateContexts,hmrOldStateContext)
|
|
195
242
|
}
|
|
196
243
|
Object.assign(el._context,component._insert)
|
|
197
244
|
}
|
package/package.json
CHANGED