Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@devboldly/react-use-window-global

Package Overview
Dependencies
Maintainers
1
Versions
9
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@devboldly/react-use-window-global - npm Package Compare versions

Comparing version 0.0.2 to 0.0.3

81

dist/hooks/useWindowGlobal.js

@@ -11,2 +11,3 @@ "use strict";

var React = __importStar(require("react"));
var events_1 = require("events");
// Learn how to write hooks:

@@ -19,24 +20,35 @@ // https://reactjs.org/docs/hooks-intro.html

var _b = React.useState(false), shouldRestore = _b[0], setShouldRestore = _b[1];
var _c = React.useState(defaultValue), reactState = _c[0], setReactState = _c[1];
var _c = React.useState(false), shouldPush = _c[0], setShouldPush = _c[1];
var _d = React.useState(defaultValue), reactState = _d[0], setReactState = _d[1];
React.useEffect(function () {
var aborted = false;
var emitterEventName = (namespace !== 'undefined' ? namespace + '.' : '') + keyName + ' change';
var setStateAndEmit = function (newState) {
setReactState(newState);
getEmitter().emit(emitterEventName);
};
var updateHandler = undefined;
if (typeof window !== 'undefined') {
var win = window;
var winObj_1 = win;
if (typeof namespace !== 'undefined') {
if (!win[namespace]) {
win[namespace] = {};
console.log(window);
var winObj_1 = getWinObj(namespace);
var emitter = getEmitter();
var keyExists = keyName in winObj_1;
var retrievedWindowState = winObj_1[keyName];
// Set up emitter
updateHandler = function (firedById) {
if (!aborted) {
setReactState(winObj_1[keyName]);
}
winObj_1 = win[namespace];
}
var keyExists = function () { return keyName in winObj_1; };
var retrievedWindowState = winObj_1[keyName];
};
emitter.on(emitterEventName, updateHandler);
// Synchronize value with window on first render
if (!loaded) {
// If on first render we actually have a value, use it.
if (keyExists()) {
setReactState(retrievedWindowState);
if (keyExists) {
setStateAndEmit(retrievedWindowState);
}
else {
setReactState(defaultValue);
// Otherwise, set the value to the default
winObj_1[keyName] = defaultValue;
setStateAndEmit(defaultValue);
}

@@ -48,5 +60,5 @@ setLoaded(true);

setShouldRestore(false);
var newVal = keyExists() ? retrievedWindowState : defaultValue;
setReactState(newVal);
if (!keyExists() && typeof defaultValue !== 'undefined') {
var newVal = keyExists ? retrievedWindowState : defaultValue;
setStateAndEmit(newVal);
if (!keyExists && typeof defaultValue !== 'undefined') {
// Set the default if it's missing.

@@ -57,7 +69,15 @@ winObj_1[keyName] = newVal;

// Synchronize window with value thereafter
else if (retrievedWindowState !== reactState) {
else if (shouldPush || retrievedWindowState !== reactState) {
winObj_1[keyName] = reactState;
setStateAndEmit(reactState);
setShouldPush(false);
}
}
}, [defaultValue, loaded, keyName, namespace, shouldRestore, reactState]);
return function () {
aborted = true;
if (typeof window !== 'undefined' && typeof updateHandler !== 'undefined') {
getEmitter().off(emitterEventName, updateHandler);
}
};
}, [defaultValue, loaded, keyName, namespace, shouldRestore, reactState, shouldPush]);
var setValue = function (newVal) {

@@ -71,2 +91,3 @@ setReactState(newVal);

setReactState(defaultValue);
setShouldPush(true);
};

@@ -77,1 +98,25 @@ var value = reactState;

exports.useWindowGlobal = useWindowGlobal;
var getWinObj = function (namespace) {
var win = window;
var winObj = win;
if (typeof namespace !== 'undefined') {
if (!win[namespace]) {
win[namespace] = {};
}
winObj = win[namespace];
}
return winObj;
};
var getEmitter = function () {
if (typeof window !== 'undefined') {
var win = window;
var emitterKey = '__USE_WINDOW_GLOBAL_HOOK_EMITTER__';
// Set up emitter
if (!win[emitterKey]) {
win[emitterKey] = new events_1.EventEmitter();
}
return win[emitterKey];
}
console.error('Window not supported');
return new events_1.EventEmitter();
};
{
"name": "@devboldly/react-use-window-global",
"version": "0.0.2",
"version": "0.0.3",
"author": "Dev Boldly <devboldly@gmail.com>",

@@ -5,0 +5,0 @@ "description": "React hook for setting and getting globals via the Window Web API. Includes namespacing.",

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc