mirror of
https://github.com/scratchfoundation/scratch-vm.git
synced 2024-12-23 14:32:59 -05:00
fix: tell fetchWithTimeout to use scratchFetch from storage instance
This commit is contained in:
parent
e696e3548b
commit
829ae1dfac
4 changed files with 30 additions and 5 deletions
|
@ -18,6 +18,7 @@ const StageLayering = require('./stage-layering');
|
||||||
const Variable = require('./variable');
|
const Variable = require('./variable');
|
||||||
const xmlEscape = require('../util/xml-escape');
|
const xmlEscape = require('../util/xml-escape');
|
||||||
const ScratchLinkWebSocket = require('../util/scratch-link-websocket');
|
const ScratchLinkWebSocket = require('../util/scratch-link-websocket');
|
||||||
|
const fetchWithTimeout = require('../util/fetch-with-timeout');
|
||||||
|
|
||||||
// Virtual I/O devices.
|
// Virtual I/O devices.
|
||||||
const Clock = require('../io/clock');
|
const Clock = require('../io/clock');
|
||||||
|
@ -1627,6 +1628,7 @@ class Runtime extends EventEmitter {
|
||||||
*/
|
*/
|
||||||
attachStorage (storage) {
|
attachStorage (storage) {
|
||||||
this.storage = storage;
|
this.storage = storage;
|
||||||
|
fetchWithTimeout.setFetch(storage.scratchFetch.scratchFetch);
|
||||||
this.resetRunId();
|
this.resetRunId();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -7,7 +7,7 @@ const Cast = require('../../util/cast');
|
||||||
const MathUtil = require('../../util/math-util');
|
const MathUtil = require('../../util/math-util');
|
||||||
const Clone = require('../../util/clone');
|
const Clone = require('../../util/clone');
|
||||||
const log = require('../../util/log');
|
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.
|
* Icon svg to be displayed in the blocks category menu, encoded as a data URI.
|
||||||
|
|
|
@ -2,7 +2,7 @@ const ArgumentType = require('../../extension-support/argument-type');
|
||||||
const BlockType = require('../../extension-support/block-type');
|
const BlockType = require('../../extension-support/block-type');
|
||||||
const Cast = require('../../util/cast');
|
const Cast = require('../../util/cast');
|
||||||
const log = require('../../util/log');
|
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 languageNames = require('scratch-translate-extension-languages');
|
||||||
const formatMessage = require('format-message');
|
const formatMessage = require('format-message');
|
||||||
|
|
||||||
|
|
|
@ -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.
|
* 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;
|
const signal = controller ? controller.signal : null;
|
||||||
// The fetch call races a timer.
|
// The fetch call races a timer.
|
||||||
return Promise.race([
|
return Promise.race([
|
||||||
scratchFetch(resource, Object.assign({signal}, init)).then(response => {
|
myFetch(resource, Object.assign({signal}, init)).then(response => {
|
||||||
clearTimeout(timeoutID);
|
clearTimeout(timeoutID);
|
||||||
return response;
|
return response;
|
||||||
}),
|
}),
|
||||||
|
@ -27,4 +47,7 @@ const fetchWithTimeout = (resource, init, timeout) => {
|
||||||
]);
|
]);
|
||||||
};
|
};
|
||||||
|
|
||||||
module.exports = fetchWithTimeout;
|
module.exports = {
|
||||||
|
fetchWithTimeout,
|
||||||
|
setFetch
|
||||||
|
};
|
||||||
|
|
Loading…
Reference in a new issue