-
-
Notifications
You must be signed in to change notification settings - Fork 37
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
15 changed files
with
168 additions
and
129 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
import { getMetroCache } from "@metro/internals/caches"; | ||
import { ModuleFlags } from "@metro/internals/enums"; | ||
import { requireModule } from "@metro/internals/modules"; | ||
|
||
import { assetsModule } from "./patches"; | ||
|
||
export interface Asset { | ||
id: number; | ||
name: string; | ||
moduleId: number; | ||
} | ||
|
||
// Cache common usage | ||
const _nameToAssetCache = {} as Record<string, Asset>; | ||
|
||
export function* iterateAssets() { | ||
const { flagsIndex } = getMetroCache(); | ||
const yielded = new Set<number>(); | ||
|
||
for (const id in flagsIndex) { | ||
if (flagsIndex[id] & ModuleFlags.ASSET) { | ||
const assetId = requireModule(Number(id)); | ||
if (typeof assetId !== "number" || yielded.has(assetId)) continue; | ||
yield getAssetById(assetId); | ||
yielded.add(assetId); | ||
} | ||
} | ||
} | ||
|
||
// Apply additional properties for convenience | ||
function getAssetById(id: number): Asset { | ||
const asset = assetsModule.getAssetByID(id); | ||
if (!asset) return asset; | ||
return Object.assign(asset, { id }); | ||
} | ||
|
||
/** | ||
* Returns the first asset registry by its registry id (number), name (string) or given filter (function) | ||
*/ | ||
export function findAsset(id: number): Asset | undefined; | ||
export function findAsset(name: string): Asset | undefined; | ||
export function findAsset(filter: (a: Asset) => boolean): Asset | undefined; | ||
|
||
export function findAsset(param: number | string | ((a: Asset) => boolean)) { | ||
if (typeof param === "number") return getAssetById(param); | ||
|
||
if (typeof param === "string" && _nameToAssetCache[param]) { | ||
return _nameToAssetCache[param]; | ||
} | ||
|
||
for (const asset of iterateAssets()) { | ||
if (typeof param === "string" && asset.name === param) { | ||
_nameToAssetCache[param] = asset; | ||
return asset; | ||
} else if (typeof param === "function" && param(asset)) { | ||
return asset; | ||
} | ||
} | ||
} | ||
|
||
export function filterAssets(param: string | ((a: Asset) => boolean)) { | ||
const filteredAssets = [] as Array<Asset>; | ||
|
||
for (const asset of iterateAssets()) { | ||
if (typeof param === "string" ? asset.name === param : param(asset)) { | ||
filteredAssets.push(asset); | ||
} | ||
} | ||
|
||
return filteredAssets; | ||
} | ||
|
||
/** | ||
* Returns the first asset ID in the registry with the given name | ||
*/ | ||
export function findAssetId(name: string) { | ||
return findAsset(name)?.id; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import { after } from "@lib/api/patcher"; | ||
import { indexAssetModuleFlag } from "@metro/internals/caches"; | ||
import { getImportingModuleId } from "@metro/internals/modules"; | ||
|
||
interface AssetModule { | ||
registerAsset(assetDefinition: any): number; | ||
getAssetByID(id: number): any; | ||
} | ||
|
||
export let assetsModule: AssetModule; | ||
|
||
/** | ||
* @internal | ||
*/ | ||
export function patchAssets(module: AssetModule) { | ||
if (assetsModule) return; | ||
assetsModule = module; | ||
|
||
const unpatch = after("registerAsset", assetsModule, () => { | ||
const moduleId = getImportingModuleId(); | ||
if (moduleId !== -1) indexAssetModuleFlag(moduleId); | ||
}); | ||
|
||
return unpatch; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export type Nullish = null | undefined; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
import { findByStoreNameLazy } from "@metro/wrappers"; | ||
|
||
export const UserStore = findByStoreNameLazy("UserStore"); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.