const tagViews = {
    namespaced: true,
    state: {
        visitedViews: [],
        cachedViews: []
    },
    mutations: {
        ADD_VISITED_VIEW: (state, view) => {
            if (state.visitedViews.some(v => v.fullPath === view.fullPath)) return;
            state.visitedViews.push(
                Object.assign({}, view, {
                    title: view.title || view.meta.name || view.name
                })
            );
        },
        ADD_CACHED_VIEW: (state, view) => {
            if (state.cachedViews.includes(view.fullPath)) return;
            if (view.meta && !view.meta.noCache) {
                state.cachedViews.push(view.fullPath);
            }
        },
        DEL_VISITED_VIEW: (state, view) => {
            for (const [i, v] of state.visitedViews.entries()) {
                if (v.fullPath === view.fullPath) {
                    state.visitedViews.splice(i, 1)
                    break;
                }
            }
        },
        DEL_CACHED_VIEW: (state, view) => {
            const index = state.cachedViews.indexOf(view.fullPath);
            index > -1 && state.cachedViews.splice(index, 1);
        }

    },
    actions: {
        //异步属性通用
        addView({ dispatch }, view) {
            dispatch('addVisitedView', view);
            dispatch('addCachedView', view);
        },
        addVisitedView({ commit }, view) {
            commit('ADD_VISITED_VIEW', view);
        },
        addCachedView({ commit }, view) {
            commit('ADD_CACHED_VIEW', view);
        },
        delView({ commit }, view) {
            commit('DEL_VISITED_VIEW', view);
            commit('DEL_CACHED_VIEW', view);
        },

    }
}




export default tagViews;


