Skip to content

Commit

Permalink
fix(extension-load-queue): fixed export
Browse files Browse the repository at this point in the history
  • Loading branch information
makhnatkin committed Sep 25, 2024
1 parent 295d8be commit b7a48f1
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 41 deletions.
70 changes: 39 additions & 31 deletions src/lib/extension-load-queue.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,21 +35,43 @@ export type ScriptStore<T> = ControllerLoadedCallback<T>[];

/**
* Interface for creating a load queue.
*
* @template T - Type of the controller created in the queue.
*/
export interface CreateLoadQueueArgs<T> {
// The store where callbacks are saved
/**
* The store where callbacks are saved.
*
* @type {ScriptStore<T>}
*/
store: ScriptStore<T>;

// 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;
}

Expand All @@ -70,7 +92,7 @@ export const getScriptStore = <T>(storeKey: symbol): ScriptStore<T> => {

return window[storeKey] as ScriptStore<T>;
} else {
throw new Error('This functionality should be employed on the client-side.');
throw new Error('Cannot initialize QueueStore in a non-browser environment.');
}
};

Expand All @@ -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.');
}
};

Expand All @@ -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;
Expand Down Expand Up @@ -177,13 +199,6 @@ export const createLoadQueue = <T>({
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.
*
Expand All @@ -194,22 +209,15 @@ export function useController<T>(store: ScriptStore<T>): T | null {
const [controller, setController] = useState<T | null>(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;
Expand Down
11 changes: 1 addition & 10 deletions src/lib/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,13 @@ import {
ControllerLoadedCallback,
CreateLoadQueueArgs,
ScriptStore,
createHandleQueueCreated,
createLoadQueue,
getQueueStore,
getScriptStore,
useController,
} 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};

0 comments on commit b7a48f1

Please sign in to comment.