2025-07-05 13:12:57 +07:00
|
|
|
import { mInitialized } from '../metro/_internal'
|
2025-06-03 16:24:58 +07:00
|
|
|
import {
|
|
|
|
|
onAnyModuleInitialized,
|
|
|
|
|
onModuleFinishedImporting,
|
|
|
|
|
} from '../metro/subscriptions'
|
2025-07-03 20:54:00 +07:00
|
|
|
import { getInitializedModuleExports } from '../metro/utils'
|
2025-06-03 16:24:58 +07:00
|
|
|
import { exportsFromFilterResultFlag, runFilter } from './_internal'
|
|
|
|
|
import type { MaybeDefaultExportMatched, Metro } from '../types'
|
|
|
|
|
import type { RunFilterReturnExportsOptions } from './_internal'
|
2025-03-30 23:32:17 +07:00
|
|
|
import type { Filter, FilterResult } from './filters'
|
|
|
|
|
|
2025-07-05 13:12:57 +07:00
|
|
|
export interface BaseWaitForModulesOptions<All extends boolean = boolean> {
|
2025-04-16 14:23:48 +07:00
|
|
|
/**
|
2025-07-04 21:38:37 +07:00
|
|
|
* Whether to include all modules, including blacklisted ones.
|
2025-04-16 14:23:48 +07:00
|
|
|
*
|
|
|
|
|
* @default false
|
|
|
|
|
*/
|
2025-07-05 13:12:57 +07:00
|
|
|
all?: All
|
2025-04-16 14:23:48 +07:00
|
|
|
}
|
|
|
|
|
|
2025-05-18 22:24:48 +07:00
|
|
|
export type WaitForModulesUnsubscribeFunction = () => boolean
|
|
|
|
|
|
|
|
|
|
export type WaitForModulesCallback<T> = (exports: T, id: Metro.ModuleID) => any
|
|
|
|
|
|
2025-04-16 14:23:48 +07:00
|
|
|
export type WaitForModulesOptions<
|
|
|
|
|
ReturnNamespace extends boolean = boolean,
|
2025-07-05 13:12:57 +07:00
|
|
|
All extends boolean = boolean,
|
2025-06-03 16:24:58 +07:00
|
|
|
> = RunFilterReturnExportsOptions<ReturnNamespace> &
|
2025-07-05 13:12:57 +07:00
|
|
|
BaseWaitForModulesOptions<All>
|
2025-04-09 03:42:06 +07:00
|
|
|
|
|
|
|
|
export type WaitForModulesResult<
|
|
|
|
|
F extends Filter,
|
|
|
|
|
O extends WaitForModulesOptions,
|
2025-06-03 16:24:58 +07:00
|
|
|
> = O extends RunFilterReturnExportsOptions<true>
|
|
|
|
|
? MaybeDefaultExportMatched<FilterResult<F>>
|
|
|
|
|
: FilterResult<F>
|
2025-03-30 23:32:17 +07:00
|
|
|
|
|
|
|
|
/**
|
2025-04-09 04:10:41 +07:00
|
|
|
* Wait for modules to initialize. **Callback won't be called if the module is already initialized!**
|
2025-03-30 23:32:17 +07:00
|
|
|
*
|
|
|
|
|
* @param filter The filter to use.
|
|
|
|
|
* @param callback The callback to call when matching modules are initialized.
|
|
|
|
|
* @param options The options to use for the wait.
|
|
|
|
|
* @returns A function to unsubscribe.
|
|
|
|
|
*
|
|
|
|
|
* @example
|
|
|
|
|
* ```ts
|
2025-04-09 04:10:41 +07:00
|
|
|
* const unsub = waitForModules(
|
2025-03-30 23:32:17 +07:00
|
|
|
* byName<typeof import('@shopify/flash-list')>('FlashList'),
|
|
|
|
|
* // (exports: typeof import('@shopify/flash-list'), id: Metro.ModuleID) => any
|
2025-05-18 22:24:48 +07:00
|
|
|
* (exports, id) => {
|
2025-04-09 04:10:41 +07:00
|
|
|
* unsub()
|
|
|
|
|
* // Do something with the module...
|
|
|
|
|
* }
|
2025-03-30 23:32:17 +07:00
|
|
|
* )
|
|
|
|
|
* ```
|
|
|
|
|
*/
|
2025-04-05 20:52:09 +07:00
|
|
|
export function waitForModules<F extends Filter>(
|
2025-03-30 23:32:17 +07:00
|
|
|
filter: F,
|
2025-05-18 22:24:48 +07:00
|
|
|
callback: WaitForModulesCallback<WaitForModulesResult<F, object>>,
|
|
|
|
|
): WaitForModulesUnsubscribeFunction
|
2025-04-05 20:52:09 +07:00
|
|
|
|
2025-04-16 14:23:48 +07:00
|
|
|
export function waitForModules<
|
2025-06-03 16:24:58 +07:00
|
|
|
F extends O extends WaitForModulesOptions<boolean, true>
|
|
|
|
|
? Filter<any, false>
|
|
|
|
|
: Filter,
|
2025-04-16 14:23:48 +07:00
|
|
|
O extends WaitForModulesOptions,
|
2025-05-18 22:24:48 +07:00
|
|
|
>(
|
|
|
|
|
filter: F,
|
|
|
|
|
callback: WaitForModulesCallback<WaitForModulesResult<F, O>>,
|
|
|
|
|
options: O,
|
|
|
|
|
): WaitForModulesUnsubscribeFunction
|
2025-04-05 20:52:09 +07:00
|
|
|
|
2025-06-03 16:24:58 +07:00
|
|
|
export function waitForModules(
|
|
|
|
|
filter: Filter,
|
|
|
|
|
callback: WaitForModulesCallback<any>,
|
|
|
|
|
options?: WaitForModulesOptions,
|
|
|
|
|
) {
|
2025-05-09 19:05:31 +07:00
|
|
|
return onAnyModuleInitialized(
|
2025-07-04 21:38:37 +07:00
|
|
|
options?.all
|
2025-05-09 19:05:31 +07:00
|
|
|
? (id, exports) => {
|
|
|
|
|
const flag = runFilter(filter, id, exports, options)
|
2025-06-03 16:24:58 +07:00
|
|
|
if (flag)
|
|
|
|
|
callback(
|
|
|
|
|
exportsFromFilterResultFlag(flag, exports, options),
|
|
|
|
|
id,
|
|
|
|
|
)
|
2025-05-09 19:05:31 +07:00
|
|
|
}
|
|
|
|
|
: (id, exports) => {
|
2025-07-05 13:12:57 +07:00
|
|
|
if (mInitialized.has(id)) {
|
2025-07-03 20:54:00 +07:00
|
|
|
const flag = runFilter(filter, id, exports, options)
|
|
|
|
|
if (flag)
|
|
|
|
|
callback(
|
|
|
|
|
exportsFromFilterResultFlag(
|
|
|
|
|
flag,
|
|
|
|
|
exports,
|
|
|
|
|
options,
|
|
|
|
|
),
|
|
|
|
|
id,
|
|
|
|
|
)
|
|
|
|
|
}
|
2025-05-09 19:05:31 +07:00
|
|
|
},
|
|
|
|
|
)
|
2025-03-30 23:32:17 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2025-04-16 14:23:48 +07:00
|
|
|
* Wait for a module to initialize by its imported path. **Callback won't be called if the module is already initialized!**
|
|
|
|
|
*
|
|
|
|
|
* Once callback is called, the subscription will be removed automatically, because modules have unique imported paths.
|
2025-04-09 04:10:41 +07:00
|
|
|
*
|
|
|
|
|
* Think of it as if you are doing `import * as exports from path`, and you are also waiting for the app to initialize the module by itself.
|
2025-04-09 03:42:06 +07:00
|
|
|
*
|
2025-03-30 23:32:17 +07:00
|
|
|
* @param path The path to wait for.
|
|
|
|
|
* @param callback The callback to call once the module is initialized.
|
2025-05-05 21:17:42 +07:00
|
|
|
* @param options The options to use for the wait.
|
2025-03-30 23:32:17 +07:00
|
|
|
* @returns A function to unsubscribe.
|
2025-04-09 04:10:41 +07:00
|
|
|
*
|
|
|
|
|
* @example
|
|
|
|
|
* ```ts
|
|
|
|
|
* waitForModuleByImportedPath(
|
|
|
|
|
* 'utils/PlatformUtils.tsx',
|
2025-05-18 22:24:48 +07:00
|
|
|
* (exports, id) => {
|
2025-04-09 04:10:41 +07:00
|
|
|
* // Do something with the module...
|
|
|
|
|
* }
|
|
|
|
|
* )
|
|
|
|
|
* ```
|
2025-03-30 23:32:17 +07:00
|
|
|
*/
|
2025-04-16 14:23:48 +07:00
|
|
|
export function waitForModuleByImportedPath<T = any>(
|
|
|
|
|
path: string,
|
2025-05-18 22:24:48 +07:00
|
|
|
callback: WaitForModulesCallback<T>,
|
2025-04-16 14:23:48 +07:00
|
|
|
options?: BaseWaitForModulesOptions,
|
|
|
|
|
) {
|
2025-05-09 19:05:31 +07:00
|
|
|
const unsub = onModuleFinishedImporting(
|
2025-07-04 21:38:37 +07:00
|
|
|
options?.all
|
2025-05-09 19:05:31 +07:00
|
|
|
? (id, cmpPath) => {
|
|
|
|
|
if (path === cmpPath) {
|
|
|
|
|
unsub()
|
2025-05-18 22:24:48 +07:00
|
|
|
callback(getInitializedModuleExports(id), id)
|
2025-05-09 19:05:31 +07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
: (id, cmpPath) => {
|
2025-07-05 13:12:57 +07:00
|
|
|
if (path === cmpPath && mInitialized.has(id)) {
|
2025-05-09 19:05:31 +07:00
|
|
|
unsub()
|
2025-05-18 22:24:48 +07:00
|
|
|
callback(getInitializedModuleExports(id), id)
|
2025-05-09 19:05:31 +07:00
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
)
|
2025-03-30 23:32:17 +07:00
|
|
|
|
|
|
|
|
return unsub
|
|
|
|
|
}
|