From 959af0c612f2a002feaa88132baabe4029517a55 Mon Sep 17 00:00:00 2001 From: Dylan Yang Date: Mon, 16 May 2022 15:52:54 +0800 Subject: [PATCH] feat(createMemoizedFetcher): support isFetching & prevResultFirst variables --- README.md | 3 ++- src/core/clearingHouse/ClearingHouse.ts | 2 +- src/core/liquidity/Liquidities.ts | 2 +- src/core/market/Market.ts | 2 +- src/core/position/PositionDraft.ts | 4 ++-- src/core/vault/Vault.ts | 8 ++++---- src/core/wallet/Wallet.ts | 8 ++++---- src/internal/createMemoizedFetcher.ts | 8 +++++++- 8 files changed, 22 insertions(+), 15 deletions(-) diff --git a/README.md b/README.md index d198ad5..9982924 100644 --- a/README.md +++ b/README.md @@ -1,8 +1,9 @@ ![Cover](https://user-images.githubusercontent.com/5022617/167766554-055c9785-00ec-4a5a-86ac-a4b3e1a42e76.png) -# Perpetual Protocol Curie SDK +# Perpetual Protocol Curie SDK (BETA) A Javascript SDK for Perpetual Protocol Curie. +`This repo is still in beta.` [![Commitizen friendly](https://img.shields.io/badge/commitizen-friendly-brightgreen.svg)](http://commitizen.github.io/cz-cli/) diff --git a/src/core/clearingHouse/ClearingHouse.ts b/src/core/clearingHouse/ClearingHouse.ts index e29574e..22a9913 100644 --- a/src/core/clearingHouse/ClearingHouse.ts +++ b/src/core/clearingHouse/ClearingHouse.ts @@ -207,7 +207,7 @@ class ClearingHouse extends Channel { eventSourceStarter: () => poll(fetchAndEmitAccountValueUpdated, this._perp.moduleConfigs?.clearingHouse?.period || DEFAULT_PERIOD) .cancel, - initEventEmitter: () => fetchAndEmitAccountValueUpdated(true), + initEventEmitter: () => fetchAndEmitAccountValueUpdated(true, true), }) return { diff --git a/src/core/liquidity/Liquidities.ts b/src/core/liquidity/Liquidities.ts index f168ef0..30373ca 100644 --- a/src/core/liquidity/Liquidities.ts +++ b/src/core/liquidity/Liquidities.ts @@ -59,7 +59,7 @@ class Liquidities extends Channel { const updateDataEventSource = new ChannelEventSource({ eventSourceStarter: () => poll(this._fetchAndEmitUpdated, this._perp.moduleConfigs?.orders?.period || DEFAULT_PERIOD).cancel, - initEventEmitter: () => this._fetchAndEmitUpdated(), + initEventEmitter: () => this._fetchAndEmitUpdated(false, true), }) return { updated: updateDataEventSource, diff --git a/src/core/market/Market.ts b/src/core/market/Market.ts index cd240be..8bc1053 100644 --- a/src/core/market/Market.ts +++ b/src/core/market/Market.ts @@ -80,7 +80,7 @@ class Market extends Channel { const { cancel } = poll(fetchAndEmitUpdated, this._perp.moduleConfigs?.market?.period || DEFAULT_PERIOD) return cancel }, - initEventEmitter: () => fetchAndEmitUpdated(true), + initEventEmitter: () => fetchAndEmitUpdated(true, true), }) // TODO: eventName typing protection, should error when invalid eventName is provided diff --git a/src/core/position/PositionDraft.ts b/src/core/position/PositionDraft.ts index 3f4e9be..7d78554 100644 --- a/src/core/position/PositionDraft.ts +++ b/src/core/position/PositionDraft.ts @@ -146,10 +146,10 @@ export class PositionDraft extends Channel new UnauthorizedError({ functionName: "_getEventSourceMap" }), ) const removeVaultUpdated = this._perp.vault.on("updated", () => { - fetchAndEmitUpdated() + fetchAndEmitUpdated(false, true) }) const removePositionsUpdated = this._perp.positions.on("updated", () => { - fetchAndEmitUpdated() + fetchAndEmitUpdated(false, true) }) return () => { removeVaultUpdated() diff --git a/src/core/vault/Vault.ts b/src/core/vault/Vault.ts index b32aed9..0047e90 100644 --- a/src/core/vault/Vault.ts +++ b/src/core/vault/Vault.ts @@ -112,14 +112,14 @@ class Vault extends Channel { const accountValueUpdated = new ChannelEventSource({ eventSourceStarter: () => poll(fetchAndEmitAccountValueUpdated, this._perp.moduleConfigs?.vault?.period || DEFAULT_PERIOD).cancel, - initEventEmitter: () => fetchAndEmitAccountValueUpdated(true), + initEventEmitter: () => fetchAndEmitAccountValueUpdated(true, true), }) const fetchAndEmitBalanceListUpdated = this._createFetchAndEmitBalanceListUpdated() const balanceListUpdated = new ChannelEventSource({ eventSourceStarter: () => poll(fetchAndEmitBalanceListUpdated, this._perp.moduleConfigs?.vault?.period || DEFAULT_PERIOD).cancel, - initEventEmitter: () => fetchAndEmitBalanceListUpdated(true), + initEventEmitter: () => fetchAndEmitBalanceListUpdated(true, true), }) const fetchAndEmitFreeCollateralUpdated = this._createFetchAndEmitFreeCollateralUpdated() @@ -127,7 +127,7 @@ class Vault extends Channel { eventSourceStarter: () => poll(fetchAndEmitFreeCollateralUpdated, this._perp.moduleConfigs?.vault?.period || DEFAULT_PERIOD) .cancel, - initEventEmitter: () => fetchAndEmitFreeCollateralUpdated(true), + initEventEmitter: () => fetchAndEmitFreeCollateralUpdated(true, true), }) const fetchAndEmitFreeCollateralListUpdated = this._createFetchAndEmitFreeCollateralListUpdated() @@ -135,7 +135,7 @@ class Vault extends Channel { eventSourceStarter: () => poll(fetchAndEmitFreeCollateralListUpdated, this._perp.moduleConfigs?.vault?.period || DEFAULT_PERIOD) .cancel, - initEventEmitter: () => fetchAndEmitFreeCollateralListUpdated(true), + initEventEmitter: () => fetchAndEmitFreeCollateralListUpdated(true, true), }) return { diff --git a/src/core/wallet/Wallet.ts b/src/core/wallet/Wallet.ts index e10c5a3..1e6049f 100644 --- a/src/core/wallet/Wallet.ts +++ b/src/core/wallet/Wallet.ts @@ -83,21 +83,21 @@ class Wallet extends Channel { eventSourceStarter: () => poll(fetchAndEmitAllowanceListUpdated, this._perp.moduleConfigs?.wallet?.period || DEFAULT_PERIOD) .cancel, - initEventEmitter: () => fetchAndEmitAllowanceListUpdated(true), + initEventEmitter: () => fetchAndEmitAllowanceListUpdated(true, true), }) const fetchAndEmitBalanceListUpdated = this._createFetchAndEmitBalanceListUpdated() const balanceListUpdated = new ChannelEventSource({ eventSourceStarter: () => poll(fetchAndEmitBalanceListUpdated, this._perp.moduleConfigs?.wallet?.period || DEFAULT_PERIOD).cancel, - initEventEmitter: () => fetchAndEmitBalanceListUpdated(true), + initEventEmitter: () => fetchAndEmitBalanceListUpdated(true, true), }) const fetchAndEmitBalanceEthUpdated = this._createFetchAndEmitBalanceEthUpdated() const balanceEthUpdated = new ChannelEventSource({ eventSourceStarter: () => poll(fetchAndEmitBalanceEthUpdated, this._perp.moduleConfigs?.wallet?.period || DEFAULT_PERIOD).cancel, - initEventEmitter: () => fetchAndEmitBalanceEthUpdated(true), + initEventEmitter: () => fetchAndEmitBalanceEthUpdated(true, true), }) const fetchAndEmitCollateralTokenPriceListUpdated = this._createFetchAndEmitCollateralTokenPriceListUpdated() @@ -107,7 +107,7 @@ class Wallet extends Channel { fetchAndEmitCollateralTokenPriceListUpdated, this._perp.moduleConfigs?.wallet?.period || DEFAULT_PERIOD, ).cancel, - initEventEmitter: () => fetchAndEmitCollateralTokenPriceListUpdated(true), + initEventEmitter: () => fetchAndEmitCollateralTokenPriceListUpdated(true, true), }) return { diff --git a/src/internal/createMemoizedFetcher.ts b/src/internal/createMemoizedFetcher.ts index 16637c6..dc1560e 100644 --- a/src/internal/createMemoizedFetcher.ts +++ b/src/internal/createMemoizedFetcher.ts @@ -29,6 +29,12 @@ export function hasNumberArrChange(prev: Big[], next: Big[]): boolean { export type MemoizedFetcher = (ignoreChangeCheck?: boolean, prevResultFirst?: boolean) => Promise +/** FIXME: + * Add the `prevResultFirst` option as a temp solution for performance. + * When `prevResultFirst` is true and result has been fetched it will skip fetch. + * When `prevResultFirst` is true and the fetcher has been called(no result yet), + * the second call is going to check the previous result per second, until the result is fetched or hit the timeout(10s). + */ export function createMemoizedFetcher( fetcher: () => Promise, handler: (args: T) => void, @@ -37,13 +43,13 @@ export function createMemoizedFetcher( let prevResults: T let isFetching = false + // FIXME: when prevResultFirst is true and fetched fail, the isFetching will not be reset. (use try/catch) return async function (ignoreChangeCheck = false, prevResultFirst = false) { if (prevResultFirst && prevResults) { handler(prevResults) return } - // If we're already fetching, wait for the result, timeout = 10s if (prevResultFirst && isFetching) { for (let i = 0; i < 10; i++) { await new Promise(resolve => setTimeout(resolve, 1000))