marketplace cache working

This commit is contained in:
Drew Ansbacher
2021-09-27 11:19:33 -06:00
committed by Matt Hill
parent 7e821d00fe
commit 0d1dac7c2e
2 changed files with 32 additions and 46 deletions

View File

@@ -115,9 +115,4 @@
</ion-row> </ion-row>
</ion-grid> </ion-grid>
</ng-template> </ng-template>
<ion-infinite-scroll [disabled]="!needInfinite" (ionInfinite)="doInfinite($event)">
<ion-infinite-scroll-content loadingSpinner="lines"></ion-infinite-scroll-content>
</ion-infinite-scroll>
</ion-content> </ion-content>

View File

@@ -4,12 +4,12 @@ import { wizardModal } from 'src/app/components/install-wizard/install-wizard.co
import { IonContent, ModalController } from '@ionic/angular' import { IonContent, ModalController } from '@ionic/angular'
import { WizardBaker } from 'src/app/components/install-wizard/prebaked-wizards' import { WizardBaker } from 'src/app/components/install-wizard/prebaked-wizards'
import { PackageDataEntry, PackageState } from 'src/app/services/patch-db/data-model' import { PackageDataEntry, PackageState } from 'src/app/services/patch-db/data-model'
import { defer, Subscription } from 'rxjs' import { Subscription } from 'rxjs'
import { ErrorToastService } from 'src/app/services/error-toast.service' import { ErrorToastService } from 'src/app/services/error-toast.service'
import { MarketplaceService } from '../marketplace.service' import { MarketplaceService } from '../marketplace.service'
import { ApiService } from 'src/app/services/api/embassy-api.service' import { ApiService } from 'src/app/services/api/embassy-api.service'
import { PatchDbService } from 'src/app/services/patch-db/patch-db.service' import { PatchDbService } from 'src/app/services/patch-db/patch-db.service'
import { catchError, finalize, take } from 'rxjs/operators' import { pauseFor } from 'src/app/util/misc.util'
@Component({ @Component({
selector: 'marketplace-list', selector: 'marketplace-list',
@@ -28,16 +28,12 @@ export class MarketplaceListPage {
data: MarketplaceData data: MarketplaceData
eos: MarketplaceEOS eos: MarketplaceEOS
allPkgs: MarketplacePkg[] = []
pkgs: MarketplacePkg[] = [] pkgs: MarketplacePkg[] = []
PackageState = PackageState PackageState = PackageState
page = 1
needInfinite = false
readonly perPage = 30
subs: Subscription[] = [] subs: Subscription[] = []
searchSub: Subscription
constructor ( constructor (
private readonly marketplaceService: MarketplaceService, private readonly marketplaceService: MarketplaceService,
@@ -62,11 +58,13 @@ export class MarketplaceListPage {
const [data, eos] = await Promise.all([ const [data, eos] = await Promise.all([
this.api.getMarketplaceData({ }), this.api.getMarketplaceData({ }),
this.api.getEos({ }), this.api.getEos({ }),
this.getPkgs(), this.getAllPkgs(),
]) ])
this.eos = eos this.eos = eos
this.data = data this.data = data
this.getPkgs()
// category should start as first item in array // category should start as first item in array
// remove here then add at beginning // remove here then add at beginning
const filterdCategories = this.data.categories.filter(cat => this.category !== cat) const filterdCategories = this.data.categories.filter(cat => this.category !== cat)
@@ -87,16 +85,10 @@ export class MarketplaceListPage {
this.subs.forEach(sub => sub.unsubscribe()) this.subs.forEach(sub => sub.unsubscribe())
} }
async doInfinite (e: any): Promise<void> {
await this.getPkgs(true)
e.target.complete()
}
async search (): Promise<void> { async search (): Promise<void> {
if (!this.query) return // you can actually press enter and run code before the data binds to this.category
this.pkgsLoading = true await pauseFor(200)
this.category = undefined this.category = undefined
this.page = 1
await this.getPkgs() await this.getPkgs()
} }
@@ -111,43 +103,42 @@ export class MarketplaceListPage {
) )
} }
private async getPkgs (doInfinite = false): Promise<void> { private async getAllPkgs (): Promise<void> {
if (this.searchSub) this.searchSub.unsubscribe() this.allPkgs = await this.marketplaceService.getPkgs(
undefined,
this.query,
1,
100000,
)
this.pkgsLoading = false
}
private async getPkgs (): Promise<void> {
if (this.category === 'updates') { if (this.category === 'updates') {
this.pkgs = this.marketplaceService.updates this.pkgs = this.allPkgs.filter(pkg => {
if (this.pkgs.length) { return this.localPkgs[pkg.manifest.id] && pkg.manifest.version !== this.localPkgs[pkg.manifest.id].manifest.version
this.pkgsLoading = false
}
this.searchSub = defer(() => this.marketplaceService.getUpdates(this.localPkgs))
.pipe(take(1), catchError(e => this.errToast.present(e)))
.subscribe(_ => {
this.pkgs = this.marketplaceService.updates
}) })
} else { } else {
this.searchSub = defer(() => this.marketplaceService.getPkgs( this.pkgs = this.allPkgs.filter(pkg => {
this.category !== 'all' ? this.category : undefined, if (this.query) {
this.query, return pkg.manifest.id.toUpperCase().includes(this.query.toUpperCase()) ||
this.page, pkg.manifest.title.toUpperCase().includes(this.query.toUpperCase()) ||
this.perPage, pkg.manifest.description.short.toUpperCase().includes(this.query.toUpperCase()) ||
)) pkg.manifest.description.long.toUpperCase().includes(this.query.toUpperCase())
.pipe(take(1), catchError(e => this.errToast.present(e))) } else {
.subscribe(pkgs => { if (this.category === 'all' || !this.category) {
if (pkgs) { return true
this.needInfinite = pkgs.length >= this.perPage } else {
this.page++ return pkg.categories.includes(this.category)
this.pkgs = doInfinite ? this.pkgs.concat(pkgs) : pkgs }
} }
this.pkgsLoading = false
}) })
} }
} }
async switchCategory (category: string): Promise<void> { async switchCategory (category: string): Promise<void> {
this.pkgsLoading = true
this.category = category this.category = category
this.query = undefined this.query = undefined
this.page = 1
await this.getPkgs() await this.getPkgs()
} }
} }