feat: session state long polling
parent
78eb7626be
commit
3748bf68af
|
@ -1,4 +1,4 @@
|
|||
import axios from "axios";
|
||||
import axios, { AxiosRequestConfig } from "axios";
|
||||
import { getSecureRandomBytes, keyPairFromSeed } from "ton-crypto";
|
||||
import { backoff } from "../utils/backoff";
|
||||
import { toUrlSafe } from "../utils/toURLsafe";
|
||||
|
@ -310,9 +310,7 @@ export class TonhubConnector {
|
|||
};
|
||||
}
|
||||
|
||||
getSessionState = async (sessionId: string): Promise<TonhubSessionState> => {
|
||||
return await backoff(async () => {
|
||||
let ex = (await axios.get('https://connect.tonhubapi.com/connect/' + sessionId, this.adapter ? { timeout: 5000, adapter: this.adapter } : { timeout: 5000 })).data;
|
||||
private ensureSessionStateCorrect = (sessionId: string, ex: any): TonhubSessionState => {
|
||||
if (!sessionStateCodec.is(ex)) {
|
||||
throw Error('Invalid response from server');
|
||||
}
|
||||
|
@ -357,10 +355,28 @@ export class TonhubConnector {
|
|||
}
|
||||
|
||||
return { state: 'revoked' };
|
||||
}
|
||||
|
||||
getSessionState = async (sessionId: string): Promise<TonhubSessionState> => {
|
||||
return await backoff(async () => {
|
||||
let ex = (await axios.get('https://connect.tonhubapi.com/connect/' + sessionId, this.adapter ? { timeout: 5000, adapter: this.adapter } : { timeout: 5000 })).data;
|
||||
return this.ensureSessionStateCorrect(sessionId, ex);
|
||||
});
|
||||
}
|
||||
|
||||
awaitSessionReady = async (sessionId: string, timeout: number): Promise<TonhubSessionAwaited> => {
|
||||
waitForSessionState = async (sessionId: string, lastUpdated?: number): Promise<TonhubSessionState> => {
|
||||
return await backoff(async () => {
|
||||
let params: AxiosRequestConfig = this.adapter ? { timeout: 30000, adapter: this.adapter } : { timeout: 30000 }
|
||||
params.timeout = 30000
|
||||
params.params = {
|
||||
lastUpdated
|
||||
};
|
||||
let ex = (await axios.get('https://connect.tonhubapi.com/connect/' + sessionId + '/wait', params)).data;
|
||||
return this.ensureSessionStateCorrect(sessionId, ex);
|
||||
})
|
||||
}
|
||||
|
||||
awaitSessionReady = async (sessionId: string, timeout: number, lastUpdated: number): Promise<TonhubSessionAwaited> => {
|
||||
let expires = Date.now() + timeout;
|
||||
let res: TonhubSessionStateReady | TonhubSessionStateExpired | TonhubSessionStateRevoked = await backoff(async () => {
|
||||
while (Date.now() < expires) {
|
||||
|
|
Loading…
Reference in New Issue