uni-global

Global namespace without polluting the global scope

Usage no npm install needed!

<script type="module">
  import uniGlobal from 'https://cdn.skypack.dev/uni-global';
</script>

README

Build status Tests coverage npm version

uni-global

Global namespace with no global scope pollution

Use case

In modules world, there's possiblity that two different instance of same modules (e.g. installed in different node_modules) are being run in same process.

If for any reason they need to operate on context instance that's same for the given application or process, then ocassionally introduced multiple installations may break the application.

This module provides interface wich can be used by those to modules to ensure that no matter how many instances of given module are loaded, in all cases they end with same context instance.

Example usage:

// No matter how many instances of given module are loaded in the process, they will always end with same context instance
const globalContext = require("uni-global")("my-scope-name");

// globalContext is a plain object, on which needed global data can be stored.
globalContext.someSingletonData = ...

Adapt manually other realm

If there's a need to adapt other environment realm (e.g. coming from iframe) to share same uni-global directory.

Register it with adaptRealm util as below:

const adaptRealm = require("uni-global/adapt-realm");

adaptRealm(iFrameWindow); // Pass global object of the other realm

Installation

npm install uni-global