From b7ff57ae33e8da7207a772fee1d0fcb77806a9a3 Mon Sep 17 00:00:00 2001 From: Gugustinette Date: Mon, 23 Dec 2024 19:34:38 +0100 Subject: [PATCH] refactor: drop web worker for classic setInterval --- packages/core/build.config.ts | 2 - packages/core/src/FScene.ts | 13 +-- packages/core/src/index.ts | 4 +- packages/core/src/pipeline/CustomWorker.ts | 26 ------ packages/core/src/pipeline/GamePipeline.ts | 15 ++++ packages/core/src/pipeline/PhysicPipeline.ts | 15 ---- .../core/src/pipeline/PhysicPipelineWorker.ts | 8 -- packages/core/src/pipeline/Pipeline.ts | 88 ++++++++++--------- packages/core/src/pipeline/RenderPipeline.ts | 15 ---- .../core/src/pipeline/RenderPipelineWorker.ts | 8 -- 10 files changed, 65 insertions(+), 129 deletions(-) delete mode 100644 packages/core/src/pipeline/CustomWorker.ts create mode 100644 packages/core/src/pipeline/GamePipeline.ts delete mode 100644 packages/core/src/pipeline/PhysicPipeline.ts delete mode 100644 packages/core/src/pipeline/PhysicPipelineWorker.ts delete mode 100644 packages/core/src/pipeline/RenderPipeline.ts delete mode 100644 packages/core/src/pipeline/RenderPipelineWorker.ts diff --git a/packages/core/build.config.ts b/packages/core/build.config.ts index b39054cb..7acbef83 100644 --- a/packages/core/build.config.ts +++ b/packages/core/build.config.ts @@ -3,8 +3,6 @@ import { defineBuildConfig } from 'unbuild' export default defineBuildConfig({ entries: [ 'src/index', - 'src/pipeline/RenderPipelineWorker', - 'src/pipeline/PhysicPipelineWorker', ], declaration: true, clean: true, diff --git a/packages/core/src/FScene.ts b/packages/core/src/FScene.ts index 184b66cb..6ce605b4 100644 --- a/packages/core/src/FScene.ts +++ b/packages/core/src/FScene.ts @@ -2,7 +2,7 @@ import type RAPIER2D from '@dimforge/rapier2d' import type RAPIER3D from '@dimforge/rapier3d' import type { FComponent } from './FComponent' import type { FLight } from './FLight' -import { CustomWorker } from './pipeline/CustomWorker' +import type { Pipeline } from './pipeline/Pipeline' export interface FSceneOptions { gravity?: { x: number, y: number, z: number } | { x: number, y: number } @@ -25,8 +25,7 @@ export abstract class FScene { /** * Pipelines */ - private __RENDER_PIPELINE__: CustomWorker | null = null - private __PHYSIC_PIPELINE__: CustomWorker | null = null + pipelines: Pipeline[] /** * DOM element that the renderer will be appended to @@ -125,12 +124,8 @@ export abstract class FScene { this.components = [] // Initialize the lights array this.lights = [] - - // Initialize workers - this.__RENDER_PIPELINE__ = new CustomWorker('./pipeline/RenderPipelineWorker.mjs') - this.__RENDER_PIPELINE__.start() - this.__PHYSIC_PIPELINE__ = new CustomWorker('./pipeline/PhysicPipelineWorker.mjs') - this.__PHYSIC_PIPELINE__.start() + // Initialize the pipelines array + this.pipelines = [] } /** diff --git a/packages/core/src/index.ts b/packages/core/src/index.ts index fc132273..15815875 100644 --- a/packages/core/src/index.ts +++ b/packages/core/src/index.ts @@ -9,10 +9,8 @@ export * from './FLight' export * from './FScene' // Pipeline -export * from './pipeline/CustomWorker' -export * from './pipeline/PhysicPipeline' +export * from './pipeline/GamePipeline' export * from './pipeline/Pipeline' -export * from './pipeline/RenderPipeline' // Types export * from './types/FVector2' diff --git a/packages/core/src/pipeline/CustomWorker.ts b/packages/core/src/pipeline/CustomWorker.ts deleted file mode 100644 index c6d2ec9d..00000000 --- a/packages/core/src/pipeline/CustomWorker.ts +++ /dev/null @@ -1,26 +0,0 @@ -import { PipelineCommands } from './Pipeline' - -/** - * A custom worker that extends the Worker class. - * It provides better type checking, and more control over the worker. - * @category Pipeline - */ -export class CustomWorker extends Worker { - constructor(path: string) { - super(new URL(path, import.meta.url), { type: 'module' }) - } - - /** - * Start the corresponding pipeline. - */ - start() { - this.postMessage(PipelineCommands.START) - } - - /** - * Stop the corresponding pipeline. - */ - stop() { - this.postMessage(PipelineCommands.STOP) - } -} diff --git a/packages/core/src/pipeline/GamePipeline.ts b/packages/core/src/pipeline/GamePipeline.ts new file mode 100644 index 00000000..19c8f2f3 --- /dev/null +++ b/packages/core/src/pipeline/GamePipeline.ts @@ -0,0 +1,15 @@ +import { Pipeline } from './Pipeline' + +/** + * Pipeline to handle the main game loop. + * @category Pipeline + */ +export class GamePipeline extends Pipeline { + constructor() { + super() + } + + frame() { + // console.log('GamePipeline frame') + } +} diff --git a/packages/core/src/pipeline/PhysicPipeline.ts b/packages/core/src/pipeline/PhysicPipeline.ts deleted file mode 100644 index 5e9f2d28..00000000 --- a/packages/core/src/pipeline/PhysicPipeline.ts +++ /dev/null @@ -1,15 +0,0 @@ -import { Pipeline } from './Pipeline' - -/** - * Pipeline to handle physic simulation tasks. - * @category Pipeline - */ -export class PhysicPipeline extends Pipeline { - constructor(sw: DedicatedWorkerGlobalScope) { - super(sw) - } - - frame() { - // console.log('PhysicPipeline frame') - } -} diff --git a/packages/core/src/pipeline/PhysicPipelineWorker.ts b/packages/core/src/pipeline/PhysicPipelineWorker.ts deleted file mode 100644 index 02d71085..00000000 --- a/packages/core/src/pipeline/PhysicPipelineWorker.ts +++ /dev/null @@ -1,8 +0,0 @@ -/// - -import { PhysicPipeline } from './PhysicPipeline' - -export type {} -declare let self: DedicatedWorkerGlobalScope - -new PhysicPipeline(self) diff --git a/packages/core/src/pipeline/Pipeline.ts b/packages/core/src/pipeline/Pipeline.ts index ff5e10fb..3f2f0b70 100644 --- a/packages/core/src/pipeline/Pipeline.ts +++ b/packages/core/src/pipeline/Pipeline.ts @@ -1,25 +1,21 @@ -/// - -export enum PipelineCommands { - START = 'start', - STOP = 'stop', -} - export enum PipelineState { RUNNING = 'running', STOPPED = 'stopped', } +export interface PipelineOptions { + frameRate?: number +} + /** - * Pipeline class that abstract the usage of a web worker. - * This is used for running background tasks that are generally CPU intensive. + * The Pipeline class is an abstract class that can be used to create pipelines. + * A pipeline is a sequence of stages that are executed in order. + * Each stage is called a frame and is executed at a specific frame rate. + * Keep in mind that if the frame method takes longer to execute than the frame rate, + * the pipeline will not be able to keep up with the desired frame rate. * @category Pipeline */ export abstract class Pipeline { - /** - * The web worker instance. - */ - sw: DedicatedWorkerGlobalScope /** * The current state of the pipeline. */ @@ -37,15 +33,22 @@ export abstract class Pipeline { */ frameRate: number - constructor(sw: DedicatedWorkerGlobalScope) { - // Save the web worker instance - this.sw = sw - this.sw.addEventListener('message', (event) => { - this.handleMessage(event) - }) + constructor(options: PipelineOptions = {}) { + // Define default values for the options + const DEFAULT_OPTIONS = { + frameRate: 30, + } + // Apply default options + options = { ...DEFAULT_OPTIONS, ...options } + // Validate the options + if (options.frameRate === undefined) + throw new Error('FibboError: The frame rate must be defined') + + // Store the options + this.frameRate = options.frameRate + // Set the initial state of the pipeline this.state = PipelineState.STOPPED - this.frameRate = 30 } /** @@ -55,31 +58,30 @@ export abstract class Pipeline { abstract frame(): void /** - * Handle a message sent to the pipeline. - * @param event The message event. + * Start the pipeline. */ - handleMessage(event: MessageEvent) { - const command = event.data - - if (command === PipelineCommands.START) { - if (this.intervalId !== null) { - return - } - // Start the pipeline by setting an interval with the frame rate - this.intervalId = setInterval(() => { - this.frame() - }, 1000 / this.frameRate) - // Update the pipeline state - this.state = PipelineState.RUNNING + start(): void { + if (this.intervalId !== null) { + return } - else if (command === PipelineCommands.STOP) { - // Stop the pipeline by clearing the interval - if (this.intervalId !== null) { - clearInterval(this.intervalId) - } - this.intervalId = null - // Update the pipeline state - this.state = PipelineState.STOPPED + // Start the pipeline by setting an interval with the frame rate + this.intervalId = setInterval(() => { + this.frame() + }, 1000 / this.frameRate) + // Update the pipeline state + this.state = PipelineState.RUNNING + } + + /** + * Stop the pipeline. + */ + stop(): void { + // Stop the pipeline by clearing the interval + if (this.intervalId !== null) { + clearInterval(this.intervalId) } + this.intervalId = null + // Update the pipeline state + this.state = PipelineState.STOPPED } } diff --git a/packages/core/src/pipeline/RenderPipeline.ts b/packages/core/src/pipeline/RenderPipeline.ts deleted file mode 100644 index 0b4cba4a..00000000 --- a/packages/core/src/pipeline/RenderPipeline.ts +++ /dev/null @@ -1,15 +0,0 @@ -import { Pipeline } from './Pipeline' - -/** - * Pipeline to handle rendering tasks. - * @category Pipeline - */ -export class RenderPipeline extends Pipeline { - constructor(sw: DedicatedWorkerGlobalScope) { - super(sw) - } - - frame() { - // console.log('RenderPipeline frame') - } -} diff --git a/packages/core/src/pipeline/RenderPipelineWorker.ts b/packages/core/src/pipeline/RenderPipelineWorker.ts deleted file mode 100644 index 71a56193..00000000 --- a/packages/core/src/pipeline/RenderPipelineWorker.ts +++ /dev/null @@ -1,8 +0,0 @@ -/// - -import { RenderPipeline } from './RenderPipeline' - -export type {} -declare let self: DedicatedWorkerGlobalScope - -new RenderPipeline(self)