All files / core/src dev.ts

100% Statements 31/31
100% Branches 11/11
100% Functions 2/2
100% Lines 31/31

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48  1x 1x 1x   1x           1x 39x 4x 4x 4x 4x   39x 485x 2x 2x 2x 2x 485x   39x 1x 1x 1x 1x   34x 34x   34x 31x 31x 34x           1x 5910x 5910x  
import type { DevTool } from './types.js';
import { isFunction, isObjectLike } from '@beerush/utils';
import { captureStack } from './exception.js';
import { DEV_TOOL_KEYS } from './constant.js';
 
let activeDevTool: DevTool | undefined = undefined;
 
/**
 * Sets the active development tool. This tool will receive callbacks for various state-related events.
 * @param {DevTool} devTool - The development tool to set as active.
 */
export function setDevTool(devTool: DevTool) {
  if (!isObjectLike(devTool)) {
    const error = new Error('Invalid argument.');
    captureStack.error.argument('The given argument is not a valid DevTool object.', error, setDevTool);
    return;
  }
 
  for (const [key, value] of Object.entries(devTool)) {
    if (DEV_TOOL_KEYS.has(key) && !isFunction(value)) {
      const error = new Error('Invalid callback.');
      captureStack.error.argument(`The given callback for "${key}" is not a function.`, error, setDevTool);
      delete devTool[key as never];
    }
  }
 
  if (!Object.keys(devTool).length) {
    const error = new Error('Invalid argument.');
    captureStack.error.argument('The given argument is not a valid DevTool object.', error, setDevTool);
    return;
  }
 
  const prevDevTool = activeDevTool;
  activeDevTool = devTool;
 
  return () => {
    activeDevTool = prevDevTool;
  };
}
 
/**
 * Retrieves the currently active development tool.
 * @returns {DevTool | undefined} The active development tool, or `undefined` if none is set.
 */
export function getDevTool(): DevTool | undefined {
  return activeDevTool;
}