fix: tell fetchWithTimeout to use scratchFetch from storage instance

This commit is contained in:
Christopher Willis-Ford 2023-03-28 13:20:58 -07:00
parent e696e3548b
commit 829ae1dfac
4 changed files with 30 additions and 5 deletions

View file

@ -18,6 +18,7 @@ const StageLayering = require('./stage-layering');
const Variable = require('./variable');
const xmlEscape = require('../util/xml-escape');
const ScratchLinkWebSocket = require('../util/scratch-link-websocket');
const fetchWithTimeout = require('../util/fetch-with-timeout');
// Virtual I/O devices.
const Clock = require('../io/clock');
@ -1627,6 +1628,7 @@ class Runtime extends EventEmitter {
*/
attachStorage (storage) {
this.storage = storage;
fetchWithTimeout.setFetch(storage.scratchFetch.scratchFetch);
this.resetRunId();
}

View file

@ -7,7 +7,7 @@ const Cast = require('../../util/cast');
const MathUtil = require('../../util/math-util');
const Clone = require('../../util/clone');
const log = require('../../util/log');
const fetchWithTimeout = require('../../util/fetch-with-timeout');
const {fetchWithTimeout} = require('../../util/fetch-with-timeout');
/**
* Icon svg to be displayed in the blocks category menu, encoded as a data URI.

View file

@ -2,7 +2,7 @@ const ArgumentType = require('../../extension-support/argument-type');
const BlockType = require('../../extension-support/block-type');
const Cast = require('../../util/cast');
const log = require('../../util/log');
const fetchWithTimeout = require('../../util/fetch-with-timeout');
const {fetchWithTimeout} = require('../../util/fetch-with-timeout');
const languageNames = require('scratch-translate-extension-languages');
const formatMessage = require('format-message');

View file

@ -1,4 +1,24 @@
const {scratchFetch} = require('scratch-storage/src/scratchFetch.js');
/**
* @callback FetchFunction
* @param {RequestInfo|URL} input
* @param {RequestInit|undefined} [init]
* @returns {Promise<Response>}
*/
/**
* @type {FetchFunction}
*/
let myFetch = global.fetch;
/**
* Tell `fetchWithTimeout` to use a specific `fetch` function.
* By default, `fetchWithTimeout` will use the global `fetch` function.
* If there is no global `fetch`, then `fetchWithTimeout` will fail unless provided with an alternative.
* @param {FetchFunction} newFetch The new `fetch` function to use within fetchWithTimeout.
*/
const setFetch = newFetch => {
myFetch = newFetch;
};
/**
* Fetch a remote resource like `fetch` does, but with a time limit.
@ -14,7 +34,7 @@ const fetchWithTimeout = (resource, init, timeout) => {
const signal = controller ? controller.signal : null;
// The fetch call races a timer.
return Promise.race([
scratchFetch(resource, Object.assign({signal}, init)).then(response => {
myFetch(resource, Object.assign({signal}, init)).then(response => {
clearTimeout(timeoutID);
return response;
}),
@ -27,4 +47,7 @@ const fetchWithTimeout = (resource, init, timeout) => {
]);
};
module.exports = fetchWithTimeout;
module.exports = {
fetchWithTimeout,
setFetch
};