Comparing version 1.2.9 to 1.2.10
@@ -40,16 +40,12 @@ export interface Options { | ||
readonly id: ID; | ||
/** The full size of the resource */ | ||
size: number; | ||
protected _size: number; | ||
protected readonly options: Options; | ||
/** Regions used to reduce unneeded allocations. Think of sparse arrays. */ | ||
readonly regions: Region[]; | ||
/** The full size of the resource */ | ||
get size(): number; | ||
set size(value: number); | ||
constructor( | ||
/** The resource ID */ | ||
id: ID, | ||
/** The full size of the resource */ | ||
size: number, options: Options, resources?: Map<ID, Resource<ID> | undefined>); | ||
/** | ||
* Ensure the full size of the resource is *at least* `newSize` | ||
*/ | ||
grow(newSize: number): void; | ||
id: ID, _size: number, options: Options, resources?: Map<ID, Resource<ID> | undefined>); | ||
/** Combines adjacent regions and combines adjacent ranges within a region */ | ||
@@ -56,0 +52,0 @@ collect(): void; |
@@ -9,25 +9,47 @@ /** A ranged cache */ | ||
id; | ||
size; | ||
_size; | ||
options; | ||
/** Regions used to reduce unneeded allocations. Think of sparse arrays. */ | ||
regions = []; | ||
/** The full size of the resource */ | ||
get size() { | ||
return this._size; | ||
} | ||
set size(value) { | ||
if (value >= this._size) { | ||
this._size = value; | ||
return; | ||
} | ||
this._size = value; | ||
for (let i = this.regions.length - 1; i >= 0; i--) { | ||
const region = this.regions[i]; | ||
if (region.offset >= value) { | ||
this.regions.splice(i, 1); | ||
continue; | ||
} | ||
const maxLength = value - region.offset; | ||
if (region.data.byteLength > maxLength) { | ||
region.data = region.data.subarray(0, maxLength); | ||
} | ||
region.ranges = region.ranges | ||
.filter(range => range.start < value) | ||
.map(range => { | ||
if (range.end > value) { | ||
return { start: range.start, end: value }; | ||
} | ||
return range; | ||
}); | ||
} | ||
} | ||
constructor( | ||
/** The resource ID */ | ||
id, | ||
/** The full size of the resource */ | ||
size, options, resources) { | ||
id, _size, options, resources) { | ||
this.id = id; | ||
this.size = size; | ||
this._size = _size; | ||
this.options = options; | ||
options.sparse ??= true; | ||
if (!options.sparse) | ||
this.regions.push({ offset: 0, data: new Uint8Array(size), ranges: [] }); | ||
this.regions.push({ offset: 0, data: new Uint8Array(_size), ranges: [] }); | ||
resources?.set(id, this); | ||
} | ||
/** | ||
* Ensure the full size of the resource is *at least* `newSize` | ||
*/ | ||
grow(newSize) { | ||
this.size = Math.max(this.size, newSize); | ||
} | ||
/** Combines adjacent regions and combines adjacent ranges within a region */ | ||
@@ -147,2 +169,3 @@ collect() { | ||
region.ranges.sort((a, b) => a.start - b.start); | ||
this.collect(); | ||
return this; | ||
@@ -160,4 +183,5 @@ } | ||
} | ||
this.collect(); | ||
return this; | ||
} | ||
} |
@@ -119,3 +119,3 @@ /* Utilities for `fetch` when using range requests. It also allows you to handle errors easier */ | ||
await _fetch(new Request(url, init), { method: 'POST' }, true); | ||
resource.add(data, offset).collect(); | ||
resource.add(data, offset); | ||
} | ||
@@ -122,0 +122,0 @@ /** |
{ | ||
"name": "utilium", | ||
"version": "1.2.9", | ||
"version": "1.2.10", | ||
"description": "Typescript utilities", | ||
@@ -5,0 +5,0 @@ "funding": { |
@@ -48,7 +48,43 @@ /** A ranged cache */ | ||
/** The full size of the resource */ | ||
public get size() { | ||
return this._size; | ||
} | ||
public set size(value: number) { | ||
if (value >= this._size) { | ||
this._size = value; | ||
return; | ||
} | ||
this._size = value; | ||
for (let i = this.regions.length - 1; i >= 0; i--) { | ||
const region = this.regions[i]; | ||
if (region.offset >= value) { | ||
this.regions.splice(i, 1); | ||
continue; | ||
} | ||
const maxLength = value - region.offset; | ||
if (region.data.byteLength > maxLength) { | ||
region.data = region.data.subarray(0, maxLength); | ||
} | ||
region.ranges = region.ranges | ||
.filter(range => range.start < value) | ||
.map(range => { | ||
if (range.end > value) { | ||
return { start: range.start, end: value }; | ||
} | ||
return range; | ||
}); | ||
} | ||
} | ||
public constructor( | ||
/** The resource ID */ | ||
public readonly id: ID, | ||
/** The full size of the resource */ | ||
public size: number, | ||
protected _size: number, | ||
protected readonly options: Options, | ||
@@ -58,3 +94,3 @@ resources?: Map<ID, Resource<ID> | undefined> | ||
options.sparse ??= true; | ||
if (!options.sparse) this.regions.push({ offset: 0, data: new Uint8Array(size), ranges: [] }); | ||
if (!options.sparse) this.regions.push({ offset: 0, data: new Uint8Array(_size), ranges: [] }); | ||
@@ -64,9 +100,2 @@ resources?.set(id, this); | ||
/** | ||
* Ensure the full size of the resource is *at least* `newSize` | ||
*/ | ||
public grow(newSize: number) { | ||
this.size = Math.max(this.size, newSize); | ||
} | ||
/** Combines adjacent regions and combines adjacent ranges within a region */ | ||
@@ -197,2 +226,3 @@ public collect(): void { | ||
this.collect(); | ||
return this; | ||
@@ -212,4 +242,5 @@ } | ||
this.collect(); | ||
return this; | ||
} | ||
} |
@@ -204,3 +204,3 @@ /* Utilities for `fetch` when using range requests. It also allows you to handle errors easier */ | ||
resource.add(data, offset).collect(); | ||
resource.add(data, offset); | ||
} | ||
@@ -207,0 +207,0 @@ |
130295
3711