2
0
mirror of https://github.com/iconify/iconify.git synced 2025-01-22 22:58:27 +00:00

In SVG framework expose internal API and fix issues preventing it from running in Node environment

This commit is contained in:
Vjacheslav Trushkin 2020-06-03 22:01:45 +03:00
parent b87edf3f20
commit 4fc2a3727b
2 changed files with 136 additions and 94 deletions

View File

@ -61,11 +61,38 @@ const loaderFlags: Record<string, Record<string, boolean>> = Object.create(
const queueFlags: Record<string, Record<string, boolean>> = Object.create(null); const queueFlags: Record<string, Record<string, boolean>> = Object.create(null);
// Redundancy instances cache, sorted by provider // Redundancy instances cache, sorted by provider
interface LocalCache { export interface IconifyAPIInternalStorage {
config: IconifyAPIConfig; config: IconifyAPIConfig;
redundancy: Redundancy; redundancy: Redundancy;
} }
const redundancyCache: Record<string, LocalCache> = Object.create(null); const redundancyCache: Record<
string,
IconifyAPIInternalStorage
> = Object.create(null);
/**
* Get Redundancy instance for provider
*/
export function getRedundancyCache(
provider: string
): IconifyAPIInternalStorage | undefined {
if (redundancyCache[provider] === void 0) {
const config = getAPIConfig(provider);
if (!config) {
// No way to load icons because configuration is not set!
return;
}
const redundancy = initRedundancy(config);
const cachedReundancy = {
config,
redundancy,
};
redundancyCache[provider] = cachedReundancy;
}
return redundancyCache[provider];
}
/** /**
* Function called when new icons have been loaded * Function called when new icons have been loaded
@ -124,7 +151,7 @@ function loadNewIcons(provider: string, prefix: string, icons: string[]): void {
} }
// Redundancy item // Redundancy item
let cachedReundancy: LocalCache; let cachedReundancy: IconifyAPIInternalStorage;
// Trigger update on next tick, mering multiple synchronous requests into one asynchronous request // Trigger update on next tick, mering multiple synchronous requests into one asynchronous request
if (!providerQueueFlags[prefix]) { if (!providerQueueFlags[prefix]) {
@ -146,23 +173,13 @@ function loadNewIcons(provider: string, prefix: string, icons: string[]): void {
// Get API config and Redundancy instance // Get API config and Redundancy instance
if (cachedReundancy === void 0) { if (cachedReundancy === void 0) {
if (redundancyCache[provider] === void 0) { const redundancy = getRedundancyCache(provider);
const config = getAPIConfig(provider); if (redundancy === void 0) {
if (!config) {
// No way to load icons because configuration is not set! // No way to load icons because configuration is not set!
err(); err();
return; return;
} }
cachedReundancy = redundancy;
const redundancy = initRedundancy(config);
cachedReundancy = {
config,
redundancy,
};
redundancyCache[provider] = cachedReundancy;
} else {
cachedReundancy = redundancyCache[provider];
}
} }
// Prepare parameters and run queries // Prepare parameters and run queries

View File

@ -39,7 +39,11 @@ import { finder as iconifyFinder } from './finders/iconify';
import { storeCache, loadCache, config } from '@iconify/core/lib/cache/storage'; import { storeCache, loadCache, config } from '@iconify/core/lib/cache/storage';
// API // API
import { API } from '@iconify/core/lib/api/'; import {
API,
getRedundancyCache,
IconifyAPIInternalStorage,
} from '@iconify/core/lib/api/';
import { setAPIModule } from '@iconify/core/lib/api/modules'; import { setAPIModule } from '@iconify/core/lib/api/modules';
import { import {
setAPIConfig, setAPIConfig,
@ -60,6 +64,7 @@ import { renderIcon } from './render';
// Scan // Scan
import { scanDOM } from './scan'; import { scanDOM } from './scan';
import { Redundancy } from '@iconify/core/node_modules/@cyberalien/redundancy';
/** /**
* Export required types * Export required types
@ -79,7 +84,12 @@ export {
export { IconifyIconBuildResult }; export { IconifyIconBuildResult };
// API // API
export { IconifyAPIConfig, IconifyIconLoaderCallback, IconifyIconLoaderAbort }; export {
IconifyAPIConfig,
IconifyIconLoaderCallback,
IconifyIconLoaderAbort,
IconifyAPIInternalStorage,
};
/** /**
* Cache types * Cache types
@ -183,6 +193,13 @@ export interface IconifyGlobal {
customConfig: Partial<IconifyAPIConfig> customConfig: Partial<IconifyAPIConfig>
) => void; ) => void;
/**
* Get internal API data, used by Icon Finder
*/
_getInternalAPI: (
provider: string
) => IconifyAPIInternalStorage | undefined;
/* Scan DOM */ /* Scan DOM */
/** /**
* Scan DOM * Scan DOM
@ -390,9 +407,12 @@ const Iconify: IconifyGlobal = {
// Resume observer // Resume observer
resumeObserver: observer.resume, resumeObserver: observer.resume,
// Add API provider // API providers
addAPIProvider: setAPIConfig, addAPIProvider: setAPIConfig,
// Get API data
_getInternalAPI: getRedundancyCache,
// Scan DOM // Scan DOM
scanDOM: scanDOM, scanDOM: scanDOM,
@ -427,14 +447,6 @@ const Iconify: IconifyGlobal = {
/** /**
* Initialise stuff * Initialise stuff
*/ */
// Add finder modules
// addFinder(iconifyIconFinder);
addFinder(iconifyFinder);
// Set cache and load existing cache
coreModules.cache = storeCache;
loadCache();
// Set API // Set API
coreModules.api = API; coreModules.api = API;
setAPIModule('', { setAPIModule('', {
@ -442,14 +454,26 @@ setAPIModule('', {
prepare: prepareQuery, prepare: prepareQuery,
}); });
// Load icons from global "IconifyPreload" if (typeof document !== 'undefined' && typeof window !== 'undefined') {
interface WindowWithIconifyPreload { // Add finder modules
// addFinder(iconifyIconFinder);
addFinder(iconifyFinder);
// Set cache and load existing cache
coreModules.cache = storeCache;
loadCache();
const _window = window;
// Load icons from global "IconifyPreload"
interface WindowWithIconifyPreload {
IconifyPreload: IconifyJSON[] | IconifyJSON; IconifyPreload: IconifyJSON[] | IconifyJSON;
} }
if ( if (
((window as unknown) as WindowWithIconifyPreload).IconifyPreload !== void 0 ((_window as unknown) as WindowWithIconifyPreload).IconifyPreload !==
) { void 0
const preload = ((window as unknown) as WindowWithIconifyPreload) ) {
const preload = ((_window as unknown) as WindowWithIconifyPreload)
.IconifyPreload; .IconifyPreload;
const err = 'Invalid IconifyPreload syntax.'; const err = 'Invalid IconifyPreload syntax.';
if (typeof preload === 'object' && preload !== null) { if (typeof preload === 'object' && preload !== null) {
@ -473,17 +497,17 @@ if (
} }
}); });
} }
} }
// Set API from global "IconifyProviders" // Set API from global "IconifyProviders"
interface WindowWithIconifyProviders { interface WindowWithIconifyProviders {
IconifyProviders: Record<string, PartialIconifyAPIConfig>; IconifyProviders: Record<string, PartialIconifyAPIConfig>;
} }
if ( if (
((window as unknown) as WindowWithIconifyProviders).IconifyProviders !== ((_window as unknown) as WindowWithIconifyProviders)
void 0 .IconifyProviders !== void 0
) { ) {
const providers = ((window as unknown) as WindowWithIconifyProviders) const providers = ((_window as unknown) as WindowWithIconifyProviders)
.IconifyProviders; .IconifyProviders;
if (typeof providers === 'object' && providers !== null) { if (typeof providers === 'object' && providers !== null) {
for (let key in providers) { for (let key in providers) {
@ -505,13 +529,14 @@ if (
} }
} }
} }
} }
// Load observer // Load observer
browserModules.observer = observer; browserModules.observer = observer;
setTimeout(() => { setTimeout(() => {
// Init on next tick when entire document has been parsed // Init on next tick when entire document has been parsed
observer.init(scanDOM); observer.init(scanDOM);
}); });
}
export default Iconify; export default Iconify;