diff --git a/packages/global-scope/README.md b/packages/global-scope/README.md index adcd4d26..285c3048 100644 --- a/packages/global-scope/README.md +++ b/packages/global-scope/README.md @@ -10,6 +10,10 @@ yarn add @alwatr/global-scope ## Usage +### globalScope + +Alternative to `globalThis` that works cross-platform. + ```typescript import {globalScope} from '@alwatr/global-scope'; @@ -22,3 +26,19 @@ globalScope.setTimeout(() => { console.log(globalScope.alwatr.version); // 1.0.0 }, 1_000); ``` + +### sharedScope_ + +A global variable that can be used to share state across modules without accessible publicly in the global scope. + +```typescript +// module1.ts +import {sharedScope_} from '@alwatr/global-scope'; +sharedScope_.foo = 'bar'; +``` + +```typescript +// module2.ts +import {sharedScope_} from '@alwatr/global-scope'; +console.log(sharedScope_.foo); // 'bar' +``` diff --git a/packages/global-scope/package.json b/packages/global-scope/package.json index f092a76b..337dfc37 100644 --- a/packages/global-scope/package.json +++ b/packages/global-scope/package.json @@ -72,6 +72,7 @@ "@alwatr/nano-build": "workspace:^", "@alwatr/prettier-config": "workspace:^", "@alwatr/tsconfig-base": "workspace:^", + "@types/node": "^20.10.5", "typescript": "^5.3.3" } } diff --git a/packages/global-scope/src/main.ts b/packages/global-scope/src/main.ts index 69121cb9..e8c74e50 100644 --- a/packages/global-scope/src/main.ts +++ b/packages/global-scope/src/main.ts @@ -1,8 +1,40 @@ /** * Alternative to `globalThis` that works cross-platform. + * + * @example + * ```typescript + * globalScope.alwatr = { + * ...globalScope.alwatr, + * foo: 'bar', + * } + * ``` */ -export const globalScope = +export const globalScope: typeof globalThis = (typeof globalThis === 'object' && globalThis) || - ('object' === typeof window && window) || - ('object' === typeof global && global) || + (typeof window === 'object' && window) || + (typeof global === 'object' && global) || self; + +/** + * A global variable that can be used to share state across modules without accessible publicly in the global scope. + * + * @example + * ```typescript + * // module1.ts + * shareScope_.foo = 'bar'; + * + * // module2.ts + * console.log(shareScope_.foo); // 'bar' + * ``` + */ +export const sharedScope_: Record = {}; + +declare global { + // eslint-disable-next-line no-var + var __shared_scope_defined__: boolean; +} + +if (globalScope.__shared_scope_defined__ !== undefined) { + throw new Error('global_scope_module_duplicated'); +} +globalScope.__shared_scope_defined__ = true; diff --git a/packages/global-scope/src/platform.ts b/packages/global-scope/src/platform.ts new file mode 100644 index 00000000..e69de29b diff --git a/yarn.lock b/yarn.lock index 716b2d54..b966da95 100644 --- a/yarn.lock +++ b/yarn.lock @@ -63,6 +63,7 @@ __metadata: "@alwatr/nano-build": "workspace:^" "@alwatr/prettier-config": "workspace:^" "@alwatr/tsconfig-base": "workspace:^" + "@types/node": "npm:^20.10.5" typescript: "npm:^5.3.3" languageName: unknown linkType: soft @@ -1058,6 +1059,15 @@ __metadata: languageName: node linkType: hard +"@types/node@npm:^20.10.5": + version: 20.10.5 + resolution: "@types/node@npm:20.10.5" + dependencies: + undici-types: "npm:~5.26.4" + checksum: be30609aae0bfe492097815f166ccc07f465220cb604647fa4e5ec05a1d16c012a41b82b5f11ecfe2485cbb479d4d20384b95b809ca0bcff6d94d5bbafa645bb + languageName: node + linkType: hard + "@types/normalize-package-data@npm:^2.4.1": version: 2.4.3 resolution: "@types/normalize-package-data@npm:2.4.3" @@ -5694,6 +5704,13 @@ __metadata: languageName: node linkType: hard +"undici-types@npm:~5.26.4": + version: 5.26.5 + resolution: "undici-types@npm:5.26.5" + checksum: bb673d7876c2d411b6eb6c560e0c571eef4a01c1c19925175d16e3a30c4c428181fb8d7ae802a261f283e4166a0ac435e2f505743aa9e45d893f9a3df017b501 + languageName: node + linkType: hard + "unicorn-magic@npm:^0.1.0": version: 0.1.0 resolution: "unicorn-magic@npm:0.1.0"