Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 | 20x 564x 564x 564x 564x 564x 564x 564x 564x 564x 564x 564x 564x 12x 4x 564x 16x 564x 3x 2820x 61x 1730x 60x 8814x 11x 558x 558x 558x 558x 558x 558x 558x 1x 558x 558x 11x 11x 1x 1x 10x 10x 10x 6x 6x 4x 10x | import { readable, writable, type Writable, type Readable } from './facade.ts';
import { commands, selectors, type ImageFacts } from './store.ts';
import { MeasurableImage } from '../MeasurableImage.ts';
import type { Dimensions, Weight } from '../MeasurableImage.ts';
/** Own per-image measurements, source tracking and the weight cache outside reducer state. */
export class MeasurableImageStore {
readonly fileSize: Writable< Dimensions >;
readonly fileWeight: Writable< Weight >;
readonly sizeOnPage: Writable< Dimensions >;
readonly potentialSavings: Readable< number | null >;
readonly expectedSize: Readable< Dimensions >;
readonly oversizedRatio: Readable< number >;
readonly url: Writable< string >;
readonly loading: Writable< boolean >;
readonly id: string;
private static nextId = 0;
readonly image: MeasurableImage;
readonly node: MeasurableImage[ 'node' ];
private weightMap: Record< string, number > = {};
private currentSrc = '';
constructor( measurableImage: MeasurableImage ) {
this.image = measurableImage;
this.node = measurableImage.node;
this.id = String( MeasurableImageStore.nextId++ );
commands.setImage( this.id, {
fileSize: { width: 0, height: 0 },
sizeOnPage: { width: 0, height: 0 },
fileWeight: { weight: -1 },
url: measurableImage.getURL(),
loading: true,
} );
this.url = this.fact( 'url' );
this.fileSize = this.fact( 'fileSize' );
this.sizeOnPage = this.fact( 'sizeOnPage' );
this.loading = this.fact( 'loading' );
// Facade weight and savings subscribers activate fetching; analytics snapshots do not.
this.fileWeight = this.fact( 'fileWeight', () => this.activate() );
this.potentialSavings = readable(
() => selectors.getPotentialSavings( this.id ),
() => this.fileWeight.subscribe( () => {} ),
undefined,
this.id
);
this.oversizedRatio = readable(
() => selectors.getOversizedRatio( this.id ),
undefined,
undefined,
this.id
);
this.expectedSize = readable(
() => selectors.getExpectedSize( this.id ),
undefined,
undefined,
this.id
);
}
private fact< K extends keyof ImageFacts >( key: K, start?: () => void ) {
return writable(
() => selectors.getImageFacts( this.id )[ key ],
value => commands.updateImage( this.id, { [ key ]: value } ),
start,
() => selectors.getImageRevision( this.id, key ),
this.id
);
}
/** Read current image facts and derived measurements without activating fetching. */
public getSnapshot() {
return {
...selectors.getImageFacts( this.id ),
expectedSize: selectors.getExpectedSize( this.id ),
oversizedRatio: selectors.getOversizedRatio( this.id ),
potentialSavings: selectors.getPotentialSavings( this.id ),
};
}
/** Fetch the current source's weight when a measurement consumer becomes active. */
public activate() {
this.maybeUpdateWeight();
}
public async updateDimensions() {
const sizeOnPage = this.image.getSizeOnPage();
this.sizeOnPage.set( sizeOnPage );
await this.updateFileDimensions();
}
private async updateFileDimensions() {
// The source can change on resize; subsequent weight activation must use the new URL.
Iif ( this.image.getURL() === this.currentSrc ) {
return;
}
this.currentSrc = this.image.getURL();
let fileSize;
try {
fileSize = await this.image.getFileSize( this.currentSrc );
} catch {
fileSize = {
height: -1,
width: -1,
};
}
this.url.set( this.currentSrc );
this.fileSize.set( fileSize );
}
private async maybeUpdateWeight() {
const url = this.currentSrc;
if ( this.weightMap[ url ] !== undefined ) {
this.fileWeight.set( { weight: this.weightMap[ url ] } );
return;
}
this.loading.set( true );
try {
const weight = await this.image.getWeight( url );
this.weightMap[ url ] = weight;
this.fileWeight.set( { weight } );
} catch {
this.fileWeight.set( { weight: -1 } );
}
this.loading.set( false );
}
}
|