Skip to content

Commit

Permalink
fix: use global constants (#290)
Browse files Browse the repository at this point in the history
* fix: use global constants

* PR review
  • Loading branch information
layershifter authored Dec 7, 2022
1 parent 264a0cd commit 735cbfd
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 6 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"type": "patch",
"comment": "fix: use global constants",
"packageName": "@griffel/core",
"email": "[email protected]",
"dependentChangeType": "patch"
}
14 changes: 14 additions & 0 deletions packages/core/src/constants.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
it('hosts global constants', async () => {
const importA = await import('./constants');

// We are reloading a module, so instances that are created inside a module will be different
// https://jestjs.io/docs/jest-object#jestresetmodules
jest.resetModules();
const importB = await import('./constants');

expect(importA.DEFINITION_LOOKUP_TABLE).toBe(importA.DEFINITION_LOOKUP_TABLE);
expect(importA.DEBUG_RESET_CLASSES).toBe(importA.DEBUG_RESET_CLASSES);

// This constant is not global, expected to be different
expect(importA.UNSUPPORTED_CSS_PROPERTIES).not.toBe(importB.UNSUPPORTED_CSS_PROPERTIES);
});
32 changes: 26 additions & 6 deletions packages/core/src/constants.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,31 @@
import { GriffelStylesUnsupportedCSSProperties, LookupItem, SequenceHash } from './types';

// ----

// Heads up!
// These constants are global and will be shared between Griffel instances.
// Any change in them should happen only in a MAJOR version. If it happens,
// please change "__NAMESPACE_PREFIX__" to include a version.

const __GLOBAL__: any = typeof window === 'undefined' ? global : window;
const __NAMESPACE_PREFIX__ = '@griffel/';

function getGlobalVar<T>(name: string, defaultValue: T): T {
if (!__GLOBAL__[Symbol.for(__NAMESPACE_PREFIX__ + name)]) {
__GLOBAL__[Symbol.for(__NAMESPACE_PREFIX__ + name)] = defaultValue;
}

return __GLOBAL__[Symbol.for(__NAMESPACE_PREFIX__ + name)];
}

/** @internal */
export const DEBUG_RESET_CLASSES = getGlobalVar<Record<string, 1>>('DEBUG_RESET_CLASSES', {});

/** @internal */
export const DEFINITION_LOOKUP_TABLE = getGlobalVar<Record<SequenceHash, LookupItem>>('DEFINITION_LOOKUP_TABLE', {});

// ----

/** @internal */
export const DATA_BUCKET_ATTR = 'data-make-styles-bucket';

Expand All @@ -24,12 +50,6 @@ export const SEQUENCE_SIZE =
? SEQUENCE_PREFIX.length + SEQUENCE_HASH_LENGTH
: SEQUENCE_PREFIX.length + SEQUENCE_HASH_LENGTH + DEBUG_SEQUENCE_SEPARATOR.length + SEQUENCE_HASH_LENGTH;

/** @internal */
export const DEBUG_RESET_CLASSES: Record<string, 1> = {};

/** @internal */
export const DEFINITION_LOOKUP_TABLE: Record<SequenceHash, LookupItem> = {};

// indexes for values in LookupItem tuple

/** @internal */
Expand Down

0 comments on commit 735cbfd

Please sign in to comment.