MCPcopy Create free account
hub / github.com/esengine/esengine / createWorkerScript

Function createWorkerScript

packages/core/src/ECS/Systems/WorkerEntitySystem.ts:375–431  ·  view source on GitHub ↗

* @zh 创建 Worker 脚本 * @en Create Worker script

()

Source from the content-addressed store, hash-verified

373 * @en Create Worker script
374 */
375 private createWorkerScript(): string {
376 const functionBody = this.extractFunctionBody(this.workerProcess.toString());
377 const sharedProcessBody = this.getSharedProcessFunctionBody();
378 const entityDataSize = this.config.entityDataSize;
379
380 return `
381 let sharedFloatArray = null;
382 const ENTITY_DATA_SIZE = ${entityDataSize};
383
384 self.onmessage = function(e) {
385 const { type, id, entities, deltaTime, systemConfig, startIndex, endIndex, sharedBuffer } = e.data;
386
387 try {
388 if (type === 'init' && sharedBuffer) {
389 sharedFloatArray = new Float32Array(sharedBuffer);
390 self.postMessage({ type: 'init', success: true });
391 return;
392 }
393
394 if (type === 'shared') {
395 if (!sharedFloatArray) {
396 self.postMessage({ id, error: 'SharedArrayBuffer not initialized' });
397 return;
398 }
399 processSharedArrayBuffer(startIndex, endIndex, deltaTime, systemConfig);
400 self.postMessage({ id, result: null });
401 return;
402 }
403
404 if (entities) {
405 function workerProcess(entities, deltaTime, systemConfig) {
406 ${functionBody}
407 }
408
409 const result = workerProcess(entities, deltaTime, systemConfig);
410
411 if (result && typeof result.then === 'function') {
412 result.then(finalResult => {
413 self.postMessage({ id, result: finalResult });
414 }).catch(error => {
415 self.postMessage({ id, error: error.message });
416 });
417 } else {
418 self.postMessage({ id, result });
419 }
420 }
421 } catch (error) {
422 self.postMessage({ id, error: error.message });
423 }
424 };
425
426 function processSharedArrayBuffer(startIndex, endIndex, deltaTime, systemConfig) {
427 if (!sharedFloatArray) return;
428 ${sharedProcessBody}
429 }
430 `;
431 }
432

Callers

nothing calls this directly

Calls 1

toStringMethod · 0.45

Tested by

no test coverage detected