2
0
mirror of https://github.com/iconify/iconify.git synced 2025-01-07 15:44:05 +00:00

Optimise API redundancy package

This commit is contained in:
Vjacheslav Trushkin 2022-06-30 09:13:25 +03:00
parent 34d0ced20f
commit e18b821005
2 changed files with 10 additions and 32 deletions

View File

@ -1,7 +1,7 @@
{ {
"name": "@iconify/api-redundancy", "name": "@iconify/api-redundancy",
"description": "Reusable redundancy library for API queries", "description": "Reusable redundancy library for API queries",
"version": "1.1.0", "version": "1.1.1",
"author": "Vjacheslav Trushkin", "author": "Vjacheslav Trushkin",
"license": "MIT", "license": "MIT",
"main": "dist/index.js", "main": "dist/index.js",

View File

@ -54,38 +54,15 @@ export interface Redundancy {
cleanup: () => void; cleanup: () => void;
} }
/**
* Set configuration
*/
function setConfig(config: Partial<RedundancyConfig>): RedundancyConfig {
if (
typeof config !== 'object' ||
typeof (config as RedundancyConfig).resources !== 'object' ||
!((config as RedundancyConfig).resources instanceof Array) ||
!(config as RedundancyConfig).resources.length
) {
throw new Error('Invalid Reduncancy configuration');
}
const newConfig = Object.create(null);
let key: keyof RedundancyConfig;
for (key in defaultConfig) {
if (config[key] !== void 0) {
newConfig[key] = config[key];
} else {
newConfig[key] = defaultConfig[key];
}
}
return newConfig;
}
/** /**
* Redundancy instance * Redundancy instance
*/ */
export function initRedundancy(cfg: Partial<RedundancyConfig>): Redundancy { export function initRedundancy(cfg: Partial<RedundancyConfig>): Redundancy {
// Configuration // Configuration
const config: RedundancyConfig = setConfig(cfg); const config: RedundancyConfig = {
...defaultConfig,
...cfg,
};
// List of queries // List of queries
let queries: GetQueryStatus[] = []; let queries: GetQueryStatus[] = [];
@ -127,10 +104,11 @@ export function initRedundancy(cfg: Partial<RedundancyConfig>): Redundancy {
* Find instance * Find instance
*/ */
function find(callback: FilterCallback): GetQueryStatus | null { function find(callback: FilterCallback): GetQueryStatus | null {
const result = queries.find((value) => { return (
return callback(value); queries.find((value) => {
}); return callback(value);
return result !== void 0 ? result : null; }) || null
);
} }
// Create and return functions // Create and return functions