1
ファイルの追加/削除を発行するオブザーバブルを作成したいとします(chokidar
経由)。私はこのようなもので、これを行うことができています:サブスクライバがない場合RxJs - サブスクライバがある場合のみ計算して出力します
Rx.Observable.create((subscriber) => {
this.watcher = chokidar.watch(
this.contentPath
);
this.watcher.on('addDir',() => { subscriber.next(); });
this.watcher.on('unlinkDir',() => { subscriber.next(); });
});
私がやりたいことは、私はファイルを見るためを停止したいされ、と何かがそれに加入する際に再び起動します。このようなものですが、RxJs:
class Notifier {
constructor() {
this.subscriberCount = 0;
}
subscribe(onNext, onError, complete) {
this.subscriberCount++;
if (this.subscriberCount === 1) {
this.startInternalWatcher();
}
return() => {
this.subscriberCount--;
if (this.subscriberCount === 0) {
this.stopInternalWatcher();
}
}
}
}
// files are not watched
const n = new Notifier();
const s1 = n.subscribe(() => {}) // files are being wacthed
const s2 = n.subscribe(() => {}) // files are being wacthed
s1() // unsubscribed from 1, files are still watched.
s2() // unsubscribed from 2, files are not watched because no one is interested in.
私はRxJsの新機能ですので、明らかな解決策がいくつかあります。これは可能ですか?