0
rxjsの被験者に一度に1人の加入者しかいないようにしたい。 と私は件名<any> subscribeとunsubscribeの方法をキャッチ
import { Subject } from 'rxjs/Subject';
/**
* FormDialogActionModel
*/
export class FormDialogActionModel {
public $customAction: Subject<CustomAction> = new Subject<CustomAction>();
private positiveActionSubscribers: number = 0;
private customActionSubscribers: number = 0;
private $positiveAction: Subject<Object> = new Subject<Object>();
private internalToken = 'FORM-DIALOG-SERVICE-GET-POSITIVE-ACTION-WITHOUT-TRIGGERING-GET-RESTRICTIONS';
/**
* This get method was created to force the number of subscribers to 1
*/
public get $$positiveAction(): Subject<Object> {
this.positiveActionSubscribers ++;
if(this.positiveActionSubscribers > 1){
throw new Error('Somebody already subscribed to a positive action. You cannot subscribe to it again until the subscribes unsubscribes');
}
return this.$positiveAction;
}
public unSubscribePositiveAction(){
this.positiveActionSubscribers --;
}
public getPositiveAction(token){
if(token != this.internalToken){
throw new Error('The get mothod getPositiveAction was created only for form-dialog.service');
}
return this.$positiveAction;
}
}
export interface CustomAction {
data: Object;
customActionIdentifier: string;
}
条件を強制するために、サブスクリプションの数をカウントするには、サブスクライブイベントをキャッシュし、カウンタ をincreseし、それをdecrese解除がそこに方法は何ですか?私は他のclasesが背後に何が起こっているのか を知らないしたいと、この1はかなりきれいに見える可能性修正プログラムは、Subjectクラスを拡張し、代わりにこの実装を使用することです
加入者が1人しかいない理由は何ですか?技術的には、2番目のサブスクリプションが発生したときに投げている場合は透明になりません。 – paulpdaniels
グローバルフォームダイアログがあり、それを使用する誰かが同じイベントに2回サブスクリプションしないように、 Rxjsの件名を拡張して問題を解決しました – Nicu
スローの目的は、プログラマーがapiを正しく使うことを確認することです – Nicu