Vscode插件配置项监听
# 缘由
自己写了一个督促下班的Vscode插件下班了吗
,下班了吗 - Visual Studio Marketplace (opens new window)
插件可以读取用户的配置,实现例如上下班时间
、下班前几分钟提醒
等
之前是使用定时器每秒钟读取一次配置来实现的监听,但是考虑到性能问题,最后在查找文档后发现可以使用vscode.workspace.onDidChangeConfiguration
方法来实现配置文件的监听。
# 核心代码实现
config.ts
import * as vscode from 'vscode';
export function getConfiguration<T extends any>(property: string): T {
return vscode.workspace.getConfiguration('xiabanlema').get(property)!;
};
1
2
3
4
5
6
2
3
4
5
6
core/index.ts
import dayjs = require('dayjs');
import * as vscode from 'vscode';
import { getConfiguration } from '../util/config';
import { calcDuration, formatLocalStringTime } from '../util/time';
// 下班主类
export default class XiaBan {
// 底部状态栏
xiabanStatusItem: vscode.StatusBarItem;
// 下班指令
xiabanCommand: string = 'xiabanlema.xiaban';
// 注册下班指令
xiabanRegister!: vscode.Disposable;
// 注册配置变化事件
xiabanConfiguration!: vscode.Disposable;
constructor() {
this.xiabanStatusItem = vscode.window.createStatusBarItem(vscode.StatusBarAlignment.Right, 100);
this.xiabanStatusItem.command = this.xiabanCommand;
this.xiabanStatusItem.tooltip = '下班了吗?';
this.registerCommand();
}
// 判断是否快下班
isOutOfWorkSoon() {
const notificationTime: number = getConfiguration('notificationTime') || 30;
const xiabanTimeStr: string = getConfiguration('xiabanTime') || '18:00';
const xiabanTime = formatLocalStringTime(xiabanTimeStr);
const notificationTimeParse = dayjs(xiabanTime).subtract(notificationTime, 'minute');
if (dayjs().format('YYYY-MM-DD HH:mm') === notificationTimeParse.format('YYYY-MM-DD HH:mm')) {
return true;
}
return false;
}
// 事件通知
notifyFunc() {
const isOutOfWorkSoon = this.isOutOfWorkSoon();
const notificationTime: boolean = getConfiguration('notification');
if (isOutOfWorkSoon && notificationTime) {
vscode.window.showInformationMessage('下班时间快到啦!');
}
}
registerCommand() {
this.xiabanRegister = vscode.commands.registerCommand(this.xiabanCommand, () => {
const { isOutOfWork, hours, minutes } = calcDuration();
if (isOutOfWork) {
vscode.window.showInformationMessage('还上啥班啊,下班了!!!');
} else {
const message = `还有${hours}小时${minutes}分钟下班`;
vscode.window.showInformationMessage(message);
}
});
this.xiabanConfiguration = vscode.workspace.onDidChangeConfiguration(() => {
this.updateStatusBarItem();
this.notifyFunc();
});
}
updateStatusBarItem(): void {
const { isOutOfWork, hours, minutes } = calcDuration();
if (isOutOfWork) {
this.xiabanStatusItem.text = `$(rocket) 下班了`;
} else {
const message = `$(symbol-event) ${hours}小时${minutes}分`;
this.xiabanStatusItem.text = message;
}
this.xiabanStatusItem.show();
}
}
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
extension.ts
import * as vscode from 'vscode';
import { setupDayjs } from './util/time';
import XiaBan from "./core/index";
import { getConfiguration } from './util/config';
let timer: NodeJS.Timer;
const xiaban = new XiaBan();
export function activate(context: vscode.ExtensionContext) {
setupDayjs();
context.subscriptions.push(xiaban.xiabanStatusItem);
context.subscriptions.push(xiaban.xiabanRegister);
context.subscriptions.push(xiaban.xiabanConfiguration);
xiaban.updateStatusBarItem();
xiaban.notifyFunc();
timer && clearInterval(timer);
timer = setInterval(() => {
xiaban.updateStatusBarItem();
// 判断是否需要监听下班时间
const notificationTime: boolean = getConfiguration('notification');
if (notificationTime) {
xiaban.notifyFunc();
}
}, 1000 * 35);
}
export function deactivate() {
timer && clearInterval(timer);
}
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
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
编辑 (opens new window)
上次更新: 2023/04/23, 17:02:50