【发布时间】:2019-02-24 03:55:00
【问题描述】:
尝试创建一个isEmpty:Observable<boolean> 方法,该方法使用switchMap 发出一个热Observable<boolean>。这是我目前所拥有的:
/**
* Notifies observers when the store is empty.
*/
protected notifyOnEmpty = new ReplaySubject<E[]>(1);
/**
* Check whether the store is empty.
*
* @return A hot {@link Observable<boolean>} that indicates whether the store is empty.
*
* @example
<pre>
source.isEmpty();
</pre>
*/
isEmpty<E>():Observable<boolean> {
const isCurrentlyEmpty = values(this.entries).length == 0;
return this.notifyOnEmpty.pipe(startWith(isCurrentlyEmpty),
switchMap((entries:E[])=>entries.length == 0));
}
想法是商店然后可以拨打notifyOnEmpty.next(Object.values(this.entries))让订阅者知道商店是否是空的。
反正switchMap语句会导致错误:
[ts] '(entries: E[]) => boolean' 类型的参数不能分配给 '(value: E[], index: number) => ObservableInput' 类型的参数。 类型 'boolean' 不可分配给类型 'ObservableInput'。 (参数)条目:E[]
想法?
【问题讨论】:
标签: javascript angular typescript rxjs switchmap