Making WordPress.org

Changeset 11447


Ignore:
Timestamp:
01/17/2022 04:03:27 AM (4 years ago)
Author:
dd32
Message:

Rosetta: PTE management: Compress projecting listing when adding to localStorage.

The JSON being cached is currently ~6.2MB, most browsers enforce an absolute maximum size of 5MB for localStorage.
This compresses the JSON down to ~1.6MB which should be small enough for quite some time.

See #6022.

Location:
sites/trunk/global.wordpress.org/public_html/wp-content/mu-plugins/roles
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • sites/trunk/global.wordpress.org/public_html/wp-content/mu-plugins/roles/js/rosetta-roles.js

    r10988 r11447  
    463463    };
    464464
     465    projects.getProjects = function() {
     466        return projects.getLocalProjects().then(
     467            function( localProjects ) {
     468                return localProjects;
     469            },
     470            function() {
     471                return projects.getRemoteProjects().then( function( remoteProjects ) {
     472                    if ( projects.hasStorage() ) {
     473                        projects.storeLocalProjects( remoteProjects );
     474                    }
     475
     476                    return remoteProjects;
     477                } );
     478            }
     479        );
     480    };
     481
    465482    projects.getRemoteProjects = function() {
    466483        return wp.ajax.post( 'rosetta-get-projects' );
     
    468485
    469486    projects.getLocalProjects = function() {
     487        if ( ! projects.hasStorage() ) {
     488            return Promise.reject();
     489        }
     490
    470491        var lastUpdated = window.localStorage.getItem( 'projectsLastUpdated' );
    471492        if ( ! lastUpdated || 'undefined' === lastUpdated ) {
    472             return false;
     493            return Promise.reject();
    473494        }
    474495
    475496        if ( lastUpdated < projects.settings.lastUpdated ) {
    476             return false;
     497            return Promise.reject();
    477498        }
    478499
    479500        var json = window.localStorage.getItem( 'projects' );
    480501        if ( ! json ) {
    481             return false;
    482         }
    483 
    484         return JSON.parse( json );
     502            return Promise.reject();
     503        }
     504
     505        if ( '[' === json.substring( 0, 1 ) ) {
     506            return Promise.resolve( JSON.parse( json ) );
     507        }
     508
     509        // decompress
     510        return projects.decompress( json ).then( function( data ) {
     511            return JSON.parse( data );
     512        } );
    485513    };
    486514
    487515    projects.storeLocalProjects = function( data ) {
    488516        window.localStorage.setItem( 'projectsLastUpdated', projects.settings.lastUpdated );
    489         window.localStorage.setItem( 'projects', JSON.stringify( data ) );
     517
     518        projects.compress( JSON.stringify( data ) ).then( function( data ) {
     519            window.localStorage.setItem( 'projects', data );
     520        } ).catch( function() {
     521            console.log( "Caching projects locally failed." );
     522        } );
     523    };
     524
     525    projects.compress = async function( data ) {
     526        if ( 'undefined' === typeof CompressionStream ) {
     527            return Promise.reject();
     528        }
     529
     530        var stream = new CompressionStream( 'gzip' ),
     531            streamWriter = stream.writable.getWriter();
     532
     533        streamWriter.write(
     534            new TextEncoder().encode( data )
     535        );
     536        streamWriter.close();
     537
     538        return new Response( stream.readable ).arrayBuffer().then( function( buffer ) {
     539            // Base64 encode it, so it can be stored in localStorage
     540            return btoa(
     541                new Uint8Array( buffer )
     542                    .reduce( ( data, byte ) => data + String.fromCharCode( byte ), '' )
     543            );
     544        } );       
     545    };
     546
     547    projects.decompress = async function( data ) {
     548        if ( 'undefined' === typeof DecompressionStream ) {
     549            return Promise.reject();
     550        }
     551
     552        // Base64 decode
     553        var buffer = Uint8Array.from( atob( data ), c => c.charCodeAt(0) ),
     554            stream = new DecompressionStream( 'gzip' ),
     555            streamWriter = stream.writable.getWriter();
     556
     557        streamWriter.write( buffer );
     558        streamWriter.close();
     559
     560        return new Response( stream.readable ).arrayBuffer().then( function( arrayBuffer ) {
     561            return new TextDecoder().decode( arrayBuffer );
     562        } );
    490563    };
    491564
     
    503576        });
    504577
    505         var data = null;
    506 
    507         if ( projects.hasStorage() ) {
    508             data = projects.getLocalProjects();
    509         }
    510 
    511         if ( data && data.length ) {
    512             projects.initFrame( data );
    513             return;
    514         }
    515 
    516         projects.getRemoteProjects().done( function( response ) {
     578        projects.getProjects().then( function( response ) {
    517579            projects.initFrame( response );
    518             if ( projects.hasStorage() ) {
    519                 projects.storeLocalProjects( response );
    520             }
    521         });
     580        } );
    522581    };
    523582
  • sites/trunk/global.wordpress.org/public_html/wp-content/mu-plugins/roles/rosetta-roles.php

    r11010 r11447  
    122122    public static function enqueue_scripts() {
    123123        wp_enqueue_script( 'string_score', plugins_url( '/js/string_score.min.js', __FILE__ ), array(), '0.1.22', true );
    124         wp_enqueue_script( 'rosetta-roles', plugins_url( '/js/rosetta-roles.js', __FILE__ ), array( 'jquery', 'wp-backbone', 'string_score' ), '11', true );
     124        wp_enqueue_script( 'rosetta-roles', plugins_url( '/js/rosetta-roles.js', __FILE__ ), array( 'jquery', 'wp-backbone', 'string_score' ), 12, true );
    125125    }
    126126
Note: See TracChangeset for help on using the changeset viewer.