Skip to content

Commit

Permalink
fix(exchanges): tick when page in background
Browse files Browse the repository at this point in the history
  • Loading branch information
iam4x committed Mar 17, 2023
1 parent 3dc0862 commit 900fbde
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 10 deletions.
7 changes: 2 additions & 5 deletions src/exchanges/binance/binance.exchange.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import {
import { adjust } from '../../utils/adjust';
import { v } from '../../utils/get-key';
import { inverseObj } from '../../utils/inverse-obj';
import { loop } from '../../utils/loop';
import { omitUndefined } from '../../utils/omit-undefined';
import { createWebSocket } from '../../utils/universal-ws';
import { BaseExchange } from '../base';
Expand Down Expand Up @@ -122,11 +123,7 @@ export class Binance extends BaseExchange {
this.emitter.emit('error', err?.message);
}

if (typeof window === 'undefined') {
setTimeout(() => this.tick(), 0);
} else {
requestAnimationFrame(() => this.tick());
}
loop(() => this.tick());
}
};

Expand Down
7 changes: 2 additions & 5 deletions src/exchanges/bybit/bybit.exchange.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import { OrderTimeInForce, OrderType, OrderSide } from '../../types';
import { adjust } from '../../utils/adjust';
import { v } from '../../utils/get-key';
import { inverseObj } from '../../utils/inverse-obj';
import { loop } from '../../utils/loop';
import { omitUndefined } from '../../utils/omit-undefined';
import { createWebSocket } from '../../utils/universal-ws';
import { BaseExchange } from '../base';
Expand Down Expand Up @@ -129,11 +130,7 @@ export class Bybit extends BaseExchange {
this.emitter.emit('error', err?.message);
}

if (typeof window === 'undefined') {
setTimeout(() => this.tick(), 0);
} else {
requestAnimationFrame(() => this.tick());
}
loop(() => this.tick());
}
};

Expand Down
7 changes: 7 additions & 0 deletions src/utils/loop.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
export const loop = (fn: () => void) => {
if (typeof window !== 'undefined' && document.visibilityState === 'visible') {
requestAnimationFrame(() => fn());
} else {
setTimeout(() => fn(), 0);
}
};

0 comments on commit 900fbde

Please sign in to comment.