Skip to content

Commit 8e14730

Browse files
author
winjo
committed
fix: 修复 dispose preference reset 问题
1 parent 64d742c commit 8e14730

2 files changed

Lines changed: 127 additions & 58 deletions

File tree

packages/alex/src/core/patch.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import { StaticServices } from '@ali/monaco-editor-core/esm/vs/editor/standalone/browser/standaloneServices';
66
import { ModesRegistry } from '@ali/monaco-editor-core/esm/vs/editor/common/modes/modesRegistry';
77
import { DirtyDiffWidget } from '@ali/ide-scm/lib/browser/dirty-diff/dirty-diff-widget';
8+
import { AbstractResourcePreferenceProvider } from '@ali/ide-preferences/lib/browser/abstract-resource-preference-provider';
89

910
// TODO: 不使用 private 如何清除副作用
1011
export const disposeMode = () => {
@@ -22,3 +23,10 @@ const _addAction = (<any>DirtyDiffWidget).prototype._addAction;
2223
if (icon === 'plus' || icon === 'rollback') return;
2324
_addAction.call(this, icon, type);
2425
};
26+
27+
// TODO: 目前 preference 在 dispose 时会重置,而这个时机目前是在 dispose 所有实例时才会触发,此时 reset 无意义,
28+
// 还会导致因 reset 触发事件获取 injector 实例发生错误(因为一些实例可能早已销毁)
29+
Object.defineProperty(AbstractResourcePreferenceProvider.prototype, 'reset', {
30+
value: () => {},
31+
configurable: true,
32+
});
Lines changed: 119 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -1,59 +1,70 @@
1-
import React, { useState, useEffect, useMemo } from 'react';
1+
import React, { useState, useMemo } from 'react';
22
import ReactDOM from 'react-dom';
33
import { AppRenderer, BrowserFSFileType as FileType, IAppRendererProps } from '@alipay/alex';
44
import '@alipay/alex/languages';
5-
import Button from 'antd/lib/button';
6-
import 'antd/lib/button/style';
5+
import Select from 'antd/lib/select';
6+
import 'antd/lib/select/style';
7+
8+
const dirMap: Record<string, [string, FileType][]> = {
9+
'/': [
10+
['lib', FileType.DIRECTORY],
11+
['Readme.md', FileType.FILE],
12+
['LICENSE', FileType.FILE],
13+
['package.json', FileType.FILE],
14+
],
15+
'/lib': [
16+
['application.js', FileType.FILE],
17+
['context.js', FileType.FILE],
18+
['request.js', FileType.FILE],
19+
['response.js', FileType.FILE],
20+
],
21+
};
22+
23+
let zipData: Buffer;
24+
25+
const zipDataPromise = (async () => {
26+
const res = await fetch(
27+
'http://alipay-rmsdeploy-image.cn-hangzhou.alipay.aliyun-inc.com/green-trail-test/dc85f34d-2467-436b-a0fe-092133ead0d6/demo.zip'
28+
);
29+
const buf = await res.arrayBuffer();
30+
zipData = Buffer.from(new Uint8Array(buf));
31+
})();
732

833
const App = () => {
9-
const [zipData, setZipData] = useState<Buffer | null>(null);
1034
const [fsType, setFsType] = useState<string>('');
1135

12-
useEffect(() => {
13-
(async () => {
14-
const res = await fetch(
15-
'http://alipay-rmsdeploy-image.cn-hangzhou.alipay.aliyun-inc.com/green-trail-test/dc85f34d-2467-436b-a0fe-092133ead0d6/demo.zip'
16-
);
17-
const buf = await res.arrayBuffer();
18-
setZipData(Buffer.from(new Uint8Array(buf)));
19-
})();
20-
}, []);
21-
22-
const workspace = useMemo<IAppRendererProps['runtimeConfig']['workspace']>(() => {
23-
if (fsType === 'ZipFS' && zipData) {
24-
return {
25-
filesystem: {
26-
fs: 'FolderAdapter',
36+
const filesystem = useMemo<
37+
NonNullable<IAppRendererProps['runtimeConfig']['workspace']>['filesystem'] | undefined
38+
>(() => {
39+
switch (fsType) {
40+
case 'IndexedDB':
41+
return {
42+
fs: 'IndexedDB',
2743
options: {
28-
folder: '/demo',
29-
wrapped: {
30-
fs: 'ZipFS',
31-
options: {
32-
zipData,
33-
},
44+
storeName: 'my_db',
45+
},
46+
};
47+
case 'InMemory':
48+
return {
49+
fs: 'InMemory',
50+
};
51+
case 'FileIndexSystem':
52+
return {
53+
fs: 'FileIndexSystem',
54+
options: {
55+
// 初始全量文件索引
56+
requestFileIndex() {
57+
return Promise.resolve({
58+
'main.html': '<div id="root"></div>',
59+
'main.css': 'body {}',
60+
'main.js': 'console.log("main")',
61+
'package.json': '{\n "name": "Riddle"\n}',
62+
});
3463
},
3564
},
36-
},
37-
};
38-
}
39-
if (fsType === 'DynamicRequest') {
40-
const dirMap: Record<string, [string, FileType][]> = {
41-
'/': [
42-
['lib', FileType.DIRECTORY],
43-
['Readme.md', FileType.FILE],
44-
['LICENSE', FileType.FILE],
45-
['package.json', FileType.FILE],
46-
],
47-
'/lib': [
48-
['application.js', FileType.FILE],
49-
['context.js', FileType.FILE],
50-
['request.js', FileType.FILE],
51-
['response.js', FileType.FILE],
52-
],
53-
};
54-
55-
return {
56-
filesystem: {
65+
};
66+
case 'DynamicRequest':
67+
return {
5768
fs: 'DynamicRequest',
5869
options: {
5970
readDirectory(p: string) {
@@ -66,26 +77,74 @@ const App = () => {
6677
return Buffer.from(await res.arrayBuffer());
6778
},
6879
},
69-
},
70-
};
80+
};
81+
case 'ZipFS':
82+
return {
83+
fs: 'ZipFS',
84+
options: {
85+
zipData,
86+
},
87+
};
88+
case 'FolderAdapter':
89+
return {
90+
fs: 'FolderAdapter',
91+
options: {
92+
folder: '/demo',
93+
wrapped: {
94+
fs: 'ZipFS',
95+
options: {
96+
zipData,
97+
},
98+
},
99+
},
100+
};
101+
case 'OverlayFS':
102+
return {
103+
fs: 'OverlayFS',
104+
options: {
105+
writable: { fs: 'InMemory' },
106+
readable: {
107+
fs: 'DynamicRequest',
108+
options: {
109+
readDirectory(p: string) {
110+
return dirMap[p];
111+
},
112+
async readFile(p) {
113+
const res = await fetch(
114+
`http://alipay-rmsdeploy-image.cn-hangzhou.alipay.aliyun-inc.com/green-trail-test/a87fb80d-3028-4b19-93a9-2da6f871f369/koa${p}`
115+
);
116+
return Buffer.from(await res.arrayBuffer());
117+
},
118+
},
119+
},
120+
},
121+
};
71122
}
72-
}, [fsType, zipData]);
123+
}, [fsType]);
124+
125+
const workspace = filesystem ? { filesystem } : undefined;
73126

74127
return (
75128
<div style={{ height: '100%' }}>
76-
<div style={{ height: 32, display: 'flex', alignItems: 'center' }}>
77-
<Button size="small" onClick={() => setFsType('ZipFS')}>
78-
ZipFS
79-
</Button>
80-
<Button size="small" style={{ marginLeft: 8 }} onClick={() => setFsType('DynamicRequest')}>
81-
DynamicRequest
82-
</Button>
129+
<div style={{ height: 48, display: 'flex', alignItems: 'center' }}>
130+
<Select<string> onChange={(e) => setFsType(e)} style={{ width: 200 }}>
131+
<Select.Option value="IndexedDB">IndexedDB</Select.Option>
132+
<Select.Option value="InMemory">InMemory</Select.Option>
133+
<Select.Option value="FileIndexSystem">FileIndexSystem</Select.Option>
134+
<Select.Option value="DynamicRequest">DynamicRequest</Select.Option>
135+
<Select.Option value="ZipFS">ZipFS</Select.Option>
136+
<Select.Option value="FolderAdapter">FolderAdapter</Select.Option>
137+
<Select.Option value="OverlayFS">OverlayFS</Select.Option>
138+
</Select>
83139
</div>
84-
<div style={{ height: 'calc(100% - 32px)' }}>
140+
<div style={{ height: 'calc(100% - 48px)' }}>
85141
<AppRenderer
86142
key={fsType}
87143
appConfig={{
88144
workspaceDir: 'filesystem',
145+
defaultPreferences: {
146+
'general.theme': 'ide-light',
147+
},
89148
}}
90149
runtimeConfig={{
91150
biz: 'filesystem',
@@ -97,4 +156,6 @@ const App = () => {
97156
);
98157
};
99158

100-
ReactDOM.render(<App />, document.getElementById('main'));
159+
zipDataPromise.then(() => {
160+
ReactDOM.render(<App />, document.getElementById('main'));
161+
});

0 commit comments

Comments
 (0)