1.1.26 • Published 8 months ago

@alwatr/global-scope v1.1.26

Weekly downloads
-
License
AGPL-3.0-only
Repository
github
Last release
8 months ago

Global Scope Module

globalScope

This TypeScript module provides a cross-platform alternative to globalThis named globalScope. This object works across different environments, including browsers (window), Node.js (global), and Web Workers (self).

import {globalScope} from '@alwatr/global-scope';

globalScope.alwatr = {
  ...globalScope.alwatr,
  version: '1.0.0',
};

globalScope.setTimeout(() => {
  console.log(globalScope.alwatr.version); // 1.0.0
}, 1_000);

The module also includes a polyfill for globalThis to ensure compatibility across different JavaScript environments.

if (globalScope.globalThis !== globalScope) {
  globalScope.globalThis = globalScope;
}

Shared Scope

The module exports a sharedScope_ object. This object can be used to share state across different modules without making the data publicly accessible in the global scope.

For example, one module can set a property on sharedScope_, and another module can read that property. This allows for data sharing between different parts of your application.

// module1.ts
import {sharedScope_} from '@alwatr/global-scope';
sharedScope_.foo = 'bar';
// module2.ts
import {sharedScope_} from '@alwatr/global-scope';
console.log(sharedScope_.foo); // 'bar'

Global Scope Duplication Check

The module includes a check for duplication of the global scope definition. If the global scope has already been defined, an error is thrown.

if (globalScope.__shared_scope_defined__ !== undefined) {
  throw new Error('global_scope_module_duplicated');
}
globalScope.__shared_scope_defined__ = true;

This ensures that the global scope module is not accidentally included multiple times, which could lead to unexpected behavior from shared scope.

1.1.23

8 months ago

1.1.22

9 months ago

1.1.26

8 months ago

1.1.25

8 months ago

1.1.24

8 months ago

1.1.19

10 months ago

1.1.18

10 months ago

1.1.21

9 months ago

1.1.20

9 months ago

1.1.17

11 months ago

1.1.16

1 year ago

1.1.15

1 year ago

1.1.14

1 year ago

1.1.13

1 year ago

1.1.12

1 year ago

1.1.11

1 year ago

1.1.10

1 year ago

1.1.9

1 year ago

1.1.8

1 year ago

1.1.7

1 year ago

1.1.6

1 year ago

1.1.5

1 year ago

1.1.4

1 year ago

1.1.3

1 year ago

1.1.2

1 year ago

1.1.1

1 year ago

1.1.0

1 year ago

1.0.1

1 year ago

1.0.0

1 year ago