@smg-automotive/advertisement-pkg
Advanced tools
Comparing version 1.0.1 to 1.0.2
@@ -186,99 +186,88 @@ 'use strict'; | ||
// Cached script statuses | ||
const cachedScriptStatuses = {}; | ||
var commonjsGlobal = typeof globalThis !== 'undefined' ? globalThis : typeof window !== 'undefined' ? window : typeof global !== 'undefined' ? global : typeof self !== 'undefined' ? self : {}; | ||
/** | ||
* Gets the script element with the specified source URL. | ||
* @param {string} src - The source URL of the script to get. | ||
* @returns {{ node: HTMLScriptElement | null, status: UseScriptStatus | undefined }} The script element and its loading status. | ||
* lodash (Custom Build) <https://lodash.com/> | ||
* Build: `lodash modularize exports="npm" -o ./` | ||
* Copyright jQuery Foundation and other contributors <https://jquery.org/> | ||
* Released under MIT license <https://lodash.com/license> | ||
* Based on Underscore.js 1.8.3 <http://underscorejs.org/LICENSE> | ||
* Copyright Jeremy Ashkenas, DocumentCloud and Investigative Reporters & Editors | ||
*/ | ||
/** Detect free variable `global` from Node.js. */ | ||
var freeGlobal = typeof commonjsGlobal == 'object' && commonjsGlobal && commonjsGlobal.Object === Object && commonjsGlobal; | ||
/** Detect free variable `self`. */ | ||
var freeSelf = typeof self == 'object' && self && self.Object === Object && self; | ||
/** Used as a reference to the global object. */ | ||
freeGlobal || freeSelf || Function('return this')(); | ||
var cachedScriptStatuses = /* @__PURE__ */ new Map(); | ||
function getScriptNode(src) { | ||
const node = document.querySelector(`script[src="${src}"]`); | ||
const status = node === null || node === void 0 ? void 0 : node.getAttribute('data-status'); | ||
return { | ||
node, | ||
status, | ||
}; | ||
const node = document.querySelector( | ||
`script[src="${src}"]` | ||
); | ||
const status = node == null ? void 0 : node.getAttribute("data-status"); | ||
return { | ||
node, | ||
status | ||
}; | ||
} | ||
/** | ||
* Custom hook for dynamically loading scripts and tracking their loading status. | ||
* @param {string | null} src - The source URL of the script to load. Set to `null` or omit to prevent loading (optional). | ||
* @param {UseScriptOptions} [options] - Additional options for controlling script loading (optional). | ||
* @param {boolean} [options.shouldPreventLoad] - If `true`, prevents the script from being loaded (optional). | ||
* @param {boolean} [options.removeOnUnmount] - If `true`, removes the script from the DOM when the component unmounts (optional). | ||
* @see [Documentation](https://usehooks-ts.com/react-hook/use-script) | ||
* @returns {UseScriptStatus} The status of the script loading, which can be one of 'idle', 'loading', 'ready', or 'error'. | ||
* @example | ||
* const scriptStatus = useScript('https://example.com/script.js', { removeOnUnmount: true }); | ||
* // Access the status of the script loading (e.g., 'loading', 'ready', 'error'). | ||
*/ | ||
function useScript(src, options) { | ||
const [status, setStatus] = React.useState(() => { | ||
var _a; | ||
if (!src || (options === null || options === void 0 ? void 0 : options.shouldPreventLoad)) { | ||
return 'idle'; | ||
} | ||
if (typeof window === 'undefined') { | ||
// SSR Handling - always return 'loading' | ||
return 'loading'; | ||
} | ||
return (_a = cachedScriptStatuses[src]) !== null && _a !== void 0 ? _a : 'loading'; | ||
}); | ||
React.useEffect(() => { | ||
var _a, _b; | ||
if (!src || (options === null || options === void 0 ? void 0 : options.shouldPreventLoad)) { | ||
return; | ||
} | ||
const cachedScriptStatus = cachedScriptStatuses[src]; | ||
if (cachedScriptStatus === 'ready' || cachedScriptStatus === 'error') { | ||
// If the script is already cached, set its status immediately | ||
setStatus(cachedScriptStatus); | ||
return; | ||
} | ||
// Fetch existing script element by src | ||
// It may have been added by another instance of this hook | ||
const script = getScriptNode(src); | ||
let scriptNode = script.node; | ||
if (!scriptNode) { | ||
// Create script element and add it to document body | ||
scriptNode = document.createElement('script'); | ||
scriptNode.src = src; | ||
scriptNode.async = true; | ||
scriptNode.setAttribute('data-status', 'loading'); | ||
document.body.appendChild(scriptNode); | ||
// Store status in attribute on script | ||
// This can be read by other instances of this hook | ||
const setAttributeFromEvent = (event) => { | ||
const scriptStatus = event.type === 'load' ? 'ready' : 'error'; | ||
scriptNode === null || scriptNode === void 0 ? void 0 : scriptNode.setAttribute('data-status', scriptStatus); | ||
}; | ||
scriptNode.addEventListener('load', setAttributeFromEvent); | ||
scriptNode.addEventListener('error', setAttributeFromEvent); | ||
} | ||
else { | ||
// Grab existing script status from attribute and set to state. | ||
setStatus((_b = (_a = script.status) !== null && _a !== void 0 ? _a : cachedScriptStatus) !== null && _b !== void 0 ? _b : 'loading'); | ||
} | ||
// Script event handler to update status in state | ||
// Note: Even if the script already exists we still need to add | ||
// event handlers to update the state for *this* hook instance. | ||
const setStateFromEvent = (event) => { | ||
const newStatus = event.type === 'load' ? 'ready' : 'error'; | ||
setStatus(newStatus); | ||
cachedScriptStatuses[src] = newStatus; | ||
}; | ||
// Add event listeners | ||
scriptNode.addEventListener('load', setStateFromEvent); | ||
scriptNode.addEventListener('error', setStateFromEvent); | ||
// Remove event listeners on cleanup | ||
return () => { | ||
if (scriptNode) { | ||
scriptNode.removeEventListener('load', setStateFromEvent); | ||
scriptNode.removeEventListener('error', setStateFromEvent); | ||
} | ||
if (scriptNode && (options === null || options === void 0 ? void 0 : options.removeOnUnmount)) { | ||
scriptNode.remove(); | ||
} | ||
}; | ||
}, [src, options === null || options === void 0 ? void 0 : options.shouldPreventLoad, options === null || options === void 0 ? void 0 : options.removeOnUnmount]); | ||
return status; | ||
const [status, setStatus] = React.useState(() => { | ||
if (!src || (options == null ? void 0 : options.shouldPreventLoad)) { | ||
return "idle"; | ||
} | ||
if (typeof window === "undefined") { | ||
return "loading"; | ||
} | ||
return cachedScriptStatuses.get(src) ?? "loading"; | ||
}); | ||
React.useEffect(() => { | ||
if (!src || (options == null ? void 0 : options.shouldPreventLoad)) { | ||
return; | ||
} | ||
const cachedScriptStatus = cachedScriptStatuses.get(src); | ||
if (cachedScriptStatus === "ready" || cachedScriptStatus === "error") { | ||
setStatus(cachedScriptStatus); | ||
return; | ||
} | ||
const script = getScriptNode(src); | ||
let scriptNode = script.node; | ||
if (!scriptNode) { | ||
scriptNode = document.createElement("script"); | ||
scriptNode.src = src; | ||
scriptNode.async = true; | ||
scriptNode.setAttribute("data-status", "loading"); | ||
document.body.appendChild(scriptNode); | ||
const setAttributeFromEvent = (event) => { | ||
const scriptStatus = event.type === "load" ? "ready" : "error"; | ||
scriptNode == null ? void 0 : scriptNode.setAttribute("data-status", scriptStatus); | ||
}; | ||
scriptNode.addEventListener("load", setAttributeFromEvent); | ||
scriptNode.addEventListener("error", setAttributeFromEvent); | ||
} else { | ||
setStatus(script.status ?? cachedScriptStatus ?? "loading"); | ||
} | ||
const setStateFromEvent = (event) => { | ||
const newStatus = event.type === "load" ? "ready" : "error"; | ||
setStatus(newStatus); | ||
cachedScriptStatuses.set(src, newStatus); | ||
}; | ||
scriptNode.addEventListener("load", setStateFromEvent); | ||
scriptNode.addEventListener("error", setStateFromEvent); | ||
return () => { | ||
if (scriptNode) { | ||
scriptNode.removeEventListener("load", setStateFromEvent); | ||
scriptNode.removeEventListener("error", setStateFromEvent); | ||
} | ||
if (scriptNode && (options == null ? void 0 : options.removeOnUnmount)) { | ||
scriptNode.remove(); | ||
cachedScriptStatuses.delete(src); | ||
} | ||
}; | ||
}, [src, options == null ? void 0 : options.shouldPreventLoad, options == null ? void 0 : options.removeOnUnmount]); | ||
return status; | ||
} | ||
@@ -285,0 +274,0 @@ |
@@ -166,99 +166,88 @@ import * as React from 'react'; | ||
// Cached script statuses | ||
const cachedScriptStatuses = {}; | ||
var commonjsGlobal = typeof globalThis !== 'undefined' ? globalThis : typeof window !== 'undefined' ? window : typeof global !== 'undefined' ? global : typeof self !== 'undefined' ? self : {}; | ||
/** | ||
* Gets the script element with the specified source URL. | ||
* @param {string} src - The source URL of the script to get. | ||
* @returns {{ node: HTMLScriptElement | null, status: UseScriptStatus | undefined }} The script element and its loading status. | ||
* lodash (Custom Build) <https://lodash.com/> | ||
* Build: `lodash modularize exports="npm" -o ./` | ||
* Copyright jQuery Foundation and other contributors <https://jquery.org/> | ||
* Released under MIT license <https://lodash.com/license> | ||
* Based on Underscore.js 1.8.3 <http://underscorejs.org/LICENSE> | ||
* Copyright Jeremy Ashkenas, DocumentCloud and Investigative Reporters & Editors | ||
*/ | ||
/** Detect free variable `global` from Node.js. */ | ||
var freeGlobal = typeof commonjsGlobal == 'object' && commonjsGlobal && commonjsGlobal.Object === Object && commonjsGlobal; | ||
/** Detect free variable `self`. */ | ||
var freeSelf = typeof self == 'object' && self && self.Object === Object && self; | ||
/** Used as a reference to the global object. */ | ||
freeGlobal || freeSelf || Function('return this')(); | ||
var cachedScriptStatuses = /* @__PURE__ */ new Map(); | ||
function getScriptNode(src) { | ||
const node = document.querySelector(`script[src="${src}"]`); | ||
const status = node === null || node === void 0 ? void 0 : node.getAttribute('data-status'); | ||
return { | ||
node, | ||
status, | ||
}; | ||
const node = document.querySelector( | ||
`script[src="${src}"]` | ||
); | ||
const status = node == null ? void 0 : node.getAttribute("data-status"); | ||
return { | ||
node, | ||
status | ||
}; | ||
} | ||
/** | ||
* Custom hook for dynamically loading scripts and tracking their loading status. | ||
* @param {string | null} src - The source URL of the script to load. Set to `null` or omit to prevent loading (optional). | ||
* @param {UseScriptOptions} [options] - Additional options for controlling script loading (optional). | ||
* @param {boolean} [options.shouldPreventLoad] - If `true`, prevents the script from being loaded (optional). | ||
* @param {boolean} [options.removeOnUnmount] - If `true`, removes the script from the DOM when the component unmounts (optional). | ||
* @see [Documentation](https://usehooks-ts.com/react-hook/use-script) | ||
* @returns {UseScriptStatus} The status of the script loading, which can be one of 'idle', 'loading', 'ready', or 'error'. | ||
* @example | ||
* const scriptStatus = useScript('https://example.com/script.js', { removeOnUnmount: true }); | ||
* // Access the status of the script loading (e.g., 'loading', 'ready', 'error'). | ||
*/ | ||
function useScript(src, options) { | ||
const [status, setStatus] = useState(() => { | ||
var _a; | ||
if (!src || (options === null || options === void 0 ? void 0 : options.shouldPreventLoad)) { | ||
return 'idle'; | ||
} | ||
if (typeof window === 'undefined') { | ||
// SSR Handling - always return 'loading' | ||
return 'loading'; | ||
} | ||
return (_a = cachedScriptStatuses[src]) !== null && _a !== void 0 ? _a : 'loading'; | ||
}); | ||
useEffect(() => { | ||
var _a, _b; | ||
if (!src || (options === null || options === void 0 ? void 0 : options.shouldPreventLoad)) { | ||
return; | ||
} | ||
const cachedScriptStatus = cachedScriptStatuses[src]; | ||
if (cachedScriptStatus === 'ready' || cachedScriptStatus === 'error') { | ||
// If the script is already cached, set its status immediately | ||
setStatus(cachedScriptStatus); | ||
return; | ||
} | ||
// Fetch existing script element by src | ||
// It may have been added by another instance of this hook | ||
const script = getScriptNode(src); | ||
let scriptNode = script.node; | ||
if (!scriptNode) { | ||
// Create script element and add it to document body | ||
scriptNode = document.createElement('script'); | ||
scriptNode.src = src; | ||
scriptNode.async = true; | ||
scriptNode.setAttribute('data-status', 'loading'); | ||
document.body.appendChild(scriptNode); | ||
// Store status in attribute on script | ||
// This can be read by other instances of this hook | ||
const setAttributeFromEvent = (event) => { | ||
const scriptStatus = event.type === 'load' ? 'ready' : 'error'; | ||
scriptNode === null || scriptNode === void 0 ? void 0 : scriptNode.setAttribute('data-status', scriptStatus); | ||
}; | ||
scriptNode.addEventListener('load', setAttributeFromEvent); | ||
scriptNode.addEventListener('error', setAttributeFromEvent); | ||
} | ||
else { | ||
// Grab existing script status from attribute and set to state. | ||
setStatus((_b = (_a = script.status) !== null && _a !== void 0 ? _a : cachedScriptStatus) !== null && _b !== void 0 ? _b : 'loading'); | ||
} | ||
// Script event handler to update status in state | ||
// Note: Even if the script already exists we still need to add | ||
// event handlers to update the state for *this* hook instance. | ||
const setStateFromEvent = (event) => { | ||
const newStatus = event.type === 'load' ? 'ready' : 'error'; | ||
setStatus(newStatus); | ||
cachedScriptStatuses[src] = newStatus; | ||
}; | ||
// Add event listeners | ||
scriptNode.addEventListener('load', setStateFromEvent); | ||
scriptNode.addEventListener('error', setStateFromEvent); | ||
// Remove event listeners on cleanup | ||
return () => { | ||
if (scriptNode) { | ||
scriptNode.removeEventListener('load', setStateFromEvent); | ||
scriptNode.removeEventListener('error', setStateFromEvent); | ||
} | ||
if (scriptNode && (options === null || options === void 0 ? void 0 : options.removeOnUnmount)) { | ||
scriptNode.remove(); | ||
} | ||
}; | ||
}, [src, options === null || options === void 0 ? void 0 : options.shouldPreventLoad, options === null || options === void 0 ? void 0 : options.removeOnUnmount]); | ||
return status; | ||
const [status, setStatus] = useState(() => { | ||
if (!src || (options == null ? void 0 : options.shouldPreventLoad)) { | ||
return "idle"; | ||
} | ||
if (typeof window === "undefined") { | ||
return "loading"; | ||
} | ||
return cachedScriptStatuses.get(src) ?? "loading"; | ||
}); | ||
useEffect(() => { | ||
if (!src || (options == null ? void 0 : options.shouldPreventLoad)) { | ||
return; | ||
} | ||
const cachedScriptStatus = cachedScriptStatuses.get(src); | ||
if (cachedScriptStatus === "ready" || cachedScriptStatus === "error") { | ||
setStatus(cachedScriptStatus); | ||
return; | ||
} | ||
const script = getScriptNode(src); | ||
let scriptNode = script.node; | ||
if (!scriptNode) { | ||
scriptNode = document.createElement("script"); | ||
scriptNode.src = src; | ||
scriptNode.async = true; | ||
scriptNode.setAttribute("data-status", "loading"); | ||
document.body.appendChild(scriptNode); | ||
const setAttributeFromEvent = (event) => { | ||
const scriptStatus = event.type === "load" ? "ready" : "error"; | ||
scriptNode == null ? void 0 : scriptNode.setAttribute("data-status", scriptStatus); | ||
}; | ||
scriptNode.addEventListener("load", setAttributeFromEvent); | ||
scriptNode.addEventListener("error", setAttributeFromEvent); | ||
} else { | ||
setStatus(script.status ?? cachedScriptStatus ?? "loading"); | ||
} | ||
const setStateFromEvent = (event) => { | ||
const newStatus = event.type === "load" ? "ready" : "error"; | ||
setStatus(newStatus); | ||
cachedScriptStatuses.set(src, newStatus); | ||
}; | ||
scriptNode.addEventListener("load", setStateFromEvent); | ||
scriptNode.addEventListener("error", setStateFromEvent); | ||
return () => { | ||
if (scriptNode) { | ||
scriptNode.removeEventListener("load", setStateFromEvent); | ||
scriptNode.removeEventListener("error", setStateFromEvent); | ||
} | ||
if (scriptNode && (options == null ? void 0 : options.removeOnUnmount)) { | ||
scriptNode.remove(); | ||
cachedScriptStatuses.delete(src); | ||
} | ||
}; | ||
}, [src, options == null ? void 0 : options.shouldPreventLoad, options == null ? void 0 : options.removeOnUnmount]); | ||
return status; | ||
} | ||
@@ -265,0 +254,0 @@ |
{ | ||
"name": "@smg-automotive/advertisement-pkg", | ||
"version": "1.0.1", | ||
"version": "1.0.2", | ||
"description": "Contains Relevant Digital Header Bidding Wrapper for advertisements", | ||
@@ -31,3 +31,3 @@ "main": "dist/cjs/index.js", | ||
"dependencies": { | ||
"usehooks-ts": "2.12.1" | ||
"usehooks-ts": "2.13.0" | ||
}, | ||
@@ -43,9 +43,9 @@ "peerDependencies": { | ||
"@rollup/plugin-typescript": "11.1.6", | ||
"@smg-automotive/eslint-config": "4.0.38", | ||
"@testing-library/jest-dom": "6.3.0", | ||
"@testing-library/react": "14.1.2", | ||
"@smg-automotive/eslint-config": "4.0.40", | ||
"@testing-library/jest-dom": "6.4.2", | ||
"@testing-library/react": "14.2.1", | ||
"@testing-library/user-event": "14.5.2", | ||
"@types/jest": "29.5.11", | ||
"@types/jest": "29.5.12", | ||
"@types/node": "20.11.16", | ||
"@types/react": "18.2.48", | ||
"@types/react": "18.2.55", | ||
"jest": "29.7.0", | ||
@@ -59,3 +59,3 @@ "jest-environment-jsdom": "29.7.0", | ||
"rollup-plugin-peer-deps-external": "2.2.4", | ||
"semantic-release": "23.0.0", | ||
"semantic-release": "23.0.2", | ||
"ts-jest": "29.1.2", | ||
@@ -62,0 +62,0 @@ "ts-node": "10.9.2", |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Uses eval
Supply chain riskPackage uses dynamic code execution (e.g., eval()), which is a dangerous practice. This can prevent the code from running in certain environments and increases the risk that the code may contain exploits or malicious behavior.
Found 1 instance in 1 package
233680
1409
2
+ Addedusehooks-ts@2.13.0(transitive)
- Removedusehooks-ts@2.12.1(transitive)
Updatedusehooks-ts@2.13.0