Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/458.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Rxjs 如何在没有订阅的情况下使可观察到的仍在发射?_Rxjs - Fatal编程技术网

Rxjs 如何在没有订阅的情况下使可观察到的仍在发射?

Rxjs 如何在没有订阅的情况下使可观察到的仍在发射?,rxjs,Rxjs,我有以下代码=> 我面临的问题是,如果有人订阅了joystickStart$、joystickMove$、joystickEnd$,则会触发可观察内容。但是如果没有人这样做(或者只订阅例如movement),则不会触发start和end 但是,这会破坏我的系统,因为设置间隔不会被清除 如何在没有订户的情况下也能正常工作?我应该自动订阅吗?更改为使用主题,并在订阅主题时使用逻辑 this.joystickRelease$ = new Subject(); this.joystickReleas

我有以下代码=>

我面临的问题是,如果有人订阅了joystickStart$、joystickMove$、joystickEnd$,则会触发可观察内容。但是如果没有人这样做(或者只订阅例如movement),则不会触发start和end

但是,这会破坏我的系统,因为设置间隔不会被清除


如何在没有订户的情况下也能正常工作?我应该自动订阅吗?

更改为使用主题,并在订阅主题时使用逻辑

this.joystickRelease$ = new Subject();

this.joystickRelease$.subscribe(
  nipple => { clearInterval(this.lastInterval); }
);

this.joystickManager.on('end',  (evt, nipple) =>  {
  this.joystickRelease$.next(nipple);
});

更改为使用主题,并在订阅主题时使用逻辑

this.joystickRelease$ = new Subject();

this.joystickRelease$.subscribe(
  nipple => { clearInterval(this.lastInterval); }
);

this.joystickManager.on('end',  (evt, nipple) =>  {
  this.joystickRelease$.next(nipple);
});

根据您的问题,您似乎希望执行以下操作-

一旦操纵杆启动,跟踪操纵杆的移动,直到第一次释放操纵杆为止。如果我的理解正确,那么可以使用rxjs运算符[反应式方法]和各种
主题
s,而不是使用setInterval或[manifest approach]:

export class JoystickComponent implements AfterViewInit {
  @ViewChild('joystick') joystick: ElementRef;
  @Input() options: nipplejs.JoystickManagerOptions;

  private lastEvent: nipplejs.JoystickOutputData;
  private refireFrequency: 1000;
  private lastInterval: any;
  private joystickManager: nipplejs.JoystickManager;

  joystickStart$ = new Subject<nipplejs.JoystickOutputData>();
  joystickMove$ = new Subject<nipplejs.JoystickOutputData>();
  joystickRelease$: Subject<nipplejs.JoystickOutputData> = new Subject<nipplejs.JoystickOutputData>();          

  constructor() { }

  ngAfterViewInit() {
    this.create();
  }

  create() {
    this.joystickManager = nipplejs.create({
      zone : this.joystick.nativeElement,
      position: {left: '50%', top: '50%'},
      mode: 'semi'
    });

    this.joystickManager.on('start',  (evt, nipple) =>  {
        this.joystickStart$.next(nipple);
      });    

    this.joystickManager.on('move',  (evt, nipple) =>  { 
        this.joystickMove$.next(nipple);
      });    

    this.joystickManager.on('end',  (evt, nipple) =>  {
        this.joystickRelease$.next(nipple);
      });

    //combine start and move events
    //and do that until you hit first released event
    combineLatest(this.joystickStart$
                      .pipe(tap(_ => console.log(`Joystick Started`))), 
                  this.joystickMove$
                      .pipe(tap(_ => console.log(`Joystick Moved`)))
                 )
    .pipe(
      takeUntil(this.joystickRelease$.pipe(tap(_ => console.log(`Joystick released`)))),
      //If you want to repeat the observable then use repeat operator
      //repeat()
    ).subscribe(([start, move]) => {
      console.log({start}, {move});
    }, () => {}, () => console.log('complete'));    
  }
}
导出类JoystickComponent实现AfterViewInit{
@ViewChild(“操纵杆”)操纵杆:ElementRef;
@Input()选项:nipplejs.JoystickManagerOptions;
私人lastEvent:nipplejs.JoystickOutputData;
私人再融资:1000;
私人最后间隔:任何;
私人joystickManager:nipplejs.joystickManager;
joystickStart$=新主题();
joystickMove$=新主题();
joystickRelease$:主题=新主题();
构造函数(){}
ngAfterViewInit(){
这个。create();
}
创建(){
this.joystickManager=nipplejs.create({
区域:this.mogage.nativeElement,
位置:{左:“50%”,右:“50%”,
模式:“半自动”
});
this.joystickManager.on('start',(evt,乳头)=>{
此.joystickStart$.next(乳头);
});    
this.joystickManager.on('move',(evt,乳头)=>{
此.joystickMove$.next(乳头);
});    
this.joystickManager.on('end',(evt,乳头)=>{
此.joystickRelease$.next(乳头);
});
//合并开始和移动事件
//并一直这样做,直到你们击中第一个发布的事件
组合测试(this.joystickStart)$
.pipe(轻触(=>console.log(`goodge start`)),
这是乔伊斯蒂克莫夫$
.pipe(点击(=>console.log(`Moved`)))
)
.烟斗(
takeUntil(this.joystickRelease$.pipe(点击(=>console.log(`maggage released`))),
//如果您想重复可观察对象,请使用repeat运算符
//重复()
).订阅(([开始,移动])=>{
log({start},{move});
},()=>{},()=>console.log('complete');
}
}

工作stackblitz-

根据您的问题,您似乎希望执行以下操作-

一旦操纵杆启动,跟踪操纵杆的移动,直到第一次释放操纵杆为止。如果我的理解正确,那么可以使用rxjs运算符[反应式方法]和各种
主题
s,而不是使用setInterval或[manifest approach]:

export class JoystickComponent implements AfterViewInit {
  @ViewChild('joystick') joystick: ElementRef;
  @Input() options: nipplejs.JoystickManagerOptions;

  private lastEvent: nipplejs.JoystickOutputData;
  private refireFrequency: 1000;
  private lastInterval: any;
  private joystickManager: nipplejs.JoystickManager;

  joystickStart$ = new Subject<nipplejs.JoystickOutputData>();
  joystickMove$ = new Subject<nipplejs.JoystickOutputData>();
  joystickRelease$: Subject<nipplejs.JoystickOutputData> = new Subject<nipplejs.JoystickOutputData>();          

  constructor() { }

  ngAfterViewInit() {
    this.create();
  }

  create() {
    this.joystickManager = nipplejs.create({
      zone : this.joystick.nativeElement,
      position: {left: '50%', top: '50%'},
      mode: 'semi'
    });

    this.joystickManager.on('start',  (evt, nipple) =>  {
        this.joystickStart$.next(nipple);
      });    

    this.joystickManager.on('move',  (evt, nipple) =>  { 
        this.joystickMove$.next(nipple);
      });    

    this.joystickManager.on('end',  (evt, nipple) =>  {
        this.joystickRelease$.next(nipple);
      });

    //combine start and move events
    //and do that until you hit first released event
    combineLatest(this.joystickStart$
                      .pipe(tap(_ => console.log(`Joystick Started`))), 
                  this.joystickMove$
                      .pipe(tap(_ => console.log(`Joystick Moved`)))
                 )
    .pipe(
      takeUntil(this.joystickRelease$.pipe(tap(_ => console.log(`Joystick released`)))),
      //If you want to repeat the observable then use repeat operator
      //repeat()
    ).subscribe(([start, move]) => {
      console.log({start}, {move});
    }, () => {}, () => console.log('complete'));    
  }
}
导出类JoystickComponent实现AfterViewInit{
@ViewChild(“操纵杆”)操纵杆:ElementRef;
@Input()选项:nipplejs.JoystickManagerOptions;
私人lastEvent:nipplejs.JoystickOutputData;
私人再融资:1000;
私人最后间隔:任何;
私人joystickManager:nipplejs.joystickManager;
joystickStart$=新主题();
joystickMove$=新主题();
joystickRelease$:主题=新主题();
构造函数(){}
ngAfterViewInit(){
这个。create();
}
创建(){
this.joystickManager=nipplejs.create({
区域:this.mogage.nativeElement,
位置:{左:“50%”,右:“50%”,
模式:“半自动”
});
this.joystickManager.on('start',(evt,乳头)=>{
此.joystickStart$.next(乳头);
});    
this.joystickManager.on('move',(evt,乳头)=>{
此.joystickMove$.next(乳头);
});    
this.joystickManager.on('end',(evt,乳头)=>{
此.joystickRelease$.next(乳头);
});
//合并开始和移动事件
//并一直这样做,直到你们击中第一个发布的事件
组合测试(this.joystickStart)$
.pipe(轻触(=>console.log(`goodge start`)),
这是乔伊斯蒂克莫夫$
.pipe(点击(=>console.log(`Moved`)))
)
.烟斗(
takeUntil(this.joystickRelease$.pipe(点击(=>console.log(`maggage released`))),
//如果您想重复可观察对象,请使用repeat运算符
//重复()
).订阅(([开始,移动])=>{
log({start},{move});
},()=>{},()=>console.log('complete');
}
}
工作突击-