<commit_before>angular.module('orderCloud')
    .factory('ocStateLoading', function($rootScope, $exceptionHandler, $injector, $q, OrderCloudSDK, ocRefreshToken) {
        var stateLoading = {};
        var service = {
            Init: _init,
            Watch: _watch,
            DefaultState: _defaultState
        };

        function _defaultState() {
            return $injector.get('defaultstate');
        }

        function _init() {
            $rootScope.$on('$stateChangeStart', function(e, toState, toParams, fromState) {
                var toParent = toState.parent || toState.name.split('.')[0];
                var fromParent = fromState.parent || fromState.name.split('.')[0];
                stateLoading[fromParent === toParent ? toParent : 'base'] = $q.defer();

                if((!toState.data || (toState.data && !toState.data.ignoreToken)) && !OrderCloudSDK.GetToken()) {
                    e.preventDefault();
                    ocRefreshToken(toState.name);
                }
            });<commit_msg>refactor: call ocRefresh if token is expired<commit_after>angular.module('orderCloud')
    .factory('ocStateLoading', function($rootScope, $exceptionHandler, $injector, $q, OrderCloudSDK, ocRefreshToken) {
        var stateLoading = {};
        var service = {
            Init: _init,
            Watch: _watch,
            DefaultState: _defaultState
        };

        function _defaultState() {
            return $injector.get('defaultstate');
        }

        function _init() {
            $rootScope.$on('$stateChangeStart', function(e, toState, toParams, fromState) {
                var toParent = toState.parent || toState.name.split('.')[0];
                var fromParent = fromState.parent || fromState.name.split('.')[0];
                stateLoading[fromParent === toParent ? toParent : 'base'] = $q.defer();

                var token = OrderCloudSDK.GetToken();
                if(token){
                    var expiresIn = JSON.parse(atob(token.split('.')[1])).exp * 1000;
                    var tokenExpired = expiresIn < Date.now();
                }

                if((!toState.data || (toState.data && !toState.data.ignoreToken)) && (!token || tokenExpired)){
                    e.preventDefault();
                    ocRefreshToken(toState.name);
                }
            });
{'fraction_non_alphanumeric': 0.13713798977853492, 'fraction_numerical': 0.0038330494037478705, 'mean_word_length': 1.9510050251256281, 'pattern_counts': {'":': 0, '<': 4, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4769872', 'n_tokens_mistral': 644, 'n_tokens_neox': 608, 'n_words': 115}