@react-three/fiber 10.0.0-canary.da7a768 → 10.0.0-canary.dc0ec15

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.
@@ -0,0 +1,278 @@
1
+ 'use strict';
2
+
3
+ const React = require('react');
4
+ require('react/jsx-runtime');
5
+ require('its-fine');
6
+ const scheduler = require('@pmndrs/scheduler');
7
+
8
+ function _interopNamespaceCompat(e) {
9
+ if (e && typeof e === 'object' && 'default' in e) return e;
10
+ const n = Object.create(null);
11
+ if (e) {
12
+ for (const k in e) {
13
+ n[k] = e[k];
14
+ }
15
+ }
16
+ n.default = e;
17
+ return n;
18
+ }
19
+
20
+ const React__namespace = /*#__PURE__*/_interopNamespaceCompat(React);
21
+
22
+ const R3F_CONTEXT = Symbol.for("@react-three/fiber.context");
23
+ const context = globalThis[R3F_CONTEXT] ?? (globalThis[R3F_CONTEXT] = React__namespace.createContext(null));
24
+
25
+ function useStore() {
26
+ const store = React.useContext(context);
27
+ if (!store) throw new Error("R3F: Hooks can only be used within the Canvas component!");
28
+ return store;
29
+ }
30
+ function useThree(selector = (state) => state, equalityFn) {
31
+ return useStore()(selector, equalityFn);
32
+ }
33
+
34
+ const useIsomorphicLayoutEffect = /* @__PURE__ */ (() => typeof window !== "undefined" && (window.document?.createElement || window.navigator?.product === "ReactNative"))() ? React__namespace.useLayoutEffect : React__namespace.useEffect;
35
+ function useMutableCallback(fn) {
36
+ const ref = React__namespace.useRef(fn);
37
+ useIsomorphicLayoutEffect(() => void (ref.current = fn), [fn]);
38
+ return ref;
39
+ }
40
+
41
+ const shownNotices = /* @__PURE__ */ new Set();
42
+ function notifyDepreciated({ heading, body, link }) {
43
+ if (typeof process !== "undefined" && (process.env.NODE_ENV === "test" || process.env.JEST_WORKER_ID !== void 0) && process.env.R3F_SHOW_DEPRECATION_WARNINGS !== "true") {
44
+ return;
45
+ }
46
+ if (shownNotices.has(heading)) return;
47
+ shownNotices.add(heading);
48
+ const caller = getCallerFrame();
49
+ if (caller) {
50
+ console.log();
51
+ }
52
+ const boxStyle = "background: #ff9800; color: #1a1a1a; padding: 8px 12px; border-radius: 4px; font-weight: 500;";
53
+ if (caller) {
54
+ console.log(`%c\u26A0\uFE0F ${heading}`, boxStyle);
55
+ }
56
+ {
57
+ let message = "";
58
+ message += body;
59
+ message += ("\n\n" ) + `More info: ${link}`;
60
+ console.warn(`%c${message}`, "font-weight: bold;");
61
+ }
62
+ }
63
+ function getCallerFrame(depth = 3) {
64
+ const stack = new Error().stack;
65
+ if (!stack) return null;
66
+ const lines = stack.split("\n");
67
+ const frame = lines[depth];
68
+ if (!frame) return null;
69
+ let match = frame.match(/^\s*at (?:(.+?) )?\(?(.+?):(\d+):(\d+)\)?$/);
70
+ if (!match) {
71
+ match = frame.match(/^(?:(.+?)@)?(.+?):(\d+):(\d+)$/);
72
+ }
73
+ if (!match) return null;
74
+ const [, fn, url, line] = match;
75
+ return {
76
+ functionName: fn ?? "<anonymous>",
77
+ location: formatLocation(url, Number(line))
78
+ };
79
+ }
80
+ function formatLocation(url, line) {
81
+ const clean = url.split("?")[0];
82
+ const file = clean.split("/").pop() ?? clean;
83
+ return `${file}:${line}`;
84
+ }
85
+
86
+ function useFrame(callback, priorityOrOptions) {
87
+ const store = React__namespace.useContext(context);
88
+ const isInsideCanvas = store !== null;
89
+ const scheduler$1 = scheduler.getScheduler();
90
+ const optionsKey = typeof priorityOrOptions === "number" ? `p:${priorityOrOptions}` : priorityOrOptions ? JSON.stringify({
91
+ id: priorityOrOptions.id,
92
+ phase: priorityOrOptions.phase,
93
+ priority: priorityOrOptions.priority,
94
+ fps: priorityOrOptions.fps,
95
+ drop: priorityOrOptions.drop,
96
+ enabled: priorityOrOptions.enabled,
97
+ before: priorityOrOptions.before,
98
+ after: priorityOrOptions.after
99
+ }) : "";
100
+ const options = React__namespace.useMemo(() => {
101
+ return typeof priorityOrOptions === "number" ? { priority: priorityOrOptions } : priorityOrOptions ?? {};
102
+ }, [optionsKey]);
103
+ const reactId = React__namespace.useId();
104
+ const id = options.id ?? reactId;
105
+ const callbackRef = useMutableCallback(callback);
106
+ const isLegacyPriority = typeof priorityOrOptions === "number" && priorityOrOptions > 0;
107
+ useIsomorphicLayoutEffect(() => {
108
+ if (!callback) return;
109
+ if (isInsideCanvas) {
110
+ const state = store.getState();
111
+ const rootId = state.internal.rootId;
112
+ if (isLegacyPriority) {
113
+ state.internal.priority++;
114
+ let parentRoot = state.previousRoot;
115
+ while (parentRoot) {
116
+ const parentState = parentRoot.getState();
117
+ if (parentState?.internal) parentState.internal.priority++;
118
+ parentRoot = parentState?.previousRoot;
119
+ }
120
+ notifyDepreciated({
121
+ heading: "useFrame with numeric priority is deprecated",
122
+ body: 'Using useFrame(callback, number) to control render order is deprecated.\n\nFor custom rendering, use: useFrame(callback, { phase: "render" })\nFor execution order within update phase, use: useFrame(callback, { priority: number })',
123
+ link: "https://docs.pmnd.rs/react-three-fiber/api/hooks#useframe"
124
+ });
125
+ }
126
+ const wrappedCallback = (frameState, delta) => {
127
+ const localState = store.getState();
128
+ const mergedState = {
129
+ ...localState,
130
+ time: frameState.time,
131
+ delta: frameState.delta,
132
+ elapsed: frameState.elapsed,
133
+ frame: frameState.frame
134
+ };
135
+ callbackRef.current?.(mergedState, delta);
136
+ };
137
+ const unregister = scheduler$1.register(wrappedCallback, {
138
+ id,
139
+ rootId,
140
+ ...options
141
+ });
142
+ return () => {
143
+ unregister();
144
+ if (isLegacyPriority) {
145
+ const currentState = store.getState();
146
+ if (currentState.internal) {
147
+ currentState.internal.priority--;
148
+ let parentRoot = currentState.previousRoot;
149
+ while (parentRoot) {
150
+ const parentState = parentRoot.getState();
151
+ if (parentState?.internal) parentState.internal.priority--;
152
+ parentRoot = parentState?.previousRoot;
153
+ }
154
+ }
155
+ }
156
+ };
157
+ } else {
158
+ return scheduler$1.register(
159
+ (state, delta) => {
160
+ const frameState = state;
161
+ if (!frameState.renderer) return;
162
+ callbackRef.current?.(frameState, delta);
163
+ },
164
+ { id, ...options }
165
+ );
166
+ }
167
+ }, [store, scheduler$1, id, optionsKey, isLegacyPriority, isInsideCanvas]);
168
+ const isPaused = React__namespace.useSyncExternalStore(
169
+ // Subscribe function
170
+ React__namespace.useCallback(
171
+ (onStoreChange) => {
172
+ return scheduler.getScheduler().subscribeJobState(id, onStoreChange);
173
+ },
174
+ [id]
175
+ ),
176
+ // getSnapshot function
177
+ React__namespace.useCallback(() => scheduler.getScheduler().isJobPaused(id), [id]),
178
+ // getServerSnapshot function (SSR)
179
+ React__namespace.useCallback(() => false, [])
180
+ );
181
+ const controls = React__namespace.useMemo(() => {
182
+ const scheduler2 = scheduler.getScheduler();
183
+ return {
184
+ /** The job's unique ID */
185
+ id,
186
+ /**
187
+ * Access to the global scheduler for frame loop control.
188
+ * Use for controlling the entire frame loop, adding phases, etc.
189
+ */
190
+ scheduler: scheduler2,
191
+ /**
192
+ * The root that currently owns this job. Resolve on access because ambient
193
+ * jobs can be adopted by a Canvas after the controls object is created.
194
+ */
195
+ get rootId() {
196
+ return scheduler.getScheduler().getJobRootId(id);
197
+ },
198
+ /**
199
+ * Manually step this job only.
200
+ * Bypasses FPS limiting - always runs.
201
+ * @param timestamp Optional timestamp (defaults to performance.now())
202
+ */
203
+ step: (timestamp) => {
204
+ scheduler.getScheduler().stepJob(id, timestamp);
205
+ },
206
+ /**
207
+ * Manually step ALL jobs in the scheduler.
208
+ * Useful for frameloop='never' mode.
209
+ * @param timestamp Optional timestamp (defaults to performance.now())
210
+ */
211
+ stepAll: (timestamp) => {
212
+ scheduler.getScheduler().step(timestamp);
213
+ },
214
+ /**
215
+ * Request frames for this job's owning root without waking sibling roots.
216
+ */
217
+ invalidate: (frames, stackFrames) => {
218
+ const scheduler3 = scheduler.getScheduler();
219
+ const rootId = scheduler3.getJobRootId(id);
220
+ if (rootId) scheduler3.invalidateRoot(rootId, frames, stackFrames);
221
+ },
222
+ /**
223
+ * Pause this job (set enabled=false).
224
+ * Job remains registered but won't run.
225
+ */
226
+ pause: () => {
227
+ scheduler.getScheduler().pauseJob(id);
228
+ },
229
+ /**
230
+ * Resume this job (set enabled=true).
231
+ */
232
+ resume: () => {
233
+ scheduler.getScheduler().resumeJob(id);
234
+ },
235
+ /**
236
+ * Reactive paused state - automatically updates when pause/resume is called.
237
+ * No need for forceUpdate() in your components.
238
+ */
239
+ isPaused
240
+ };
241
+ }, [id, isPaused]);
242
+ return controls;
243
+ }
244
+
245
+ const R3F_EXTENSIONS = Symbol.for("@react-three/fiber.extensions");
246
+ function getRegistry() {
247
+ return globalThis[R3F_EXTENSIONS] ?? (globalThis[R3F_EXTENSIONS] = {
248
+ extensions: /* @__PURE__ */ new Map(),
249
+ roots: /* @__PURE__ */ new Set(),
250
+ applied: /* @__PURE__ */ new WeakMap()
251
+ });
252
+ }
253
+ function setupRoot(registry, store, extension) {
254
+ let applied = registry.applied.get(store);
255
+ if (!applied) registry.applied.set(store, applied = /* @__PURE__ */ new Map());
256
+ if (applied.has(extension.name)) return;
257
+ applied.set(extension.name, extension);
258
+ const patch = extension.setup?.(store);
259
+ if (patch) store.setState(patch);
260
+ }
261
+ function registerRootExtension(extension) {
262
+ const registry = getRegistry();
263
+ registry.extensions.set(extension.name, extension);
264
+ for (const store of registry.roots) setupRoot(registry, store, extension);
265
+ return () => {
266
+ if (registry.extensions.get(extension.name) === extension) registry.extensions.delete(extension.name);
267
+ };
268
+ }
269
+ function setRenderOverride(store, render) {
270
+ store.getState().internal.renderOverride = render;
271
+ }
272
+
273
+ exports.context = context;
274
+ exports.registerRootExtension = registerRootExtension;
275
+ exports.setRenderOverride = setRenderOverride;
276
+ exports.useFrame = useFrame;
277
+ exports.useStore = useStore;
278
+ exports.useThree = useThree;