Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: buffer not released when modified #29

Merged
merged 7 commits into from
May 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 11 additions & 44 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,50 +4,17 @@
"license": "MIT",
"type": "module",
"exports": {
".": {
"import": "./dist/core/index.js",
"types": "./dist/core/index.d.ts"
},
"./cache-buster": {
"import": "./dist/cache-buster/index.js",
"types": "./dist/cache-buster/index.d.ts"
},
"./cli": {
"import": "./dist/cli/index.js",
"types": "./dist/cli/index.d.ts"
},
"./ffmpeg": {
"import": "./dist/ffmpeg/index.js",
"types": "./dist/ffmpeg/index.d.ts"
},
"./image": {
"import": "./dist/image/index.js",
"types": "./dist/image/index.d.ts"
},
"./json": {
"import": "./dist/json/index.js",
"types": "./dist/json/index.d.ts"
},
"./manifest": {
"import": "./dist/manifest/index.js",
"types": "./dist/manifest/index.d.ts"
},
"./pixi": {
"import": "./dist/pixi/index.js",
"types": "./dist/pixi/index.d.ts"
},
"./spine": {
"import": "./dist/spine/index.js",
"types": "./dist/spine/index.d.ts"
},
"./texture-packer": {
"import": "./dist/texture-packer/index.js",
"types": "./dist/texture-packer/index.d.ts"
},
"./webfont": {
"import": "./dist/webfont/index.js",
"types": "./dist/webfont/index.d.ts"
}
".": "./dist/core/index.js",
"./cache-buster": "./dist/cache-buster/index.js",
"./cli": "./dist/cli/index.js",
"./ffmpeg": "./dist/ffmpeg/index.js",
"./image": "./dist/image/index.js",
"./json": "./dist/json/index.js",
"./manifest": "./dist/manifest/index.js",
"./pixi": "./dist/pixi/index.js",
"./spine": "./dist/spine/index.js",
"./texture-packer": "./dist/texture-packer/index.js",
"./webfont": "./dist/webfont/index.js"
},
"main": "dist/core/index.js",
"module": "dist/core/index.js",
Expand Down
12 changes: 12 additions & 0 deletions src/core/Asset.ts
Original file line number Diff line number Diff line change
Expand Up @@ -237,5 +237,17 @@ export class Asset
this.transformChildren[i].releaseBuffers();
}
}

/**
* Release all buffers from this asset and its children
*/
releaseChildrenBuffers()
{
this.releaseBuffers();
for (let i = 0; i < this.children.length; i++)
{
this.children[i].releaseChildrenBuffers();
}
}
}

3 changes: 3 additions & 0 deletions src/core/AssetPack.ts
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,9 @@ export class AssetPack
// write back to the cache...
await (assetCache as AssetCache).write(root);

// release the buffers from the cache
root.releaseChildrenBuffers();

Logger.info('cache updated.');
}
}
Expand Down
5 changes: 1 addition & 4 deletions src/core/pipes/PipeSystem.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,6 @@ export class PipeSystem
async transform(asset: Asset): Promise<void>
{
await this._transform(asset, 0);

// clean up any buffers still held for gc!
asset.releaseBuffers();
}

private async _transform(asset: Asset, pipeIndex = 0): Promise<void>
Expand All @@ -77,7 +74,7 @@ export class PipeSystem

const options = mergePipeOptions(pipe, asset);

if (pipe.transform && pipe.test && pipe.test(asset, options))
if (options !== false && pipe.transform && pipe.test?.(asset, options))
{
asset.transformName = pipe.name;
asset.transformChildren = [];
Expand Down
8 changes: 6 additions & 2 deletions src/core/pipes/mergePipeOptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,13 @@ import { merge } from '../utils/merge.js';
import type { Asset } from '../Asset.js';
import type { AssetPipe, PluginOptions } from './AssetPipe.js';

export function mergePipeOptions<T extends PluginOptions<any>>(pipe: AssetPipe<T>, asset: Asset): T
export function mergePipeOptions<T extends PluginOptions<any>>(pipe: AssetPipe<T>, asset: Asset): T | false
{
if (!asset.settings) return pipe.defaultOptions;

return merge.recursive(pipe.defaultOptions, asset.settings);
const pipeSettings = asset.settings[pipe.name];

if (pipeSettings === false) return false;

return merge.recursive(pipe.defaultOptions, pipeSettings ?? {});
}
4 changes: 2 additions & 2 deletions src/core/pipes/multiPipe.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ export function multiPipe(options: MultiPipeOptions): AssetPipe<MultiPipeOptions

const options = mergePipeOptions(pipe, asset);

if (pipe.transform && pipe.test && pipe.test(asset, options))
if (options !== false && pipe.transform && pipe.test?.(asset, options))
{
return true;
}
Expand All @@ -46,7 +46,7 @@ export function multiPipe(options: MultiPipeOptions): AssetPipe<MultiPipeOptions

const options = mergePipeOptions(pipe, asset);

if (pipe.transform && pipe.test && pipe.test(asset, options))
if (options !== false && pipe.transform && pipe.test?.(asset, options))
{
promises.push(pipe.transform(asset, options, pipeSystem));
}
Expand Down
4 changes: 3 additions & 1 deletion test/core/Assetpack.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ describe('Core', () =>
const assetpack = new AssetPack({
entry: inputDir, cacheLocation: getCacheDir(pkg, testName),
output: outputDir,
cache: false,
cache: true,
});

await assetpack.watch();
Expand All @@ -109,6 +109,7 @@ describe('Core', () =>

expect(existsSync(join(outputDir, 'new-json-file.json'))).toBe(true);
expect(existsSync(join(outputDir, 'json.json'))).toBe(true);
fs.writeJSONSync(join(inputDir, 'json.json'), { nice: 'test' });

fs.removeSync(testFile);

Expand All @@ -121,6 +122,7 @@ describe('Core', () =>

expect(existsSync(join(outputDir, 'new-json-file.json'))).toBe(false);
expect(existsSync(join(outputDir, 'json.json'))).toBe(true);
expect(fs.readJSONSync(join(outputDir, 'json.json'))).toStrictEqual({ nice: 'test' });
});

it('should ignore specified files when watching', async () =>
Expand Down
83 changes: 78 additions & 5 deletions test/core/Utils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,16 +63,16 @@ describe('Utils', () =>
});

let counter = 0;
let assetTemp;
let optionsTemp;
const plugin = createAssetPipe({
folder: true,
test: ((asset: Asset, _options: any) =>
test: ((asset: Asset, options: any) =>
{
counter++;
if (counter === 1) return false;

expect(asset.allMetaData).toEqual({
override: [1, 2]
});
assetTemp = asset;
optionsTemp = options;

return true;
}) as any,
Expand All @@ -91,6 +91,13 @@ describe('Utils', () =>
assetSettings: [
{
files: ['**'],
settings: {
test: {
tags: {
test: 'override',
}
}
},
metaData: {
override: [1, 2]
},
Expand All @@ -99,6 +106,72 @@ describe('Utils', () =>
});

await assetpack.run();

expect(optionsTemp).toEqual({
tags: {
test: 'override',
}
});
expect(assetTemp!.allMetaData).toEqual({
override: [1, 2]
});
});

it('should allow for plugin to be turned off', async () =>
{
const testName = 'plugin-off';
const inputDir = getInputDir(pkg, testName);
const outputDir = getOutputDir(pkg, testName);

createFolder(
pkg,
{
name: testName,
files: [],
folders: [
{
name: 'anything',
files: [],
folders: [],
},
],
});

let counter = 0;
const plugin = createAssetPipe({
folder: true,
test: (() =>
{
counter++;

return true;
}) as any,
start: true,
finish: true,
transform: true,
}) as AssetPipe<any>;

const assetpack = new AssetPack({
entry: inputDir, cacheLocation: getCacheDir(pkg, testName),
output: outputDir,
pipes: [
plugin// as Plugin<any>
],
cache: false,
assetSettings: [
{
files: ['**'],
settings: {
test: false
}
},
]
});

await assetpack.run();

// it equals 1 because the first time it is called it is called on the input path, not the asset
expect(counter).toEqual(1);
});

it('should create a unique cache name', async () =>
Expand Down
Loading