Skip to content

Commit

Permalink
feat(createMemoizedFetcher): support isFetching & prevResultFirst var…
Browse files Browse the repository at this point in the history
…iables
  • Loading branch information
DylanYang0523 committed May 16, 2022
1 parent 4c4750c commit 959af0c
Show file tree
Hide file tree
Showing 8 changed files with 22 additions and 15 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -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/)

Expand Down
2 changes: 1 addition & 1 deletion src/core/clearingHouse/ClearingHouse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@ class ClearingHouse extends Channel<ClearingHouseEventName> {
eventSourceStarter: () =>
poll(fetchAndEmitAccountValueUpdated, this._perp.moduleConfigs?.clearingHouse?.period || DEFAULT_PERIOD)
.cancel,
initEventEmitter: () => fetchAndEmitAccountValueUpdated(true),
initEventEmitter: () => fetchAndEmitAccountValueUpdated(true, true),
})

return {
Expand Down
2 changes: 1 addition & 1 deletion src/core/liquidity/Liquidities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ class Liquidities extends Channel<LiquiditiesEventName> {
const updateDataEventSource = new ChannelEventSource<LiquiditiesEventName>({
eventSourceStarter: () =>
poll(this._fetchAndEmitUpdated, this._perp.moduleConfigs?.orders?.period || DEFAULT_PERIOD).cancel,
initEventEmitter: () => this._fetchAndEmitUpdated(),
initEventEmitter: () => this._fetchAndEmitUpdated(false, true),
})
return {
updated: updateDataEventSource,
Expand Down
2 changes: 1 addition & 1 deletion src/core/market/Market.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ class Market extends Channel<MarketEventName> {
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
Expand Down
4 changes: 2 additions & 2 deletions src/core/position/PositionDraft.ts
Original file line number Diff line number Diff line change
Expand Up @@ -146,10 +146,10 @@ export class PositionDraft<EventName extends string = string> extends Channel<Po
() => 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()
Expand Down
8 changes: 4 additions & 4 deletions src/core/vault/Vault.ts
Original file line number Diff line number Diff line change
Expand Up @@ -112,30 +112,30 @@ class Vault extends Channel<VaultEventName> {
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()
const freeCollateralUpdated = new ChannelEventSource({
eventSourceStarter: () =>
poll(fetchAndEmitFreeCollateralUpdated, this._perp.moduleConfigs?.vault?.period || DEFAULT_PERIOD)
.cancel,
initEventEmitter: () => fetchAndEmitFreeCollateralUpdated(true),
initEventEmitter: () => fetchAndEmitFreeCollateralUpdated(true, true),
})

const fetchAndEmitFreeCollateralListUpdated = this._createFetchAndEmitFreeCollateralListUpdated()
const freeCollateralListUpdated = new ChannelEventSource({
eventSourceStarter: () =>
poll(fetchAndEmitFreeCollateralListUpdated, this._perp.moduleConfigs?.vault?.period || DEFAULT_PERIOD)
.cancel,
initEventEmitter: () => fetchAndEmitFreeCollateralListUpdated(true),
initEventEmitter: () => fetchAndEmitFreeCollateralListUpdated(true, true),
})

return {
Expand Down
8 changes: 4 additions & 4 deletions src/core/wallet/Wallet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,21 +83,21 @@ class Wallet extends Channel<WalletEventName> {
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()
Expand All @@ -107,7 +107,7 @@ class Wallet extends Channel<WalletEventName> {
fetchAndEmitCollateralTokenPriceListUpdated,
this._perp.moduleConfigs?.wallet?.period || DEFAULT_PERIOD,
).cancel,
initEventEmitter: () => fetchAndEmitCollateralTokenPriceListUpdated(true),
initEventEmitter: () => fetchAndEmitCollateralTokenPriceListUpdated(true, true),
})

return {
Expand Down
8 changes: 7 additions & 1 deletion src/internal/createMemoizedFetcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,12 @@ export function hasNumberArrChange(prev: Big[], next: Big[]): boolean {

export type MemoizedFetcher = (ignoreChangeCheck?: boolean, prevResultFirst?: boolean) => Promise<void>

/** 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<T>(
fetcher: () => Promise<T>,
handler: (args: T) => void,
Expand All @@ -37,13 +43,13 @@ export function createMemoizedFetcher<T>(
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))
Expand Down

0 comments on commit 959af0c

Please sign in to comment.