Skip to content

Commit

Permalink
optimize weak cache code
Browse files Browse the repository at this point in the history
  • Loading branch information
SantyWang committed Aug 16, 2021
1 parent 0a1356a commit 8f359c4
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 16 deletions.
6 changes: 3 additions & 3 deletions cocos/core/asset-manager/asset-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -161,7 +161,7 @@ export class AssetManager {
* 已加载 bundle 的集合, 你能通过 {{#crossLink "AssetManager/removeBundle:method"}}{{/crossLink}} 来移除缓存
*
*/
public bundles: Cache<Bundle> = bundles;
public bundles: ICache<Bundle> = bundles;

/**
* @en
Expand All @@ -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 = '';
Expand Down
14 changes: 13 additions & 1 deletion cocos/core/asset-manager/cache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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;

Expand Down
15 changes: 12 additions & 3 deletions cocos/core/asset-manager/deprecated.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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;
}
}

/**
Expand Down
3 changes: 0 additions & 3 deletions cocos/core/asset-manager/pack-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down
2 changes: 0 additions & 2 deletions cocos/core/asset-manager/release-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand Down
7 changes: 3 additions & 4 deletions cocos/core/asset-manager/weak-cache.ts
Original file line number Diff line number Diff line change
@@ -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]);
Expand Down

0 comments on commit 8f359c4

Please sign in to comment.