Return a default value with scan Observable
我可以阅读rxjs文档(http://reactivex.io/rxjs/class/es6/Observable.js~Observable.html#instance-method-scan)中有关方法扫描的观点。
在下面的代码中,我想返回_channels Observables的默认值。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | export interface IChannelOperation extends Function { (channels: Channel[]): Channel[]; } let initialChannel: Channel[] = []; @Injectable() export class ChannelsCatalog{ defaultChannel: Subject<Channel> = new BehaviorSubject<Channel>(null); private _currentChannel: Subject<Channel> = new BehaviorSubject<Channel>(null); private _channels: Observable<Channel[]>; private _updates: Subject = new Subject(); constructor(){ this._channels = this._updates .scan((channels: Channel[], operation: IChannelOperation) => operation(channels), initialChannel) .publishReplay(1) .refCount(); } getAllChannelsCatalog(): Observable<Channel[]>{ return this._channels; } } |
但是,当我订阅可观察的ex时,seed参数不会返回:
1 2 | var channelsCatalog = new ChannelsCatolog(); channelsCatalog.getAllChannelsCatalog.subscribe((value) => console.log(value)); |
种子值
您正在寻找
1 2 3 4 5 | this._channels = this._updates .scan((channels: Channel[], operation: IChannelOperation) => operation(channels), initialChannel) .startWith(initialChannel) .publishReplay(1) .refCount(); |
您仍然需要