Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(core): Global config #269

Merged
merged 4 commits into from
Feb 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions docs/whats-new.md
Original file line number Diff line number Diff line change
Expand Up @@ -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()`).
Expand Down
29 changes: 22 additions & 7 deletions modules/core/src/lib/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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<ConfigurationOptions> = {
EPSILON: 1e-12,
debug: false,
precision: 4,
printTypes: false,
printDegrees: false,
printRowMajor: true
printRowMajor: true,
_cartographicRadians: false
};

export function configure(options?: Partial<ConfigurationOptions>): ConfigurationOptions {
// We use a global field to store the config
declare global {
// eslint-disable-next-line no-var
var mathgl: {
config: Required<ConfigurationOptions>;
};
}

// 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>): ConfigurationOptions {
// Only copy existing keys
Object.assign(config, options);
return config;
}
Expand Down
13 changes: 12 additions & 1 deletion modules/types/src/array-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,29 @@
export type TypedArray =
| Int8Array
| Uint8Array
| Uint8ClampedArray
| Int16Array
| Uint16Array
| Int32Array
| Uint32Array
| Uint8ClampedArray
| Float32Array
| Float64Array;

// TODO
// | 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
*/
Expand Down
2 changes: 1 addition & 1 deletion modules/types/src/index.ts
Original file line number Diff line number Diff line change
@@ -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';