feat(lib): small performance improvements
This commit is contained in:
parent
3c541aefee
commit
7d7d6a4358
7 changed files with 28 additions and 47 deletions
|
|
@ -45,11 +45,9 @@ export function* getCustomAssets(): Generator<CustomAsset> {
|
|||
* Yields all registered packager assets, including ones with same name but different types.
|
||||
*/
|
||||
export function* getPackagerAssets(): Generator<PackagerAsset> {
|
||||
for (const name in cache) {
|
||||
const reg = cache[name]
|
||||
for (const type in reg)
|
||||
yield AssetsRegistry.getAssetByID(__r(reg[type]))
|
||||
}
|
||||
for (const reg of Object.values(cache))
|
||||
for (const moduleId of Object.values(reg))
|
||||
yield AssetsRegistry.getAssetByID(__r(moduleId))
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -90,7 +88,11 @@ export function getAssetsByName(
|
|||
|
||||
/**
|
||||
* Get an asset ID by its name.
|
||||
* If more than one asset is registered with the same name, this will return the one with the preferred type, or the first registered one.
|
||||
*
|
||||
* If more than one asset is registered with the same name, this will return the one with the preferred type.
|
||||
*
|
||||
* Unless **explicitly** calling with a preferred type,
|
||||
* another asset with type mismatching the {@link setPreferredAssetType current preferred type} may be returned as a fallback.
|
||||
*
|
||||
* @param name The asset name.
|
||||
* @param type The preferred asset type, defaults to the current preferred type.
|
||||
|
|
@ -102,22 +104,15 @@ export function getAssetIdByName(
|
|||
const reg = cache[name]
|
||||
if (!reg) return
|
||||
|
||||
if (type) {
|
||||
if (type !== undefined) {
|
||||
const mid = reg[type]
|
||||
if (mid === undefined) return
|
||||
return __r(mid)
|
||||
return mid && __r(mid)
|
||||
}
|
||||
|
||||
let mid = reg[_preferredType]
|
||||
if (mid === undefined)
|
||||
for (const t in reg) {
|
||||
mid = reg[t]
|
||||
break
|
||||
}
|
||||
mid ??= Object.values(reg)[0]
|
||||
|
||||
if (mid === undefined) return
|
||||
|
||||
return __r(mid)
|
||||
return mid && __r(mid)
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
|||
|
|
@ -75,7 +75,7 @@ export function registerSettingsItem(key: string, item: SettingsItem) {
|
|||
export function registerSettingsItems(record: Record<string, SettingsItem>) {
|
||||
Object.assign(sConfig, record)
|
||||
return () => {
|
||||
for (const key in record) delete sConfig[key]
|
||||
for (const key of Object.keys(record)) delete sConfig[key]
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -154,15 +154,8 @@ export type BySingleProp = FilterGenerator<
|
|||
*/
|
||||
export const bySingleProp = createFilterGenerator<Parameters<BySingleProp>>(
|
||||
([prop], _, exports) => {
|
||||
if (typeof exports === 'object' && prop in exports) {
|
||||
let hasMultipleProperties = false
|
||||
for (const _ in exports) {
|
||||
if (hasMultipleProperties) return false
|
||||
hasMultipleProperties = true
|
||||
}
|
||||
|
||||
return true
|
||||
}
|
||||
if (typeof exports === 'object' && prop in exports)
|
||||
return Object.keys(exports).length === 1
|
||||
|
||||
return false
|
||||
},
|
||||
|
|
|
|||
|
|
@ -170,8 +170,7 @@ export function* lookupModules(filter: Filter, options?: LookupModulesOptions) {
|
|||
if (reg) {
|
||||
cached = new Set()
|
||||
|
||||
for (const sId in reg) {
|
||||
const flag = reg[sId]
|
||||
for (const [sId, flag] of Object.entries(reg)) {
|
||||
const id = Number(sId)
|
||||
let exports: Metro.ModuleExports | undefined
|
||||
|
||||
|
|
@ -297,8 +296,7 @@ export function lookupModule(filter: Filter, options?: LookupModulesOptions) {
|
|||
if (reg === null) return NotFoundResult
|
||||
|
||||
if (reg)
|
||||
for (const sId in reg) {
|
||||
const flag = reg[sId]
|
||||
for (const [sId, flag] of Object.entries(reg)) {
|
||||
const id = Number(sId)
|
||||
let exports: Metro.ModuleExports
|
||||
|
||||
|
|
|
|||
|
|
@ -124,7 +124,7 @@ function handleFactoryCall(
|
|||
// @ts-expect-error: Intentional
|
||||
// biome-ignore lint/suspicious/noFallthroughSwitchClause: Intentional
|
||||
case 'object': {
|
||||
if (Reflect.ownKeys(exports).length) {
|
||||
if (Object.keys(exports).length) {
|
||||
mInitialized.add(mInitializingId)
|
||||
break
|
||||
}
|
||||
|
|
|
|||
|
|
@ -83,15 +83,8 @@ export const byGeneratedIconComponent = createFilterGenerator<
|
|||
>(
|
||||
(names, id, exports) => {
|
||||
if (typeof exports === 'object') {
|
||||
if (typeof exports[names[0]] === 'function') {
|
||||
let hasMultipleProps = false
|
||||
for (const _ in exports) {
|
||||
if (hasMultipleProps) return false
|
||||
hasMultipleProps = true
|
||||
}
|
||||
|
||||
return true
|
||||
}
|
||||
if (typeof exports[names[0]] === 'function')
|
||||
return Object.keys(exports).length === 1
|
||||
} else {
|
||||
let filter = IconComponentFilter
|
||||
|
||||
|
|
|
|||
|
|
@ -19,11 +19,11 @@ export function isObject(val: any): val is AnyObject {
|
|||
*/
|
||||
export function mergeDeep(target: AnyObject, source: AnyObject) {
|
||||
if (isObject(target) && isObject(source))
|
||||
for (const key in source)
|
||||
if (isObject(source[key])) {
|
||||
for (const [key, value] of Object.entries(source))
|
||||
if (isObject(value)) {
|
||||
if (!target[key]) Object.assign(target, { [key]: {} })
|
||||
mergeDeep(target[key], source[key])
|
||||
} else Object.assign(target, { [key]: source[key] })
|
||||
mergeDeep(target[key], value)
|
||||
} else Object.assign(target, { [key]: value })
|
||||
|
||||
return target
|
||||
}
|
||||
|
|
@ -61,8 +61,10 @@ export function defineLazyProperties<T extends object>(
|
|||
) {
|
||||
const descs: PropertyDescriptorMap = {}
|
||||
|
||||
for (const key in loaders)
|
||||
descs[key] = lazyPropDesc<T, keyof typeof loaders>(key, loaders[key]!)
|
||||
for (const [key, loader] of Object.entries(loaders) as Array<
|
||||
[keyof T, (typeof loaders)[keyof T]]
|
||||
>)
|
||||
descs[key] = lazyPropDesc<T, keyof typeof loaders>(key, loader!)
|
||||
|
||||
return Object.defineProperties(target, descs)
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue