import { invoke } from "@tauri-apps/api/tauri"; import { Subject, debounceTime, from } from "rxjs"; export interface PlanInterface { plan: PlanElement[]; current: number; stored_path?: string; } export class Plan { plan: PlanElement[]; current: number; private path?: string; private saveSubject: Subject = new Subject(); constructor(plan: PlanInterface) { this.plan = plan.plan; this.current = plan.current; if(plan.stored_path) { this.path = plan.stored_path; } this.saveSubject.pipe(debounceTime(500)).subscribe(() => this.underlyingSave()); } setPath(path: string) { this.path = path; } next() { if (this.current + 1 < this.plan!.length) { this.current++; this.save(); } } prev() { if (this.current - 1 >= 0) { this.current--; this.save(); } } setPrevious(prev: string) { from(invoke('path_for_previous', { prev })).subscribe(path => { if (path) { this.setPath(path); } }); } private save() { if (this.path) { this.saveSubject.next(); } } private underlyingSave() { console.log("Underlying save"); invoke('save_plan', { plan: { plan: this.plan, current: this.current }, path: this.path }); } } export interface PlanElement { area_key: string; notes?: string; uuid?: string; edited: boolean; }