|
| 1 | +import * as Vue from 'vue'; |
| 2 | +import * as Vuex from 'vuex'; |
| 3 | +import createLogger from 'vuex/logger'; |
| 4 | + |
| 5 | +Vue.use(Vuex); |
| 6 | + |
| 7 | +interface ISimpleState { |
| 8 | + count: number; |
| 9 | +} |
| 10 | + |
| 11 | +const INCREMENT = 'INCREMENT'; |
| 12 | +const INCREMENT_OBJECT = 'INCREMENT_OBJECT'; |
| 13 | + |
| 14 | +function createStore(): Vuex.Store<ISimpleState> { |
| 15 | + const state: ISimpleState = { |
| 16 | + count: 0 |
| 17 | + }; |
| 18 | + |
| 19 | + const mutations: Vuex.MutationTree<ISimpleState> = { |
| 20 | + [INCREMENT] (state: ISimpleState, amount: number) { |
| 21 | + state.count = state.count + amount; |
| 22 | + }, |
| 23 | + [INCREMENT_OBJECT] (state: ISimpleState, payload: number) { |
| 24 | + state.count = state.count + payload; |
| 25 | + } |
| 26 | + }; |
| 27 | + |
| 28 | + return new Vuex.Store({ |
| 29 | + state, |
| 30 | + mutations, |
| 31 | + strict: true |
| 32 | + }); |
| 33 | +} |
| 34 | + |
| 35 | +namespace TestDispatch { |
| 36 | + const store = createStore(); |
| 37 | + |
| 38 | + store.dispatch(INCREMENT, 1); |
| 39 | + store.dispatch({ |
| 40 | + type: INCREMENT_OBJECT, |
| 41 | + silent: true, |
| 42 | + payload: 10 |
| 43 | + }); |
| 44 | +} |
| 45 | + |
| 46 | +namespace TestWithComponent { |
| 47 | + const store = createStore(); |
| 48 | + |
| 49 | + const a: vuejs.ComponentOption = { |
| 50 | + vuex: { |
| 51 | + getters: { |
| 52 | + count: (state: ISimpleState) => state.count |
| 53 | + }, |
| 54 | + actions: { |
| 55 | + incrementCounter({ dispatch, state }: Vuex.Store<ISimpleState>) { |
| 56 | + dispatch(INCREMENT, 1); |
| 57 | + } |
| 58 | + } |
| 59 | + } |
| 60 | + }; |
| 61 | + |
| 62 | + const app = new Vue({ |
| 63 | + el: '#app', |
| 64 | + components: { a }, |
| 65 | + store |
| 66 | + }); |
| 67 | + |
| 68 | + const b: number = app.$store.state.count; |
| 69 | +} |
| 70 | + |
| 71 | +namespace TestModules { |
| 72 | + interface IModuleAState { |
| 73 | + value: number; |
| 74 | + } |
| 75 | + |
| 76 | + interface IModuleBState { |
| 77 | + value: string; |
| 78 | + } |
| 79 | + |
| 80 | + interface IModuleState { |
| 81 | + a: IModuleAState; |
| 82 | + b: IModuleBState; |
| 83 | + } |
| 84 | + |
| 85 | + const aState: IModuleAState = { value: 1 }; |
| 86 | + const bState: IModuleBState = { value: 'test' }; |
| 87 | + |
| 88 | + const aMutations: Vuex.MutationTree<IModuleAState> = { |
| 89 | + INCREMENT (state: IModuleAState) { |
| 90 | + state.value = state.value + 1; |
| 91 | + } |
| 92 | + }; |
| 93 | + |
| 94 | + const bMutations: Vuex.MutationTree<IModuleBState> = { |
| 95 | + APPEND (state: IModuleBState, value: string) { |
| 96 | + state.value = state.value + value; |
| 97 | + } |
| 98 | + }; |
| 99 | + |
| 100 | + const a = { state: aState, mutations: aMutations }; |
| 101 | + const b = { state: bState, mutations: bMutations }; |
| 102 | + |
| 103 | + const store = new Vuex.Store<IModuleState>({ |
| 104 | + modules: { a, b } |
| 105 | + }); |
| 106 | + |
| 107 | + const valA: number = store.state.a.value; |
| 108 | + const valB: string = store.state.b.value; |
| 109 | +} |
| 110 | + |
| 111 | +namespace TestPlugin { |
| 112 | + const a = (store: Vuex.Store<any>) => {}; |
| 113 | + |
| 114 | + const b = (store: Vuex.Store<ISimpleState>) => {}; |
| 115 | + |
| 116 | + new Vuex.Store<ISimpleState>({ |
| 117 | + state: { count: 1 }, |
| 118 | + plugins: [a, b] |
| 119 | + }); |
| 120 | +} |
| 121 | + |
| 122 | +namespace TestReplaceState { |
| 123 | + const store = createStore(); |
| 124 | + |
| 125 | + store.___replaceState({ count: 10 }); |
| 126 | +} |
| 127 | + |
| 128 | +namespace TestWatch { |
| 129 | + const store = createStore(); |
| 130 | + |
| 131 | + store.watch(state => state.count, value => { |
| 132 | + const a: number = value; |
| 133 | + }, { |
| 134 | + deep: true, |
| 135 | + immidiate: true |
| 136 | + }); |
| 137 | +} |
| 138 | + |
| 139 | +namespace TestHotUpdate { |
| 140 | + const store = createStore(); |
| 141 | + |
| 142 | + store.hotUpdate({ |
| 143 | + mutations: { |
| 144 | + INCREMENT (state) { |
| 145 | + state.count += 10; |
| 146 | + } |
| 147 | + } |
| 148 | + }); |
| 149 | + |
| 150 | + store.hotUpdate({ |
| 151 | + modules: { |
| 152 | + a: { |
| 153 | + state: 1, |
| 154 | + mutations: { |
| 155 | + INCREMENT (state) { |
| 156 | + state.value++; |
| 157 | + } |
| 158 | + } |
| 159 | + }, |
| 160 | + b: { |
| 161 | + state: 'test', |
| 162 | + mutations: { |
| 163 | + APPEND (state, value) { |
| 164 | + state.value += value; |
| 165 | + } |
| 166 | + } |
| 167 | + } |
| 168 | + } |
| 169 | + }); |
| 170 | +} |
| 171 | + |
| 172 | +namespace TestEvents { |
| 173 | + const store = createStore(); |
| 174 | + |
| 175 | + const handler = (mutation: Vuex.MutationObject<any>, state: ISimpleState) => { |
| 176 | + state.count += 1; |
| 177 | + }; |
| 178 | + |
| 179 | + store.on('mutation', handler); |
| 180 | + store.once('mutation', handler); |
| 181 | + |
| 182 | + store.off(); |
| 183 | + store.off('mutation'); |
| 184 | + store.off('mutation', handler); |
| 185 | + |
| 186 | + store.emit('some-event', 1, 'a', []); |
| 187 | +} |
| 188 | + |
| 189 | +namespace TestLogger { |
| 190 | + const logger = createLogger<ISimpleState>({ |
| 191 | + collapsed: false, |
| 192 | + transformer: state => state.count, |
| 193 | + mutationTransformer: m => m |
| 194 | + }); |
| 195 | + |
| 196 | + new Vuex.Store<ISimpleState>({ |
| 197 | + state: { count: 1 }, |
| 198 | + plugins: [logger] |
| 199 | + }); |
| 200 | +} |
0 commit comments