one
Version:
One is a new React Framework that makes Vite serve both native and web.
161 lines (159 loc) • 6.92 kB
JavaScript
import { Worker } from "worker_threads";
import { cpus } from "os";
import { fileURLToPath } from "url";
import { dirname, join } from "path";
function _class_call_check(instance, Constructor) {
if (!(instance instanceof Constructor)) throw new TypeError("Cannot call a class as a function");
}
function _defineProperties(target, props) {
for (var i = 0; i < props.length; i++) {
var descriptor = props[i];
descriptor.enumerable = descriptor.enumerable || !1, descriptor.configurable = !0, "value" in descriptor && (descriptor.writable = !0), Object.defineProperty(target, descriptor.key, descriptor);
}
}
function _create_class(Constructor, protoProps, staticProps) {
return protoProps && _defineProperties(Constructor.prototype, protoProps), staticProps && _defineProperties(Constructor, staticProps), Constructor;
}
function _define_property(obj, key, value) {
return key in obj ? Object.defineProperty(obj, key, {
value,
enumerable: !0,
configurable: !0,
writable: !0
}) : obj[key] = value, obj;
}
var __filename = fileURLToPath(import.meta.url),
__dirname = dirname(__filename),
BuildWorkerPool = /* @__PURE__ */function () {
"use strict";
function BuildWorkerPool2() {
var _this,
_loop = function (i2) {
var worker = new Worker(workerPath);
worker.on("message", function (msg) {
if (msg.type === "ready") _this1.readyCount++, _this1.available.push(worker), _this1.readyCount === size && _this1._resolveReady();else if (msg.type === "init-done") _this1.initCount++, _this1.initCount === size && _this1._resolveInitialized(), _this1.dispatch();else if (msg.type === "done" || msg.type === "error") {
var pending = _this1.pendingById.get(msg.id);
pending && (_this1.pendingById.delete(msg.id), msg.type === "done" ? pending.resolve(msg.result) : pending.reject(new Error(msg.error))), _this1.available.push(worker), _this1.dispatch();
}
}), worker.on("error", function (err) {
console.error("[BuildWorkerPool] Worker error:", err);
}), _this.workers.push(worker);
},
_this1 = this,
size = arguments.length > 0 && arguments[0] !== void 0 ? arguments[0] : Math.max(1, cpus().length - 1);
_class_call_check(this, BuildWorkerPool2), _define_property(this, "workers", []), _define_property(this, "available", []), _define_property(this, "taskQueue", []), _define_property(this, "pendingById", /* @__PURE__ */new Map()), _define_property(this, "nextId", 0), _define_property(this, "readyCount", 0), _define_property(this, "initCount", 0), _define_property(this, "_ready", void 0), _define_property(this, "_resolveReady", void 0), _define_property(this, "_initialized", void 0), _define_property(this, "_resolveInitialized", void 0), _define_property(this, "_terminated", !1), this._ready = new Promise(function (resolve) {
_this1._resolveReady = resolve;
}), this._initialized = new Promise(function (resolve) {
_this1._resolveInitialized = resolve;
});
for (var workerPath = join(__dirname, "buildPageWorker.mjs"), i = 0; i < size; i++) _this = this, _loop(i);
}
return _create_class(BuildWorkerPool2, [{
key: "size",
get: function () {
return this.workers.length;
}
}, {
key: "initialize",
value:
// initialize all workers - they load config themselves from vite.config
async function () {
await this._ready;
var _iteratorNormalCompletion = !0,
_didIteratorError = !1,
_iteratorError = void 0;
try {
for (var _iterator = this.workers[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = !0) {
var worker = _step.value;
worker.postMessage({
type: "init",
id: this.nextId++
});
}
} catch (err) {
_didIteratorError = !0, _iteratorError = err;
} finally {
try {
!_iteratorNormalCompletion && _iterator.return != null && _iterator.return();
} finally {
if (_didIteratorError) throw _iteratorError;
}
}
await this._initialized;
}
}, {
key: "dispatch",
value: function () {
for (; this.available.length > 0 && this.taskQueue.length > 0;) {
var worker = this.available.shift(),
{
msg,
pending
} = this.taskQueue.shift();
this.pendingById.set(pending.id, pending), worker.postMessage(msg);
}
}
}, {
key: "buildPage",
value: async function (args) {
var _this = this,
_args_foundRoute_layouts,
_args_foundRoute_middlewares;
if (this._terminated) throw new Error("Worker pool has been terminated");
var serializedRoute = {
type: args.foundRoute.type,
file: args.foundRoute.file,
// only keep serializable layout data
layouts: (_args_foundRoute_layouts = args.foundRoute.layouts) === null || _args_foundRoute_layouts === void 0 ? void 0 : _args_foundRoute_layouts.map(function (layout) {
return {
contextKey: layout.contextKey,
loaderServerPath: layout.loaderServerPath,
layoutRenderMode: layout.layoutRenderMode
};
}),
// only keep contextKey from middlewares
middlewares: (_args_foundRoute_middlewares = args.foundRoute.middlewares) === null || _args_foundRoute_middlewares === void 0 ? void 0 : _args_foundRoute_middlewares.map(function (mw) {
return {
contextKey: mw.contextKey
};
})
},
id = this.nextId++,
msg = {
type: "build",
id,
args: {
...args,
foundRoute: serializedRoute
}
};
return new Promise(function (resolve, reject) {
var pending = {
id,
resolve,
reject
};
_this.taskQueue.push({
msg,
pending
}), _this.dispatch();
});
}
}, {
key: "terminate",
value: async function () {
this._terminated = !0, await Promise.all(this.workers.map(function (w) {
return w.terminate();
})), this.workers = [], this.available = [];
}
}]), BuildWorkerPool2;
}(),
pool = null;
function getWorkerPool(size) {
return pool || (pool = new BuildWorkerPool(size)), pool;
}
async function terminateWorkerPool() {
pool && (await pool.terminate(), pool = null);
}
export { BuildWorkerPool, getWorkerPool, terminateWorkerPool };
//# sourceMappingURL=workerPool.native.js.map