Skip to content

Commit

Permalink
Merge branch 'develop' into feature/swap-layout
Browse files Browse the repository at this point in the history
  • Loading branch information
Nikita-Polyakov authored Jul 19, 2024
2 parents 42b6e52 + 1ed4735 commit 94335ab
Showing 1 changed file with 30 additions and 48 deletions.
78 changes: 30 additions & 48 deletions src/components/shared/Widget/PriceChart.vue
Original file line number Diff line number Diff line change
Expand Up @@ -744,31 +744,29 @@ export default class PriceChartWidget extends Mixins(
}
// ordered ty timestamp DESC
private async fetchData(entityId: string): Promise<Snapshot> {
private async fetchData(entityId: string): Promise<SnapshotItem[]> {
const { type, count } = this.selectedFilter;
const pageInfo = this.pageInfos[entityId];
const hasNextPage = pageInfo?.hasNextPage ?? true;
const endCursor = pageInfo?.endCursor ?? undefined;
const loadedItems = this.dataset.length;
const buffer = this.snapshotBuffer[entityId] ?? [];
const fromBuffer = buffer.slice(loadedItems);
if (fromBuffer.length >= count) {
return {
nodes: fromBuffer,
hasNextPage,
endCursor,
};
const pageInfoBuffer = this.pageInfos[entityId];
const hasNextPage = pageInfoBuffer?.hasNextPage ?? true;
const endCursor = pageInfoBuffer?.endCursor ?? undefined;
const snapshotsBuffer = this.snapshotBuffer[entityId] ?? [];
const snapshotsUsedCount = this.dataset.length;
const snapshotsUnused = snapshotsBuffer.slice(snapshotsUsedCount);
if (snapshotsUnused.length >= count || !hasNextPage) {
return snapshotsUnused;
}
const response = await this.requestData(entityId, type, count, hasNextPage, endCursor);
const { nodes, ...pageInfo } = await this.requestData(entityId, type, count, hasNextPage, endCursor);
const lastTimestamp = last(snapshotsUnused)?.timestamp ?? last(this.dataset)?.timestamp ?? Date.now();
const snapshotsNormalized = normalizeSnapshots(nodes, this.timeDifference, lastTimestamp);
return {
nodes: [...fromBuffer, ...response.nodes],
hasNextPage: response.hasNextPage,
endCursor: response.endCursor,
};
this.fillSnapshotBuffer(entityId, snapshotsNormalized);
this.pageInfos[entityId] = pageInfo;
return [...snapshotsUnused, ...snapshotsNormalized];
}
private async fetchDataLastUpdates(entities: string[]): Promise<Nullable<LastUpdates>> {
Expand Down Expand Up @@ -804,38 +802,26 @@ export default class PriceChartWidget extends Mixins(
const addresses = [...this.entities];
const requestId = Date.now();
const lastTimestamp = last(this.dataset)?.timestamp ?? Date.now();
this.priceUpdateRequestId = requestId;
await this.withApi(async () => {
try {
const snapshots: Snapshot[] = await Promise.all(addresses.map((address) => this.fetchData(address)));
const snapshots = await Promise.all(addresses.map((address) => this.fetchData(address)));
if (!(snapshots && isEqual(addresses)(this.entities) && isEqual(requestId)(this.priceUpdateRequestId))) return;
const pageInfos: Record<string, Partial<PageInfo>> = {};
const dataset: SnapshotItem[] = [];
const groups: SnapshotItem[][] = [];
const timestamp =
lastTimestamp ??
Math.max(snapshots[0]?.nodes[0]?.timestamp ?? 0, snapshots[1]?.nodes[0]?.timestamp ?? 0) * 1000;
if (!(isEqual(addresses)(this.entities) && isEqual(requestId)(this.priceUpdateRequestId))) return;
snapshots.forEach(async ({ hasNextPage, endCursor, nodes }, index) => {
const address = addresses[index];
const normalized = normalizeSnapshots(nodes, this.timeDifference, timestamp);
this.fillSnapshotBuffer(address, normalized);
groups.push(normalized);
pageInfos[address] = { hasNextPage, endCursor };
});
const size = Math.min(groups[0]?.length ?? Infinity, groups[1]?.length ?? Infinity, this.selectedFilter.count);
const dataset: SnapshotItem[] = [];
const size = Math.min(
snapshots[0]?.length ?? Infinity,
snapshots[1]?.length ?? Infinity,
this.selectedFilter.count
);
let { min, max } = this.limits;
for (let i = 0; i < size; i++) {
const a = groups[0]?.[i];
const b = groups[1]?.[i];
const a = snapshots[0]?.[i];
const b = snapshots[1]?.[i];
const { timestamp, price, volume } = mergeSnapshots(a, b);
// skip item, if one of the prices is incorrect
Expand All @@ -850,7 +836,6 @@ export default class PriceChartWidget extends Mixins(
}
this.limits = { min, max };
this.pageInfos = pageInfos;
this.precision = this.getUpdatedPrecision(min, max);
this.updateDataset([...this.dataset, ...dataset]);
Expand All @@ -864,10 +849,7 @@ export default class PriceChartWidget extends Mixins(
private fillSnapshotBuffer(entityId: string, normalized: SnapshotItem[]): void {
const existingNodes = this.snapshotBuffer[entityId] ?? [];
const normalizedTimestamps = new Set(normalized.map((item) => item.timestamp));
const filteredExistingNodes = existingNodes.filter((item) => !normalizedTimestamps.has(item.timestamp));
this.snapshotBuffer[entityId] = Object.freeze([...filteredExistingNodes, ...normalized]);
this.snapshotBuffer[entityId] = Object.freeze([...existingNodes, ...normalized]);
}
// common
Expand Down

0 comments on commit 94335ab

Please sign in to comment.