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 481x 1x   1x   1x           1x 40x 4x 4x 4x 4x   40x 500x 2x 2x 2x 2x 500x   40x 1x 1x 1x 1x   35x 35x   35x 32x 32x 35x           1x 7004x 7004x  
import { DEV_TOOL_KEYS } from './constant.js';
import { captureStack } from './exception.js';
import type { DevTool } from './types.js';
import { closure, isFunction, isObjectLike } from './utils/index.js';
 
const DEV_TOOL_SYMBOL = Symbol('dev-tool');
 
/**
 * 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 = closure.get<DevTool>(DEV_TOOL_SYMBOL);
  closure.set(DEV_TOOL_SYMBOL, devTool);
 
  return () => {
    closure.set(DEV_TOOL_SYMBOL, 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 closure.get(DEV_TOOL_SYMBOL);
}