Skip to content

Commit

Permalink
feat(global-scope): Add globalScope and sharedScope_ variables
Browse files Browse the repository at this point in the history
  • Loading branch information
AliMD committed Dec 22, 2023
1 parent b3cd89e commit 9e9f2ee
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 3 deletions.
20 changes: 20 additions & 0 deletions packages/global-scope/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand All @@ -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'
```
1 change: 1 addition & 0 deletions packages/global-scope/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
}
38 changes: 35 additions & 3 deletions packages/global-scope/src/main.ts
Original file line number Diff line number Diff line change
@@ -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<string, unknown> = {};

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;
Empty file.
17 changes: 17 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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"
Expand Down Expand Up @@ -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"
Expand Down

0 comments on commit 9e9f2ee

Please sign in to comment.