刘光辉
13 小时以前 0dfe84494048ce27ba8449831782128412d3eb13
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
import type { PluginContext } from '../domain/plugin-context';
 
import { acceptHMRUpdate, defineStore } from 'pinia';
 
import { parsePluginContext } from '../domain/plugin-context';
 
export interface PluginContextState {
  context: null | PluginContext;
  error: string;
  status: 'error' | 'initializing' | 'ready';
}
 
function initialState(): PluginContextState {
  return { status: 'initializing', context: null, error: '' };
}
 
export const usePluginContextStore = defineStore('plugin-context', {
  state: initialState,
  actions: {
    initialize(options: unknown): boolean {
      try {
        this.context = parsePluginContext(options);
        this.status = 'ready';
        this.error = '';
        return true;
      } catch {
        this.context = null;
        this.status = 'error';
        this.error = '插件运行上下文无效';
        return false;
      }
    },
    reset() {
      this.$state = initialState();
    },
  },
});
 
const hot = import.meta.hot;
if (hot) {
  hot.accept(acceptHMRUpdate(usePluginContextStore, hot));
}