diff --git a/docs/whats-new.md b/docs/whats-new.md index a40c38c3..0aed2b18 100644 --- a/docs/whats-new.md +++ b/docs/whats-new.md @@ -27,6 +27,10 @@ ## v3.7 (In Development) +**`@math.gl/core` + +- `config` is now truly global (stored on `globalThis`). + **`@math.gl/types` - Add `isTypedArray()` and `isNumericArray()` utilities that both check values and return properly restricted types to help write strictly typed code (avoids the `DataView` issue with `ArrayBuffer.isView()`). diff --git a/modules/core/src/lib/common.ts b/modules/core/src/lib/common.ts index a8f82adb..9558c30c 100644 --- a/modules/core/src/lib/common.ts +++ b/modules/core/src/lib/common.ts @@ -4,6 +4,9 @@ import type {NumericArray} from '@math.gl/types'; import type MathArray from '../classes/base/math-array'; +const RADIANS_TO_DEGREES = (1 / Math.PI) * 180; +const DEGREES_TO_RADIANS = (1 / 180) * Math.PI; + export type ConfigurationOptions = { EPSILON: number; debug?: boolean; @@ -14,20 +17,32 @@ export type ConfigurationOptions = { _cartographicRadians?: boolean; }; -const RADIANS_TO_DEGREES = (1 / Math.PI) * 180; -const DEGREES_TO_RADIANS = (1 / 180) * Math.PI; - -// TODO - remove -export const config: ConfigurationOptions = { +const DEFAULT_CONFIG: Required = { EPSILON: 1e-12, debug: false, precision: 4, printTypes: false, printDegrees: false, - printRowMajor: true + printRowMajor: true, + _cartographicRadians: false }; -export function configure(options?: Partial): ConfigurationOptions { +// We use a global field to store the config +declare global { + // eslint-disable-next-line no-var + var mathgl: { + config: Required; + }; +} + +// Configuration is truly global as of v3.6 to ensure single config even if multiple copies of math.gl +// Multiple copies of config can be quite tricky to debug... +globalThis.mathgl = globalThis.mathgl || {config: {...DEFAULT_CONFIG}}; + +export const config = globalThis.mathgl.config; + +export function configure(options: Partial): ConfigurationOptions { + // Only copy existing keys Object.assign(config, options); return config; } diff --git a/modules/types/src/array-types.ts b/modules/types/src/array-types.ts index 0f6ed9b9..29f4e3b8 100644 --- a/modules/types/src/array-types.ts +++ b/modules/types/src/array-types.ts @@ -4,11 +4,11 @@ export type TypedArray = | Int8Array | Uint8Array + | Uint8ClampedArray | Int16Array | Uint16Array | Int32Array | Uint32Array - | Uint8ClampedArray | Float32Array | Float64Array; @@ -16,6 +16,17 @@ export type TypedArray = // | BigInt64Array // | BigUint64Array; +export type TypedArrayConstructor = + | Int8ArrayConstructor + | Uint8ArrayConstructor + | Uint8ClampedArrayConstructor + | Int16ArrayConstructor + | Uint16ArrayConstructor + | Int32ArrayConstructor + | Uint32ArrayConstructor + | Float32ArrayConstructor + | Float64ArrayConstructor; + /** * TypeScript type covering all typed arrays and classic arrays consisting of numbers */ diff --git a/modules/types/src/index.ts b/modules/types/src/index.ts index 815f0b6d..090eb9b1 100644 --- a/modules/types/src/index.ts +++ b/modules/types/src/index.ts @@ -1,2 +1,2 @@ -export type {TypedArray, NumericArray, NumberArray} from './array-types'; +export type {TypedArray, TypedArrayConstructor, NumericArray, NumberArray} from './array-types'; export {isTypedArray, isNumericArray} from './is-array';