@babylonjs/core
Version:
Getting started? Play directly with the Babylon.js API using our [playground](https://playground.babylonjs.com/). It also contains a lot of samples to learn how to use it.
108 lines • 4.19 kB
JavaScript
const WarnedMap = {};
/**
* @internal
*/
export function _WarnImport(name, warnOnce = false) {
if (warnOnce && WarnedMap[name]) {
return;
}
WarnedMap[name] = true;
return `${name} needs to be imported before as it contains a side-effect required by your code.`;
}
const _StubWarnedMap = {};
let _MissingSideEffectWarningsEnabled = false;
let _MissingSideEffectWarningsSuppressionDepth = 0;
/**
* Enables or disables console warnings when generated side-effect stubs are called.
* Warnings are disabled by default because internal code may probe optional augmented APIs.
* @param enabled - Whether missing side-effect stub calls should emit one-time warnings.
*/
export function SetMissingSideEffectWarningsEnabled(enabled) {
_MissingSideEffectWarningsEnabled = enabled;
}
/**
* Runs a callback while missing side-effect stub warnings are suppressed.
* @param callback - The callback to run with warnings suppressed.
* @returns The callback result.
*/
export function SuppressMissingSideEffectWarnings(callback) {
_MissingSideEffectWarningsSuppressionDepth++;
try {
return callback();
}
finally {
_MissingSideEffectWarningsSuppressionDepth--;
}
}
/**
* Returns a stub function that acts as a placeholder for augmented methods that
* require a side-effect import. The stub is tagged with `__isSideEffectStub` so
* that feature-detection code can check `_IsSideEffectImplemented()` before calling.
*
* The stub does NOT warn by default because internal engine code frequently calls
* augmented methods as feature checks (e.g., `getBoundingBoxRenderer?.()` in the
* render loop). Warnings would fire every frame for features the user never requested.
*
* If the user actually tries to use the returned value (which is undefined), they
* will get a clear TypeError at the point of use.
*
* @param className - The class name (for diagnostic purposes)
* @param methodName - The method name (for diagnostic purposes)
* @param warn - If true, emit a one-time console warning when the stub is called.
* Use this only for methods that are never called internally as feature checks.
* @internal
*/
export function _MissingSideEffect(className, methodName, warn = false) {
const stub = function () {
if ((warn || _MissingSideEffectWarningsEnabled) && _MissingSideEffectWarningsSuppressionDepth === 0) {
const key = `${className}.${methodName}`;
if (!_StubWarnedMap[key]) {
_StubWarnedMap[key] = true;
// eslint-disable-next-line no-console
console.warn(`[Babylon.js] ${key}() requires a side-effect import. See: https://doc.babylonjs.com/setup/treeshaking`);
}
}
};
stub.__isSideEffectStub = true;
return stub;
}
/**
* Checks whether a value is a side-effect stub (i.e., a placeholder function
* generated by _MissingSideEffect). Use this instead of truthiness checks when
* doing feature detection on methods that may be stubbed for tree-shaking.
* @param fn The function/method to check
* @returns true if the function is a real implementation (not a stub or undefined/null)
* @internal
*/
export function _IsSideEffectImplemented(fn) {
if (!fn) {
return false;
}
return !fn.__isSideEffectStub;
}
/**
* Returns a property descriptor that acts as a placeholder for augmented properties
* that require a side-effect import. The getter returns undefined (allowing pure code
* to feature-detect by checking truthiness), and the setter replaces the stub with a
* real data property on the instance.
* @internal
*/
export function _MissingSideEffectProperty(className, propName) {
return {
get() {
return undefined;
},
set(value) {
// Replace the stub with a real data property so the value is stored
Object.defineProperty(this, propName, {
value,
writable: true,
configurable: true,
enumerable: true,
});
},
configurable: true,
enumerable: true,
};
}
//# sourceMappingURL=devTools.js.map