-
Notifications
You must be signed in to change notification settings - Fork 0
feat: Introducing Scheduler to Core Features (FM-814) #12
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
bernhardccodev
wants to merge
8
commits into
main
Choose a base branch
from
fm-814
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
f15ec45
feature: Added Scheduler to core features (FM-814).
bernhardccodev 2c044c5
feature: Code clean up and added test spec for scheduler and timeout …
bernhardccodev c703823
feature: Added scheduler service to main index for properly usage and…
bernhardccodev a7ad8dc
feature: Added documentation on README.md for scheduler (FM-814)
bernhardccodev 2e831b8
feature: Code clean up based on coderabbit feedback (FM-814).
bernhardccodev 74d6d77
feature: Code clean up based on coderabbit feedback (FM-814).
bernhardccodev 321bd38
feature: Code clean up based on coderabbit feedback (FM-814).
bernhardccodev 7081b35
feature: Added documentation on README.md for scheduler (FM-814)
bernhardccodev File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,2 +1,88 @@ | ||
| # core | ||
| A collection of reusable core features, classes and functionalities used to create web-based apps and games. | ||
|
|
||
| # SchedulerService | ||
|
|
||
| A game-loop-driven timer scheduler that respects pause state. Provides `setTimeout` equivalents driven by `deltaTime` instead of the browser clock — so timers pause when your game pauses. | ||
|
|
||
| --- | ||
|
|
||
| ## Usage | ||
|
|
||
| ### 1. Wire the game loop | ||
|
|
||
| Call `update()` every frame. Skip it when paused — timers will naturally pause too. | ||
|
|
||
| ```typescript | ||
| import schedulerService from '@curiouslearning/core'; | ||
|
|
||
| draw(deltaTime: number) { | ||
| if (!this.isPaused) { | ||
| schedulerService.update(deltaTime); | ||
| } | ||
| } | ||
| ``` | ||
|
|
||
| > **Note:** If used outside of `requestAnimationFrame` — for example in a custom loop that may produce high or unpredictable delta values — it is recommended to cap the delta time to prevent timers from misfiring: | ||
| > ```typescript | ||
| > const MAX_DELTA_MS = 100; | ||
| > schedulerService.update(Math.min(deltaTime, MAX_DELTA_MS)); | ||
| > ``` | ||
|
|
||
|
|
||
| --- | ||
|
|
||
| ### 2. Create a registry per component | ||
|
|
||
| Call `createRegistry()` to get a `TimeoutRegistry` scoped to your component. Schedule timers through the registry and call `cancelAll()` on teardown. | ||
|
|
||
| ```typescript | ||
| import schedulerService from '@curiouslearning/core'; | ||
| import type { TimeoutRegistry, TimerId } from '@curiouslearning/core'; | ||
|
|
||
| class TutorialHandler { | ||
| private registry: TimeoutRegistry = schedulerService.createRegistry(); | ||
| private timerId: TimerId | null = null; | ||
|
|
||
| startDelay() { | ||
| this.timerId = this.registry.setTimeout(() => { | ||
| this.onDelayComplete(); | ||
| }, 6000); | ||
| } | ||
|
|
||
| reset() { | ||
| this.registry.cancel(this.timerId); | ||
| this.timerId = null; | ||
| } | ||
|
|
||
| destroy() { | ||
| this.registry.cancelAll(); | ||
| } | ||
| } | ||
| ``` | ||
|
|
||
| --- | ||
|
|
||
| ## API | ||
|
|
||
| ### `schedulerService` | ||
|
|
||
| The singleton instance. Import and use directly. | ||
|
|
||
| | Method | Description | | ||
| |---|---| | ||
| | `update(delta: number)` | Ticks all active timers. Must be called every frame from the game loop. | | ||
| | `createRegistry()` | Returns a new `TimeoutRegistry` instance scoped to a component or entity. | | ||
| | `destroy()` | Clears all active timers. Call on full game teardown. | | ||
|
|
||
| --- | ||
|
|
||
| ### `TimeoutRegistry` | ||
|
|
||
| Returned by `schedulerService.createRegistry()`. Manages timers scoped to a single component or entity. | ||
|
|
||
| | Method | Description | | ||
| |---|---| | ||
| | `setTimeout(callback, delay)` | Schedules a one-time callback after `delay` ms. Returns a `TimerId`. Auto-removes itself from the registry after firing. | | ||
| | `cancel(id)` | Cancels a specific timer by ID. Safely accepts `null` or `undefined`. | | ||
| | `cancelAll()` | Cancels all timers owned by this registry. Call on component teardown. | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,41 @@ | ||
| import Scheduler from './scheduler'; | ||
| import { TimeoutRegistry } from './timeout-registry'; | ||
| import type { TimerId } from './scheduler'; | ||
|
|
||
| class SchedulerService { | ||
| // Making scheduler a singleton. | ||
| private scheduler = new Scheduler(); | ||
|
|
||
| /** | ||
| * Drives all active timers. Must be called every frame from the game loop. | ||
| * @param delta Time elapsed since last frame in milliseconds. | ||
| */ | ||
| update(delta: number): void { | ||
| this.scheduler.update(delta); | ||
| } | ||
|
|
||
| /** | ||
| * Creates a new TimeoutRegistry instance scoped to a component or entity. | ||
| * Use the returned registry to schedule and manage timers tied to that scope. | ||
| * Call registry.cancelAll() on component teardown. | ||
| * @returns A new TimeoutRegistry instance. | ||
| */ | ||
| createRegistry(): TimeoutRegistry { | ||
| return new TimeoutRegistry(this.scheduler); | ||
| } | ||
|
|
||
| /** | ||
| * Clears all active timers across all registries. | ||
| * Call on full game teardown. | ||
| */ | ||
| destroy(): void { | ||
| this.scheduler.destroy(); | ||
| } | ||
| } | ||
|
|
||
| export type { TimerId }; | ||
| export type { TimeoutRegistry }; | ||
|
|
||
| // Making scheduler service a singleton. | ||
| const schedulerService = new SchedulerService(); | ||
| export default schedulerService; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,55 @@ | ||
| import Scheduler from './scheduler'; | ||
|
|
||
| describe('Scheduler', () => { | ||
| let scheduler: Scheduler; | ||
|
|
||
| beforeEach(() => { | ||
| scheduler = new Scheduler(); | ||
| }); | ||
|
|
||
| it('should fire a setTimeout callback after the specified delay', () => { | ||
| const callback = jest.fn(); | ||
| scheduler.setTimeout(callback, 1000); | ||
|
|
||
| scheduler.update(999); | ||
| expect(callback).not.toHaveBeenCalled(); | ||
|
|
||
| scheduler.update(1); | ||
| expect(callback).toHaveBeenCalledTimes(1); | ||
| }); | ||
|
|
||
| it('should not fire a setTimeout callback after it has been cancelled', () => { | ||
| const callback = jest.fn(); | ||
| const id = scheduler.setTimeout(callback, 1000); | ||
|
|
||
| scheduler.cancel(id); | ||
| scheduler.update(1000); | ||
|
|
||
| expect(callback).not.toHaveBeenCalled(); | ||
| }); | ||
|
|
||
| it('should fire a setInterval callback repeatedly', () => { | ||
| const callback = jest.fn(); | ||
| scheduler.setInterval(callback, 500); | ||
|
|
||
| scheduler.update(500); | ||
| expect(callback).toHaveBeenCalledTimes(1); | ||
|
|
||
| scheduler.update(500); | ||
| expect(callback).toHaveBeenCalledTimes(2); | ||
|
|
||
| scheduler.update(500); | ||
| expect(callback).toHaveBeenCalledTimes(3); | ||
| }); | ||
|
|
||
| it('should clear all timers on destroy', () => { | ||
| const callback = jest.fn(); | ||
| scheduler.setTimeout(callback, 500); | ||
| scheduler.setInterval(callback, 200); | ||
|
|
||
| scheduler.destroy(); | ||
| scheduler.update(1000); | ||
|
|
||
| expect(callback).not.toHaveBeenCalled(); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,103 @@ | ||
| import { Opaque } from 'type-fest'; | ||
|
|
||
| type TimerId = Opaque<number, 'TimerId'>; | ||
| type Timer = { | ||
| id: TimerId; | ||
| callback: () => void; | ||
| delay: number; | ||
| remaining: number; | ||
| loop: boolean; | ||
| }; | ||
|
|
||
| /** | ||
| * Custom scheduler for managing time-based events within the game loop. | ||
| * Unlike standard window.setTimeout/setInterval, this respects the game's | ||
| * pause state and is driven by deltaTime from the main update loop. | ||
| */ | ||
| export default class Scheduler { | ||
| private nextTimerId = 0; | ||
| private timers: Map<TimerId, Timer> = new Map(); | ||
|
|
||
| /** | ||
| * Schedules a one-time callback after a specified delay. | ||
| * @param callback The function to execute. | ||
| * @param delay Delay in milliseconds. | ||
| * @returns A unique TimerId for clearing the timeout. | ||
| */ | ||
| setTimeout(callback: () => void, delay: number): TimerId { | ||
| const id = this.nextTimerId++ as TimerId; | ||
| this.timers.set(id, { | ||
| id, | ||
| callback, | ||
| delay, | ||
| remaining: delay, | ||
| loop: false, | ||
| }); | ||
| return id; | ||
| } | ||
|
|
||
| /** | ||
| * Cancels a previously scheduled timeout or interval. | ||
| * @param id The ID of the timer to clear. | ||
| */ | ||
| cancel(id: TimerId): void { | ||
| if (id !== undefined && id !== null) { | ||
| this.timers.delete(id); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Schedules a repeating callback at a specified interval. | ||
| * @param callback The function to execute. | ||
| * @param delay Interval in milliseconds. | ||
| * @returns A unique TimerId for clearing the interval. | ||
| */ | ||
| setInterval(callback: () => void, delay: number): TimerId { | ||
| const id = this.nextTimerId++ as TimerId; | ||
| const safeDelay = Math.max(1, delay); // Minimum 1ms to prevent runaway intervals | ||
| this.timers.set(id, { | ||
| id, | ||
| callback, | ||
| delay: safeDelay, | ||
| remaining: safeDelay, | ||
| loop: true, | ||
| }); | ||
| return id; | ||
| } | ||
|
|
||
| /** | ||
| * Updates all active timers by subtracting the provided delta time. | ||
| * Executed by the main game loop. | ||
| * @param delta The time elapsed since the last update in milliseconds. | ||
| */ | ||
| update(delta: number): void { | ||
| const snapshot = [...this.timers.values()]; | ||
| for (const timer of snapshot) { | ||
| if (!this.timers.has(timer.id)) continue; // already cancelled | ||
| timer.remaining -= delta; | ||
| if (timer.remaining <= 0) { | ||
| try { | ||
| timer.callback(); | ||
| } catch (e) { | ||
| console.error("Error in scheduled callback:", e); | ||
| } | ||
|
|
||
| if (timer.loop) { | ||
| timer.remaining += timer.delay; | ||
| } else { | ||
| this.cancel(timer.id); | ||
| } | ||
coderabbitai[bot] marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Clears all active timers and resets the scheduler state. | ||
| */ | ||
| destroy(): void { | ||
| this.timers.clear(); | ||
| } | ||
| } | ||
|
|
||
|
|
||
| export { TimerId }; | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,57 @@ | ||
| import Scheduler from './scheduler'; | ||
| import { TimeoutRegistry } from './timeout-registry'; | ||
|
|
||
| describe('TimeoutRegistry', () => { | ||
| let scheduler: Scheduler; | ||
| let registry: TimeoutRegistry; | ||
|
|
||
| beforeEach(() => { | ||
| scheduler = new Scheduler(); | ||
| registry = new TimeoutRegistry(scheduler); | ||
| }); | ||
|
|
||
| it('should fire a setTimeout callback after the specified delay', () => { | ||
| const callback = jest.fn(); | ||
| registry.setTimeout(callback, 1000); | ||
|
|
||
| scheduler.update(999); | ||
| expect(callback).not.toHaveBeenCalled(); | ||
|
|
||
| scheduler.update(1); | ||
| expect(callback).toHaveBeenCalledTimes(1); | ||
| }); | ||
|
|
||
| it('should not fire a callback after it has been cancelled', () => { | ||
| const callback = jest.fn(); | ||
| const id = registry.setTimeout(callback, 1000); | ||
|
|
||
| registry.cancel(id); | ||
| scheduler.update(1000); | ||
|
|
||
| expect(callback).not.toHaveBeenCalled(); | ||
| }); | ||
|
|
||
| it('should cancel all timers on cancelAll', () => { | ||
| const callbackA = jest.fn(); | ||
| const callbackB = jest.fn(); | ||
| registry.setTimeout(callbackA, 500); | ||
| registry.setTimeout(callbackB, 800); | ||
|
|
||
| registry.cancelAll(); | ||
| scheduler.update(1000); | ||
|
|
||
| expect(callbackA).not.toHaveBeenCalled(); | ||
| expect(callbackB).not.toHaveBeenCalled(); | ||
| }); | ||
|
|
||
| it('should not throw when cancelling an already-fired timer', () => { | ||
| const callback = jest.fn(); | ||
| const id = registry.setTimeout(callback, 500); | ||
|
|
||
| scheduler.update(500); | ||
| expect(callback).toHaveBeenCalledTimes(1); | ||
|
|
||
| // cancel after fire should not throw | ||
| expect(() => registry.cancel(id)).not.toThrow(); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,37 @@ | ||
| import Scheduler, { TimerId } from './scheduler'; | ||
|
|
||
| export class TimeoutRegistry { | ||
| private timeouts: Set<TimerId> = new Set(); | ||
| private scheduler: Scheduler; | ||
|
|
||
| constructor(scheduler: Scheduler) { | ||
| this.scheduler = scheduler; | ||
| } | ||
|
|
||
| setTimeout(callback: () => void, delay: number): TimerId { | ||
| const timerId = this.scheduler.setTimeout(() => { | ||
| this.timeouts.delete(timerId); | ||
| callback(); | ||
| }, delay); | ||
| this.timeouts.add(timerId); | ||
| return timerId; | ||
| } | ||
|
|
||
| cancel(timerId: TimerId | null | undefined): void { | ||
| if (timerId === null || timerId === undefined) { | ||
| return; | ||
| } | ||
| this.scheduler.cancel(timerId); | ||
| this.timeouts.delete(timerId); | ||
| } | ||
|
|
||
| cancelAll(): void { | ||
| try { | ||
| for (const timerId of this.timeouts) { | ||
| this.scheduler.cancel(timerId); | ||
| } | ||
| } finally { | ||
| this.timeouts.clear(); | ||
| } | ||
| } | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.