core-api: simplify ApiFactory type

This commit is contained in:
Patrik Oldsberg
2020-09-04 15:54:32 +02:00
parent d1eeb01b01
commit 4e390d6e9d
4 changed files with 22 additions and 31 deletions
@@ -41,9 +41,9 @@ type FactoryTuple = {
export class ApiFactoryRegistry implements ApiFactoryHolder {
private readonly factories = new Map<AnyApiRef, FactoryTuple>();
register<Api, Impl, Deps extends { [name in string]: unknown }>(
register<Api, Deps extends { [name in string]: unknown }>(
scope: ApiFactoryScope,
factory: ApiFactory<Api, Impl, Deps>,
factory: ApiFactory<Api, Deps>,
) {
const level = ScopeLevels[scope];
const existing = this.factories.get(factory.implements);
@@ -55,14 +55,12 @@ export class ApiFactoryRegistry implements ApiFactoryHolder {
return true;
}
get<T>(
api: ApiRef<T>,
): ApiFactory<T, T, { [x: string]: unknown }> | undefined {
get<T>(api: ApiRef<T>): ApiFactory<T, { [x: string]: unknown }> | undefined {
const tuple = this.factories.get(api);
if (!tuple) {
return undefined;
}
return tuple.factory as ApiFactory<T, T, { [x: string]: unknown }>;
return tuple.factory as ApiFactory<T, { [x: string]: unknown }>;
}
getAllApis(): Set<AnyApiRef> {
@@ -31,7 +31,9 @@ export class ApiTestRegistry implements ApiHolder {
return this.load(ref);
}
register<A, I, D>(factory: ApiFactory<A, I, D>): ApiTestRegistry {
register<A, D extends { [name in string]: unknown }>(
factory: ApiFactory<A, D>,
): ApiTestRegistry {
this.factories.set(factory.implements, factory);
return this;
}
+11 -12
View File
@@ -14,7 +14,7 @@
* limitations under the License.
*/
import { ApiFactory } from './types';
import { ApiFactory, TypesToApiRefs } from './types';
import { ApiRef } from './ApiRef';
/**
@@ -26,25 +26,24 @@ export function createApiFactory<
Api,
Impl extends Api,
Deps extends { [name in string]: unknown }
>(factory: ApiFactory<Api, Impl, Deps>): ApiFactory<Api, Impl, Deps>;
export function createApiFactory<Api, Impl extends Api>(
>(factory: ApiFactory<Api, Deps>): ApiFactory<Api, Deps>;
export function createApiFactory<Api>(
api: ApiRef<Api>,
instance: Impl,
): ApiFactory<Api, Impl, {}>;
instance: Api,
): ApiFactory<Api, {}>;
export function createApiFactory<
Api,
Impl extends Api,
Deps extends { [name in string]: unknown }
>(
factory: ApiFactory<Api, Impl, Deps> | ApiRef<Api>,
instance?: Impl,
): ApiFactory<Api, Impl, Deps> {
factory: ApiFactory<Api, Deps> | ApiRef<Api>,
instance?: Api,
): ApiFactory<Api, Deps> {
if ('id' in factory) {
return ({
return {
implements: factory,
deps: {},
deps: {} as TypesToApiRefs<Deps>,
factory: () => instance!,
} as unknown) as ApiFactory<Api, Impl, Deps>;
};
}
return factory;
}
+4 -12
View File
@@ -30,24 +30,16 @@ export type ApiHolder = {
get<T>(api: ApiRef<T>): T | undefined;
};
export type ApiFactory<
Api,
Impl,
Deps extends { [name in string]: unknown }
> = {
export type ApiFactory<Api, Deps extends { [name in string]: unknown }> = {
implements: ApiRef<Api>;
deps: TypesToApiRefs<Deps>;
factory(deps: Deps): Impl extends Api ? Impl : never;
factory(deps: Deps): Api;
};
export type AnyApiFactory = ApiFactory<
unknown,
unknown,
{ [key in string]: unknown }
>;
export type AnyApiFactory = ApiFactory<unknown, { [key in string]: unknown }>;
export type ApiFactoryHolder = {
get<T>(
api: ApiRef<T>,
): ApiFactory<T, T, { [key in string]: unknown }> | undefined;
): ApiFactory<T, { [key in string]: unknown }> | undefined;
};