mirror of
https://github.com/cocos/cocos4.git
synced 2026-11-19 14:52:40 +08:00
optimize weak cache code (#9192)
This commit is contained in:
@@ -33,7 +33,7 @@ import { error } from '../platform/debug';
|
||||
import { sys } from '../platform/sys';
|
||||
import { basename, extname } from '../utils/path';
|
||||
import Bundle from './bundle';
|
||||
import Cache from './cache';
|
||||
import Cache, { ICache } from './cache';
|
||||
import CacheManager from './cache-manager';
|
||||
import dependUtil from './depend-util';
|
||||
import downloader from './downloader';
|
||||
@@ -161,7 +161,7 @@ export class AssetManager {
|
||||
* 已加载 bundle 的集合, 你能通过 {{#crossLink "AssetManager/removeBundle:method"}}{{/crossLink}} 来移除缓存
|
||||
*
|
||||
*/
|
||||
public bundles: Cache<Bundle> = bundles;
|
||||
public bundles: ICache<Bundle> = bundles;
|
||||
|
||||
/**
|
||||
* @en
|
||||
@@ -170,7 +170,7 @@ export class AssetManager {
|
||||
* @zh
|
||||
* 已加载资源的集合, 你能通过 {{#crossLink "AssetManager/releaseAsset:method"}}{{/crossLink}} 来移除缓存
|
||||
*/
|
||||
public assets: Cache<Asset> = assets;
|
||||
public assets: ICache<Asset> = assets;
|
||||
|
||||
public generalImportBase = '';
|
||||
public generalNativeBase = '';
|
||||
|
||||
@@ -28,6 +28,18 @@
|
||||
*/
|
||||
import { js } from '../utils/js';
|
||||
|
||||
export interface ICache<T> {
|
||||
add (key: string, val: T): T;
|
||||
get (key: string): T | undefined | null;
|
||||
has (key: string): boolean;
|
||||
remove (key: string): T | undefined | null;
|
||||
clear (): void;
|
||||
forEach (func: (val: T, key: string) => void): void;
|
||||
find (predicate: (val: T, key: string) => boolean): T | null;
|
||||
readonly count: number;
|
||||
destroy (): void;
|
||||
}
|
||||
|
||||
/**
|
||||
* @en
|
||||
* use to cache something
|
||||
@@ -36,7 +48,7 @@ import { js } from '../utils/js';
|
||||
* 用于缓存某些内容
|
||||
*
|
||||
*/
|
||||
export default class Cache<T = any> {
|
||||
export default class Cache<T = any> implements ICache<T> {
|
||||
protected _map: Record<string, T> | null = null;
|
||||
protected _count = 0;
|
||||
|
||||
|
||||
@@ -34,6 +34,7 @@ import { legacyCC } from '../global-exports';
|
||||
import { getError, warn, warnID } from '../platform/debug';
|
||||
import { macro } from '../platform/macro';
|
||||
import { path, removeProperty, replaceProperty } from '../utils';
|
||||
import Cache from './cache';
|
||||
import assetManager, { AssetManager } from './asset-manager';
|
||||
import { resources } from './bundle';
|
||||
import dependUtil from './depend-util';
|
||||
@@ -108,9 +109,17 @@ export class CCLoader {
|
||||
public _autoReleaseSetting: Record<string, boolean> = Object.create(null);
|
||||
private _parseLoadResArgs = parseLoadResArgs;
|
||||
|
||||
public get _cache () {
|
||||
// @ts-expect-error return private property
|
||||
return assets._map;
|
||||
public get _cache (): Record<string, Asset> {
|
||||
if (assets instanceof Cache) {
|
||||
// @ts-expect-error return private property
|
||||
return assets._map;
|
||||
} else {
|
||||
const map = {};
|
||||
assets.forEach((val, key) => {
|
||||
map[key] = val;
|
||||
});
|
||||
return map;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -28,10 +28,7 @@
|
||||
*/
|
||||
|
||||
import { ImageAsset, Texture2D } from '../assets';
|
||||
import MissingScript from '../components/missing-script';
|
||||
import { packCustomObjData, unpackJSONs } from '../data/deserialize';
|
||||
// import MissingScript from '../components/missing-script';
|
||||
// import { packCustomObjData, unpackJSONs } from '../data/deserialize-compiled';
|
||||
import { error, errorID } from '../platform/debug';
|
||||
import { js } from '../utils/js';
|
||||
import Cache from './cache';
|
||||
|
||||
@@ -33,8 +33,6 @@ import { Node, Scene } from '../scene-graph';
|
||||
import Cache from './cache';
|
||||
import dependUtil from './depend-util';
|
||||
import { assets, references } from './shared';
|
||||
import { ImageAsset } from '../assets/image-asset';
|
||||
import { TextureBase } from '../assets/texture-base';
|
||||
import { callInNextTick } from '../utils/misc';
|
||||
import { js } from '../utils/js';
|
||||
|
||||
|
||||
@@ -1,17 +1,16 @@
|
||||
import { js } from '../utils/js';
|
||||
import Cache from './cache';
|
||||
import { ICache } from './cache';
|
||||
|
||||
declare class WeakRef<T> {
|
||||
constructor (obj: T);
|
||||
deref (): T | null;
|
||||
}
|
||||
|
||||
export default class WeakCache<T> extends Cache<T> {
|
||||
export default class WeakCache<T> implements ICache<T> {
|
||||
protected _weakMap: Record<string, WeakRef<T>> = {};
|
||||
|
||||
constructor (map?: Record<string, T>) {
|
||||
super();
|
||||
this._map = null;
|
||||
if (typeof window.WeakRef === 'undefined') throw new Error('this platform does not support WeakRef!');
|
||||
if (map) {
|
||||
for (const key in map) {
|
||||
this._weakMap[key] = new WeakRef(map[key]);
|
||||
|
||||
Reference in New Issue
Block a user