javascript - 使用 Observables(DRY) 避免重复代码
问题描述
这是来自 Angular 的我的代码(带有重命名的方法):
我有一个函数接口/合约
type IAutocomlete = (
term: string,
selectedIds?: string[]
) => Observable<TableFilterQueryResponse[]>;
这是我想在未来注入到我的组件中的草稿服务。我将有更多具有类似实现的方法。
export class SuperCleanService {
constructor(
private dataController: DataController,
private serviceController: ServiceController
) {}
.....
private searchAs$: IAutocomlete = (term, selectedIds) => {
return this.dataController
.service1GET(term, selectedIds)
.pipe(
map((res) =>
res.map((a) => ({
viewValue: a.name,
value: a.id
}))
)
);
};
private searchB$: IAutocomlete = (term, selectedIds) => {
return this.serviceController
.service1(term, selectedIds)
.pipe(
map((res) =>
res.map((a) => ({
viewValue: a.id,
value: a.id
}))
)
);
};
private searchC$: IAutocomlete = (term, selectedIds) => {
return this.dataController
.service2GET(term, selectedIds)
.pipe(
map((res) =>
res.map((a) => ({
viewValue: a.name,
value: a.id
}))
)
);
};
...
}
我如何重构(遵循 DRY 规则)并使其尽可能干净?我的主要目标是避免在每个函数中使用 map。
解决方案
自定义运算符
考虑创建一个包含可重用逻辑的自定义运算符。
自定义管道.ts
import { pipe, of } from "rxjs";
import { startWith, switchMap } from "rxjs/operators";
export function myCustomOperator() {
return pipe(
map((res) =>
res.map((a) => ({
viewValue: a.name,
value: a.id
}))
)
);
}
您的组件将变为:
export class SuperCleanService {
constructor(
private dataController: DataController,
private serviceController: ServiceController
) {}
.....
private searchAs$: IAutocomlete = (term, selectedIds) => {
return this.dataController
.service1GET(term, selectedIds)
.pipe(
myCustomOperator()
);
};
private searchB$: IAutocomlete = (term, selectedIds) => {
return this.serviceController
.service1(term, selectedIds)
.pipe(
myCustomOperator()
);
};
private searchC$: IAutocomlete = (term, selectedIds) => {
return this.dataController
.service2GET(term, selectedIds)
.pipe(
myCustomOperator()
);
};
...
}
推荐阅读
- c++ - 询问用户两个字母的序列是否在一个随机的字母数组中
- django - Django 在扩展类中使用 __init__ 方法(admin.ModelAdmin)
- php - 使用 Guzzle 将多个帖子发送到一个 URL - 不同的参数
- excel - 条件格式不适用于所选的整个范围
- java - 有人可以解释一下这个在 Java 中找到 BigInteger 平方根的函数吗?
- vba - 替代嵌套 for 循环以加快运行时间
- c++ - 与实现无关的浮点/整数转换
- c# - 如何从字符串数据中检索 varbinary?
- javascript - 如何使用 vanilla js 和 sass 创建可重用的按钮组件?
- ios - 将数据从单元格中的按钮传递到另一个表格视图?