import * as Q from 'q' ; import * as React from 'react' ; import { QueryExpand , QueryHierarchyItem } from "TFS/WorkItemTracking/Contracts" ; import * as FluxTypes from "./FluxTypes" ; import { Store } from "VSS/Flux/Store" ; import { Action } from "VSS/Flux/Action" ; export interface Props { } export interface State { } export interface ConfigurationState extends State { } export class StoreBase < T > extends Store { private state : T ; private actions : ActionsBase ; constructor ( actions , initialState ) { super ( ) ; this . actions = actions ; this . state = initialState ; this . actions . stateChanged . addListener ( ( data ) => { this . recieveAndNotify ( data ) ; } ) ; } public getState ( ) { return this . state ; } public setState ( state ) { this . state = state ; } private recieveAndNotify ( state ) { this . setState ( state ) ; this . emitChanged ( ) ; } } export class ActionsBase { public stateChanged : Action < State > ; constructor ( ) { this . stateChanged = new Action < State > ( ) ; } } export class ActionCreator < T extends State > { private actions : ActionsBase ; constructor ( actions ) { this . actions = actions ; } public getDefaultState ( ) { return { } as T ; } public requestInitialState ( ) { let promise = this . requestData ( ) ; promise . then ( ( state ) => { return this . notifyListenersOfStateChange ( state ) ; } ) ; return promise ; } public requestData ( ) { return Q ( { } as T ) as IPromise < T > ; } public notifyListenersOfStateChange ( state ) { this . actions . stateChanged . invoke ( state ) ; } } export class ComponentBase < P extends Props , S extends State > extends React . Component < P , S > { protected actionCreator : ActionCreator < S > ; protected store : StoreBase < S > ; protected actions : ActionsBase ; private setStoreStateDelegate = ( ) => this . setStoreState ( ) ; private setStateDelegate = ( state ) => { this . setState ( state ) ; } ; public componentWillMount ( ) { this . actions = this . createActions ( ) ; this . actionCreator = this . createActionCreator ( ) ; this . store = this . createStore ( ) ; this . registerStateListener ( ) ; this . initiateRequest ( ) ; } private registerStateListener ( ) { this . actions . stateChanged . addListener ( this . setStateDelegate ) ; } protected initiateRequest ( ) { this . actionCreator . requestInitialState ( ) ; } private setStoreState ( ) { this . setState ( this . getStoreState ( ) ) ; } public getStoreState ( ) { return this . store . getState ( ) ; } protected createActionCreator ( ) { return new ActionCreator < S > ( this . actions ) ; } protected createActions ( ) { return new ActionsBase ( ) ; } protected createStore ( ) { return new StoreBase < S > ( this . actions , this . actionCreator . getDefaultState ( ) as S ) ; } public componentDidMount ( ) { this . store && this . store . addChangedListener ( this . setStoreStateDelegate ) ; } public componentWillUnmount ( ) { this . store && this . store . removeChangedListener ( this . setStoreStateDelegate ) ; this . actions . stateChanged . removeListener ( this . setStateDelegate ) ; } }