/* This Source Code Form is subject to the terms of the Mozilla Public *License,v.2.0.IfacopyoftheMPLwasnotdistributedwiththis
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
// If we don't stub these functions out, React throws warnings in the console // upon being loaded.
let window = self;
window.requestAnimationFrame = () => {};
window.cancelAnimationFrame = () => {};
// ChromeUtils is defined inside of a Worker, but we don't want the // activity-stream.bundle.js to detect it when loading, since that results // in it attempting to import JSMs on load, which is not allowed in // a Worker. So we temporarily clear ChromeUtils so that activity-stream.bundle.js // thinks its being loaded in content scope. // // eslint-disable-next-line no-implicit-globals, no-global-assign
ChromeUtils = undefined;
const templateResources = new Map([
["pageTemplate", PAGE_TEMPLATE_RESOURCE_PATH],
["scriptTemplate", SCRIPT_TEMPLATE_RESOURCE_PATH],
]);
this._templates = {};
for (let [templateName, path] of templateResources) { const xhr = new XMLHttpRequest(); // Using a synchronous XHR in a worker is fine.
xhr.open("GET", path, false);
xhr.responseType = "text";
xhr.send(null); this._templates[templateName] = xhr.responseText;
}
returnthis._templates;
},
/** *Constructsthecachedabout:homedocumentusingReactDOMServer.Thiswill *becalledwhen"construct"messagesaresenttothisPromiseWorker. * *@paramstate(Object) *ThemostrecentActivityStreamReduxstate. *@returnObject *Anobjectwiththefollowingproperties: * *page(String): *Thegeneratedmarkupforthedocument. * *script(String): *Thegeneratedscriptforthedocument.
*/
construct(state) { // If anything in this function throws an exception, PromiseWorker // runs the risk of leaving the Promise associated with this method // forever unresolved. This is particularly bad when this method is // called via AsyncShutdown, since the forever unresolved Promise can // result in a AsyncShutdown timeout crash. // // To help ensure that no matter what, the Promise resolves with something, // we wrap the whole operation in a try/catch. try { returnthis._construct(state);
} catch (e) {
console.error("about:home startup cache construction failed:", e); return { page: null, script: null };
}
},
// ReactDOMServer.renderToString expects a Redux store to pull // the state from, so we mock out a minimal store implementation.
let fakeStore = {
getState() { return state;
},
dispatch() {},
};
let { pageTemplate, scriptTemplate } = this.getOrCreateTemplates();
let cacheTime = new Date().toUTCString();
let page = pageTemplate
.replace("{{ MARKUP }}", markup)
.replace("{{ CACHE_TIME }}", cacheTime);
let script = scriptTemplate.replace( "{{ STATE }}",
JSON.stringify(state, null, "\t")
);
return { page, script };
},
};
// This boilerplate connects the PromiseWorker to the Agent so // that messages from the main thread map to methods on the // Agent.
let worker = new PromiseWorker.AbstractWorker();
worker.dispatch = function (method, args = []) { return Agent[method](...args);
};
worker.postMessage = function (result, ...transfers) {
self.postMessage(result, ...transfers);
};
worker.close = function () {
self.close();
};
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.