11'use strict' ;
2+ const { UID } = require ( '../shared/constants.js' ) ;
3+ const { WeakMap } = require ( '../shared/poorlyfills.js' ) ;
4+
25// hyperHTML.Component is a very basic class
36// able to create Custom Elements like components
47// including the ability to listen to connect/disconnect
58// events via onconnect/ondisconnect attributes
9+ // Components can be created imperatively or declaratively.
10+ // The main difference is that declared components
11+ // will not automatically render on setState(...)
12+ // to simplify state handling on render.
613function Component ( ) { }
714Object . defineProperty ( exports , '__esModule' , { value : true } ) . default = Component
815
916// components will lazily define html or svg properties
1017// as soon as these are invoked within the .render() method
1118// Such render() method is not provided by the base class
1219// but it must be available through the Component extend.
20+ // Declared components could implement a
21+ // render(props) method too and use props as needed.
1322function setup ( content ) {
23+ const children = new WeakMap ;
24+ const create = Object . create ;
25+ const createEntry = ( wm , id , component ) => {
26+ wm . set ( id , component ) ;
27+ return component ;
28+ } ;
29+ const get = ( Class , info , id ) => {
30+ switch ( typeof id ) {
31+ case 'object' :
32+ case 'function' :
33+ const wm = info . w || ( info . w = new WeakMap ) ;
34+ return wm . get ( id ) || createEntry ( wm , id , new Class ) ;
35+ default :
36+ const sm = info . p || ( info . p = create ( null ) ) ;
37+ return sm [ id ] || ( sm [ id ] = new Class ) ;
38+ }
39+ } ;
40+ const set = context => {
41+ const info = { w : null , p : null } ;
42+ children . set ( context , info ) ;
43+ return info ;
44+ } ;
45+ Object . defineProperties (
46+ Component ,
47+ {
48+ for : {
49+ configurable : true ,
50+ value ( context , id ) {
51+ const info = children . get ( context ) || set ( context ) ;
52+ return get ( this , info , id == null ? 'default' : id ) ;
53+ }
54+ }
55+ }
56+ ) ;
1457 Object . defineProperties (
1558 Component . prototype ,
1659 {
@@ -25,11 +68,12 @@ function setup(content) {
2568 svg : lazyGetter ( 'svg' , content ) ,
2669 state : lazyGetter ( 'state' , function ( ) { return this . defaultState ; } ) ,
2770 defaultState : { get ( ) { return { } ; } } ,
28- setState : { value ( state ) {
71+ setState : { value ( state , render ) {
2972 const target = this . state ;
3073 const source = typeof state === 'function' ? state . call ( this , target ) : state ;
3174 for ( const key in source ) target [ key ] = source [ key ] ;
32- this . render ( ) ;
75+ if ( render !== false ) this . render ( ) ;
76+ return this ;
3377 } }
3478 }
3579 ) ;
0 commit comments