Skip to content

Commit

Permalink
refactor: drop web worker for classic setInterval
Browse files Browse the repository at this point in the history
  • Loading branch information
Gugustinette committed Dec 23, 2024
1 parent f336bb9 commit b7ff57a
Show file tree
Hide file tree
Showing 10 changed files with 65 additions and 129 deletions.
2 changes: 0 additions & 2 deletions packages/core/build.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ import { defineBuildConfig } from 'unbuild'
export default defineBuildConfig({
entries: [
'src/index',
'src/pipeline/RenderPipelineWorker',
'src/pipeline/PhysicPipelineWorker',
],
declaration: true,
clean: true,
Expand Down
13 changes: 4 additions & 9 deletions packages/core/src/FScene.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
Expand All @@ -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
Expand Down Expand Up @@ -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 = []
}

/**
Expand Down
4 changes: 1 addition & 3 deletions packages/core/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down
26 changes: 0 additions & 26 deletions packages/core/src/pipeline/CustomWorker.ts

This file was deleted.

15 changes: 15 additions & 0 deletions packages/core/src/pipeline/GamePipeline.ts
Original file line number Diff line number Diff line change
@@ -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')
}
}
15 changes: 0 additions & 15 deletions packages/core/src/pipeline/PhysicPipeline.ts

This file was deleted.

8 changes: 0 additions & 8 deletions packages/core/src/pipeline/PhysicPipelineWorker.ts

This file was deleted.

88 changes: 45 additions & 43 deletions packages/core/src/pipeline/Pipeline.ts
Original file line number Diff line number Diff line change
@@ -1,25 +1,21 @@
/// <reference lib="WebWorker" />

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.
*/
Expand All @@ -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
}

/**
Expand All @@ -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
}
}
15 changes: 0 additions & 15 deletions packages/core/src/pipeline/RenderPipeline.ts

This file was deleted.

8 changes: 0 additions & 8 deletions packages/core/src/pipeline/RenderPipelineWorker.ts

This file was deleted.

0 comments on commit b7ff57a

Please sign in to comment.