Explorar o código

Add support for sorting unloaded content

Garrett Johnson %!s(int64=5) %!d(string=hai) anos
pai
achega
b1b832a9ef
Modificáronse 2 ficheiros con 28 adicións e 4 borrados
  1. 3 1
      src/base/TilesRendererBase.js
  2. 25 3
      src/utilities/LRUCache.js

+ 3 - 1
src/base/TilesRendererBase.js

@@ -9,6 +9,8 @@ import { UNLOADED, LOADING, PARSING, LOADED, FAILED } from './constants.js';
 // TODO: See if declaring function inline improves performance
 // TODO: Make sure active state works as expected
 
+const lruSort = ( a, b ) => a.__depth - b.__depth;
+
 export class TilesRendererBase {
 
 	get rootTileSet() {
@@ -104,7 +106,7 @@ export class TilesRendererBase {
 		toggleTiles( root, this );
 
 		// TODO: We may want to add this function in the requestTileContents function
-		lruCache.scheduleUnload( null );
+		lruCache.scheduleUnload( lruSort );
 
 	}
 

+ 25 - 3
src/utilities/LRUCache.js

@@ -107,7 +107,7 @@ class LRUCache {
 
 	// TODO: this should be renamed because it's not necessarily unloading all unused content
 	// Maybe call it "cleanup" or "unloadToMinSize"
-	unloadUnusedContent( prioritySortCb ) {
+	unloadUnusedContent( prioritySortCb = null ) {
 
 		const unloadPercent = this.unloadPercent;
 		const targetSize = this.minSize;
@@ -120,7 +120,29 @@ class LRUCache {
 
 		if ( excess > 0 && unused > 0 ) {
 
-			// TODO: sort by priority
+			if ( prioritySortCb ) {
+
+				itemList.sort( ( a, b ) => {
+
+					const usedA = usedSet.has( a );
+					const usedB = usedSet.has( b );
+					if ( usedA && usedB ) {
+
+						return 0;
+
+					} else if ( ! usedA && ! usedB ) {
+
+						return prioritySortCb( a, b );
+
+					} else {
+
+						return usedA ? - 1 : 1;
+
+					}
+
+				} );
+
+			}
 
 			let nodesToUnload = Math.min( targetSize * unloadPercent, unused );
 			nodesToUnload = Math.ceil( nodesToUnload );
@@ -139,7 +161,7 @@ class LRUCache {
 
 	}
 
-	scheduleUnload( prioritySortCb, markAllUnused = true ) {
+	scheduleUnload( prioritySortCb = null, markAllUnused = true ) {
 
 		if ( ! this.scheduled ) {