angular - debounceTime 不适用于合并?
问题描述
我对单流和响应多值有疑问。不起作用的代码:
slugChecker<PipeInput, Out>(
inputController: AbstractControl,
pipeInput: Observable<PipeInput>,
checker: (input: PipeInput & { name: string, slug: string }) => Observable<Out>,
opt?: { debounceTime?: number; ignoreFirstValue?: boolean }
) {
return inputController.valueChanges.pipe(
shareReplay(1),
debounceTime(opt?.debounceTime || 1000), // <= not works correctly
// startWith(true),
skip(opt?.ignoreFirstValue ? 1 : 0), // skip first check for edit mode
switchMap(e => merge(
of(null)
.pipe(tap(_ => console.log('set null'))),
of(e)
.pipe(
// delay(2000), <= bad idea
tap(_ => console.log('run pipe', opt)),
map(input => slugify(input, {
replacement: '-',
remove: /[*+~.()'"!:@]/g,
locale: 'pl',
lower: true,
})),
switchMap(slug => pipeInput.pipe(map(input => ({input, slug})))),
switchMap(({input, slug}) => {
console.log('mapped with ', {input, slug})
return checker({...input, name: inputController.value, slug})
}),
)
),
))
}
我需要一个流,所以当我写入时,inputController
流应该发出null
。请求完成后,该值来自后端true
或false
来自后端(+false
有任何错误)。debounceTime
由于合并,此代码草案不起作用吗?
编辑:
好的,我找到了灵魂:D
slugChecker<PipeInput>(
inputController: AbstractControl,
pipeInput: Observable<PipeInput>,
checker: (input: PipeInput & { name: string, slug: string }) => Observable<boolean>,
opt?: {
debounceTime?: number;
ignoreFirstValue?: boolean,
slugController?: AbstractControl,
}
) {
return merge(
inputController.valueChanges.pipe(
map(_ => null)
),
inputController.valueChanges.pipe(
shareReplay(1),
debounceTime(opt?.debounceTime || 1000),
map(input => slugify(input, {
replacement: '-',
remove: /[*+~.()'"!:@]/g,
locale: 'pl',
lower: true,
})),
switchMap(slug => pipeInput.pipe(map(input => ({input, slug})))),
switchMap(({input, slug}) => {
console.log('mapped with ', {input, slug})
if (opt?.slugController) {
opt.slugController.patchValue(slug)
}
return checker({...input, name: inputController.value, slug})
}),
)
);
}
有用
解决方案
推荐阅读
- reactjs - 如何在 next.js 应用程序中加载自定义字体?
- javascript - extjs文件放在哪里?
- python - 我想在循环中的python中制作一个列表列表
- c++ - 二维数组遍历编码问题C++/递归函数没有返回正确的值
- php - 在 PHP 中使用 GPS 位置围绕线创建多边形
- extjs - 普通图像 Extjs 上的操作图标
- ios - 使用 Cloud Firestore 时位置是否重要?
- vue.js - 如何在本地将 Material Design Iconic Font 托管到 vue 应用中
- html - 用于在浏览器中打开的组合文件 HTML + CSS
- python - 代码在循环中做算术,即使它应该在循环之外