Changeset 11447
- Timestamp:
- 01/17/2022 04:03:27 AM (4 years ago)
- Location:
- sites/trunk/global.wordpress.org/public_html/wp-content/mu-plugins/roles
- Files:
-
- 2 edited
-
js/rosetta-roles.js (modified) (3 diffs)
-
rosetta-roles.php (modified) (1 diff)
Legend:
- Unmodified
- Added
- Removed
-
sites/trunk/global.wordpress.org/public_html/wp-content/mu-plugins/roles/js/rosetta-roles.js
r10988 r11447 463 463 }; 464 464 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 465 482 projects.getRemoteProjects = function() { 466 483 return wp.ajax.post( 'rosetta-get-projects' ); … … 468 485 469 486 projects.getLocalProjects = function() { 487 if ( ! projects.hasStorage() ) { 488 return Promise.reject(); 489 } 490 470 491 var lastUpdated = window.localStorage.getItem( 'projectsLastUpdated' ); 471 492 if ( ! lastUpdated || 'undefined' === lastUpdated ) { 472 return false;493 return Promise.reject(); 473 494 } 474 495 475 496 if ( lastUpdated < projects.settings.lastUpdated ) { 476 return false;497 return Promise.reject(); 477 498 } 478 499 479 500 var json = window.localStorage.getItem( 'projects' ); 480 501 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 } ); 485 513 }; 486 514 487 515 projects.storeLocalProjects = function( data ) { 488 516 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 } ); 490 563 }; 491 564 … … 503 576 }); 504 577 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 ) { 517 579 projects.initFrame( response ); 518 if ( projects.hasStorage() ) { 519 projects.storeLocalProjects( response ); 520 } 521 }); 580 } ); 522 581 }; 523 582 -
sites/trunk/global.wordpress.org/public_html/wp-content/mu-plugins/roles/rosetta-roles.php
r11010 r11447 122 122 public static function enqueue_scripts() { 123 123 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 ); 125 125 } 126 126
Note: See TracChangeset
for help on using the changeset viewer.