import { Injectable } from "@angular/core"; import { Utility } from "./utility"; import { IRslt, Media } from "../../public-api"; import { MediaService } from "../services/mediaService"; @Injectable({ providedIn: 'root' }) export class MediaState { //i'm no longer using individual properties on states. I'm using the merchant and shop dto's to simplify the process medias: Array = new Array(); awaitIDs: string[] = []; retrieveIDs:string[] = []; timerId!: any; isCleared: boolean = false; constructor(private service: MediaService, private utility: Utility) { } manageAll(done: any, err: any) { const ids:string[] = [] this.awaitIDs.forEach(f => { if(!this.retrieveIDs.some(s => s === f)){ this.retrieveIDs.push(f) ids.push(f) } }) if(ids.length === 0){ return; } this.getAll(ids, (medias: Array) => { medias.forEach(fo => { if (!this.medias.some(s => s._id === fo._id)) { this.medias.push(fo) } const index = this.awaitIDs.findIndex(f => f === fo._id.toString()) if (index >= 0) { this.awaitIDs.splice(index, 1) } }) done() }, (e: any) => { err() }) } checkImage(ids: string[], done: any, err: any) { ids.forEach(f => { if (!this.awaitIDs.some(s => s === f) && !this.retrieveIDs.some(s => s === f)) { this.awaitIDs.push(f) } }) // if (this.timerId) { // // setTimeout(() => { // // console.log('timer clear callback') // // done() // // }, 300) // clearTimeout(this.timerId) // if (this.awaitIDs.length > 10) { // this.manageAll(done, err); // } // console.log("timer cleared: timer id is:" + this.timerId) // } // this.timerId = setTimeout(() => { if (this.awaitIDs.length > 0) { this.manageAll(done, err) } }, 1000) } getAll(ids: string[], done: any, error: any) { this.service.getAll(ids).subscribe({ next(rslt) { const data = rslt as IRslt done(data.results) }, error(e) { error(e) }, complete() { } }) } post(product: Media, done: any, error: any) { this.service.post(product).subscribe({ next(rslt) { const data = rslt as IRslt done(data.results) }, error(e) { error(e) }, complete() { } }) } put(product: Media, done: any, error: any) { const self = this; this.service.put(product).subscribe({ next(rslt) { const data = rslt as IRslt done(data.results) }, error(e) { error(e) }, complete() { } }) } }