Skip to content

Commit

Permalink
fix: cache not being applied correctly
Browse files Browse the repository at this point in the history
  • Loading branch information
Zyie committed Oct 22, 2024
1 parent f293a15 commit a35ecce
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 23 deletions.
21 changes: 21 additions & 0 deletions packages/assetpack/src/core/Asset.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import { extractTagsFromFileName } from './utils/extractTagsFromFileName.js';
import { getHash } from './utils/getHash.js';
import { path } from './utils/path.js';

import type { CachedAsset } from './AssetCache.js';

export interface AssetOptions
{
path: string;
Expand Down Expand Up @@ -288,5 +290,24 @@ export class Asset

return res;
}

toCacheData(saveHash: boolean): CachedAsset
{
const data: CachedAsset = {
isFolder: this.isFolder,
parent: this.parent?.path,
transformParent: this.transformParent?.path,
metaData: { ...this.metaData },
inheritedMetaData: { ...this.inheritedMetaData },
transformData: { ...this.transformData }
};

if (!this.isFolder && saveHash)
{
data.hash = this.hash;
}

return data;
}
}

14 changes: 2 additions & 12 deletions packages/assetpack/src/core/AssetCache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,18 +51,7 @@ export class AssetCache

private _serializeAsset(asset: Asset, schema: AssetCacheData['assets'], saveHash = false)
{
const serializeAsset: CachedAsset = {
isFolder: asset.isFolder,
parent: asset.parent?.path,
transformParent: asset.transformParent?.path,
metaData: asset.metaData,
transformData: asset.transformData
};

if (!asset.isFolder && saveHash)
{
serializeAsset.hash = asset.hash;
}
const serializeAsset: CachedAsset = asset.toCacheData(saveHash);

schema[asset.path] = serializeAsset;

Expand All @@ -85,6 +74,7 @@ export interface CachedAsset
hash?: string;
parent: string | undefined;
metaData: Record<string, any>;
inheritedMetaData: Record<string, any>;
transformData: Record<string, any>;
transformParent: string | undefined;
}
Expand Down
2 changes: 1 addition & 1 deletion packages/assetpack/src/core/AssetPack.ts
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ export class AssetPack
if (cache)
{
// write back to the cache...
await (assetCache as AssetCache).write(root);
(assetCache as AssetCache).write(root);

// release the buffers from the cache
root.releaseChildrenBuffers();
Expand Down
2 changes: 1 addition & 1 deletion packages/assetpack/src/core/AssetWatcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export interface AssetWatcherOptions
assetSettingsData?: AssetSettings[];
ignore?: string | string[];
onUpdate: (root: Asset) => Promise<void>;
onComplete: (root: Asset) => Promise<void>;
onComplete: (root: Asset) => void;
}

interface ChangeData
Expand Down
20 changes: 12 additions & 8 deletions packages/assetpack/src/core/utils/syncAssetsWithCache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ function syncAssetsFromCache(assetHash: Record<string, Asset>, cachedData: Recor
});

assetToDelete.metaData = cachedAsset.metaData;
assetToDelete.transformData = cachedAsset.transformData;
assetToDelete.inheritedMetaData = cachedAsset.inheritedMetaData;

assetToDelete.state = 'deleted';

Expand Down Expand Up @@ -87,22 +89,24 @@ function syncTransformedAssetsFromCache(assetHash: Record<string, Asset>, cached
for (const i in cachedData)
{
const cachedAssetData = cachedData[i];
let asset = assetHash[i];

if (cachedAssetData.transformParent)
if (!asset)
{
const transformedAsset = new Asset({
asset = new Asset({
path: i,
isFolder: cachedAssetData.isFolder
});

transformedAsset.metaData = cachedAssetData.metaData;
transformedAsset.transformData = cachedAssetData.transformData;
transformedAssets[i] = asset;
assetHash[i] = asset;

transformedAssets[i] = transformedAsset;
assetHash[i] = transformedAsset;

transformedAsset.transformParent = assetHash[cachedAssetData.transformParent];
asset.transformParent = assetHash[cachedAssetData.transformParent!];
}

asset.inheritedMetaData = cachedAssetData.inheritedMetaData;
asset.transformData = cachedAssetData.transformData;
asset.metaData = cachedAssetData.metaData;
}

for (const i in transformedAssets)
Expand Down
4 changes: 3 additions & 1 deletion packages/assetpack/test/core/AssetCache.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,16 +37,18 @@ describe('AssetCache', () =>

expect(cachedAssetData).toEqual({
test: {
inheritedMetaData: {},
isFolder: true,
metaData: {},
transformData: {},
},
'test/test.json': {
isFolder: false,
hash: '12345',
inheritedMetaData: {},
parent: 'test',
transformData: {},
metaData: {}
metaData: {},
}
});
});
Expand Down

0 comments on commit a35ecce

Please sign in to comment.