import * as Toolkit from 'chipmunk.client.toolkit' ; import { Dependency , SessionGetter } from '../session.dependency' ; export interface IState { [ key ] : any ; } export class ControllerSessionTabStates implements Dependency { private _logger : Toolkit . Logger ; private _uuid : string ; private _states : Map < string , IState > = new Map ( ) ; private _session : SessionGetter ; constructor ( uuid , getter ) { this . _uuid = uuid ; this . _session = getter ; this . _logger = new Toolkit . Logger ( `SessionStates [${ uuid }]` ) ; } public init ( ) { return Promise . resolve ( ) ; } public destroy ( ) { return new Promise ( ( resolve , reject ) => { this . _states . clear ( ) ; resolve ( ) ; } ) ; } public getName ( ) { return 'ControllerSessionTabStates' ; } public set < T > ( key , state ) { let stored = this . get ( key ) ; if ( stored === undefined ) { stored = { } ; } this . _states . set ( key , Object . assign ( stored , state ) ) ; } public drop ( key ) { this . _states . delete ( key ) ; } public get < T > ( key ) { return this . _states . get ( key ) as T ; } public applyStateTo ( key , target ) { const stored = this . get ( key ) ; if ( stored === undefined ) { return false ; } Object . keys ( stored ) . forEach ( ( prop ) => { target [ prop ] = stored [ prop ] ; } ) ; return true ; } }