fetch item prices - throttle end points with promise queue

This commit is contained in:
2018-08-28 21:45:46 -05:00
parent 97acc0d4fa
commit 7ca8606284
17 changed files with 241 additions and 64 deletions

View File

@@ -1,7 +1,7 @@
import Store from 'electron-store';
import * as _ from 'lodash';
import { action, computed, observable } from 'mobx';
import { setHeader } from '../http';
import { ILeague } from '../model';
import { IItemPrice, ILeague } from '../model';
import { IItem } from '../model/item';
import { IStashTab } from '../model/stash-tab';
import { PoeService } from '../services';
@@ -14,7 +14,7 @@ export class AppStore {
@observable
public sessionID?: string;
@observable
public activeLeague: ILeague;
public activeLeague?: ILeague;
@observable
public selectedTabIndex: number = 0;
@observable
@@ -46,7 +46,7 @@ export class AppStore {
@action
private async loadLeagues() {
try {
const leagues = await PoeService.getLeagues();
const leagues = await PoeService.getLeagues(this.sessionCookieHeader);
if (!leagues) {
return;
}
@@ -67,19 +67,29 @@ export class AppStore {
@action
public resetState() {
delete this.username;
delete this.sessionID;
setHeader('Cookie', '');
this.username = undefined;
this.sessionID = undefined;
this.activeLeague = undefined;
this.selectedTabIndex = 0;
this.stashTabs = [];
this.stashItems = [];
this.leagues = [];
store.clear();
}
@action
public setSessionID(sessionID: string) {
setHeader('Cookie', `POESESSID=${sessionID}`);
this.sessionID = sessionID;
StorageService.storeSessionID(sessionID);
}
@computed
public get sessionCookieHeader(): { Cookie: string } {
return {
Cookie: `POESESSID=${this.sessionID}`,
};
}
@action
public setUsername(username: string) {
this.username = username;
@@ -101,14 +111,27 @@ export class AppStore {
@action
public async performLogin(sessionID: string) {
this.setSessionID(sessionID);
const username = await PoeService.getUsername();
const username = await PoeService.getUsername(this.sessionCookieHeader);
this.setUsername(username);
this.loadLeagues();
}
@action
public async loadItems(tabIndex: number | string) {
const data = await PoeService.getStash(this.username!, this.activeLeague.id, tabIndex);
PoeService.clearRequestQueue();
const data = await PoeService.getStash(this.username!, this.activeLeague!.id, tabIndex);
this.stashTabs = data.tabs;
this.stashItems = data.items;
}
/**
* Updates the price of an item in the list - updates based on id
*/
@action
public updateItemPrice(id: string, itemPrice: IItemPrice) {
const item = _.find(this.stashItems, i => i.id === id);
if (item) {
item.itemPrice = itemPrice;
}
}
}