1- import { findElementByPath } from './dom'
21import { HashType } from '../jsx/VirtualElement'
32import { ContextType } from '../compiler/template/state-usage'
43import { AbstractState } from './abstract-state'
54import { getStateListenerCleanupMap } from './state-listener'
5+ import { updateStateUsages } from './state-usage'
66
77function cloneStateValue < V > ( value : V ) : V {
88 if ( [ 'string' , 'number' , 'boolean' ] . includes ( typeof value ) || [ null , undefined ] . includes ( value ) ) {
@@ -16,11 +16,6 @@ function cloneStateValue<V>(value: V): V {
1616}
1717
1818export type StateChangeHandlerType = ( ...args : ( [ any , ( value : any ) => void ] | HTMLElement ) [ ] ) => ( ( ) => void ) | void
19- type ClientContextType < T extends 'child' | 'prop' > = ContextType < T > & { makeString : ( value : string ) => string }
20-
21- declare const stateUsages : Map < HashType , ( ...values : any [ ] ) => string >
22- declare const stateUsagesParameters : Map < HashType , State [ ] >
23- declare const stateUsagesContexts : Map < HashType , ClientContextType < any > [ ] >
2419
2520export class State < V = any > extends AbstractState < V > {
2621 private _listeners : ( StateChangeHandlerType ) [ ] = [ ]
@@ -53,36 +48,7 @@ export class State<V = any> extends AbstractState<V> {
5348 cleanup ( )
5449 } )
5550 this . _value = cloneStateValue ( value )
56- stateStateUsagesMap [ this . _id ] ?. forEach ( stateUsageId => {
57- const contexts = stateUsagesContexts . get ( stateUsageId )
58- contexts . forEach ( context => {
59- let transform
60- if ( stateUsages . has ( stateUsageId ) ) {
61- transform = stateUsages . get ( stateUsageId )
62- transform = stateUsages . get ( stateUsageId )
63- } else {
64- transform = value => String ( value )
65- }
66- const transformParameters = stateUsagesParameters . get ( stateUsageId )
67- const target = findElementByPath ( context . contextElement )
68- const newString = context . makeString (
69- transform (
70- ...transformParameters . map ( state => state . valueOf ( ) )
71- )
72- )
73- if ( context . type === 'child' ) {
74- if ( target . childElementCount == 0 ) {
75- target . innerText = newString
76- } else {
77- target . children [ context . beforeChild ] . previousSibling . replaceWith (
78- document . createTextNode ( newString )
79- )
80- }
81- } else if ( context . type === 'prop' ) {
82- // todo
83- }
84- } )
85- } )
51+ updateStateUsages ( this . _id )
8652 if ( executeListeners ) {
8753 this . _listeners . forEach ( listener => stateListenerCleanupMap . set ( listener , listener ( ) ) )
8854 }
@@ -102,7 +68,6 @@ export class State<V = any> extends AbstractState<V> {
10268}
10369
10470const states = new Map ( )
105- declare const stateStateUsagesMap : { [ key : HashType ] : HashType [ ] }
10671
10772export function getClientState ( id : HashType , value ?: any ) {
10873 if ( ! states . has ( id ) )
0 commit comments