|
568 | 568 | } ); |
569 | 569 | } |
570 | 570 |
|
571 | | - let badgesDataLoaded = false; |
572 | | - |
573 | | - /** @type {any[]} */ |
574 | | - let badgesData = []; |
| 571 | + /** @type {Promise<any[]> | null} */ |
| 572 | + let badgesDataPromise = null; |
575 | 573 |
|
576 | 574 | /** |
577 | 575 | * @param {HTMLElement} element |
578 | 576 | * @param {any} description |
579 | 577 | * @param {string} steamid |
| 578 | + * @param {any[]} badges |
580 | 579 | */ |
581 | | - function AddBadgeInformation( element, description, steamid ) |
| 580 | + function AddBadgeInformation( element, description, steamid, badges ) |
582 | 581 | { |
583 | 582 | if( !description.market_fee_app ) |
584 | 583 | { |
|
607 | 606 | const CreateLink = ( foil ) => |
608 | 607 | `https://steamcommunity.com/profiles/${steamid}/gamecards/${description.market_fee_app}${foil ? '?border=1' : ''}`; |
609 | 608 |
|
610 | | - for( const badge of badgesData ) |
| 609 | + for( const badge of badges ) |
611 | 610 | { |
612 | 611 | if( badge.appid !== description.market_fee_app ) |
613 | 612 | { |
|
656 | 655 | */ |
657 | 656 | function LoadBadgeInformation( element, description, steamid ) |
658 | 657 | { |
659 | | - if( badgesDataLoaded ) |
| 658 | + if( !badgesDataPromise ) |
660 | 659 | { |
661 | | - if( badgesData.length > 0 ) |
| 660 | + const applicationConfigElement = document.getElementById( 'application_config' ); |
| 661 | + |
| 662 | + if( !applicationConfigElement ) |
662 | 663 | { |
663 | | - AddBadgeInformation( element, description, steamid ); |
| 664 | + return; |
664 | 665 | } |
665 | 666 |
|
666 | | - return; |
667 | | - } |
| 667 | + const applicationConfig = JSON.parse( applicationConfigElement.dataset.config ); |
| 668 | + const accessToken = JSON.parse( applicationConfigElement.dataset.loyalty_webapi_token ); |
668 | 669 |
|
669 | | - // TODO: This has a race condition if user switches to another item before the fetch request completes |
670 | | - // but the only problem they will get is no badge info will be displayed. |
671 | | - badgesDataLoaded = true; |
672 | | - |
673 | | - const applicationConfigElement = document.getElementById( 'application_config' ); |
| 670 | + if( !accessToken ) |
| 671 | + { |
| 672 | + return; |
| 673 | + } |
674 | 674 |
|
675 | | - if( !applicationConfigElement ) |
676 | | - { |
677 | | - return; |
| 675 | + const params = new URLSearchParams(); |
| 676 | + params.set( 'origin', location.origin ); |
| 677 | + params.set( 'format', 'json' ); |
| 678 | + params.set( 'access_token', accessToken ); |
| 679 | + params.set( 'steamid', steamid ); |
| 680 | + params.set( 'x_requested_with', 'SteamDB' ); |
| 681 | + |
| 682 | + badgesDataPromise = fetch( `${applicationConfig.WEBAPI_BASE_URL}IPlayerService/GetBadges/v1/?${params.toString()}` ) |
| 683 | + .then( ( response ) => response.json() ) |
| 684 | + .then( ( /** @type {any} */ response ) => /** @type {any[]} */ ( response.response?.badges ?? [] ) ) |
| 685 | + .catch( ( /** @type {any} */ err ) => |
| 686 | + { |
| 687 | + console.error( '[SteamDB] Badge info error', err ); |
| 688 | + return /** @type {any[]} */ ( [] ); |
| 689 | + } ); |
678 | 690 | } |
679 | 691 |
|
680 | | - const applicationConfig = JSON.parse( applicationConfigElement.dataset.config ); |
681 | | - const accessToken = JSON.parse( applicationConfigElement.dataset.loyalty_webapi_token ); |
682 | | - |
683 | | - if( !accessToken ) |
| 692 | + badgesDataPromise.then( ( badges ) => |
684 | 693 | { |
685 | | - return; |
686 | | - } |
687 | | - |
688 | | - const params = new URLSearchParams(); |
689 | | - params.set( 'origin', location.origin ); |
690 | | - params.set( 'format', 'json' ); |
691 | | - params.set( 'access_token', accessToken ); |
692 | | - params.set( 'steamid', steamid ); |
693 | | - params.set( 'x_requested_with', 'SteamDB' ); |
694 | | - |
695 | | - fetch( `${applicationConfig.WEBAPI_BASE_URL}IPlayerService/GetBadges/v1/?${params.toString()}` ) |
696 | | - .then( ( response ) => response.json() ) |
697 | | - .then( ( response ) => |
| 694 | + if( badges.length > 0 ) |
698 | 695 | { |
699 | | - if( response.response?.badges ) |
700 | | - { |
701 | | - badgesData = response.response.badges; |
702 | | - |
703 | | - AddBadgeInformation( element, description, steamid ); |
704 | | - } |
705 | | - } ) |
706 | | - .catch( ( err ) => |
707 | | - { |
708 | | - console.error( '[SteamDB] Badge info error', err ); |
709 | | - } ); |
| 696 | + AddBadgeInformation( element, description, steamid, badges ); |
| 697 | + } |
| 698 | + } ); |
710 | 699 | } |
711 | 700 |
|
712 | 701 | /** |
|
0 commit comments