包详细信息

uni-global

medikoo3.8mISC1.0.0

Global namespace without polluting the global scope

global, globalThis

自述文件

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

更新日志

Changelog

All notable changes to this project will be documented in this file. See standard-version for commit guidelines.

1.0.0 (2021-10-06)

Features

  • Main functionality (76c407e)
  • adaptRealm to adapt globals from other realms (68dd172)