apexcharts
Version:
A JavaScript Chart Library
106 lines (95 loc) • 3.14 kB
JavaScript
// @ts-check
/**
* Chart Factory - Runtime registry for chart type classes.
*
* The registry is stored on `globalThis` so that multiple copies of this
* module (e.g. from bundler deduplication failures or dual CJS/ESM instances)
* all share a single registry. Without this, calling ApexCharts.use() in one
* module copy and rendering in another silently loses the registration.
*
* The registry is populated at module load time by whichever entry point is
* used:
* - Each entry point (src/entries/*.js) calls ApexCharts.use() with the
* types it includes; full.js registers all, sub-entries register a subset,
* allowing bundlers to tree-shake unused chart classes.
*
* @module ChartFactory
*/
const REGISTRY_KEY = '__apexcharts_registry__'
// Marks (#11): names registered via registerSeriesType, so dispatch + the
// canvas-promotion heuristic can tell a custom series from a built-in without
// duplicating the built-in list. globalThis-backed like the type registry.
const CUSTOM_KEY = '__apexcharts_custom_types__'
if (!/** @type {any} */ (globalThis)[REGISTRY_KEY]) {
;/** @type {any} */ (globalThis)[REGISTRY_KEY] = {}
}
if (!/** @type {any} */ (globalThis)[CUSTOM_KEY]) {
;/** @type {any} */ (globalThis)[CUSTOM_KEY] = new Set()
}
/** @returns {Record<string, new (...args: any[]) => any>} */
function getRegistry() {
return /** @type {any} */ (globalThis)[REGISTRY_KEY]
}
/** @returns {Set<string>} */
function getCustomTypes() {
return /** @type {any} */ (globalThis)[CUSTOM_KEY]
}
/**
* Mark a type name as a custom (Marks) series type.
* @param {string} name
*/
export function markCustom(name) {
getCustomTypes().add(name)
}
/**
* Whether a type name is a registered custom (Marks) series type.
* @param {string} name
* @returns {boolean}
*/
export function isCustom(name) {
return getCustomTypes().has(name)
}
/**
* Whether a chart type is registered (built-in or custom). Non-throwing lookup
* so callers can guard without try/catch.
* @param {string} type
* @returns {boolean}
*/
export function hasChartClass(type) {
return !!getRegistry()[type]
}
/**
* Remove a registered type + its custom flag. Used by
* ApexCharts.unregisterSeriesType (tests, hot-reload); built-ins re-register on
* the next entry-point import, custom types are simply gone.
* @param {string} name
*/
export function unregister(name) {
delete getRegistry()[name]
getCustomTypes().delete(name)
}
/**
* Register one or more chart type constructors.
*
* @param {Record<string, new (...args: any[]) => any>} typeMap e.g. { line: Line, area: Line }
*/
export function register(typeMap) {
Object.assign(getRegistry(), typeMap)
}
/**
* Look up the constructor for a chart type.
* Throws a clear error if the type was not registered.
*
* @param {string} type
* @returns {new (...args: any[]) => any}
*/
export function getChartClass(type) {
const Cls = getRegistry()[type]
if (!Cls) {
throw new Error(
`ApexCharts: chart type "${type}" is not registered. ` +
`Import it via ApexCharts.use() or use the full apexcharts bundle.`,
)
}
return Cls
}