Skip to content

Commit

Permalink
explorer: Delay fetching market data if the current data is not stale
Browse files Browse the repository at this point in the history
Resolves #1955
  • Loading branch information
ascartabelli committed Jul 10, 2024
1 parent a2ea6cd commit 1ca073a
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 2 deletions.
49 changes: 48 additions & 1 deletion explorer/src/lib/stores/__tests__/marketDataStore.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,7 @@ describe("marketDataStore", async () => {
beforeEach(() => {
vi.resetModules();
vi.clearAllTimers();
vi.mocked(duskAPI.getMarketData).mockClear();
});

afterEach(() => {
Expand Down Expand Up @@ -278,6 +279,52 @@ describe("marketDataStore", async () => {
getDataSpy.mockRestore();
});

it("should start the polling as usual if there's data stored, but it's stale", async () => {
const storedData = {
data: "D",
lastUpdate: new Date(Date.now() - marketDataFetchInterval - 1),
};

// @ts-expect-error we don't care to pass the correct type
marketDataStorage.set(storedData);

marketDataStore = (await import("../marketDataStore")).default;

expect(duskAPI.getMarketData).toHaveBeenCalledTimes(1);

await vi.advanceTimersByTimeAsync(settleTime + marketDataFetchInterval);

expect(duskAPI.getMarketData).toHaveBeenCalledTimes(2);
});

it("should delay the polling if there's data stored and it's not stale", async () => {
const offset = Math.floor(marketDataFetchInterval / 2);
const expectedDelay = marketDataFetchInterval - offset;
const storedData = {
data: "D",
lastUpdate: new Date(Date.now() - marketDataFetchInterval + offset),
};

// @ts-expect-error we don't care to pass the correct type
marketDataStorage.set(storedData);

marketDataStore = (await import("../marketDataStore")).default;

expect(duskAPI.getMarketData).not.toHaveBeenCalled();

await vi.advanceTimersByTimeAsync(expectedDelay - 1);

expect(duskAPI.getMarketData).not.toHaveBeenCalled();

await vi.advanceTimersByTimeAsync(1);

expect(duskAPI.getMarketData).toHaveBeenCalledTimes(1);

await vi.advanceTimersByTimeAsync(marketDataFetchInterval + settleTime);

expect(duskAPI.getMarketData).toHaveBeenCalledTimes(2);
});

it("should save the received data in local storage if the request has new data", async () => {
const setDataSpy = vi.spyOn(marketDataStorage, "set");

Expand All @@ -304,7 +351,7 @@ describe("marketDataStore", async () => {
await vi.advanceTimersByTimeAsync(marketDataFetchInterval + settleTime);

expect(setDataSpy).toHaveBeenCalledTimes(1);
expect(duskAPI.getMarketData).toHaveBeenCalledTimes(3);
expect(duskAPI.getMarketData).toHaveBeenCalledTimes(2);
expect(get(marketDataStore)).toStrictEqual(expectedStore);
await expect(marketDataStorage.get()).resolves.toStrictEqual(
expectedStorage
Expand Down
9 changes: 8 additions & 1 deletion explorer/src/lib/stores/marketDataStore.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,14 @@ function isDataStale() {
);
}

pollingDataStore.start();
if (!initialState.lastUpdate || isDataStale()) {
pollingDataStore.start();
} else {
setTimeout(
pollingDataStore.start,
+initialState.lastUpdate + fetchInterval - Date.now()
);
}

/** @type {MarketDataStore} */
export default {
Expand Down

0 comments on commit 1ca073a

Please sign in to comment.