/*****************
 * State
 */ import { define , Record , Store } from 'type-r' ; import { ComponentClass } from './common' ; export interface StateDefinition { state ? : object | typeof Record ; State ? : typeof Record ; } export interface StateProto { State ? : typeof Record ; } export default function process ( this , definition , BaseComponentClass ) { const { prototype } = this ; let { state , State } = definition ; if ( typeof state === 'function' ) { State = state ; state = void 0 ; } if ( state ) { const BaseClass = State || prototype . State || Record ; @ define class ComponentState extends BaseClass { static attributes = state ; } prototype . State = ComponentState ; } else if ( State ) { prototype . State = State ; } if ( state || State ) { this . mixins . merge ( [ StateMixin , UpdateOnNestedChangesMixin ] ) ; } } export const StateMixin = { _initializeState ( ) { const state = this . state = this . props . __keepState || new this . State ( ) ; state . _owner = this ; state . _ownerKey = 'state' ; } , context : { _nestedStore : Store } , getStore ( ) { let context , state ; return ( ( context = this . context ) && context . _nestedStore ) || ( ( state = this . state ) && state . _defaultStore ) ; } , componentWillUnmount ( ) { const { state } = this ; state . _owner = state . _ownerKey = void 0 ; this . _preventDispose || state . dispose ( ) ; this . state = void 0 ; } } ; export const UpdateOnNestedChangesMixin = { _onChildrenChange ( ) { } , componentDidMount ( ) { this . _onChildrenChange = this . asyncUpdate ; } } ;