automatick 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +20 -0
- package/README.md +106 -0
- package/dist/chunk-66FUVAAG.js +7 -0
- package/dist/chunk-AIKB2FNR.js +17 -0
- package/dist/chunk-HTH6FQ7C.js +192 -0
- package/dist/chunk-OA6FGXTP.js +9 -0
- package/dist/chunk-SK5SHIWY.js +13 -0
- package/dist/chunk-YNLOTPDY.js +206 -0
- package/dist/engine.cjs +231 -0
- package/dist/engine.d.cts +72 -0
- package/dist/engine.d.ts +72 -0
- package/dist/engine.js +8 -0
- package/dist/react/EngineContext.cjs +43 -0
- package/dist/react/EngineContext.d.cts +18 -0
- package/dist/react/EngineContext.d.ts +18 -0
- package/dist/react/EngineContext.js +6 -0
- package/dist/react/PerformanceOverlay.cjs +341 -0
- package/dist/react/PerformanceOverlay.d.cts +9 -0
- package/dist/react/PerformanceOverlay.d.ts +9 -0
- package/dist/react/PerformanceOverlay.js +290 -0
- package/dist/react/Simulation.cjs +481 -0
- package/dist/react/Simulation.d.cts +31 -0
- package/dist/react/Simulation.d.ts +31 -0
- package/dist/react/Simulation.js +235 -0
- package/dist/react/SimulationContext.cjs +41 -0
- package/dist/react/SimulationContext.d.cts +25 -0
- package/dist/react/SimulationContext.d.ts +25 -0
- package/dist/react/SimulationContext.js +6 -0
- package/dist/react/SimulationControls.cjs +298 -0
- package/dist/react/SimulationControls.d.cts +33 -0
- package/dist/react/SimulationControls.d.ts +33 -0
- package/dist/react/SimulationControls.js +80 -0
- package/dist/react/controlPrimitives.cjs +247 -0
- package/dist/react/controlPrimitives.d.cts +45 -0
- package/dist/react/controlPrimitives.d.ts +45 -0
- package/dist/react/controlPrimitives.js +22 -0
- package/dist/react/hooks.cjs +53 -0
- package/dist/react/hooks.d.cts +26 -0
- package/dist/react/hooks.d.ts +26 -0
- package/dist/react/hooks.js +7 -0
- package/dist/react/stableCallback.cjs +47 -0
- package/dist/react/stableCallback.d.cts +3 -0
- package/dist/react/stableCallback.d.ts +3 -0
- package/dist/react/stableCallback.js +6 -0
- package/dist/react/useSimulationCanvas.cjs +92 -0
- package/dist/react/useSimulationCanvas.d.cts +38 -0
- package/dist/react/useSimulationCanvas.d.ts +38 -0
- package/dist/react/useSimulationCanvas.js +53 -0
- package/dist/sim.cjs +32 -0
- package/dist/sim.d.cts +40 -0
- package/dist/sim.d.ts +40 -0
- package/dist/sim.js +7 -0
- package/dist/worker/createSimWorker.cjs +147 -0
- package/dist/worker/createSimWorker.d.cts +19 -0
- package/dist/worker/createSimWorker.d.ts +19 -0
- package/dist/worker/createSimWorker.js +122 -0
- package/dist/worker/protocol.cjs +18 -0
- package/dist/worker/protocol.d.cts +59 -0
- package/dist/worker/protocol.d.ts +59 -0
- package/dist/worker/protocol.js +0 -0
- package/dist/worker/workerRunner.cjs +100 -0
- package/dist/worker/workerRunner.d.cts +30 -0
- package/dist/worker/workerRunner.d.ts +30 -0
- package/dist/worker/workerRunner.js +73 -0
- package/package.json +102 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Jerome Cukier
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE DEALINGS IN THE SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,106 @@
|
|
|
1
|
+
# automatick
|
|
2
|
+
|
|
3
|
+
State-machine engine for tick-based simulations in React. You write the rules (`init`, `step`, params); automatick handles the loop, timing, status state machine, controls, and (optionally) running the simulation in a Web Worker.
|
|
4
|
+
|
|
5
|
+
## Install
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
npm install automatick
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
React is an optional peer dependency — the engine has no React surface.
|
|
12
|
+
|
|
13
|
+
## Quick start
|
|
14
|
+
|
|
15
|
+
A simulation has three parts: a **sim module** (pure logic), a `<Simulation>` wrapper, and a render component reading state via `useSimulation()`.
|
|
16
|
+
|
|
17
|
+
```ts
|
|
18
|
+
// counterSim.ts
|
|
19
|
+
import { defineSim } from 'automatick/sim';
|
|
20
|
+
|
|
21
|
+
export default defineSim<{ count: number }, { increment: number }>({
|
|
22
|
+
defaultParams: { increment: 1 },
|
|
23
|
+
init: () => ({ count: 0 }),
|
|
24
|
+
step: ({ data, params }) => ({ count: data.count + params.increment }),
|
|
25
|
+
});
|
|
26
|
+
```
|
|
27
|
+
|
|
28
|
+
```tsx
|
|
29
|
+
// Counter.tsx
|
|
30
|
+
import { Simulation } from 'automatick/react/simulation';
|
|
31
|
+
import { useSimulation } from 'automatick/react/hooks';
|
|
32
|
+
import { StandardControls } from 'automatick/react/controls';
|
|
33
|
+
import counterSim from './counterSim';
|
|
34
|
+
|
|
35
|
+
function Display() {
|
|
36
|
+
const { data, tick } = useSimulation<typeof counterSim>();
|
|
37
|
+
return <p>Tick {tick}: count is {data.count}</p>;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
export default function Counter() {
|
|
41
|
+
return (
|
|
42
|
+
<Simulation sim={counterSim}>
|
|
43
|
+
<Display />
|
|
44
|
+
<StandardControls />
|
|
45
|
+
</Simulation>
|
|
46
|
+
);
|
|
47
|
+
}
|
|
48
|
+
```
|
|
49
|
+
|
|
50
|
+
`StandardControls` gives you play/pause, reset, step, seek, and parameter inputs out of the box. For finer control, individual primitives live at `automatick/react/control-primitives`.
|
|
51
|
+
|
|
52
|
+
## Web worker
|
|
53
|
+
|
|
54
|
+
Same sim module, one prop change — the simulation now runs off the main thread:
|
|
55
|
+
|
|
56
|
+
```tsx
|
|
57
|
+
<Simulation worker={() => import('./counterSim')}>
|
|
58
|
+
<Display />
|
|
59
|
+
<StandardControls />
|
|
60
|
+
</Simulation>
|
|
61
|
+
```
|
|
62
|
+
|
|
63
|
+
Useful when `step` is expensive (large grids, n-body simulations, fluid solvers) and you want the UI to stay responsive.
|
|
64
|
+
|
|
65
|
+
## API at a glance
|
|
66
|
+
|
|
67
|
+
`defineSim<Data, Params>({ init, step, shouldStop?, defaultParams })` declares a sim module.
|
|
68
|
+
|
|
69
|
+
`step` receives `{ data, params, tick }` and returns the next `Data`.
|
|
70
|
+
|
|
71
|
+
`shouldStop(data, params) => boolean` is an optional terminal predicate; the engine moves to `'stopped'` when it returns true.
|
|
72
|
+
|
|
73
|
+
`useSimulation<typeof sim>()` returns the current snapshot and actions:
|
|
74
|
+
|
|
75
|
+
| Field | Type | Description |
|
|
76
|
+
|---|---|---|
|
|
77
|
+
| `data` | `Data` | Current simulation state |
|
|
78
|
+
| `params` | `Params` | Current parameters |
|
|
79
|
+
| `tick` | `number` | Current tick (starts at 0) |
|
|
80
|
+
| `status` | `'idle' \| 'playing' \| 'paused' \| 'stopped'` | Engine status |
|
|
81
|
+
| `play()`, `pause()`, `stop()` | `() => void` | Lifecycle controls |
|
|
82
|
+
| `seek(tick)` | `(n: number) => void` | Jump forward; pauses |
|
|
83
|
+
| `advance(n?)` | `(n?: number) => void` | Step forward by `n` ticks (default 1) |
|
|
84
|
+
| `setParams(patch)` | `(patch: Partial<Params>) => void` | Update params without reinit |
|
|
85
|
+
| `resetWith(patch?)` | `(patch?: Partial<Params>) => void` | Re-run `init` with optional param patch |
|
|
86
|
+
|
|
87
|
+
## Package entry points
|
|
88
|
+
|
|
89
|
+
| Subpath | Exports |
|
|
90
|
+
|---|---|
|
|
91
|
+
| `automatick` | `createEngine`, engine + status types |
|
|
92
|
+
| `automatick/sim` | `defineSim`, `SimModule`, `SimData`, `SimParams` |
|
|
93
|
+
| `automatick/worker/runner` | Worker-side runtime |
|
|
94
|
+
| `automatick/worker/create` | Worker host factory |
|
|
95
|
+
| `automatick/worker/protocol` | Message protocol types |
|
|
96
|
+
| `automatick/react/simulation` | `<Simulation>` |
|
|
97
|
+
| `automatick/react/hooks` | `useSimulation()` |
|
|
98
|
+
| `automatick/react/controls` | `<StandardControls>` |
|
|
99
|
+
| `automatick/react/control-primitives` | Individual UI primitives |
|
|
100
|
+
| `automatick/react/canvas` | `useSimulationCanvas()` |
|
|
101
|
+
| `automatick/react/performance` | `<PerformanceOverlay>` |
|
|
102
|
+
| `automatick/react/context` | `<SimulationContext>` |
|
|
103
|
+
|
|
104
|
+
## License
|
|
105
|
+
|
|
106
|
+
MIT
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import {
|
|
2
|
+
SimulationContext
|
|
3
|
+
} from "./chunk-66FUVAAG.js";
|
|
4
|
+
|
|
5
|
+
// src/react/hooks.ts
|
|
6
|
+
import React from "react";
|
|
7
|
+
function useSimulation() {
|
|
8
|
+
const ctx = React.useContext(SimulationContext);
|
|
9
|
+
if (!ctx) {
|
|
10
|
+
throw new Error("useSimulation must be used within a <Simulation>");
|
|
11
|
+
}
|
|
12
|
+
return ctx;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
export {
|
|
16
|
+
useSimulation
|
|
17
|
+
};
|
|
@@ -0,0 +1,192 @@
|
|
|
1
|
+
import {
|
|
2
|
+
useSimulation
|
|
3
|
+
} from "./chunk-AIKB2FNR.js";
|
|
4
|
+
|
|
5
|
+
// src/react/controlPrimitives.tsx
|
|
6
|
+
import { jsx, jsxs } from "react/jsx-runtime";
|
|
7
|
+
var btnStyle = {
|
|
8
|
+
padding: "6px 12px",
|
|
9
|
+
borderRadius: 6,
|
|
10
|
+
border: "1px solid rgba(0,0,0,0.2)",
|
|
11
|
+
background: "#fff",
|
|
12
|
+
cursor: "pointer"
|
|
13
|
+
};
|
|
14
|
+
function PlayPauseButton(props) {
|
|
15
|
+
const { status, play, pause } = useSimulation();
|
|
16
|
+
const { playingLabel = "Pause", pausedLabel = "Play", disabled } = props;
|
|
17
|
+
const isPlaying = status === "playing";
|
|
18
|
+
const canPlay = status === "idle" || status === "paused";
|
|
19
|
+
return /* @__PURE__ */ jsx(
|
|
20
|
+
"button",
|
|
21
|
+
{
|
|
22
|
+
type: "button",
|
|
23
|
+
style: btnStyle,
|
|
24
|
+
onClick: isPlaying ? pause : play,
|
|
25
|
+
disabled: disabled ?? (!canPlay && !isPlaying),
|
|
26
|
+
children: isPlaying ? playingLabel : pausedLabel
|
|
27
|
+
}
|
|
28
|
+
);
|
|
29
|
+
}
|
|
30
|
+
function StopButton(props) {
|
|
31
|
+
const { status, stop } = useSimulation();
|
|
32
|
+
const canStop = status === "playing" || status === "paused";
|
|
33
|
+
return /* @__PURE__ */ jsx(
|
|
34
|
+
"button",
|
|
35
|
+
{
|
|
36
|
+
type: "button",
|
|
37
|
+
style: btnStyle,
|
|
38
|
+
onClick: stop,
|
|
39
|
+
disabled: props.disabled ?? !canStop,
|
|
40
|
+
children: props.label ?? "Stop"
|
|
41
|
+
}
|
|
42
|
+
);
|
|
43
|
+
}
|
|
44
|
+
function StepButton(props) {
|
|
45
|
+
const { status, advance } = useSimulation();
|
|
46
|
+
const ticks = props.ticks ?? 1;
|
|
47
|
+
const canStep = status === "idle" || status === "paused";
|
|
48
|
+
return /* @__PURE__ */ jsx(
|
|
49
|
+
"button",
|
|
50
|
+
{
|
|
51
|
+
type: "button",
|
|
52
|
+
style: btnStyle,
|
|
53
|
+
onClick: () => advance(ticks),
|
|
54
|
+
disabled: props.disabled ?? !canStep,
|
|
55
|
+
title: "Advance one or more ticks while paused",
|
|
56
|
+
children: props.label ?? `Step${ticks > 1 ? ` (${ticks})` : ""}`
|
|
57
|
+
}
|
|
58
|
+
);
|
|
59
|
+
}
|
|
60
|
+
function ResetButton(props) {
|
|
61
|
+
const { resetWith } = useSimulation();
|
|
62
|
+
return /* @__PURE__ */ jsx("button", { type: "button", style: btnStyle, onClick: () => resetWith(), children: props.label ?? "Reset" });
|
|
63
|
+
}
|
|
64
|
+
function TickSeekSlider(props) {
|
|
65
|
+
const { tick, status, seek } = useSimulation();
|
|
66
|
+
const min = props.min ?? 0;
|
|
67
|
+
const max = props.max;
|
|
68
|
+
const disabled = status === "stopped";
|
|
69
|
+
if (!Number.isFinite(max)) return null;
|
|
70
|
+
return /* @__PURE__ */ jsxs(
|
|
71
|
+
"div",
|
|
72
|
+
{
|
|
73
|
+
style: {
|
|
74
|
+
display: "flex",
|
|
75
|
+
gap: 10,
|
|
76
|
+
alignItems: "center",
|
|
77
|
+
width: "100%"
|
|
78
|
+
},
|
|
79
|
+
children: [
|
|
80
|
+
/* @__PURE__ */ jsx(
|
|
81
|
+
"input",
|
|
82
|
+
{
|
|
83
|
+
type: "range",
|
|
84
|
+
min,
|
|
85
|
+
max,
|
|
86
|
+
step: 1,
|
|
87
|
+
value: tick,
|
|
88
|
+
onChange: (e) => seek(Number(e.target.value)),
|
|
89
|
+
disabled,
|
|
90
|
+
style: { flex: 1 }
|
|
91
|
+
}
|
|
92
|
+
),
|
|
93
|
+
/* @__PURE__ */ jsx(
|
|
94
|
+
"span",
|
|
95
|
+
{
|
|
96
|
+
style: {
|
|
97
|
+
fontFamily: "monospace",
|
|
98
|
+
opacity: 0.8,
|
|
99
|
+
minWidth: 48,
|
|
100
|
+
textAlign: "right"
|
|
101
|
+
},
|
|
102
|
+
children: tick
|
|
103
|
+
}
|
|
104
|
+
)
|
|
105
|
+
]
|
|
106
|
+
}
|
|
107
|
+
);
|
|
108
|
+
}
|
|
109
|
+
function ParamRangeField(props) {
|
|
110
|
+
const { params, setParams } = useSimulation();
|
|
111
|
+
const label = props.label ?? props.param;
|
|
112
|
+
const raw = params[props.param];
|
|
113
|
+
const numericValue = Number(raw);
|
|
114
|
+
return /* @__PURE__ */ jsxs(
|
|
115
|
+
"label",
|
|
116
|
+
{
|
|
117
|
+
style: {
|
|
118
|
+
display: "flex",
|
|
119
|
+
gap: 10,
|
|
120
|
+
alignItems: "center",
|
|
121
|
+
width: "100%"
|
|
122
|
+
},
|
|
123
|
+
children: [
|
|
124
|
+
/* @__PURE__ */ jsx("span", { style: { minWidth: 120, opacity: 0.9 }, children: label }),
|
|
125
|
+
/* @__PURE__ */ jsx(
|
|
126
|
+
"input",
|
|
127
|
+
{
|
|
128
|
+
type: "range",
|
|
129
|
+
min: props.min,
|
|
130
|
+
max: props.max,
|
|
131
|
+
step: props.step ?? 1,
|
|
132
|
+
value: Number.isFinite(numericValue) ? numericValue : props.min,
|
|
133
|
+
onChange: (e) => {
|
|
134
|
+
const v = Number(e.target.value);
|
|
135
|
+
setParams({ [props.param]: v });
|
|
136
|
+
},
|
|
137
|
+
style: { flex: 1 }
|
|
138
|
+
}
|
|
139
|
+
),
|
|
140
|
+
/* @__PURE__ */ jsx(
|
|
141
|
+
"span",
|
|
142
|
+
{
|
|
143
|
+
style: {
|
|
144
|
+
width: 72,
|
|
145
|
+
textAlign: "right",
|
|
146
|
+
fontFamily: "monospace",
|
|
147
|
+
opacity: 0.8
|
|
148
|
+
},
|
|
149
|
+
children: numericValue
|
|
150
|
+
}
|
|
151
|
+
)
|
|
152
|
+
]
|
|
153
|
+
}
|
|
154
|
+
);
|
|
155
|
+
}
|
|
156
|
+
function ParamToggleField(props) {
|
|
157
|
+
const { params, setParams } = useSimulation();
|
|
158
|
+
const label = props.label ?? props.param;
|
|
159
|
+
const checked = Boolean(params[props.param]);
|
|
160
|
+
return /* @__PURE__ */ jsxs("label", { style: { display: "flex", gap: 10, alignItems: "center" }, children: [
|
|
161
|
+
/* @__PURE__ */ jsx("span", { style: { minWidth: 120, opacity: 0.9 }, children: label }),
|
|
162
|
+
/* @__PURE__ */ jsx(
|
|
163
|
+
"input",
|
|
164
|
+
{
|
|
165
|
+
type: "checkbox",
|
|
166
|
+
checked,
|
|
167
|
+
onChange: (e) => {
|
|
168
|
+
setParams({ [props.param]: e.target.checked });
|
|
169
|
+
}
|
|
170
|
+
}
|
|
171
|
+
)
|
|
172
|
+
] });
|
|
173
|
+
}
|
|
174
|
+
function TickReadout(props) {
|
|
175
|
+
const { tick } = useSimulation();
|
|
176
|
+
return /* @__PURE__ */ jsxs("span", { style: { fontFamily: "monospace", opacity: 0.85 }, children: [
|
|
177
|
+
props.prefix ?? "",
|
|
178
|
+
"tick: ",
|
|
179
|
+
tick
|
|
180
|
+
] });
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
export {
|
|
184
|
+
PlayPauseButton,
|
|
185
|
+
StopButton,
|
|
186
|
+
StepButton,
|
|
187
|
+
ResetButton,
|
|
188
|
+
TickSeekSlider,
|
|
189
|
+
ParamRangeField,
|
|
190
|
+
ParamToggleField,
|
|
191
|
+
TickReadout
|
|
192
|
+
};
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
// src/react/stableCallback.ts
|
|
2
|
+
import React from "react";
|
|
3
|
+
function useStableCallback(fn) {
|
|
4
|
+
const fnRef = React.useRef(fn);
|
|
5
|
+
React.useEffect(() => {
|
|
6
|
+
fnRef.current = fn;
|
|
7
|
+
});
|
|
8
|
+
return React.useCallback((...args) => fnRef.current(...args), []);
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
export {
|
|
12
|
+
useStableCallback
|
|
13
|
+
};
|
|
@@ -0,0 +1,206 @@
|
|
|
1
|
+
// src/engine.ts
|
|
2
|
+
var PERF_BUFFER_SIZE = 120;
|
|
3
|
+
var SimulationEngine = class {
|
|
4
|
+
data;
|
|
5
|
+
params;
|
|
6
|
+
tick = 0;
|
|
7
|
+
status = "idle";
|
|
8
|
+
lastUpdateMs = null;
|
|
9
|
+
lastStepMs = 0;
|
|
10
|
+
initFn;
|
|
11
|
+
stepFn;
|
|
12
|
+
shouldStopFn;
|
|
13
|
+
maxTime;
|
|
14
|
+
delayMs;
|
|
15
|
+
ticksPerFrame;
|
|
16
|
+
listeners = /* @__PURE__ */ new Set();
|
|
17
|
+
historyListeners = /* @__PURE__ */ new Set();
|
|
18
|
+
perfBuffer = [];
|
|
19
|
+
constructor(config) {
|
|
20
|
+
this.initFn = config.init;
|
|
21
|
+
this.stepFn = config.step;
|
|
22
|
+
this.shouldStopFn = config.shouldStop;
|
|
23
|
+
this.maxTime = config.maxTime;
|
|
24
|
+
this.delayMs = config.delayMs ?? 0;
|
|
25
|
+
this.ticksPerFrame = config.ticksPerFrame ?? 1;
|
|
26
|
+
this.params = { ...config.initialParams };
|
|
27
|
+
this.data = this.initFn(this.params);
|
|
28
|
+
}
|
|
29
|
+
getSnapshot() {
|
|
30
|
+
return {
|
|
31
|
+
data: this.data,
|
|
32
|
+
params: this.params,
|
|
33
|
+
tick: this.tick,
|
|
34
|
+
status: this.status,
|
|
35
|
+
stepDurationMs: this.lastStepMs
|
|
36
|
+
};
|
|
37
|
+
}
|
|
38
|
+
getStatus() {
|
|
39
|
+
return this.status;
|
|
40
|
+
}
|
|
41
|
+
getPerformance() {
|
|
42
|
+
return this.perfBuffer;
|
|
43
|
+
}
|
|
44
|
+
setDelayMs(ms) {
|
|
45
|
+
this.delayMs = ms;
|
|
46
|
+
}
|
|
47
|
+
setTicksPerFrame(n) {
|
|
48
|
+
this.ticksPerFrame = n;
|
|
49
|
+
}
|
|
50
|
+
recordDrawTime(tick, ms) {
|
|
51
|
+
for (let i = this.perfBuffer.length - 1; i >= 0; i--) {
|
|
52
|
+
if (this.perfBuffer[i].tick === tick) {
|
|
53
|
+
this.perfBuffer[i].drawMs = ms;
|
|
54
|
+
return;
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
subscribe(listener) {
|
|
59
|
+
this.listeners.add(listener);
|
|
60
|
+
return () => {
|
|
61
|
+
this.listeners.delete(listener);
|
|
62
|
+
};
|
|
63
|
+
}
|
|
64
|
+
subscribeHistory(listener) {
|
|
65
|
+
this.historyListeners.add(listener);
|
|
66
|
+
return () => {
|
|
67
|
+
this.historyListeners.delete(listener);
|
|
68
|
+
};
|
|
69
|
+
}
|
|
70
|
+
play() {
|
|
71
|
+
if (this.status === "stopped") return;
|
|
72
|
+
if (this.status === "playing") return;
|
|
73
|
+
this.status = "playing";
|
|
74
|
+
this.lastUpdateMs = null;
|
|
75
|
+
this.emit();
|
|
76
|
+
}
|
|
77
|
+
pause() {
|
|
78
|
+
if (this.status !== "playing") return;
|
|
79
|
+
this.status = "paused";
|
|
80
|
+
this.emit();
|
|
81
|
+
}
|
|
82
|
+
stop() {
|
|
83
|
+
if (this.status === "idle" || this.status === "stopped") return;
|
|
84
|
+
this.status = "stopped";
|
|
85
|
+
this.emit();
|
|
86
|
+
}
|
|
87
|
+
seek(targetTick) {
|
|
88
|
+
if (this.status === "stopped") return;
|
|
89
|
+
if (targetTick <= this.tick) {
|
|
90
|
+
if (this.status !== "paused") {
|
|
91
|
+
this.status = "paused";
|
|
92
|
+
this.emit();
|
|
93
|
+
}
|
|
94
|
+
return;
|
|
95
|
+
}
|
|
96
|
+
this.status = "paused";
|
|
97
|
+
this.lastUpdateMs = null;
|
|
98
|
+
this.advanceTicks(targetTick - this.tick);
|
|
99
|
+
if (this.status === "paused") {
|
|
100
|
+
this.emit();
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
advance(count = 1) {
|
|
104
|
+
if (this.status === "stopped") return;
|
|
105
|
+
if (this.status === "playing" || this.status === "idle") {
|
|
106
|
+
this.status = "paused";
|
|
107
|
+
}
|
|
108
|
+
this.lastUpdateMs = null;
|
|
109
|
+
this.advanceTicks(count);
|
|
110
|
+
if (this.status === "paused") {
|
|
111
|
+
this.emit();
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
setParams(patch) {
|
|
115
|
+
this.params = { ...this.params, ...patch };
|
|
116
|
+
this.emit();
|
|
117
|
+
}
|
|
118
|
+
resetWith(patch) {
|
|
119
|
+
if (patch) {
|
|
120
|
+
this.params = { ...this.params, ...patch };
|
|
121
|
+
}
|
|
122
|
+
this.data = this.initFn(this.params);
|
|
123
|
+
this.tick = 0;
|
|
124
|
+
this.status = "idle";
|
|
125
|
+
this.lastUpdateMs = null;
|
|
126
|
+
this.lastStepMs = 0;
|
|
127
|
+
this.perfBuffer.length = 0;
|
|
128
|
+
this.emit();
|
|
129
|
+
}
|
|
130
|
+
handleAnimationFrame(nowMs) {
|
|
131
|
+
if (this.status !== "playing") return;
|
|
132
|
+
if (this.lastUpdateMs === null) {
|
|
133
|
+
this.lastUpdateMs = nowMs;
|
|
134
|
+
return;
|
|
135
|
+
}
|
|
136
|
+
if (this.delayMs > 0 && nowMs - this.lastUpdateMs < this.delayMs) return;
|
|
137
|
+
this.lastUpdateMs = nowMs;
|
|
138
|
+
this.advanceTicks(this.ticksPerFrame);
|
|
139
|
+
if (this.status === "playing") {
|
|
140
|
+
this.emit();
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
destroy() {
|
|
144
|
+
this.listeners.clear();
|
|
145
|
+
this.historyListeners.clear();
|
|
146
|
+
}
|
|
147
|
+
/**
|
|
148
|
+
* Run step up to `count` ticks. Returns true if all ticks completed
|
|
149
|
+
* without termination, false if stopped early.
|
|
150
|
+
*/
|
|
151
|
+
advanceTicks(count) {
|
|
152
|
+
for (let i = 0; i < count; i++) {
|
|
153
|
+
if (this.maxTime !== void 0 && this.tick >= this.maxTime) {
|
|
154
|
+
this.status = "stopped";
|
|
155
|
+
this.emit();
|
|
156
|
+
return false;
|
|
157
|
+
}
|
|
158
|
+
this.tick += 1;
|
|
159
|
+
const t0 = performance.now();
|
|
160
|
+
this.data = this.stepFn({
|
|
161
|
+
data: this.data,
|
|
162
|
+
params: this.params,
|
|
163
|
+
tick: this.tick
|
|
164
|
+
});
|
|
165
|
+
const t1 = performance.now();
|
|
166
|
+
this.lastStepMs = t1 - t0;
|
|
167
|
+
if (this.perfBuffer.length >= PERF_BUFFER_SIZE) {
|
|
168
|
+
this.perfBuffer.shift();
|
|
169
|
+
}
|
|
170
|
+
this.perfBuffer.push({ tick: this.tick, stepMs: this.lastStepMs });
|
|
171
|
+
this.emitHistory();
|
|
172
|
+
if (this.shouldStopFn?.(this.data, this.params)) {
|
|
173
|
+
this.status = "stopped";
|
|
174
|
+
this.emit();
|
|
175
|
+
return false;
|
|
176
|
+
}
|
|
177
|
+
if (this.maxTime !== void 0 && this.tick >= this.maxTime) {
|
|
178
|
+
this.status = "stopped";
|
|
179
|
+
this.emit();
|
|
180
|
+
return false;
|
|
181
|
+
}
|
|
182
|
+
}
|
|
183
|
+
return true;
|
|
184
|
+
}
|
|
185
|
+
emit() {
|
|
186
|
+
const snap = this.getSnapshot();
|
|
187
|
+
for (const l of this.listeners) {
|
|
188
|
+
l(snap);
|
|
189
|
+
}
|
|
190
|
+
}
|
|
191
|
+
emitHistory() {
|
|
192
|
+
if (this.historyListeners.size === 0) return;
|
|
193
|
+
const entry = { tick: this.tick, data: this.data };
|
|
194
|
+
for (const l of this.historyListeners) {
|
|
195
|
+
l(entry);
|
|
196
|
+
}
|
|
197
|
+
}
|
|
198
|
+
};
|
|
199
|
+
function createEngine(config) {
|
|
200
|
+
return new SimulationEngine(config);
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
export {
|
|
204
|
+
SimulationEngine,
|
|
205
|
+
createEngine
|
|
206
|
+
};
|