From b7a48f146d4bd17c6904b41f613e5a4df502a029 Mon Sep 17 00:00:00 2001 From: makhnatkin Date: Wed, 25 Sep 2024 12:49:00 +0200 Subject: [PATCH] fix(extension-load-queue): fixed export --- src/lib/extension-load-queue.ts | 70 ++++++++++++++++++--------------- src/lib/index.ts | 11 +----- 2 files changed, 40 insertions(+), 41 deletions(-) diff --git a/src/lib/extension-load-queue.ts b/src/lib/extension-load-queue.ts index f0d89cd..e31d46d 100644 --- a/src/lib/extension-load-queue.ts +++ b/src/lib/extension-load-queue.ts @@ -35,21 +35,43 @@ export type ScriptStore = ControllerLoadedCallback[]; /** * Interface for creating a load queue. + * + * @template T - Type of the controller created in the queue. */ export interface CreateLoadQueueArgs { - // The store where callbacks are saved + /** + * The store where callbacks are saved. + * + * @type {ScriptStore} + */ store: ScriptStore; - // Function that creates a controller + /** + * Function that creates a controller. + * + * @returns {T} A new controller instance. + */ createController: () => T; - // Flag to check if the queue is already created + /** + * Flag to check if the queue is already created. + * + * @type {boolean} [isQueueCreated] + */ isQueueCreated?: boolean; - // Callback to handle queue creation + /** + * Callback to handle queue creation. + * + * @param {boolean} created - Indicates if the queue was successfully created. + */ onQueueCreated?: (created: boolean) => void; - // Symbol key for identifying the queue + /** + * Symbol key for identifying the queue. + * + * @type {symbol} [queueKey] + */ queueKey?: symbol; } @@ -70,7 +92,7 @@ export const getScriptStore = (storeKey: symbol): ScriptStore => { return window[storeKey] as ScriptStore; } else { - throw new Error('This functionality should be employed on the client-side.'); + throw new Error('Cannot initialize QueueStore in a non-browser environment.'); } }; @@ -85,7 +107,7 @@ const ensureQueuesSymbolInitialized = (): void => { window[QUEUES_SYMBOL] = {}; } } else { - throw new Error('This functionality should be employed on the client-side.'); + throw new Error('Cannot initialize QueueStore in a non-browser environment.'); } }; @@ -107,7 +129,7 @@ export const getQueueStore = (queueKey: symbol): boolean => { * @returns {function(boolean): void} A function that takes a boolean `created` * and marks the queue as created. */ -export const createHandleQueueCreated = (queueKey: symbol): ((created: boolean) => void) => { +const createHandleQueueCreated = (queueKey: symbol): ((created: boolean) => void) => { ensureQueuesSymbolInitialized(); return (created: boolean) => { window[QUEUES_SYMBOL][queueKey] = created; @@ -177,13 +199,6 @@ export const createLoadQueue = ({ unqueue(); }; -/** - * A no-operation function used as a default cleanup function. - * - * @returns {void} - */ -const noop = (): void => {}; - /** * React hook to manage and use a controller with a script store. * @@ -194,22 +209,15 @@ export function useController(store: ScriptStore): T | null { const [controller, setController] = useState(null); useEffect(() => { - if (store) { - store.push(setController); // Add setController to the store - - return () => { - const index = store.indexOf(setController); - if (index > -1) { - // Remove setController when unmounting - store.splice(index, 1); - } - }; - } else { - // eslint-disable-next-line no-console - console.warn('Store is not provided to useController'); // Replace console.warn with a logging function if necessary - } - - return noop; + store.push(setController); // Add setController to the store + + return () => { + const index = store.indexOf(setController); + if (index > -1) { + // Remove setController when unmounting + store.splice(index, 1); + } + }; }, []); return controller; diff --git a/src/lib/index.ts b/src/lib/index.ts index bcb6109..4aeb25f 100644 --- a/src/lib/index.ts +++ b/src/lib/index.ts @@ -3,7 +3,6 @@ import { ControllerLoadedCallback, CreateLoadQueueArgs, ScriptStore, - createHandleQueueCreated, createLoadQueue, getQueueStore, getScriptStore, @@ -11,14 +10,6 @@ import { } from './extension-load-queue'; import {isBrowser} from './browser'; -export { - AttrsParser, - createLoadQueue, - getQueueStore, - getScriptStore, - createHandleQueueCreated, - isBrowser, - useController, -}; +export {AttrsParser, createLoadQueue, getQueueStore, getScriptStore, isBrowser, useController}; export type {ControllerLoadedCallback, CreateLoadQueueArgs, ScriptStore};