Skip to content

Commit 14e4983

Browse files
twinkle77liujuping
authored andcommitted
feat: filter out duplicate data sources
1 parent b1914db commit 14e4983

File tree

3 files changed

+53
-40
lines changed

3 files changed

+53
-40
lines changed

demo/src/index.ts

Lines changed: 33 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -9,39 +9,39 @@ preference.set('DataSourcePane', {
99
dataSourceTypes: [
1010
{
1111
type: 'fetch',
12-
schema: {
13-
type: "object",
14-
properties: {
15-
id: {
16-
type: "string"
17-
},
18-
type: {
19-
type: "string"
20-
},
21-
isInit: {
22-
type: "boolean",
23-
},
24-
options: {
25-
type: "object",
26-
properties: {
27-
method: {
28-
type: "string",
29-
},
30-
isCors: {
31-
type: "boolean",
32-
},
33-
timeout: {
34-
type: "number",
35-
},
36-
uri: {
37-
type: "string",
38-
},
39-
},
40-
required: ["method", "isCors", "timeout", "uri"]
41-
}
42-
},
43-
required: ["id", "type", "isInit", "options"]
44-
}
12+
// schema: {
13+
// type: "object",
14+
// properties: {
15+
// id: {
16+
// type: "string"
17+
// },
18+
// type: {
19+
// type: "string"
20+
// },
21+
// isInit: {
22+
// type: "boolean",
23+
// },
24+
// options: {
25+
// type: "object",
26+
// properties: {
27+
// method: {
28+
// type: "string",
29+
// },
30+
// isCors: {
31+
// type: "boolean",
32+
// },
33+
// timeout: {
34+
// type: "number",
35+
// },
36+
// uri: {
37+
// type: "string",
38+
// },
39+
// },
40+
// required: ["method", "isCors", "timeout", "uri"]
41+
// }
42+
// },
43+
// required: ["id", "type", "isInit", "options"]
44+
// }
4545
},
4646
{
4747
type: 'jsonp',

packages/plugin-datasource-pane/src/components/DataSourceImport/DataSourceImport.tsx

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ export class DataSourceImport extends PureComponent<
6767

6868
constructor(props: DataSourceImportProps) {
6969
super(props);
70-
this.state.code = JSON.stringify(this.deriveValue(this.props.defaultValue));
70+
this.state.code = JSON.stringify(this.deriveValue(this.props.defaultValue), null, 2);
7171
this.handleEditorDidMount = this.handleEditorDidMount.bind(this);
7272
this.handleEditorChange = this.handleEditorChange.bind(this);
7373
this.handleComplete = this.handleComplete.bind(this);
@@ -97,11 +97,17 @@ export class DataSourceImport extends PureComponent<
9797

9898
if (!dataSourceType) return false;
9999

100-
// 校验失败的数据源,给予用户提示
101-
const validate = ajv.compile(dataSourceType.schema)
102-
const valid = validate(dataSource)
103-
if (!valid) console.warn(validate.errors)
104-
return valid
100+
// 向下兼容
101+
if (dataSourceType.schema) {
102+
// 校验失败的数据源,给予用户提示
103+
const validate = ajv.compile(dataSourceType.schema)
104+
const valid = validate(dataSource)
105+
if (!valid) console.warn(validate.errors)
106+
return valid
107+
} else {
108+
// 用户不传入 schema 校验规则,默认返回 true
109+
return true
110+
}
105111
});
106112
};
107113

packages/plugin-datasource-pane/src/utils/stateMachine.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,14 @@ export const createStateMachine = (dataSourceList: DataSourceConfig[] = []) => c
161161
target: 'idle',
162162
actions: assign({
163163
dataSourceList: (context, event) => {
164-
return context.dataSourceList.concat(event.payload);
164+
// 直接 concat 会出现重复
165+
const filterDataSourceList = context.dataSourceList.filter((item) => {
166+
return !event.payload.find(
167+
(dataSource: DataSourceConfig) => dataSource.id === item.id,
168+
)
169+
})
170+
171+
return filterDataSourceList.concat(event.payload);
165172
},
166173
detail: {
167174
visible: false,

0 commit comments

Comments
 (0)