Skip to content

Commit

Permalink
remove add/remove parsers
Browse files Browse the repository at this point in the history
  • Loading branch information
Zyie committed Jul 14, 2022
1 parent 84096a7 commit 063870f
Show file tree
Hide file tree
Showing 7 changed files with 58 additions and 151 deletions.
32 changes: 7 additions & 25 deletions packages/assets/src/cache/Cache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,38 +19,14 @@ import type { CacheParser } from './CacheParser';
*/
class CacheClass
{
/** All loader parsers registered */
public parsers: CacheParser[] = [];
private _parsers: CacheParser[] = [];

private readonly _cache: Map<string, any> = new Map();
private readonly _cacheMap: Map<string, {
keys: string[],
cacheKeys: string[],
}> = new Map();

/**
* Use this to add any parsers to the `cache.set` function to
* @param newParsers - An array of parsers to add to the cache or just a single one
*/
public addParser(...newParsers: CacheParser[]): void
{
this.parsers.push(...newParsers);
}

/**
* For exceptional situations where a cache parser might be causing some trouble,
* @param parsersToRemove - An array of parsers to remove from the cache, or just a single one
*/
public removeParser(...parsersToRemove: CacheParser[]): void
{
for (const parser of parsersToRemove)
{
const index = this.parsers.indexOf(parser);

if (index >= 0) this.parsers.splice(index, 1);
}
}

/** Clear all entries. */
public reset(): void
{
Expand Down Expand Up @@ -193,6 +169,12 @@ class CacheClass
this._cacheMap.delete(key);
});
}

/** All loader parsers registered */
public get parsers(): CacheParser[]
{
return this._parsers;
}
}

export const Cache = new CacheClass();
32 changes: 7 additions & 25 deletions packages/assets/src/loader/Loader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,7 @@ import type { PromiseAndParser, LoadAsset } from './types';
*/
export class Loader
{
/** All loader parsers registered */
public parsers: LoaderParser[] = [];
private _parsers: LoaderParser[] = [];

/** Cache loading promises that ae currently active */
public promiseCache: Record<string, PromiseAndParser> = {};
Expand All @@ -27,29 +26,6 @@ export class Loader
this.promiseCache = {};
}

/**
* Use this to add any parsers to the loadAssets function to use
* @param newParsers - An array of parsers to add to the loader, or just a single one
*/
public addParser(...newParsers: LoaderParser[]): void
{
this.parsers.push(...newParsers);
}

/**
* Use this to remove any parsers you've added or any of the default ones.
* @param parsersToRemove - An array of parsers to remove from the loader, or just a single one
*/
public removeParser(...parsersToRemove: LoaderParser[]): void
{
for (const parser of parsersToRemove)
{
const index = this.parsers.indexOf(parser);

if (index >= 0) this.parsers.splice(index, 1);
}
}

/**
* Used internally to generate a promise for the asset to be loaded.
* @param url - The URL to be loaded
Expand Down Expand Up @@ -216,4 +192,10 @@ export class Loader

await Promise.all(promises);
}

/** All loader parsers registered */
public get parsers(): LoaderParser[]
{
return this._parsers;
}
}
58 changes: 16 additions & 42 deletions packages/assets/src/resolver/Resolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@ export class Resolver
{
private _assetMap: Record<string, ResolveAsset[]> = {};
private _preferredOrder: PreferOrder[] = [];

private _parsers: ResolveURLParser[] = [];

private _resolverHash: Record<string, ResolveAsset> = {};
Expand Down Expand Up @@ -100,24 +99,11 @@ export class Resolver
return this._basePath;
}

public get parsers(): ResolveURLParser[]
{
return this._parsers;
}

/** Used for testing, this resets the resolver to its initial state */
public reset(): void
{
this._preferredOrder = [];

this._resolverHash = {};
this._assetMap = {};
this._basePath = null;
this._manifest = null;
}

/**
* A URL parser helps the parser to extract information and create an asset object-based on parsing the URL itself.
* All the active URL parsers that help the parser to extract information and create
* an asset object-based on parsing the URL itself.
*
* Can be added using the extensions API
* @example
* resolver.add('foo', [
* {
Expand All @@ -132,9 +118,9 @@ export class Resolver
* }
* ]);
*
*
* // with a url parser the information such as resolution and file format could extracted from the url itself:
* resolver.addUrlParser({
* extensions.add({
* extension: ExtensionType.ResolveParser,
* test: loadTextures.test, // test if url ends in an image
* parse: (value: string) =>
* ({
Expand All @@ -149,34 +135,22 @@ export class Resolver
* 'image@2x.png'
* 'image.png'
* ]);
* @param urlParsers - The URL parser that you want to add to the resolver
* @
*/
public addUrlParser(...urlParsers: ResolveURLParser[]): void
public get parsers(): ResolveURLParser[]
{
urlParsers.forEach((parser) =>
{
if (this._parsers.includes(parser)) return;

this._parsers.push(parser);
});
return this._parsers;
}

/**
* Remove a URL parser from the resolver
* @param urlParsers - the URL parser that you want to remove from the resolver
*/
public removeUrlParser(...urlParsers: ResolveURLParser[]): void
/** Used for testing, this resets the resolver to its initial state */
public reset(): void
{
for (let i = urlParsers.length - 1; i >= 0; i--)
{
const parser = urlParsers[i];
const index = this._parsers.indexOf(parser);
this._preferredOrder = [];

if (index !== -1)
{
this._parsers.splice(index, 1);
}
}
this._resolverHash = {};
this._assetMap = {};
this._basePath = null;
this._manifest = null;
}

/**
Expand Down
21 changes: 5 additions & 16 deletions packages/assets/test/cache.tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,23 +21,12 @@ describe('Cache', () =>
beforeEach(() =>
{
Cache.reset();
Cache.removeParser(testParser);
});

it('should add and remove a plugin', () =>
{
Cache.addParser(testParser);

expect(Cache.parsers).toHaveLength(3);

Cache.removeParser(testParser);

expect(Cache.parsers).toHaveLength(2);
Cache['_parsers'] = [];
});

it('should process a custom parsers correctly', () =>
{
Cache.addParser(testParser);
Cache['_parsers'].push(testParser);

Cache.set('test', 'hello');

Expand All @@ -48,7 +37,7 @@ describe('Cache', () =>

it('should process multiple keys with a custom parser correctly', () =>
{
Cache.addParser(testParser);
Cache['_parsers'].push(testParser);

Cache.set(['test', 'chicken'], 'hello');

Expand All @@ -63,7 +52,7 @@ describe('Cache', () =>

it('should remove keys with a custom parsers correctly', () =>
{
Cache.addParser(testParser);
Cache['_parsers'].push(testParser);

Cache.set('test', 'hello');

Expand All @@ -76,7 +65,7 @@ describe('Cache', () =>

it('should remove multiple keys with a custom parser correctly', () =>
{
Cache.addParser(testParser);
Cache['_parsers'].push(testParser);

Cache.set(['test', 'chicken'], 'hello');

Expand Down
6 changes: 3 additions & 3 deletions packages/assets/test/loader-compressed.tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ describe('Compressed Loader', () =>
{
const loader = new Loader();

loader.addParser(loadKTX);
loader['_parsers'].push(loadKTX);

// eslint-disable-next-line max-len
const texture: Texture = await loader.load(`https://pixijs.io/compressed-textures-example/images/PixiJS-Logo_PNG_BC3_KTX.KTX`);
Expand All @@ -22,7 +22,7 @@ describe('Compressed Loader', () =>
{
const loader = new Loader();

loader.addParser(loadDDS);
loader['_parsers'].push(loadDDS);

// eslint-disable-next-line max-len
const texture: Texture = await loader.load(`https://pixijs.io/compressed-textures-example/images/airplane-boeing_JPG_BC3_1.DDS`);
Expand All @@ -43,7 +43,7 @@ describe('Compressed Loader', () =>
// // console.log('DOING!!');
// const loader = new Loader();

// loader.addParser(loadBasis);
// loader['_parsers'].push(loadBasis);

// const texture: Texture = await loader.load(`https://pixijs.io/compressed-textures-example/images/kodim20.basis`);

Expand Down

0 comments on commit 063870f

Please sign in to comment.