brick-module
Version:
Better React Native native module development
64 lines (62 loc) • 2.19 kB
TypeScript
import { TurboModule } from "react-native";
//#region src/BrickModule.d.ts
/**
* Base interface that all Brick module specs must extend
*/
interface BrickModuleInterface extends TurboModule {
readonly moduleName: string;
readonly supportedEvents?: readonly string[];
}
/**
* Type alias for module specifications
* Use this as the base interface when defining your module specs
* @example
* export interface MyModuleSpec extends BrickModuleSpec {
* readonly moduleName: "MyModule";
* readonly supportedEvents: ["eventA", "eventB"];
* myMethod(param: string): Promise<string>;
* }
*/
type BrickModuleSpec = BrickModuleInterface;
/**
* Enhanced typed module interface with event listeners
*/
type BrickModuleWithEvents<T extends BrickModuleInterface> = T & {
/**
* Adds a type-safe event listener for this module
* @param eventName - One of the supported events defined in the module spec
* @param listener - Callback function to handle the event
* @returns Unsubscription function to remove the listener
*/
addEventListener<TEvent = unknown>(eventName: T["supportedEvents"] extends readonly (infer U)[] ? U : never, listener: (event: TEvent) => void): () => void;
};
/**
* Gets a typed module instance by name with explicit type parameter
* @param moduleName - The exact name of the module as defined in its spec
* @returns Typed module interface with all methods, constants, and event listeners
*/
declare function get<T extends BrickModuleInterface>(moduleName: string): BrickModuleWithEvents<T>;
/**
* Gets list of all registered modules
* @returns Promise resolving to array of module names
*/
declare function getRegisteredModules(): string[];
/**
* Clears the module cache (useful for testing or hot reloading)
* @internal
*/
declare function clearCache(): void;
/**
* Main Brick Module API object
* Provides type-safe access to native modules
*/
declare const BrickModule: {
readonly get: typeof get;
readonly getRegisteredModules: typeof getRegisteredModules;
readonly clearCache: typeof clearCache;
};
/**
* Default export for convenience
*/
//#endregion
export { BrickModule, BrickModuleInterface, BrickModuleSpec };