Skip to content

Commit 8a01c89

Browse files
author
Christopher Willis-Ford
committed
feat: create addHelper to add helper w/ priority
The list of helpers in `ScratchStorage` is no longer fixed: now it's possible to add a new helper using the `addHelper` method. Helpers are sorted by their relative priority values, which are provided at add time. As a follow-up task, `WebHelper` should probably be adjusted to represent only one endpoint, with create/update/delete features for that one endpoint. A normal Scratch session might then have one `WebHelper` for projects and a second for images and sounds. The main reason to do this would be to concentrate the "try the next place" logic in `ScratchStorage` instead of having a second version of it in `WebHelper`.
1 parent 9ec8c38 commit 8a01c89

File tree

1 file changed

+24
-1
lines changed

1 file changed

+24
-1
lines changed

src/ScratchStorage.js

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,17 @@ class ScratchStorage {
1414
this.builtinHelper = new BuiltinHelper(this);
1515
this.webHelper = new WebHelper(this);
1616
this.builtinHelper.registerDefaultAssets(this);
17+
18+
this._helpers = [
19+
{
20+
helper: this.builtinHelper,
21+
priority: 100
22+
},
23+
{
24+
helper: this.webHelper,
25+
priority: -100
26+
}
27+
];
1728
}
1829

1930
/**
@@ -58,6 +69,18 @@ class ScratchStorage {
5869
return _AssetType;
5970
}
6071

72+
/**
73+
* Add a storage helper to this manager. Helpers with a higher priority number will be checked first when loading
74+
* or storing assets. For comparison, the helper for built-in assets has `priority=100` and the default web helper
75+
* has `priority=-100`. The relative order of helpers with equal priorities is undefined.
76+
* @param {Helper} helper - the helper to be added.
77+
* @param {number} [priority] - the priority for this new helper (default: 0).
78+
*/
79+
addHelper (helper, priority = 0) {
80+
this._helpers.push({helper, priority});
81+
this._helpers.sort((a, b) => b.priority - a.priority);
82+
}
83+
6184
/**
6285
* Synchronously fetch a cached asset from built-in storage. Assets are cached when they are loaded.
6386
* @param {string} assetId - The id of the asset to fetch.
@@ -152,7 +175,7 @@ class ScratchStorage {
152175
*/
153176
load (assetType, assetId, dataFormat) {
154177
/** @type {Helper[]} */
155-
const helpers = [this.builtinHelper, this.webHelper];
178+
const helpers = this._helpers.map(x => x.helper);
156179
const errors = [];
157180
let helperIndex = 0;
158181
dataFormat = dataFormat || assetType.runtimeFormat;

0 commit comments

Comments
 (0)