|
1 | 1 | <h3 align="center" > |
2 | 2 | <div> |
3 | 3 | <a href="https://github.com/nickesc/steploop"><img alt="Source: Github" src="https://img.shields.io/badge/source-github-brightgreen?style=for-the-badge&logo=github&labelColor=%23505050"></a> |
4 | | - <a href="https://github.com/nickesc/steploop/actions/workflows/node.js.yml"><img alt="Tests: github.com/nickesc/steploop/actions/workflows/node.js.yml" src="https://img.shields.io/github/actions/workflow/status/nickesc/steploop/steploop-ts-tests.yml?logo=github&label=tests&logoColor=white&style=for-the-badge&labelColor=%23505050"></a> |
| 4 | + <a href="https://github.com/nickesc/steploop/actions/workflows/steploop-ts-tests.yml"><img alt="Tests: github.com/nickesc/steploop/actions/workflows/steploop-ts-tests.yml" src="https://img.shields.io/github/actions/workflow/status/nickesc/steploop/steploop-ts-tests.yml?logo=github&label=tests&logoColor=white&style=for-the-badge&labelColor=%23505050"></a> |
5 | 5 | <br> |
6 | 6 | <a href="https://www.npmjs.com/package/steploop"><img alt="NPM: npmjs.com/package/steploop" src="https://img.shields.io/npm/v/steploop?style=for-the-badge&logo=npm&logoColor=white&label=npm&color=%23C12127&labelColor=%23505050"></a> |
7 | 7 | </div> |
|
17 | 17 | by <a href="https://nickesc.github.io">N. Escobar</a> / <a href="https://github.com/nickesc">nickesc</a> |
18 | 18 | </h6> |
19 | 19 | <h6 align="center"> |
20 | | - a fully-featured main-loop written in TypeScript |
| 20 | + a foundation for building loops that<br> |
| 21 | + execute at a consistent, specified rate |
21 | 22 | </h6> |
22 | 23 | </h3> |
23 | 24 |
|
24 | 25 | <br> |
25 | 26 |
|
26 | 27 | ## About `steploop` |
27 | 28 |
|
28 | | -`steploop` is a fully-featured main-loop written in TypeScript (and JS) with no additional dependencies. `steploop` provides a strong base to build a loop that runs once-per-frame. Inspired by game engine main-loops like Godot's [MainLoop](https://docs.godotengine.org/en/stable/classes/class_mainloop.html) or Unity's [Update() loop](https://docs.unity3d.com/Manual/execution-order.html). |
| 29 | +`steploop` is a fully-featured main-loop written in TypeScript with no additional dependencies. It provides a strong foundation for building loops that execute at a consistent, specified rate, inspired by game engine main-loops like Godot's [`MainLoop`](https://docs.godotengine.org/en/stable/classes/class_mainloop.html) or Unity's [`Update()` loop](https://docs.unity3d.com/Manual/execution-order.html). |
29 | 30 |
|
30 | | -> // TODO |
| 31 | +`steploop` provides a structured lifecycle with methods that can be overridden to implement custom behavior. |
31 | 32 |
|
32 | | -### Install |
| 33 | +The `StepLoop` class manages the timing and execution flow, supporting both fixed-step updates via [`setTimeout()`](https://developer.mozilla.org/en-US/docs/Web/API/Window/setTimeout) and smoother, display-synchronized updates using [`window.requestAnimationFrame()`](https://developer.mozilla.org/en-US/docs/Web/API/Window/requestAnimationFrame). |
| 34 | + |
| 35 | +## Install |
33 | 36 |
|
34 | 37 | Install `steploop` via NPM: |
35 | 38 |
|
36 | 39 | ```sh |
37 | | -$ npm i steploop ## not published yet |
| 40 | +npm i steploop # NOT PUBLISHED |
| 41 | +``` |
| 42 | + |
| 43 | +Import the `StepLoop` class in your TypeScript or JavaScript file: |
| 44 | + |
| 45 | +```ts |
| 46 | +import { StepLoop } from "steploop"; |
38 | 47 | ``` |
39 | 48 |
|
40 | | -### Basic Usage |
| 49 | +> [!NOTE] |
| 50 | +> `steploop` should be imported as an ES6 module. |
41 | 51 |
|
42 | | -##### Import: |
| 52 | +## Basic Usage |
43 | 53 |
|
44 | | -Import the library in your code: |
| 54 | +To define a new loop, extend the `StepLoop` class and override its methods to implement custom behavior. |
45 | 55 |
|
46 | | -```js |
| 56 | +```ts |
47 | 57 | import { StepLoop } from "steploop"; |
| 58 | + |
| 59 | +class App extends StepLoop { |
| 60 | + override initial(): void { |
| 61 | + console.log("Loop starting"); |
| 62 | + } |
| 63 | + |
| 64 | + override step(): void { |
| 65 | + console.log(`Executing step: ${this.get_step()}`); |
| 66 | + } |
| 67 | + |
| 68 | + override final(): void { |
| 69 | + console.log("Loop finished"); |
| 70 | + } |
| 71 | +} |
| 72 | + |
| 73 | +// Create a new loop that runs at 60 steps-per-second for 100 steps |
| 74 | +const app = new App(60, 100); |
| 75 | +app.start(); |
48 | 76 | ``` |
49 | | -> `steploop` must be imported as an ES6 module. |
50 | 77 |
|
51 | | -##### Usage: |
52 | | -> // TODO |
| 78 | +## Lifecycle |
| 79 | + |
| 80 | +The `StepLoop` class executes in three distinct stages, with hooks that can be overridden to add custom logic: |
| 81 | + |
| 82 | +1. **Initialization:** Runs once at the beginning of the loop |
| 83 | + - `initial()`: Runs once at the beginning of the loop. |
| 84 | +2. **Looping:** The core of the loop, which repeatedly executes the following sequence: |
| 85 | + - `background()`: Runs asynchronously at the beginning of each step. |
| 86 | + - `before()`: Runs before the main `step()` method. |
| 87 | + - `step()`: The main update function for your loop. |
| 88 | + - `after()`: Runs after the `step()` method. |
| 89 | +3. **Termination:** Runs once when the loop ends, either by reaching the end of its lifespan or being manually stopped |
| 90 | + - `final()`: Runs once when the loop ends. |
| 91 | + |
| 92 | +The loop can run indefinitely or for a set number of steps, and its execution can be precisely controlled, allowing it to be paused, resumed, and dynamically modified at runtime. |
53 | 93 |
|
54 | | -## Documentation |
| 94 | +## Reference |
55 | 95 |
|
56 | | -> // TODO |
| 96 | +For full documentation of the module and its methods, please see the [Documentation](/docs/documentation.md) page. |
57 | 97 |
|
58 | 98 | ## License |
59 | 99 |
|
|
0 commit comments