@nori-zk/mina-token-bridge
Version:
A Mina zk-program contract allowing users to mint tokens on Nori Bridge.
156 lines (155 loc) • 5.65 kB
TypeScript
/**
* Base interface for a communication endpoint between a parent and a worker.
*/
export interface BaseWorkerEndpoint {
/**
* Registers a handler for incoming messages.
* @param callback - Callback to invoke with the received message.
*/
onMessageHandler(callback: (response: string) => void): void;
/**
* Registers a handler for error events.
* @param callback - Callback to invoke with the error.
*/
onErrorHandler(callback: (error: any) => void): void;
/**
* Terminates the communication channel.
*/
terminate(): void;
}
/**
* Interface representing the parent side of the worker communication.
*/
export interface WorkerChildParentInterface extends BaseWorkerEndpoint {
/**
* Sends a message to the child.
* @param args - The message arguments.
*/
send(...args: any[]): void;
/**
* Terminates the child worker.
*/
terminate: () => void;
}
/**
* Interface representing the child side of the worker communication.
*/
export interface WorkerParentChildInterface extends BaseWorkerEndpoint {
/**
* Sends a message to the parent.
* @param args - The message arguments.
*/
call(...args: any[]): Promise<void>;
/**
* Terminates the child endpoint.
*/
terminate: () => void;
/**
* Worker is ready
*/
ready: () => Promise<void>;
}
/**
* Extracts keys of methods that return a Promise from type T.
*/
type MethodNames<T> = {
[K in keyof T]: T[K] extends (...args: any) => Promise<any> ? K : never;
}[keyof T];
/**
* A generic mapping of async worker method names to their function signatures.
*/
export type WorkerMethodSpec = Record<string, (...args: any[]) => Promise<any>>;
/**
* Creates a proxy type from T containing only async methods.
*/
export type WorkerChildInfer<T> = {
[K in MethodNames<T>]: T[K] extends (...args: infer Args) => Promise<infer Res> ? (...args: Args) => Promise<Res> : never;
};
/**
* A deferred promise pattern with externally accessible resolve and reject.
*/
export declare class DeferredPromise<T = void, E = void> {
resolve: (output: T) => void;
reject: (error: E) => void;
promise: Promise<T>;
constructor();
}
/**
* Parent-side base handler for managing requests sent to the child.
*/
export declare class WorkerParentBase {
#private;
/**
* Constructs the WorkerParentBase and attaches communication handlers.
* @param child - The child endpoint to communicate with.
*/
constructor(child: WorkerParentChildInterface);
/**
* Sends a request to the child with a method name and arguments, and returns a promise for the response.
*
* @param methodName - The name of the method to invoke on the child.
* @param args - Arguments to pass to the child method.
* @returns A promise that resolves with the result of the child method call.
*/
call<Res>(methodName: string, ...args: any[]): Promise<Res>;
/**
* Terminates the child endpoint.
*/
terminate(): void;
}
/**
* Child-side base handler for responding to messages from the parent.
*/
export declare class WorkerChildBase {
#private;
/**
* Constructs the WorkerChildBase and attaches communication handlers.
* @param parent - The parent endpoint to respond to.
*/
constructor(parent: WorkerChildParentInterface);
}
/**
* Binds worker logic implementation to a communication channel as a child.
*
* @param parent - The parent communication endpoint
* @param ChildClass - The class defining worker logic
* @param args - Arguments passed to the worker logic constructor
* @returns An object with a terminate method
*/
export declare function createWorker<T>(parent: WorkerChildParentInterface, ChildClass: new (...args: any[]) => T, ...args: any[]): {
terminate: () => void;
};
/**
* Creates a proxy for the parent to invoke child logic via async calls.
*
* @param child - The child endpoint
* @param ChildClass - The class defining the child’s logic
* @param args - Arguments passed to the logic class constructor
* @returns A proxy object with async methods and a terminate method
*/
export declare function createProxy<T>(child: WorkerParentChildInterface, ChildClass: new (...args: any[]) => T, ...args: any[]): WorkerChildInfer<T> & {
terminate(): void;
};
/**
* Creates a proxy for the parent to invoke child logic via async calls,
* using a predefined method specification instead of inferring methods
* from a class prototype.
*
* @param child - The child endpoint
* @param methodSpec - An object whose keys are method names and values are functions
* describing the expected parameters/return type for each call
* @returns A proxy object with async methods matching the provided specification
* and a terminate method for stopping the worker.
*
* @remarks
* - This is useful when the child logic is not represented by a single class
* or when you want to strictly control which methods are exposed to the parent.
* - It also avoids the need to construct a class instance in the parent context,
* which can be valuable if that constructor has heavy setup logic or depends on
* modules with top-level async/await that would otherwise block initialization.
* - All generated methods return Promises, since worker communication is asynchronous.
*/
export declare function createProxyFromSpec<T extends WorkerMethodSpec>(child: WorkerParentChildInterface, methodSpec: T): { [K in keyof T]: (...args: Parameters<T[K]>) => ReturnType<T[K]>; } & {
terminate(): void;
};
export {};