Don't cache projects when loaded

Because the project ID doesn't indicate its contents, it should not be cached when it's loaded. Additionally the id should not be calculated from its contents.
This commit is contained in:
Ray Schamp 2017-05-11 02:01:12 -04:00
parent 15b6a16af3
commit 1af20ad4a1
3 changed files with 17 additions and 10 deletions

View file

@ -8,32 +8,38 @@ const DataFormat = require('./DataFormat');
* @property {string} name - The human-readable name of this asset type.
* @property {DataFormat} runtimeFormat - The format used for runtime, in-memory storage of this asset. For example, a
* project stored in SB2 format on disk will be returned as JSON when loaded into memory.
* @property {boolean} immutable - Indicates if the asset id is determined by the asset content.
*/
const AssetType = {
ImageBitmap: {
contentType: 'image/png',
name: 'ImageBitmap',
runtimeFormat: DataFormat.PNG
runtimeFormat: DataFormat.PNG,
immutable: true
},
ImageVector: {
contentType: 'image/svg+xml',
name: 'ImageVector',
runtimeFormat: DataFormat.SVG
runtimeFormat: DataFormat.SVG,
immutable: true
},
Project: {
contentType: 'application/json',
name: 'Project',
runtimeFormat: DataFormat.JSON
runtimeFormat: DataFormat.JSON,
immutable: false
},
Sound: {
contentType: 'audio/x-wav',
name: 'Sound',
runtimeFormat: DataFormat.WAV
runtimeFormat: DataFormat.WAV,
immutable: true
},
Sprite: {
contentType: 'application/json',
name: 'Sprite',
runtimeFormat: DataFormat.JSON
runtimeFormat: DataFormat.JSON,
immutable: true
}
};

View file

@ -9,7 +9,7 @@ const Helper = require('./Helper');
* @typedef {object} BuiltinAssetRecord
* @property {AssetType} type - The type of the asset.
* @property {DataFormat} format - The format of the asset's data.
* @property {string} id - The asset's unique ID.
* @property {?string} id - The asset's unique ID.
* @property {Buffer} data - The asset's data.
*/
@ -97,8 +97,8 @@ class BuiltinHelper extends Helper {
cache (assetType, dataFormat, data, id) {
if (!dataFormat) throw new Error('Data cached without specifying its format');
if (id) {
if (this.assets.hasOwnProperty(id)) return id;
} else {
if (this.assets.hasOwnProperty(id) && assetType.immutable) return id;
} else if (assetType.immutable) {
const hash = crypto.createHash('md5');
hash.update(data);
id = hash.digest('hex');

View file

@ -117,11 +117,12 @@ class ScratchStorage {
tryNextHelper();
} else {
// TODO? this.localHelper.cache(assetType, assetId, asset);
if (helper !== this.builtinHelper) {
if (helper !== this.builtinHelper && assetType.immutable) {
asset.assetId = this.builtinHelper.cache(
assetType,
asset.dataFormat,
asset.data
asset.data,
assetId
);
}
// Note that other attempts may have caused errors, effectively suppressed here.