Some reactivity troubleshooting caused me to use services and "NgZone" to bring async tauri functions into angular's scope.
parent
2218f7ff90
commit
479a09c612
@ -0,0 +1,22 @@
|
||||
import { Injectable, NgZone } from '@angular/core';
|
||||
import { listen, Event, emit } from '@tauri-apps/api/event';
|
||||
import { Observable, from } from 'rxjs';
|
||||
|
||||
@Injectable({
|
||||
providedIn: 'root'
|
||||
})
|
||||
export class EventsService {
|
||||
constructor(private zone: NgZone) { }
|
||||
listen<T>(name: string,): Observable<Event<T>> {
|
||||
return new Observable<Event<T>>((subscriber) => {
|
||||
const unlisten = listen(name, (v: Event<T>) => {
|
||||
this.zone.run(() => subscriber.next(v));
|
||||
});
|
||||
return async () => { (await unlisten)() };
|
||||
});
|
||||
}
|
||||
|
||||
emit<T>(name: string, event: T): Observable<void> {
|
||||
return from(emit(name, event));
|
||||
}
|
||||
}
|
@ -0,0 +1,18 @@
|
||||
import { Injectable, NgZone } from '@angular/core';
|
||||
import { register } from '@tauri-apps/api/globalShortcut';
|
||||
import { Observable } from 'rxjs';
|
||||
|
||||
@Injectable({
|
||||
providedIn: 'root'
|
||||
})
|
||||
export class ShortcutService {
|
||||
constructor(private zone: NgZone) { }
|
||||
register(shortcut: string) {
|
||||
|
||||
return new Observable((subscriber) => {
|
||||
register(shortcut, (s) => {
|
||||
this.zone.run(() => subscriber.next(s));
|
||||
})
|
||||
});
|
||||
}
|
||||
}
|
Loading…
Reference in new issue