import { fail , freeze , NodeLifeCycle , Hook , BaseNode , AnyObjectNode , SimpleType , devMode } from "../../internal" ; import { action } from "mobx" ; internal hidden export class ScalarNode < C , S , T > extends BaseNode < C , S , T > { declare readonly type : SimpleType < C , S , T > ; constructor ( simpleType , parent , subpath , environment , initialSnapshot ) { super ( simpleType , parent , subpath , environment ) ; try { this . storedValue = simpleType . createNewInstance ( initialSnapshot ) ; } catch ( e ) { this . state = NodeLifeCycle . DEAD ; throw e ; } this . state = NodeLifeCycle . CREATED ; this . finalizeCreation ( ) ; } get root ( ) : AnyObjectNode { if ( ! this . parent ) throw fail ( `This scalar node is not part of a tree` ) ; return this . parent . root ; } setParent ( newParent , subpath ) { const parentChanged = this . parent !== newParent ; const subpathChanged = this . subpath !== subpath ; if ( ! parentChanged && ! subpathChanged ) { return ; } if ( devMode ( ) ) { if ( ! subpath ) { throw fail ( "assertion failed: subpath expected" ) ; } if ( ! newParent ) { throw fail ( "assertion failed: parent expected" ) ; } if ( parentChanged ) { throw fail ( "assertion failed: scalar nodes cannot change their parent" ) ; } } this . environment = undefined ; this . baseSetParent ( this . parent , subpath ) ; } get snapshot ( ) : S { return freeze ( this . getSnapshot ( ) ) ; } getSnapshot ( ) { return this . type . getSnapshot ( this ) ; } toString ( ) { const path = ( this . isAlive ? this . path : this . pathUponDeath ) || "<root>" ; return `${ this . type . name }@${ path }${ this . isAlive ? "" : " [dead]" }` ; } die ( ) { if ( ! this . isAlive || this . state === NodeLifeCycle . DETACHING ) return ; this . aboutToDie ( ) ; this . finalizeDeath ( ) ; } finalizeCreation ( ) { this . baseFinalizeCreation ( ) ; } aboutToDie ( ) { this . baseAboutToDie ( ) ; } finalizeDeath ( ) { this . baseFinalizeDeath ( ) ; } protected fireHook ( name ) { this . fireInternalHook ( name ) ; } } ScalarNode . prototype . die = action ( ScalarNode . prototype . die ) ;