jails-js 6.0.1-beta.9 → 6.0.1
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/dist/index.js +23 -16
- package/dist/index.js.map +1 -1
- package/dist/jails.js +1 -1
- package/dist/jails.js.map +1 -1
- package/package.json +9 -2
- package/readme.md +6 -13
- package/src/component.ts +20 -14
- package/src/element.ts +4 -2
- package/src/index.ts +1 -0
- package/tsconfig.json +3 -0
- package/types.d.ts +47 -0
package/src/component.ts
CHANGED
@@ -2,7 +2,10 @@ import { safe, g, dup } from './utils'
|
|
2
2
|
import { Idiomorph } from 'idiomorph/dist/idiomorph.esm'
|
3
3
|
import { publish, subscribe } from './utils/pubsub'
|
4
4
|
|
5
|
-
export const Component = ({ name, module, dependencies, node, templates, signal }) => {
|
5
|
+
export const Component = ({ name, module, dependencies, node, templates, signal, register }) => {
|
6
|
+
|
7
|
+
let tick
|
8
|
+
let preserve = []
|
6
9
|
|
7
10
|
const _model = module.model || {}
|
8
11
|
const initialState = (new Function( `return ${node.getAttribute('html-model') || '{}'}`))()
|
@@ -13,8 +16,6 @@ export const Component = ({ name, module, dependencies, node, templates, signal
|
|
13
16
|
const model = dup(module?.model?.apply ? _model({ elm:node, initialState }) : _model)
|
14
17
|
const state = Object.assign({}, scope, model, initialState)
|
15
18
|
const view = module.view? module.view : (data) => data
|
16
|
-
let preserve = []
|
17
|
-
let tick
|
18
19
|
|
19
20
|
const base = {
|
20
21
|
name,
|
@@ -62,9 +63,10 @@ export const Component = ({ name, module, dependencies, node, templates, signal
|
|
62
63
|
}
|
63
64
|
|
64
65
|
const newstate = Object.assign({}, state, scope)
|
65
|
-
render(newstate)
|
66
66
|
|
67
|
-
return Promise
|
67
|
+
return new Promise((resolve) => {
|
68
|
+
render(newstate, () => resolve(newstate))
|
69
|
+
})
|
68
70
|
},
|
69
71
|
|
70
72
|
get() {
|
@@ -139,36 +141,40 @@ export const Component = ({ name, module, dependencies, node, templates, signal
|
|
139
141
|
}
|
140
142
|
}
|
141
143
|
|
142
|
-
const render = ( data ) => {
|
144
|
+
const render = ( data, callback = (() => {}) ) => {
|
143
145
|
clearTimeout( tick )
|
144
146
|
tick = setTimeout(() => {
|
145
147
|
const html = tpl.render.call( view(data), node, safe, g )
|
146
|
-
Idiomorph.morph( node, html, IdiomorphOptions(node) )
|
148
|
+
Idiomorph.morph( node, html, IdiomorphOptions(node, register) )
|
147
149
|
Promise.resolve().then(() => {
|
148
150
|
node.querySelectorAll('[tplid]')
|
149
151
|
.forEach((element) => {
|
150
|
-
|
151
|
-
|
152
|
-
|
152
|
+
const child = register.get(element)
|
153
|
+
if(!child) return
|
154
|
+
child.state.protected().forEach( key => delete data[key] )
|
155
|
+
child.state.set(data)
|
153
156
|
})
|
154
|
-
Promise.resolve().then(() =>
|
157
|
+
Promise.resolve().then(() => {
|
158
|
+
g.scope = {}
|
159
|
+
callback()
|
160
|
+
})
|
155
161
|
})
|
156
162
|
})
|
157
163
|
}
|
158
164
|
|
159
165
|
render( state )
|
160
|
-
|
166
|
+
register.set( node, base )
|
161
167
|
return module.default( base )
|
162
168
|
}
|
163
169
|
|
164
|
-
const IdiomorphOptions = ( parent ) => ({
|
170
|
+
const IdiomorphOptions = ( parent, register ) => ({
|
165
171
|
callbacks: {
|
166
172
|
beforeNodeMorphed( node ) {
|
167
173
|
if( node.nodeType === 1 ) {
|
168
174
|
if( 'html-static' in node.attributes ) {
|
169
175
|
return false
|
170
176
|
}
|
171
|
-
if( node
|
177
|
+
if( register.get(node) && node !== parent ) {
|
172
178
|
return false
|
173
179
|
}
|
174
180
|
}
|
package/src/element.ts
CHANGED
@@ -1,5 +1,7 @@
|
|
1
1
|
import { Component } from './component'
|
2
2
|
|
3
|
+
const register = new WeakMap()
|
4
|
+
|
3
5
|
export const Element = ({ component, templates, start }) => {
|
4
6
|
|
5
7
|
const { name, module, dependencies } = component
|
@@ -24,7 +26,8 @@ export const Element = ({ component, templates, start }) => {
|
|
24
26
|
module,
|
25
27
|
dependencies,
|
26
28
|
templates,
|
27
|
-
signal: this.abortController.signal
|
29
|
+
signal: this.abortController.signal,
|
30
|
+
register
|
28
31
|
})
|
29
32
|
|
30
33
|
if ( rtrn && rtrn.constructor === Promise ) {
|
@@ -39,7 +42,6 @@ export const Element = ({ component, templates, start }) => {
|
|
39
42
|
disconnectedCallback() {
|
40
43
|
this.dispatchEvent( new CustomEvent(':unmount') )
|
41
44
|
this.abortController.abort()
|
42
|
-
delete this.base
|
43
45
|
}
|
44
46
|
}
|
45
47
|
}
|
package/src/index.ts
CHANGED
package/tsconfig.json
CHANGED
package/types.d.ts
ADDED
@@ -0,0 +1,47 @@
|
|
1
|
+
type EventCallback = ( Event: Event, data?:any ) => void
|
2
|
+
|
3
|
+
export declare const templateConfig: (options: any) => void;
|
4
|
+
export declare const register: (name: string, module: Module, dependencies?: any) => void
|
5
|
+
export declare const start: (target?: HTMLElement) => void;
|
6
|
+
export declare const subscribe:( subject: string, callback: (data:any) => void ) => Function
|
7
|
+
export declare const publish: ( subject: string, data :any ) => void
|
8
|
+
|
9
|
+
export type Component = {
|
10
|
+
|
11
|
+
elm: HTMLElement
|
12
|
+
dependencies: any
|
13
|
+
|
14
|
+
state : {
|
15
|
+
protected( props: Array<string> ): Array<string>
|
16
|
+
set( data: object ) : Promise<any>
|
17
|
+
set( callback: ( state: object ) => any ) : Promise<any>
|
18
|
+
get() : object
|
19
|
+
getRaw() : object
|
20
|
+
}
|
21
|
+
|
22
|
+
main( mainArgs: ( t: Component ) => void ): void
|
23
|
+
|
24
|
+
publish( name: string, value: any ) : void
|
25
|
+
|
26
|
+
subscribe( name: string, value: Function ) : Function
|
27
|
+
|
28
|
+
unmount( callback: () => void ) : void
|
29
|
+
|
30
|
+
on( eventName: string, selector: string | EventCallback, callback?: EventCallback ): void
|
31
|
+
|
32
|
+
emit( eventName: string, data: any ) : void
|
33
|
+
|
34
|
+
off( eventName: string, callback: () => void ): void
|
35
|
+
|
36
|
+
trigger( eventName: string, selector :string, data: any ): void
|
37
|
+
|
38
|
+
innerHTML( target: HTMLElement | string, html?: string ) : void
|
39
|
+
}
|
40
|
+
|
41
|
+
export type Model = {
|
42
|
+
[key: string] : object
|
43
|
+
}
|
44
|
+
|
45
|
+
export type View = ( state: object ) => object
|
46
|
+
|
47
|
+
export type Template = ({ elm: HTMLElement, children: string }) => string
|