@bitcobblers/wod-wiki-lang 0.60.2250 → 0.60.2258

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/dist/react.d.cts CHANGED
@@ -7,24 +7,13 @@ import React$1, { ReactNode } from 'react';
7
7
  * useRuntimeExecution - Custom React hook for managing ScriptRuntime execution
8
8
  *
9
9
  * Encapsulates execution logic, interval management, and lifecycle cleanup.
10
- *
11
- * Architecture Decision:
12
- * - Fixed 20ms tick rate (50 ticks per second)
13
- * - No speed control (removed for simplicity)
14
- * - Automatic cleanup on unmount
15
- * - Reusable across components
16
10
  */
17
11
 
18
- /**
19
- * Execution status states
20
- */
21
12
  type ExecutionStatus = 'idle' | 'running' | 'paused' | 'completed' | 'error';
22
- /**
23
- * Return type for useRuntimeExecution hook
24
- */
25
13
  interface UseRuntimeExecutionReturn {
26
14
  status: ExecutionStatus;
27
15
  elapsedTime: number;
16
+ getElapsedTime: () => number;
28
17
  stepCount: number;
29
18
  startTime: number | null;
30
19
  start: () => void;
@@ -33,12 +22,6 @@ interface UseRuntimeExecutionReturn {
33
22
  reset: () => void;
34
23
  step: () => void;
35
24
  }
36
- /**
37
- * Manages ScriptRuntime execution lifecycle with fixed 20ms tick rate
38
- *
39
- * @param runtime - The ScriptRuntime instance to execute (null if not ready)
40
- * @returns Object with execution state and control methods
41
- */
42
25
  declare const useRuntimeExecution: (runtime: ScriptRuntime | null) => UseRuntimeExecutionReturn;
43
26
 
44
27
  /**
package/dist/react.d.ts CHANGED
@@ -7,24 +7,13 @@ import React$1, { ReactNode } from 'react';
7
7
  * useRuntimeExecution - Custom React hook for managing ScriptRuntime execution
8
8
  *
9
9
  * Encapsulates execution logic, interval management, and lifecycle cleanup.
10
- *
11
- * Architecture Decision:
12
- * - Fixed 20ms tick rate (50 ticks per second)
13
- * - No speed control (removed for simplicity)
14
- * - Automatic cleanup on unmount
15
- * - Reusable across components
16
10
  */
17
11
 
18
- /**
19
- * Execution status states
20
- */
21
12
  type ExecutionStatus = 'idle' | 'running' | 'paused' | 'completed' | 'error';
22
- /**
23
- * Return type for useRuntimeExecution hook
24
- */
25
13
  interface UseRuntimeExecutionReturn {
26
14
  status: ExecutionStatus;
27
15
  elapsedTime: number;
16
+ getElapsedTime: () => number;
28
17
  stepCount: number;
29
18
  startTime: number | null;
30
19
  start: () => void;
@@ -33,12 +22,6 @@ interface UseRuntimeExecutionReturn {
33
22
  reset: () => void;
34
23
  step: () => void;
35
24
  }
36
- /**
37
- * Manages ScriptRuntime execution lifecycle with fixed 20ms tick rate
38
- *
39
- * @param runtime - The ScriptRuntime instance to execute (null if not ready)
40
- * @returns Object with execution state and control methods
41
- */
42
25
  declare const useRuntimeExecution: (runtime: ScriptRuntime | null) => UseRuntimeExecutionReturn;
43
26
 
44
27
  /**
package/dist/react.mjs CHANGED
@@ -28,18 +28,36 @@ var useRuntimeExecution = (runtime) => {
28
28
  const [stepCount, setStepCount] = useState(0);
29
29
  const [startTime, setStartTime] = useState(null);
30
30
  const intervalRef = useRef(null);
31
- const startTimeRef = useRef(null);
31
+ const statusRef = useRef("idle");
32
+ const accumulatedElapsedRef = useRef(0);
33
+ const segmentStartRef = useRef(null);
34
+ const hasStartTimeRef = useRef(false);
35
+ const readNowMs = useCallback(
36
+ () => runtime?.nowProvider?.nowMs() ?? wallClockNow.nowMs(),
37
+ [runtime]
38
+ );
39
+ const readNowDate = useCallback(
40
+ () => runtime?.nowProvider?.now() ?? wallClockNow.now(),
41
+ [runtime]
42
+ );
43
+ const getElapsedTime = useCallback(() => {
44
+ if (statusRef.current === "running" && segmentStartRef.current !== null) {
45
+ return accumulatedElapsedRef.current + Math.max(0, readNowMs() - segmentStartRef.current);
46
+ }
47
+ return accumulatedElapsedRef.current;
48
+ }, [readNowMs]);
32
49
  const stop = useCallback(() => {
33
50
  if (intervalRef.current) {
34
51
  clearInterval(intervalRef.current);
35
52
  intervalRef.current = null;
36
53
  }
37
- if (startTimeRef.current !== null) {
38
- const now = runtime?.nowProvider?.nowMs() ?? wallClockNow.nowMs();
39
- setElapsedTime(now - startTimeRef.current);
40
- }
54
+ const finalElapsed = getElapsedTime();
55
+ accumulatedElapsedRef.current = finalElapsed;
56
+ segmentStartRef.current = null;
57
+ statusRef.current = "idle";
58
+ setElapsedTime(finalElapsed);
41
59
  setStatus("idle");
42
- }, [runtime]);
60
+ }, [getElapsedTime]);
43
61
  const executeStep = useCallback(() => {
44
62
  if (!runtime) return;
45
63
  try {
@@ -48,26 +66,28 @@ var useRuntimeExecution = (runtime) => {
48
66
  setStepCount((prev) => prev + 1);
49
67
  } catch {
50
68
  stop();
69
+ statusRef.current = "error";
51
70
  setStatus("error");
52
71
  }
53
72
  }, [runtime, stop]);
54
73
  useEffect(() => {
55
74
  if (!runtime) return;
56
75
  const unsubscribe = runtime.subscribeToStack((snapshot) => {
57
- if (snapshot.blocks.length === 0 && status !== "completed") {
76
+ if (snapshot.blocks.length === 0 && statusRef.current !== "completed") {
58
77
  if (intervalRef.current) {
59
78
  clearInterval(intervalRef.current);
60
79
  intervalRef.current = null;
61
80
  }
62
- if (startTimeRef.current !== null) {
63
- const now = runtime?.nowProvider?.nowMs() ?? wallClockNow.nowMs();
64
- setElapsedTime(now - startTimeRef.current);
65
- }
81
+ const finalElapsed = getElapsedTime();
82
+ accumulatedElapsedRef.current = finalElapsed;
83
+ segmentStartRef.current = null;
84
+ statusRef.current = "completed";
85
+ setElapsedTime(finalElapsed);
66
86
  setStatus("completed");
67
87
  }
68
88
  });
69
89
  return unsubscribe;
70
- }, [runtime, status]);
90
+ }, [runtime, getElapsedTime]);
71
91
  useEffect(() => {
72
92
  return () => {
73
93
  if (intervalRef.current) {
@@ -81,69 +101,75 @@ var useRuntimeExecution = (runtime) => {
81
101
  console.warn("useRuntimeExecution: Cannot start execution without a runtime");
82
102
  return;
83
103
  }
84
- if (status === "running") {
104
+ if (statusRef.current === "running") {
85
105
  console.warn("useRuntimeExecution: Runtime is already running");
86
106
  return;
87
107
  }
88
- if (status === "paused") {
108
+ if (statusRef.current === "paused") {
89
109
  runtime.handle({
90
110
  name: "timer:resume",
91
- timestamp: /* @__PURE__ */ new Date(),
111
+ timestamp: readNowDate(),
92
112
  data: {}
93
113
  });
94
114
  }
95
- setStatus("running");
96
- const now = runtime?.nowProvider?.nowMs() ?? wallClockNow.nowMs();
97
- startTimeRef.current = now - elapsedTime;
98
- if (elapsedTime === 0) {
115
+ const now = readNowMs();
116
+ segmentStartRef.current = now;
117
+ if (!hasStartTimeRef.current) {
118
+ hasStartTimeRef.current = true;
99
119
  setStartTime(now);
100
120
  }
121
+ statusRef.current = "running";
122
+ setStatus("running");
101
123
  intervalRef.current = setInterval(executeStep, EXECUTION_TICK_RATE_MS);
102
- }, [runtime, status, elapsedTime, executeStep]);
124
+ }, [runtime, readNowDate, readNowMs, executeStep]);
103
125
  const pause = useCallback(() => {
104
126
  if (intervalRef.current) {
105
127
  clearInterval(intervalRef.current);
106
128
  intervalRef.current = null;
107
129
  }
108
- if (startTimeRef.current !== null) {
109
- const now = runtime?.nowProvider?.nowMs() ?? wallClockNow.nowMs();
110
- setElapsedTime(now - startTimeRef.current);
111
- }
130
+ const pausedElapsed = getElapsedTime();
131
+ accumulatedElapsedRef.current = pausedElapsed;
132
+ segmentStartRef.current = null;
133
+ setElapsedTime(pausedElapsed);
112
134
  if (runtime) {
113
- const pauseDate = runtime.nowProvider?.now() ?? wallClockNow.now();
114
135
  runtime.handle({
115
136
  name: "timer:pause",
116
- timestamp: pauseDate,
137
+ timestamp: readNowDate(),
117
138
  data: {}
118
139
  });
119
140
  }
141
+ statusRef.current = "paused";
120
142
  setStatus("paused");
121
- }, [runtime]);
143
+ }, [runtime, getElapsedTime, readNowDate]);
122
144
  const reset = useCallback(() => {
123
145
  if (intervalRef.current) {
124
146
  clearInterval(intervalRef.current);
125
147
  intervalRef.current = null;
126
148
  }
149
+ accumulatedElapsedRef.current = 0;
150
+ segmentStartRef.current = null;
151
+ hasStartTimeRef.current = false;
152
+ statusRef.current = "idle";
127
153
  setElapsedTime(0);
128
154
  setStepCount(0);
129
155
  setStatus("idle");
130
156
  setStartTime(null);
131
- startTimeRef.current = null;
132
- }, [runtime]);
157
+ }, []);
133
158
  const step = useCallback(() => {
134
- if (!runtime || status === "running") {
159
+ if (!runtime || statusRef.current === "running") {
135
160
  return;
136
161
  }
162
+ statusRef.current = "paused";
137
163
  setStatus("paused");
138
164
  executeStep();
139
- }, [runtime, status, executeStep]);
165
+ }, [runtime, executeStep]);
140
166
  useEffect(() => {
141
- if (status !== "running" || !startTimeRef.current) return;
167
+ if (status !== "running") return;
142
168
  const updateInterval = setInterval(() => {
143
- setElapsedTime((runtime?.nowProvider?.nowMs() ?? wallClockNow.nowMs()) - startTimeRef.current);
169
+ setElapsedTime(getElapsedTime());
144
170
  }, 100);
145
171
  return () => clearInterval(updateInterval);
146
- }, [status, runtime?.nowProvider]);
172
+ }, [status, getElapsedTime]);
147
173
  useEffect(() => {
148
174
  return () => {
149
175
  if (intervalRef.current) {
@@ -158,6 +184,7 @@ var useRuntimeExecution = (runtime) => {
158
184
  return useMemo(() => ({
159
185
  status,
160
186
  elapsedTime,
187
+ getElapsedTime,
161
188
  stepCount,
162
189
  startTime,
163
190
  start,
@@ -165,7 +192,7 @@ var useRuntimeExecution = (runtime) => {
165
192
  stop,
166
193
  reset,
167
194
  step
168
- }), [status, elapsedTime, stepCount, startTime, start, pause, stop, reset, step]);
195
+ }), [status, elapsedTime, getElapsedTime, stepCount, startTime, start, pause, stop, reset, step]);
169
196
  };
170
197
  function useBlockMemory(block, type) {
171
198
  const tag = type;