import pick from '@tinkoff/utils/object/pick' ; import type { BaseStore as BaseStoreInterface } from '@tramvai/types-actions-state-context' ; import { SimpleEmitter } from './SimpleEmitter' ; export { BaseStoreConstructor } from '@tramvai/types-actions-state-context' ; deprecated export abstract class BaseStore < State > extends SimpleEmitter implements BaseStoreInterface < State > { static storeName : string ; state : Readonly < State > = Object . create ( null ) ; hydrateKeys : Record < keyof State , boolean > = Object . create ( null ) ; dispatcher ? : Readonly < Record < string , any > > ; static handlers : Record < string , Function | string > ; getState ( ) { return this . state ; } dehydrate ( ) { return pick ( Object . keys ( this . hydrateKeys ) , this . state ) as Partial < State > ; } rehydrate ( state ) { this . setStateSilently ( state ) ; } protected writeHydrateKeys < K extends keyof State > ( state ) { Object . keys ( state ) . forEach ( ( key ) => { this . hydrateKeys [ key as K ] = true ; } ) ; } protected shouldStateUpdate < K extends keyof State > ( nextState ) { return ! ! Object . keys ( nextState ) . length ; } protected setState < K extends keyof State > ( state , force = false ) { if ( force || this . shouldStateUpdate ( state ) ) { this . setStateSilently ( state ) ; this . emit ( 'change' ) ; } } protected setStateSilently < K extends keyof State > ( state ) { this . state = { ... this . state , ... state } ; this . writeHydrateKeys ( state ) ; } protected replaceState ( state ) { this . replaceStateSilently ( state ) ; this . emit ( 'change' ) ; } protected replaceStateSilently ( state ) { this . state = state ; this . hydrateKeys = Object . create ( null ) ; this . writeHydrateKeys ( state ) ; } }