-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(global-scope): Add globalScope and sharedScope_ variables
- Loading branch information
Showing
5 changed files
with
73 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters