Add Session.genRequestNumDiscriminator()
This commit is contained in:
		
							
								
								
									
										15
									
								
								src/usimp.ts
									
									
									
									
									
								
							
							
						
						
									
										15
									
								
								src/usimp.ts
									
									
									
									
									
								
							@@ -21,7 +21,7 @@ interface DomainServerJson {
 | 
			
		||||
 | 
			
		||||
interface OutputEnvelopeJson {
 | 
			
		||||
    status: string,
 | 
			
		||||
    requestNr: number,
 | 
			
		||||
    request_nr: number,
 | 
			
		||||
    data: any,
 | 
			
		||||
    action: string | null | undefined,
 | 
			
		||||
    error: {
 | 
			
		||||
@@ -156,6 +156,7 @@ export class Session {
 | 
			
		||||
    httpBaseUrl: string | null;
 | 
			
		||||
    websocket: WebSocket | null;
 | 
			
		||||
    numRequests: number;
 | 
			
		||||
    requestNumDiscriminator: number;
 | 
			
		||||
    subscriptions: {
 | 
			
		||||
        callback: (a: EventJson) => void,
 | 
			
		||||
        requestNr: number | undefined,
 | 
			
		||||
@@ -168,6 +169,7 @@ export class Session {
 | 
			
		||||
    constructor(domain: Domain) {
 | 
			
		||||
        this.domain = domain;
 | 
			
		||||
        this.numRequests = 0;
 | 
			
		||||
        this.requestNumDiscriminator = Session.genRequestNumDiscriminator();
 | 
			
		||||
        this.server = null;
 | 
			
		||||
        this.token = null;
 | 
			
		||||
        this.httpBaseUrl = null;
 | 
			
		||||
@@ -176,6 +178,14 @@ export class Session {
 | 
			
		||||
        this.subscriptionEndpoints = [];
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    private static genRequestNumDiscriminator(): number {
 | 
			
		||||
        // JS uses 64-bit double precision floating point numbers.
 | 
			
		||||
        // These can accurately represent integers up to 53 bit.
 | 
			
		||||
        // In JS bitwise operators only operate on 32-bit integers.
 | 
			
		||||
        // Here a 48-bit integer number is generated.
 | 
			
		||||
        return Math.floor(Math.random() * 0x1000000) * 0x1000000;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    async close(keepEndpoints: boolean = false) {
 | 
			
		||||
        await this.unsubscribeAll(keepEndpoints);
 | 
			
		||||
        if (this.websocket && (this.websocket.readyState !== WebSocket.CLOSING && this.websocket.readyState !== WebSocket.CLOSED)) {
 | 
			
		||||
@@ -303,7 +313,7 @@ export class Session {
 | 
			
		||||
        while (true) {
 | 
			
		||||
            try {
 | 
			
		||||
                if (!forceHttp && this.websocket !== null) {
 | 
			
		||||
                    const req_nr = this.numRequests;
 | 
			
		||||
                    const req_nr = this.numRequests + this.requestNumDiscriminator;
 | 
			
		||||
                    const startTime = performance.now();
 | 
			
		||||
 | 
			
		||||
                    await this.waitForWebSocket(timeout);
 | 
			
		||||
@@ -421,7 +431,6 @@ export class Session {
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    private async _subscribe(cb: (a: EventJson) => void) {
 | 
			
		||||
        this.numRequests++;
 | 
			
		||||
        if (this.websocket !== null) {
 | 
			
		||||
            const subscription = await this.send('subscribe', {}, 60_000, false, undefined, (res) => {
 | 
			
		||||
                if (res.action === 'push') {
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user