@webext-core/proxy-serviceDeepAsynctype DeepAsync<TService> = TService extends (...args: any) => any
? ToAsyncFunction<TService>
: TService extends { [key: string]: any }
? {
[fn in keyof TService]: DeepAsync<TService[fn]>;
}
: never;
A recursive type that deeply converts all methods in TService to be async.
defineProxyServicefunction defineProxyService<TService extends Service, TArgs extends any[]>(
name: string,
init: (...args: TArgs) => TService,
config?: ProxyServiceConfig,
): [
registerService: (...args: TArgs) => TService,
getService: () => ProxyService<TService>,
] {
// ...
}
Utility for creating a service whose functions are executed in the background script regardless of the JS context the they are called from.
name: stringinit: (...args: TArgs) => TServiceregisterService will require the same set of arguments.config?: ProxyServiceConfigregisterService: Used to register your service in the backgroundgetService: Used to get an instance of the service anywhere in the extension.flattenPromisefunction flattenPromise<T>(promise: Promise<T>): DeepAsync<T> {
// ...
}
Given a promise of a variable, return a proxy to that awaits the promise internally so you don't
have to call await twice.
This can be used to simplify handling
Promise<Dependency>passed in your services.
function createService(dependencyPromise: Promise<SomeDependency>) {
const dependency = flattenPromise(dependencyPromise);
return {
doSomething() {
await dependency.someAsyncWork();
// Instead of `await (await dependencyPromise).someAsyncWork();`
}
}
}
ProxyServicetype ProxyService<TService> =
TService extends DeepAsync<TService> ? TService : DeepAsync<TService>;
A type that ensures a service has only async methods.
DeepAsync of the service.ProxyServiceConfiginterface ProxyServiceConfig extends ExtensionMessagingConfig {}
Configure a proxy service's behavior. It uses @webext-core/messaging internally, so any
config from ExtensionMessagingConfig can be passed as well.
API reference generated by docs/generate-api-references.ts