angular.module('fbMocks') .factory('stateService', [ function (): fb.IStateService { var states: fb.IState[] = []; var addState = function (controller: string, id: any, callbackFn: () => {}) { var state = getState(controller, id); if (state) { state.callbackFn = callbackFn; } else { states.push({ controller: controller, id: id, callbackFn: callbackFn }); } } var loadState = function (controller: string, id: any): Object { var state = getState(controller, id); if (state && state.obj) { return state.obj; } return {}; } var clearState = function (controller: string, id: any): Object { var state = getState(controller, id); var stateObj = (state && state.obj) ? state.obj : {}; if (state) { removeState(state); } return stateObj; } var offLoadState = function (controller: string, id: any): void { var state = getState(controller, id); if (state) { if (state.stateObject) { state.obj = state.stateObject; } else { state.obj = state.callbackFn(); } } } var setState = function (controller: string, id: any, stateObject: any) { var state = getState(controller, id); if (state) { state.stateObject = stateObject; } else { states.push({ controller: controller, id: id, stateObject: stateObject }); } } var getState = function (controller: string, id: any): fb.IState { return _.findWhere(states, { controller: controller, id: id }); } var removeState = function (state: fb.IState): void { states = _.reject(states, function (s: fb.IState) { return s.controller === state.controller && s.id === state.id; }); } return { addState: addState, loadState: loadState, clearState: clearState, setState: setState, offLoadState: offLoadState }; } ]);