modifiche face offline e 2 secondi di attesa
This commit is contained in:
@@ -17,17 +17,21 @@ export class MyCantiService {
|
||||
|
||||
private _storage: Storage | null = null;
|
||||
public myCanti = signal<Canto[]>([]);
|
||||
private initPromise!: Promise<void>;
|
||||
|
||||
constructor() {
|
||||
this.init();
|
||||
this.initPromise = this.init();
|
||||
}
|
||||
|
||||
async init() {
|
||||
async init(): Promise<void> {
|
||||
this._storage = this.cantiService.getStorage();
|
||||
if (!this._storage) {
|
||||
// If CantiService hasn't initialized storage yet, wait a bit
|
||||
setTimeout(() => this.init(), 500);
|
||||
return;
|
||||
return new Promise<void>((resolve) => {
|
||||
setTimeout(async () => {
|
||||
await this.init();
|
||||
resolve();
|
||||
}, 500);
|
||||
});
|
||||
}
|
||||
const saved = await this._storage.get('my-canti');
|
||||
if (saved) {
|
||||
@@ -36,6 +40,7 @@ export class MyCantiService {
|
||||
}
|
||||
|
||||
async saveCanto(canto: Partial<Canto>): Promise<Canto> {
|
||||
await this.initPromise;
|
||||
const current = this.myCanti();
|
||||
let updated: Canto[];
|
||||
let targetCanto: Canto;
|
||||
@@ -108,6 +113,7 @@ export class MyCantiService {
|
||||
}
|
||||
|
||||
async deleteCanto(id: string) {
|
||||
await this.initPromise;
|
||||
const updated = this.myCanti().filter(c => c.id !== id);
|
||||
this.myCanti.set(updated);
|
||||
await this._storage?.set('my-canti', updated);
|
||||
|
||||
Reference in New Issue
Block a user