Skip to content

Commit ea86059

Browse files
committed
feat: add actions dispatch
1 parent f32951a commit ea86059

File tree

6 files changed

+55
-29
lines changed

6 files changed

+55
-29
lines changed

package.json

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,6 @@
3131
"command": "codingPlugin.depotTreeItemClick",
3232
"title": "Click Depot Tree Item"
3333
},
34-
{
35-
"command": "codingPlugin.createProjectAndDepot",
36-
"title": "Create Project and Depot"
37-
},
3834
{
3935
"command": "codingPlugin.createDepot",
4036
"title": "Create Depot"

src/extension.ts

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import init from './init';
22
import WebviewProvider from './webview';
33
import CodingServer from './services/codingServer';
4+
import ACTIONS, { dispatch } from './utils/actions';
45
import { proxyCtx } from './utils/proxy';
56

67
const accessToken = '1b7fca3bd7594a89b0f5e2a0250c1147';
@@ -30,13 +31,16 @@ async function activate(context: IContext) {
3031
repoInfo,
3132
);
3233

33-
context.ctx = {
34-
webviewProvider,
35-
codingServer,
36-
repoInfo,
37-
depots: [],
38-
selectedDepot: null,
39-
};
34+
dispatch(ACTIONS.SET_CTX, {
35+
context,
36+
value: {
37+
webviewProvider,
38+
codingServer,
39+
repoInfo,
40+
depots: [],
41+
selectedDepot: null,
42+
},
43+
});
4044

4145
proxyCtx(context);
4246
init(context);

src/init.ts

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
import hx from 'hbuilderx';
22
import DepotTreeDataProvider from './trees/depot';
33
import MRTreeDataProvider from './trees/mr';
4-
import { getMRUrl } from './utils/repo';
4+
55
import toast from './utils/toast';
6+
import { getMRUrl } from './utils/repo';
7+
import ACTIONS, { dispatch } from './utils/actions';
68
import { IDepot, IMRItem } from './typings/common';
79

810
export function registerCommands(context: IContext) {
@@ -23,17 +25,10 @@ export function registerCommands(context: IContext) {
2325

2426
context.subscriptions.push(
2527
hx.commands.registerCommand('codingPlugin.depotTreeItemClick', function (param: IDepot) {
26-
toast.info(param.name);
27-
}),
28-
);
29-
30-
context.subscriptions.push(
31-
hx.commands.registerCommand('codingPlugin.createProjectAndDepot', async function () {
32-
const project = await hx.window.showInputBox({
33-
prompt: '请输入项目名',
34-
});
35-
const depot = await hx.window.showInputBox({
36-
prompt: '请输入仓库名',
28+
toast.info(`选中仓库:${param.name}`);
29+
dispatch(ACTIONS.SET_SELECTED_DEPOT, {
30+
context,
31+
value: param,
3732
});
3833
}),
3934
);

src/trees/depot.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import hx from 'hbuilderx';
2+
import ACTIONS, { dispatch } from '../utils/actions';
23
import { IDepot } from '../typings/common';
34

45
interface IItem extends ITreeItem {
@@ -24,7 +25,11 @@ class DepotTreeDataProvider extends hx.TreeDataProvider {
2425

2526
try {
2627
const depots = await this.context.codingServer.getDepotList();
27-
this.context.depots = depots;
28+
29+
dispatch(ACTIONS.SET_DEPOTS, {
30+
context: this.context,
31+
value: depots,
32+
});
2833

2934
return Promise.resolve([
3035
{

src/trees/mr.ts

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,9 @@ class MRTreeDataProvider extends hx.TreeDataProvider {
1414
}
1515

1616
getRepoInfo() {
17-
const user = this.context.codingServer.session?.user;
18-
19-
const repoInfo = getMrListParams(this.context.repoInfo, this.context.selectedDepot, this.context.depots, user);
20-
21-
return repoInfo;
17+
const { repoInfo, selectedDepot, depots, codingServer } = this.context;
18+
const user = codingServer.session?.user;
19+
return getMrListParams(repoInfo, selectedDepot, depots, user);
2220
}
2321

2422
async getChildren(element?: IMRItem & IItem) {

src/utils/actions.ts

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
const enum ACTIONS {
2+
SET_DEPOTS = 'SET_DEPOTS',
3+
SET_SELECTED_DEPOT = 'SET_SELECTED_DEPOT',
4+
SET_CTX = 'SET_CTX',
5+
}
6+
7+
interface IPayload {
8+
context: IContext;
9+
value: any;
10+
}
11+
12+
export const dispatch = (type: ACTIONS, { context, value }: IPayload) => {
13+
switch (type) {
14+
case ACTIONS.SET_DEPOTS:
15+
context.depots = value;
16+
break;
17+
case ACTIONS.SET_SELECTED_DEPOT:
18+
context.selectedDepot = value;
19+
break;
20+
case ACTIONS.SET_CTX:
21+
context.ctx = value;
22+
break;
23+
default:
24+
return;
25+
}
26+
};
27+
28+
export default ACTIONS;

0 commit comments

Comments
 (0)