import { Action, getModule, Module, Mutation, VuexModule } from 'vuex-module-decorators';
import store from '@/store';

@Module({
  namespaced: true,
  dynamic: true,
  store,
  name: 'DemoModule',
})
export class DemoModule extends VuexModule {
  /**
   * 当前计数
   * @private
   */
  count = 1;

  /**
   * 增加当前计数
   * @param delta
   */
  @Mutation
  increment(delta: number): void {
    // 当前计数++
    this.count += delta;
  }

  @Mutation
  decrement(delta: number): void {
    this.count -= delta;
  }

  @Action({ commit: 'increment' })
  async incr(payload: number): Promise<number> {
    return new Promise((resolve) => {
      setTimeout(() => resolve(payload), 1000);
    });
  }
}

export default getModule(DemoModule);
