jails-js 6.5.5 → 6.7.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/src/component.ts CHANGED
@@ -8,6 +8,7 @@ export const Component = ({ name, module, dependencies, node, templates, signal,
8
8
  let preserve = []
9
9
  let observer = null
10
10
  let observables = []
11
+ let effect = null
11
12
 
12
13
  const _model = module.model || {}
13
14
  const initialState = (new Function( `return ${node.getAttribute('html-model') || '{}'}`))()
@@ -15,7 +16,7 @@ export const Component = ({ name, module, dependencies, node, templates, signal,
15
16
  const scopeid = node.getAttribute('html-scopeid')
16
17
  const tpl = templates[ tplid ]
17
18
  const scope = g.scope[ scopeid ]
18
- const model = dup(module?.model?.apply ? _model({ elm:node, initialState }) : _model)
19
+ const model = dup(module?.model?.apply ? _model({ elm:node, initialState, dependencies }) : _model)
19
20
  const state = Object.assign({}, scope, model, initialState)
20
21
  const view = module.view? module.view : (data) => data
21
22
 
@@ -32,6 +33,14 @@ export const Component = ({ name, module, dependencies, node, templates, signal,
32
33
  node.addEventListener(':mount', fn)
33
34
  },
34
35
 
36
+ effect(fn) {
37
+ if( fn ) {
38
+ effect = fn
39
+ } else {
40
+ return effect
41
+ }
42
+ },
43
+
35
44
  /**
36
45
  * @State
37
46
  */
@@ -220,7 +229,17 @@ export const Component = ({ name, module, dependencies, node, templates, signal,
220
229
  const child = register.get(element)
221
230
  if(!child) return
222
231
  child.state.protected().forEach( key => delete data[key] )
223
- child.state.set(data)
232
+ const useEffect = child.effect()
233
+ if( useEffect ) {
234
+ const promise = useEffect(data)
235
+ if( promise && promise.then ) {
236
+ promise.then(() => child.state.set(data))
237
+ } else {
238
+ child.state.set(data)
239
+ }
240
+ } else {
241
+ child.state.set(data)
242
+ }
224
243
  })
225
244
  Promise.resolve().then(() => {
226
245
  g.scope = {}
package/types.d.ts CHANGED
@@ -24,6 +24,8 @@ export type Component = {
24
24
  getRaw() : any
25
25
  }
26
26
 
27
+ effect( callback: ( state: any ) => Promise<any> | void )
28
+
27
29
  main( mainArgs: ( t: Component ) => void ): void
28
30
 
29
31
  publish( name: string, value: any ) : void