pawajs 1.4.22 → 1.4.24

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/CHANGELOG.md CHANGED
@@ -8,4 +8,6 @@ CHANGELOG.md
8
8
  + version 1.4.13 - 1.4.16 fixed issues for resuming component
9
9
  + version 1.4.18 - 1.4.19 fixed issue with component inside template and added event modifiers
10
10
  + version 1.4.19 - 1.4.21 made runEffect Global
11
- + version 1.4.22 fixed merger if when the ssr render else and else render down in pawajs engine
11
+ + version 1.4.22 fixed merger if when the ssr render else and else render down in pawajs engine
12
+ + version 1.4.23 fixed _context from production
13
+ + version 1.4.24 - add HMR mapping
package/index.js CHANGED
@@ -187,7 +187,8 @@ let stateContext = {
187
187
  _template: '',
188
188
  _serializedData:{},
189
189
  _static: [],
190
- _id:""
190
+ _id:"",
191
+ hmr:false
191
192
  }
192
193
 
193
194
  export const getCurrentContext = () => {
@@ -559,6 +560,7 @@ export const setStateContext = (context) => {
559
560
  stateContext._template = ''
560
561
  stateContext._resume = false
561
562
  stateContext._suspense=''
563
+ stateContext._hmr=false
562
564
  stateContext._hook={
563
565
  beforeMount:[],
564
566
  reactiveEffect:[],
@@ -739,6 +741,7 @@ const stateWatch = (callback, dependencies) => {
739
741
  export const restoreContext = (state_context) => {
740
742
  stateContext = state_context._formerContext
741
743
  }
744
+ export const HmrComponentMap=new Map()
742
745
  /**
743
746
  *
744
747
  * @param {PawaElement|HTMLElement} el
@@ -833,6 +836,7 @@ const mainAttribute = (el, exp) => {
833
836
  el._mainAttribute[exp.name] = exp.value
834
837
  el._checkStatic()
835
838
  let enter=false
839
+ const context=el._context
836
840
  const evaluate = () => {
837
841
 
838
842
  try {
@@ -848,7 +852,7 @@ const mainAttribute = (el, exp) => {
848
852
  if (checkKeywordsExistence(el._staticContext, expression)) {
849
853
  return ''
850
854
  } else {
851
- const result = el.safeEval(el._context, expression, `error at attribute ${exp.name}`, true)
855
+ const result = el.safeEval(context, expression, `error at attribute ${exp.name}`, true)
852
856
  isBoolean = result
853
857
  if (typeof result !== 'boolean') {
854
858
  return result ?? ''
@@ -915,7 +919,9 @@ const textContentHandler = (el, isName) => {
915
919
  nodesMap.set(node, node.nodeValue);
916
920
  });
917
921
  el._checkStatic()
922
+ const context=el._context
918
923
  const evaluate = () => {
924
+
919
925
  try {
920
926
  textNodes.forEach(textNode => {
921
927
  // Always use original content from map for evaluation
@@ -928,7 +934,7 @@ const textContentHandler = (el, isName) => {
928
934
  } else {
929
935
  if(expression === '')return value
930
936
  el._textContent[expression] = value
931
- const res = el.safeEval(el._context, expression, 'textContent', true)
937
+ const res = el.safeEval(context, expression, 'textContent', true)
932
938
  return String(res ?? '');
933
939
  }
934
940
  });
@@ -1138,7 +1144,8 @@ export const render = (el, contexts = {}, notRender, isName) => {
1138
1144
  render(child, context, number, isName)
1139
1145
  })
1140
1146
  el._callMount()
1141
- if (el.hasAttribute('p:c') && !el.hasAttribute('p-async')) {
1147
+ if (el.hasAttribute('p:c') && !el.hasAttribute('p-async')) {
1148
+ el._clearContext()
1142
1149
  el.removeAttribute('p:c')
1143
1150
  }
1144
1151
  }
package/merger/for.js CHANGED
@@ -17,14 +17,13 @@ export const merger_for = (el, stateContext, attr, arrayName, arrayItem, indexes
17
17
  }
18
18
  try {
19
19
  if (!func) {
20
- func=el.safeEval(el._context, arrayName, 'for-each');
20
+ func=el.safeEval(context, arrayName, 'for-each');
21
21
  }
22
22
  let array = func(...getEvalValues(context))
23
23
  let update;
24
24
  if (!firstEnter) {
25
25
  const div = document.createElement('div')
26
26
  array.forEach((item, index) => {
27
- const context = el._context
28
27
  const itemContext = {
29
28
  [arrayItem]: item,
30
29
  [indexes]: index,
@@ -94,7 +93,6 @@ export const merger_for = (el, stateContext, attr, arrayName, arrayItem, indexes
94
93
  })
95
94
  Array.from(div.children).forEach((child, index) => {
96
95
  const key = child.getAttribute('for-key') || index
97
- const context = el._context
98
96
  const itemContext = {
99
97
  [arrayItem]: array[index],
100
98
  [indexes]: index,
@@ -151,7 +149,7 @@ export const merger_for = (el, stateContext, attr, arrayName, arrayItem, indexes
151
149
  }
152
150
  if (firstEnter && !resume) {
153
151
  array.forEach((item, index) => {
154
- const context = el._context
152
+ const context = context
155
153
  const itemContext = {
156
154
  [arrayItem]: item,
157
155
  [indexes]: index,
@@ -190,7 +188,6 @@ export const merger_for = (el, stateContext, attr, arrayName, arrayItem, indexes
190
188
  const number={notRender:null,index:null}
191
189
  elementArray.forEach((keyComment) => {
192
190
  if(keyComment.nextElementSibling === null) return
193
- const context = el._context
194
191
  const itemContext = {
195
192
  [arrayItem]: array[keyComment._index],
196
193
  [indexes]: keyComment._index,
@@ -206,9 +203,10 @@ export const merger_for = (el, stateContext, attr, arrayName, arrayItem, indexes
206
203
  message: `Error from For directive ${error.message}`,
207
204
  error: error,
208
205
  template: el._template,
209
- el: el._el
206
+ el: el
210
207
  })
211
208
  }
212
209
  }
210
+ el?._clearContext()
213
211
  return evaluate
214
212
  }
package/merger/if.js CHANGED
@@ -13,6 +13,7 @@ export const merger_if=(el,attr,stateContext,resume=false,{comment,endComment,ch
13
13
  if (resume) {
14
14
  oldChain={id:attr.value}
15
15
  }
16
+ const context=el._context
16
17
  const evaluate = () => {
17
18
  if (endComment.parentElement === null) {
18
19
  el._deleteEffects()
@@ -23,11 +24,11 @@ export const merger_if=(el,attr,stateContext,resume=false,{comment,endComment,ch
23
24
  func =new Map()
24
25
  chained.forEach((item)=>{
25
26
  if(item.condition === 'else')return
26
- let funcs=el.safeEval(el._context,item.exp,'if')
27
+ let funcs=el.safeEval(context,item.exp,'if')
27
28
  func.set(item.exp,funcs)
28
29
  })
29
30
  }
30
- const values = getEvalValues(el._context)
31
+ const values = getEvalValues(context)
31
32
  let current
32
33
  chained.forEach(element => {
33
34
  if (current || element.condition === 'else') return
@@ -57,7 +58,7 @@ export const merger_if=(el,attr,stateContext,resume=false,{comment,endComment,ch
57
58
  }
58
59
  comment.data=`condition ${exp}`
59
60
  parent.insertBefore(newElement, endComment)
60
- render(newElement, el._context)
61
+ render(newElement, context)
61
62
  stateContext._hasRun = true
62
63
  }
63
64
  if (comment.nextSibling !== endComment && oldChain.id !== latestChain.id) {
@@ -110,7 +111,7 @@ export const merger_if=(el,attr,stateContext,resume=false,{comment,endComment,ch
110
111
  number.index = isIndex
111
112
  isIndex++
112
113
  if (number.notRender !== null && isIndex <= number.notRender) return
113
- render(value,el._context,number)
114
+ render(value,context,number)
114
115
  })
115
116
  stateContext._hasRun=true
116
117
  }
@@ -129,5 +130,6 @@ export const merger_if=(el,attr,stateContext,resume=false,{comment,endComment,ch
129
130
  })
130
131
  }
131
132
  }
133
+ el?._clearContext()
132
134
  return evaluate
133
135
  }
package/merger/key.js CHANGED
@@ -13,6 +13,7 @@ export const merger_key=(el,attr,stateContext,resume=false,{comment,endComment,c
13
13
  if (resume) {
14
14
  oldsate=old
15
15
  }
16
+ const context=el._context
16
17
  const evaluate = () => {
17
18
  if (endComment.parentElement === null) {
18
19
  el._deleteEffects()
@@ -21,9 +22,9 @@ export const merger_key=(el,attr,stateContext,resume=false,{comment,endComment,c
21
22
  let value=attr.value
22
23
  let keyValue
23
24
  if (!func) {
24
- func=el.safeEval(el._context,attr.value,'key')
25
+ func=el.safeEval(context,attr.value,'key')
25
26
  }
26
- const values = getEvalValues(el._context)
27
+ const values = getEvalValues(context)
27
28
  let current
28
29
 
29
30
  if (!firstEnter) {
@@ -39,7 +40,7 @@ export const merger_key=(el,attr,stateContext,resume=false,{comment,endComment,c
39
40
  comment.data=`key ${latestState}`
40
41
  endComment.data=`/ key ${latestState}`
41
42
  parent.insertBefore(newElement, endComment)
42
- render(newElement, el._context)
43
+ render(newElement, context)
43
44
  stateContext._hasRun = true
44
45
  }
45
46
  latestState=func(...values)
@@ -52,7 +53,7 @@ export const merger_key=(el,attr,stateContext,resume=false,{comment,endComment,c
52
53
  children.forEach((value, index) => {
53
54
  number.index=index
54
55
  if (number.notRender !== null && index <= number.notRender) return
55
- render(value,el._context,number)
56
+ render(value,context,number)
56
57
  })
57
58
  stateContext._hasRun=true
58
59
  once=true
@@ -100,5 +101,6 @@ export const merger_key=(el,attr,stateContext,resume=false,{comment,endComment,c
100
101
  })
101
102
  }
102
103
  }
104
+ el?._clearContext()
103
105
  return evaluate
104
106
  }
package/merger/switch.js CHANGED
@@ -13,6 +13,7 @@ export const merger_switch=(el,attr,stateContext,resume=false,{comment,endCommen
13
13
  if (resume) {
14
14
  oldChain={id:caseValue.value}
15
15
  }
16
+ const context=el._context
16
17
  const evaluate = () => {
17
18
  if (endComment.parentElement === null) {
18
19
  el._deleteEffects()
@@ -23,12 +24,12 @@ export const merger_switch=(el,attr,stateContext,resume=false,{comment,endCommen
23
24
  func =new Map()
24
25
  chained.forEach((item)=>{
25
26
  if(item.condition === 'default')return
26
- let funcs=safeEval(el._context,item.exp,item.element)
27
+ let funcs=safeEval(context,item.exp,item.element)
27
28
  func.set(item.exp,funcs)
28
29
  })
29
- switchFunc=safeEval(el._context,attr.value,el)
30
+ switchFunc=safeEval(context,attr.value,el)
30
31
  }
31
- const values = getEvalValues(el._context)
32
+ const values = getEvalValues(context)
32
33
  let current
33
34
  chained.forEach(element => {
34
35
  if (current || element.condition === 'default') return
@@ -58,7 +59,7 @@ export const merger_switch=(el,attr,stateContext,resume=false,{comment,endCommen
58
59
  }
59
60
  comment.data=`condition ${exp}`
60
61
  parent.insertBefore(newElement, endComment)
61
- render(newElement, el._context)
62
+ render(newElement, context)
62
63
  stateContext._hasRun = true
63
64
  }
64
65
  if (comment.nextSibling !== endComment && oldChain.id !== latestChain.id) {
@@ -110,7 +111,7 @@ export const merger_switch=(el,attr,stateContext,resume=false,{comment,endCommen
110
111
  number.index = isIndex
111
112
  isIndex++
112
113
  if (number.notRender !== null && isIndex <= number.notRender) return
113
- render(value,el._context,number)
114
+ render(value,context,number)
114
115
  })
115
116
  stateContext._hasRun=true
116
117
  }
@@ -128,5 +129,6 @@ export const merger_switch=(el,attr,stateContext,resume=false,{comment,endCommen
128
129
  })
129
130
  }
130
131
  }
132
+ el?._clearContext()
131
133
  return evaluate
132
134
  }
@@ -1,6 +1,6 @@
1
1
  import {propsValidator, setPawaDevError, pawaWayRemover, checkKeywordsExistence, sanitizeTemplate } from '../utils.js';
2
2
  import {PawaElement,PawaComment} from '../pawaElement.js';
3
- import {keepContext,render} from '../index.js'
3
+ import {keepContext,render, HmrComponentMap } from '../index.js'
4
4
  import {createEffect} from '../reactive.js'
5
5
  export const normal_component=(el,stateContext,setStateContext,mapsPlugin,formerStateContext,pawaContext,stateWatch)=>{
6
6
  const compoBeforeCall=mapsPlugin.compoBeforeCall
@@ -79,6 +79,21 @@ export const normal_component=(el,stateContext,setStateContext,mapsPlugin,former
79
79
  let compo
80
80
  let awaits=false
81
81
  let suspense=''
82
+ if (__pawaDev.tool) {
83
+ const id= crypto.randomUUID()
84
+ if (HmrComponentMap.has(stateContexts._name)) {
85
+ HmrComponentMap.get(stateContexts._name).push({id:id,template:el._template,el:el,stateContext:stateContexts})
86
+ }else{
87
+ HmrComponentMap.set(stateContexts._name,[{id:id,template:el._template,el:el,stateContext:stateContexts}])
88
+ }
89
+ el._setUnMount(()=>{
90
+ const array=HmrComponentMap.get(stateContexts._name)
91
+ if(array){
92
+ const index=array.findIndex(item => item.id === id)
93
+ if(index !== -1) array.splice(index,1)
94
+ }
95
+ })
96
+ }
82
97
  try {
83
98
  if(done){
84
99
  const compoCall=component.component(app)
@@ -103,6 +118,9 @@ export const normal_component=(el,stateContext,setStateContext,mapsPlugin,former
103
118
  childInsert(false)
104
119
  Promise.resolve().then(()=>{
105
120
  lifeCircle()
121
+
122
+ }).finally(()=>{
123
+ el._clearContext()
106
124
  })
107
125
  storeContext._hasRun=true
108
126
  stateContext=null
@@ -132,6 +150,7 @@ export const normal_component=(el,stateContext,setStateContext,mapsPlugin,former
132
150
  if (component?._insert) {
133
151
  Object.assign(el._context,component._insert)
134
152
  }
153
+ const context=el._context
135
154
  const propsSetter=()=>{
136
155
  if(Object.entries(el._restProps).length > 0){
137
156
  const findElement=div.querySelector('[--]') || div.querySelector('[rest]')
@@ -183,7 +202,7 @@ export const normal_component=(el,stateContext,setStateContext,mapsPlugin,former
183
202
  stateContexts?._error?.forEach((error) => {
184
203
  throw Error(error)
185
204
  })
186
- render(child, el._context)
205
+ render(child, context)
187
206
  }
188
207
  }
189
208
  }
@@ -226,7 +245,10 @@ export const normal_component=(el,stateContext,setStateContext,mapsPlugin,former
226
245
 
227
246
  })
228
247
  }
229
- if(awaits === false)lifeCircle()
248
+ if(awaits === false){
249
+ lifeCircle()
250
+ el._clearContext()
251
+ }
230
252
  stateContexts._hasRun=true
231
253
  keepContext(stateContexts._formerContext)
232
254
  if (stateContexts._transportContext) {
@@ -235,5 +257,5 @@ export const normal_component=(el,stateContext,setStateContext,mapsPlugin,former
235
257
  }
236
258
  stateContext=formerStateContext
237
259
  __pawaDev.totalComponent++
238
-
260
+
239
261
  }
@@ -28,4 +28,5 @@ export const templates=(el,notRender)=>{
28
28
  element.forEach(child=>{
29
29
  render(child,el._context,isResume?notRender:{ notRender: null, index: null })
30
30
  })
31
+ el?._clearContext()
31
32
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "pawajs",
3
- "version": "1.4.22",
3
+ "version": "1.4.24",
4
4
  "type":"module",
5
5
  "description": "pawajs library (reactive web runtime) for easily building web ui, enhancing html element, micro frontend etc ",
6
6
  "main": "index.js",
package/pawaElement.js CHANGED
@@ -5,12 +5,14 @@ import PawaComponent from './pawaComponent.js';
5
5
 
6
6
  export class PawaElement {
7
7
 
8
+ #context
8
9
  /**
9
10
  *
10
11
  * @param {HTMLElement} element
11
12
  * @param {object} context
12
13
  */
13
- constructor(element,context) {
14
+ constructor(element,context) {
15
+ this.#context=context
14
16
  const div=document.createElement('div'); div.appendChild(element.cloneNode(true))
15
17
  Object.assign(this, {
16
18
  _resetEffects: new Set(), _context: context, _avoidPawaRender: element.hasAttribute('pawa-avoid'),
@@ -25,7 +27,8 @@ constructor(element,context) {
25
27
  _pawaElementComponent: null, _componentTerminate: null, _cacheSetUp: false, _effectsCarrier: null,
26
28
  _pawaElementComponentName: '', _reCallEffect: this.reCallEffect, _ElementEffects: new Map(),
27
29
  _deCompositionElement: false, _restProps: {}, _kill: null, _isKill: false, _scriptFetching: element.hasAttribute('script'),
28
- _scriptDone: false, _underControl: null, safeEval: this.safeEval, _reactiveProps: {}
30
+ _scriptDone: false, _underControl: null, safeEval: this.safeEval, _reactiveProps: {},
31
+ _clearContext:this.clearContext
29
32
  })
30
33
  if (this._lazy) this._componentOrTemplate = true
31
34
  if(this._avoidPawaRender) {
@@ -42,6 +45,7 @@ constructor(element,context) {
42
45
  getChildrenTree(){
43
46
  return Array.from(this._el.children)
44
47
  }
48
+
45
49
  safeEval(context,expression,directive,resolve=false){
46
50
  try{
47
51
  const keys = Object.keys(context);
@@ -146,7 +150,7 @@ constructor(element,context) {
146
150
  })
147
151
  }
148
152
  reCheckStaticContext(){
149
- const context=this._context
153
+ const context=this.context
150
154
  if (this._staticContext.length > 0) {
151
155
  for (const [key,value] of Object.entries(context)) {
152
156
  if (this._staticContext.includes(key)) {
@@ -174,7 +178,6 @@ constructor(element,context) {
174
178
  return animate
175
179
  } catch (error) {
176
180
  console.error(error);
177
-
178
181
  }
179
182
  } else {
180
183
  this._callUnMOunt()
@@ -254,6 +257,11 @@ constructor(element,context) {
254
257
 
255
258
  }
256
259
  }
260
+ clearContext(){
261
+ if (!__pawaDev.tool) {
262
+ this._context={}
263
+ }
264
+ }
257
265
  setProps(){
258
266
  if (this._avoidPawaRender) {
259
267
  return
package/power.js CHANGED
@@ -99,7 +99,7 @@ const checkKeyModifiers = (e, modifiers, eventType) => {
99
99
  };
100
100
 
101
101
  // Shared helper to execute event with debounce/throttle/error handling
102
- const processEventExecution = (el, attrName, modifiers, callback, eventType, directiveName) => {
102
+ const processEventExecution = (el, attrName, modifiers, callback, eventType, directiveName,context) => {
103
103
  const execute = () => {
104
104
  try { callback(); }
105
105
  catch (error) { setPawaDevError({ message: `Error from ${directiveName}-${eventType} directive ${error.message}`, error, template: el._template }); }
@@ -255,6 +255,7 @@ export const event = (el, attr, stateContext) => {
255
255
  if (el._running || checkKeywordsExistence(el._staticContext, attr.value)) {
256
256
  return
257
257
  }
258
+ const context = el._context
258
259
 
259
260
  const directive = attr.name.substring(3); // 'on-click.prevent' → 'click.prevent'
260
261
  const parts = directive.split('.');
@@ -262,7 +263,6 @@ export const event = (el, attr, stateContext) => {
262
263
  const modifiers = new Set(parts.slice(1));
263
264
 
264
265
  el.removeAttribute(attr.name)
265
- const context = el._context
266
266
  const keys = Object.keys(context);
267
267
  const resolvePath = (path, obj) => {
268
268
  return path.split('.').reduce((acc, key) => acc?.[key], obj);
@@ -315,10 +315,10 @@ export const event = (el, attr, stateContext) => {
315
315
  });
316
316
  }
317
317
 
318
- const createBoundCallback = (el, code) => {
319
- const keys = Object.keys(el._context);
318
+ const createBoundCallback = (el, code,context) => {
319
+ const keys = Object.keys(context);
320
320
  const resolvePath = (path, obj) => path.split('.').reduce((acc, key) => acc?.[key], obj);
321
- const values = keys.map((key) => resolvePath(key, el._context));
321
+ const values = keys.map((key) => resolvePath(key, context));
322
322
  const func = new Function(...keys, code);
323
323
  return () => func(...values);
324
324
  };
@@ -326,7 +326,7 @@ const createBoundCallback = (el, code) => {
326
326
  export const mountElement = (el, attr) => {
327
327
  if (el._running) return;
328
328
  try {
329
- const func = createBoundCallback(el, attr.value);
329
+ const func = createBoundCallback(el, attr.value,el._context);
330
330
  el._MountFunctions.push(func);
331
331
  el.removeAttribute(attr.name);
332
332
  } catch (error) {
@@ -341,7 +341,7 @@ export const mountElement = (el, attr) => {
341
341
  export const unMountElement = (el, attr) => {
342
342
  if (el._running) return;
343
343
  try {
344
- const func = createBoundCallback(el, attr.value);
344
+ const func = createBoundCallback(el, attr.value,el._context);
345
345
  el._unMountFunctions.push(func);
346
346
  el.removeAttribute(attr.name);
347
347
  } catch (error) {
@@ -485,6 +485,7 @@ export const documentEvent = (el, attr) => {
485
485
  const resolvePath = (path, obj) => {
486
486
  return path.split('.').reduce((acc, key) => acc?.[key], obj);
487
487
  };
488
+ const values = keys.map((key) => resolvePath(key, el._context));
488
489
  const func = new Function('e', '$element', ...keys, `
489
490
  try{
490
491
  ${attr.value}
@@ -492,7 +493,6 @@ export const documentEvent = (el, attr) => {
492
493
  console.error(error.message, error.stack)
493
494
  __pawaDev.setError({msg: error.message, stack: error.stack, directives: 'out-event'})
494
495
  }`)
495
- const values = keys.map((key) => resolvePath(key, el._context));
496
496
 
497
497
  const handler = (e) => {
498
498
  if (!checkCommonModifiers(e, modifiers)) return;