Skip to content

Commit 2d1e4e2

Browse files
authored
fix: add patch lockfile for multiple instances (#500)
add lockfile for multiple instances (#495)
1 parent aea57c9 commit 2d1e4e2

File tree

8 files changed

+51
-35
lines changed

8 files changed

+51
-35
lines changed

src/background/Background.ts

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import path from 'path';
44

55
import vscode, { Disposable, l10n, Uri } from 'vscode';
66

7-
import { utils } from '../utils';
87
import { ENCODING, EXTENSION_NAME, TOUCH_JSFILE_PATH, VERSION } from '../utils/constants';
98
import { vscodePath } from '../utils/vscodePath';
109
import { vsHelp } from '../utils/vsHelp';
@@ -19,6 +18,7 @@ type TConfigType = vscode.WorkspaceConfiguration & TPatchGeneratorConfig;
1918

2019
/**
2120
* 插件逻辑类
21+
* Extension logic
2222
*
2323
* @export
2424
* @class Background
@@ -36,6 +36,7 @@ export class Background implements Disposable {
3636
public jsFile = new JsPatchFile(vscodePath.jsPath);
3737

3838
/**
39+
* Current config
3940
* 当前用户配置
4041
*
4142
* @private
@@ -220,10 +221,6 @@ export class Background implements Disposable {
220221
return;
221222
}
222223

223-
// 50~550ms 的延时,对于可能的多实例,错开对于文件的操作
224-
// 虽然有锁了,但这样更安心 =。=
225-
await utils.sleep(50 + ~~(Math.random() * 500));
226-
227224
this.onConfigChange();
228225
})
229226
);

src/background/CssFile.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import fs, { constants as fsConstants } from 'fs';
88
import { tmpdir } from 'os';
99
import path from 'path';
1010

11-
import { utils } from '../utils';
11+
import { _ } from '../utils';
1212
import { BACKGROUND_VER, ENCODING, VERSION } from '../utils/constants';
1313
import { vsc } from '../utils/vsc';
1414

@@ -116,7 +116,7 @@ export class CssFile {
116116
try {
117117
const mvcmd = process.platform === 'win32' ? 'move /Y' : 'mv -f';
118118
const cmdarg = `${mvcmd} "${tempFilePath}" "${this.filePath}"`;
119-
await utils.sudoExec(cmdarg, { name: 'Visual Studio Code Background Extension' });
119+
await _.sudoExec(cmdarg, { name: 'Visual Studio Code Background Extension' });
120120
return true;
121121
} catch (e: any) {
122122
await vsc.window.showErrorMessage(e.message);
@@ -177,7 +177,7 @@ export class CssFile {
177177
*/
178178
public async uninstall(): Promise<boolean> {
179179
try {
180-
await utils.lock();
180+
await _.lock();
181181
let content = await this.getContent();
182182
content = this.clearContent(content);
183183
// 异常case return
@@ -189,7 +189,7 @@ export class CssFile {
189189
console.log(ex);
190190
return false;
191191
} finally {
192-
await utils.unlock();
192+
await _.unlock();
193193
}
194194
}
195195
}

src/background/PatchFile/PatchFile.base.ts

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import fs, { constants as fsConstants } from 'fs';
33
import { tmpdir } from 'os';
44
import path from 'path';
55

6-
import { utils } from '../../utils';
6+
import { _ } from '../../utils';
77
import { BACKGROUND_VER, ENCODING, VERSION } from '../../utils/constants';
88
import { vsc } from '../../utils/vsc';
99

@@ -96,7 +96,7 @@ export abstract class AbsPatchFile {
9696
try {
9797
const mvcmd = process.platform === 'win32' ? 'move /Y' : 'mv -f';
9898
const cmdarg = `${mvcmd} "${tempFilePath}" "${filePath}"`;
99-
await utils.sudoExec(cmdarg, { name: 'Background Extension' });
99+
await _.sudoExec(cmdarg, { name: 'Background Extension' });
100100
return true;
101101
} catch (e: any) {
102102
vsc.window.showErrorMessage(e.message, { title: 'Common Issue' }).then(confirm => {
@@ -145,11 +145,15 @@ export abstract class AbsPatchFile {
145145
protected abstract cleanPatches(content: string): string;
146146

147147
public async restore() {
148-
await utils.lock();
149-
let content = await this.getContent();
150-
content = this.cleanPatches(content);
151-
const ok = await this.write(content);
152-
await utils.unlock();
153-
return ok;
148+
try {
149+
await _.lock();
150+
let content = await this.getContent();
151+
content = this.cleanPatches(content);
152+
return await this.write(content);
153+
} catch {
154+
return false;
155+
} finally {
156+
await _.unlock();
157+
}
154158
}
155159
}

src/background/PatchFile/PatchFile.javascript.ts

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { _ } from '../../utils';
12
import { BACKGROUND_VER, VERSION } from '../../utils/constants';
23
import { AbsPatchFile } from './PatchFile.base';
34

@@ -13,16 +14,28 @@ import { AbsPatchFile } from './PatchFile.base';
1314
*/
1415
export class JsPatchFile extends AbsPatchFile {
1516
public async applyPatches(patchContent: string): Promise<boolean> {
16-
let content = await this.getContent();
17-
content = this.cleanPatches(content);
18-
content += [
19-
//
20-
`\n// vscode-background-start ${BACKGROUND_VER}.${VERSION}`,
21-
patchContent,
22-
'// vscode-background-end'
23-
].join('\n');
17+
try {
18+
await _.lock();
19+
const curContent = await this.getContent();
20+
let content = this.cleanPatches(curContent);
21+
content += [
22+
//
23+
`\n// vscode-background-start ${BACKGROUND_VER}.${VERSION}`,
24+
patchContent,
25+
'// vscode-background-end'
26+
].join('\n');
2427

25-
return this.write(content);
28+
// file unchanged
29+
if (curContent === content) {
30+
return true;
31+
}
32+
33+
return await this.write(content);
34+
} catch {
35+
return false;
36+
} finally {
37+
await _.unlock();
38+
}
2639
}
2740

2841
protected cleanPatches(content: string): string {

src/background/PatchGenerator/PatchGenerator.base.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import * as stylis from 'stylis';
22
import vscode from 'vscode';
33

4-
import { utils } from '../../utils';
4+
import { _ } from '../../utils';
55

66
/**
77
* 用于触发开发工具 css in js 语言支持
@@ -126,7 +126,7 @@ container.appendChild(div);
126126
script
127127
]
128128
.filter(n => !!n.length)
129-
.map(n => utils.withIIFE(n))
129+
.map(n => _.withIIFE(n))
130130
.join(';');
131131
}
132132
}

src/background/PatchGenerator/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import uglifyjs from 'uglify-js';
22

3-
import { utils } from '../../utils';
3+
import { _ } from '../../utils';
44
import { ChecksumsPatchGenerator } from './PatchGenerator.checksums';
55
import {
66
EditorPatchGenerator,
@@ -28,7 +28,7 @@ export class PatchGenerator {
2828
new PanelPatchGenerator(options.panel).create(), // panel
2929
new FullscreenPatchGenerator(options.fullscreen).create() // fullscreen
3030
]
31-
.map(n => utils.withIIFE(n))
31+
.map(n => _.withIIFE(n))
3232
.join(';');
3333

3434
// return script;

src/utils/index.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import lockfile from 'lockfile';
44
import { LOCK_PATH } from './constants';
55
import { vsc } from './vsc';
66

7-
export namespace utils {
7+
export namespace _ {
88
/**
99
* if zh-CN
1010
*/
@@ -43,7 +43,9 @@ export namespace utils {
4343
lockfile.lock(
4444
LOCK_PATH,
4545
{
46-
wait: 5000 // 应该能撑200的并发了,,,>_<#@!
46+
// When multiple VSCode instances are running, all instances' commands need to be executed within the `wait` time
47+
// 在打开了多个vscode实例时,需要所有实例的命令在`wait`时间内执行完毕
48+
wait: 1000 * 30
4749
},
4850
err => {
4951
if (err) {

src/utils/vscodePath.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import path from 'path';
22

3-
import { utils } from './index';
3+
import { _ } from './index';
44
import { vsc } from './vsc';
55

66
// 基础目录
@@ -18,7 +18,7 @@ const cssPath = (() => {
1818
// https://github.com/microsoft/vscode/pull/141263
1919
const webPath = getCssPath('workbench.web.main.css');
2020

21-
if (utils.isDesktop) {
21+
if (_.isDesktop) {
2222
return defPath;
2323
}
2424
return webPath;
@@ -29,7 +29,7 @@ const jsPath = (() => {
2929

3030
// desktop
3131
// /Applications/Visual Studio Code.app/Contents/Resources/app/out/vs/workbench/workbench.desktop.main.js
32-
if (utils.isDesktop) {
32+
if (_.isDesktop) {
3333
return path.join(base, 'vs/workbench/workbench.desktop.main.js');
3434
}
3535

0 commit comments

Comments
 (0)