feat: add market result store
This commit is contained in:
@@ -0,0 +1,63 @@
|
||||
import type {
|
||||
MarketApiFailureReason,
|
||||
MarketRecord,
|
||||
MarketRowSnapshot
|
||||
} from "./types";
|
||||
import type { AfterSearchRates } from "./types";
|
||||
|
||||
export function createMarketResultStore() {
|
||||
const records = new Map<string, MarketRecord>();
|
||||
|
||||
return {
|
||||
getRecord(authorId: string) {
|
||||
return records.get(authorId) ?? null;
|
||||
},
|
||||
listRecords() {
|
||||
return Array.from(records.values());
|
||||
},
|
||||
setAuthorFailed(authorId: string, reason: MarketApiFailureReason) {
|
||||
const existingRecord = ensureRecord(authorId);
|
||||
existingRecord.status = "failed";
|
||||
existingRecord.failureReason = reason;
|
||||
},
|
||||
setAuthorLoading(authorId: string) {
|
||||
const existingRecord = ensureRecord(authorId);
|
||||
existingRecord.status = "loading";
|
||||
delete existingRecord.failureReason;
|
||||
},
|
||||
setAuthorSuccess(authorId: string, rates: Required<AfterSearchRates>) {
|
||||
const existingRecord = ensureRecord(authorId);
|
||||
existingRecord.status = "success";
|
||||
existingRecord.rates = rates;
|
||||
delete existingRecord.failureReason;
|
||||
},
|
||||
upsertMarketRow(row: MarketRowSnapshot) {
|
||||
const existingRecord = records.get(row.authorId);
|
||||
if (existingRecord) {
|
||||
return existingRecord;
|
||||
}
|
||||
|
||||
const nextRecord: MarketRecord = {
|
||||
...row,
|
||||
status: "idle"
|
||||
};
|
||||
records.set(row.authorId, nextRecord);
|
||||
return nextRecord;
|
||||
}
|
||||
};
|
||||
|
||||
function ensureRecord(authorId: string): MarketRecord {
|
||||
const existingRecord = records.get(authorId);
|
||||
if (existingRecord) {
|
||||
return existingRecord;
|
||||
}
|
||||
|
||||
const nextRecord: MarketRecord = {
|
||||
authorId,
|
||||
authorName: authorId,
|
||||
status: "idle"
|
||||
};
|
||||
records.set(authorId, nextRecord);
|
||||
return nextRecord;
|
||||
}
|
||||
}
|
||||
@@ -3,6 +3,21 @@ export interface AfterSearchRates {
|
||||
singleVideoAfterSearchRate?: string;
|
||||
}
|
||||
|
||||
export type MarketRecordStatus = "idle" | "loading" | "success" | "failed" | "missing";
|
||||
|
||||
export interface MarketRowSnapshot {
|
||||
authorId: string;
|
||||
authorName: string;
|
||||
location?: string;
|
||||
price21To60s?: string;
|
||||
}
|
||||
|
||||
export interface MarketRecord extends MarketRowSnapshot {
|
||||
status: MarketRecordStatus;
|
||||
failureReason?: MarketApiFailureReason;
|
||||
rates?: Required<AfterSearchRates>;
|
||||
}
|
||||
|
||||
export type MarketApiFailureReason =
|
||||
| "bad-response"
|
||||
| "missing-rate"
|
||||
|
||||
Reference in New Issue
Block a user