backend-plugin-api: refactor service factory to use new option callback pattern
Co-authored-by: Fredrik Adelöw <freben@gmail.com> Co-authored-by: blam <ben@blam.sh> Co-authored-by: Johan Haals <johan.haals@gmail.com> Signed-off-by: Patrik Oldsberg <poldsberg@gmail.com>
This commit is contained in:
@@ -43,13 +43,13 @@ describe('createServiceFactory', () => {
|
||||
|
||||
it('should create a meta factory with optional options', () => {
|
||||
const ref = createServiceRef<string>({ id: 'x' });
|
||||
const metaFactory = createServiceFactory({
|
||||
const metaFactory = createServiceFactory((_opts?: { x: number }) => ({
|
||||
service: ref,
|
||||
deps: {},
|
||||
async factory(_deps, _opts?: { x: number }) {
|
||||
async factory() {
|
||||
return async () => 'x';
|
||||
},
|
||||
});
|
||||
}));
|
||||
expect(metaFactory).toEqual(expect.any(Function));
|
||||
|
||||
// @ts-expect-error
|
||||
@@ -67,13 +67,13 @@ describe('createServiceFactory', () => {
|
||||
|
||||
it('should create a meta factory with required options', () => {
|
||||
const ref = createServiceRef<string>({ id: 'x' });
|
||||
const metaFactory = createServiceFactory({
|
||||
const metaFactory = createServiceFactory((_opts: { x: number }) => ({
|
||||
service: ref,
|
||||
deps: {},
|
||||
async factory(_deps, _opts: { x: number }) {
|
||||
async factory() {
|
||||
return async () => 'x';
|
||||
},
|
||||
});
|
||||
}));
|
||||
expect(metaFactory).toEqual(expect.any(Function));
|
||||
|
||||
// @ts-expect-error
|
||||
@@ -96,13 +96,13 @@ describe('createServiceFactory', () => {
|
||||
x: number;
|
||||
}
|
||||
const ref = createServiceRef<string>({ id: 'x' });
|
||||
const metaFactory = createServiceFactory({
|
||||
const metaFactory = createServiceFactory((_opts?: TestOptions) => ({
|
||||
service: ref,
|
||||
deps: {},
|
||||
async factory(_deps, _opts?: TestOptions) {
|
||||
async factory() {
|
||||
return async () => 'x';
|
||||
},
|
||||
});
|
||||
}));
|
||||
expect(metaFactory).toEqual(expect.any(Function));
|
||||
|
||||
// @ts-expect-error
|
||||
@@ -123,13 +123,13 @@ describe('createServiceFactory', () => {
|
||||
x: number;
|
||||
}
|
||||
const ref = createServiceRef<string>({ id: 'x' });
|
||||
const metaFactory = createServiceFactory({
|
||||
const metaFactory = createServiceFactory((_opts: TestOptions) => ({
|
||||
service: ref,
|
||||
deps: {},
|
||||
async factory(_deps, _opts: TestOptions) {
|
||||
async factory() {
|
||||
return async () => 'x';
|
||||
},
|
||||
});
|
||||
}));
|
||||
expect(metaFactory).toEqual(expect.any(Function));
|
||||
|
||||
// @ts-expect-error
|
||||
@@ -149,78 +149,78 @@ describe('createServiceFactory', () => {
|
||||
|
||||
it('should only allow objects as options', () => {
|
||||
const ref = createServiceRef<string>({ id: 'x' });
|
||||
const metaFactory = createServiceFactory({
|
||||
// @ts-expect-error
|
||||
const metaFactory = createServiceFactory((_opts: string) => ({
|
||||
service: ref,
|
||||
deps: {},
|
||||
// @ts-expect-error
|
||||
async factory(_deps, _opts: string) {
|
||||
async factory() {
|
||||
return async () => 'x';
|
||||
},
|
||||
});
|
||||
}));
|
||||
expect(metaFactory).toEqual(expect.any(Function));
|
||||
createServiceFactory({
|
||||
// @ts-expect-error
|
||||
createServiceFactory((_opts: number) => ({
|
||||
service: ref,
|
||||
deps: {},
|
||||
// @ts-expect-error
|
||||
async factory(_deps, _opts: number) {
|
||||
async factory() {
|
||||
return async () => 'x';
|
||||
},
|
||||
});
|
||||
createServiceFactory({
|
||||
}));
|
||||
// @ts-expect-error
|
||||
createServiceFactory((_opts: symbol) => ({
|
||||
service: ref,
|
||||
deps: {},
|
||||
// @ts-expect-error
|
||||
async factory(_deps, _opts: symbol) {
|
||||
async factory() {
|
||||
return async () => 'x';
|
||||
},
|
||||
});
|
||||
createServiceFactory({
|
||||
}));
|
||||
// @ts-expect-error
|
||||
createServiceFactory((_opts: bigint) => ({
|
||||
service: ref,
|
||||
deps: {},
|
||||
// @ts-expect-error
|
||||
async factory(_deps, _opts: bigint) {
|
||||
async factory() {
|
||||
return async () => 'x';
|
||||
},
|
||||
});
|
||||
createServiceFactory({
|
||||
}));
|
||||
// @ts-expect-error
|
||||
createServiceFactory((_opts: 'string') => ({
|
||||
service: ref,
|
||||
deps: {},
|
||||
// @ts-expect-error
|
||||
async factory(_deps, _opts: 'string') {
|
||||
async factory() {
|
||||
return async () => 'x';
|
||||
},
|
||||
});
|
||||
createServiceFactory({
|
||||
}));
|
||||
// @ts-expect-error
|
||||
createServiceFactory((_opts: Array) => ({
|
||||
service: ref,
|
||||
deps: {},
|
||||
// @ts-expect-error
|
||||
async factory(_deps, _opts: Array) {
|
||||
async factory() {
|
||||
return async () => 'x';
|
||||
},
|
||||
});
|
||||
createServiceFactory({
|
||||
}));
|
||||
// @ts-expect-error
|
||||
createServiceFactory((_opts: Map) => ({
|
||||
service: ref,
|
||||
deps: {},
|
||||
// @ts-expect-error
|
||||
async factory(_deps, _opts: Map) {
|
||||
async factory() {
|
||||
return async () => 'x';
|
||||
},
|
||||
});
|
||||
createServiceFactory({
|
||||
}));
|
||||
// @ts-expect-error
|
||||
createServiceFactory((_opts: Set) => ({
|
||||
service: ref,
|
||||
deps: {},
|
||||
// @ts-expect-error
|
||||
async factory(_deps, _opts: Set) {
|
||||
async factory() {
|
||||
return async () => 'x';
|
||||
},
|
||||
});
|
||||
createServiceFactory({
|
||||
}));
|
||||
// @ts-expect-error
|
||||
createServiceFactory((_opts: null) => ({
|
||||
service: ref,
|
||||
deps: {},
|
||||
// @ts-expect-error
|
||||
async factory(_deps, _opts: null) {
|
||||
async factory() {
|
||||
return async () => 'x';
|
||||
},
|
||||
});
|
||||
}));
|
||||
});
|
||||
});
|
||||
|
||||
@@ -14,7 +14,11 @@
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
import { FactoryFunctionWithOptions, MaybeOptions } from '../../types';
|
||||
import {
|
||||
FactoryFunctionConfig,
|
||||
FactoryFunctionWithOptions,
|
||||
MaybeOptions,
|
||||
} from '../../types';
|
||||
|
||||
/**
|
||||
* TODO
|
||||
@@ -144,13 +148,11 @@ export interface ServiceFactoryConfig<
|
||||
TScope extends 'root' | 'plugin',
|
||||
TImpl extends TService,
|
||||
TDeps extends { [name in string]: ServiceRef<unknown> },
|
||||
TOpts extends MaybeOptions = undefined,
|
||||
> {
|
||||
service: ServiceRef<TService, TScope>;
|
||||
deps: TDeps;
|
||||
factory(
|
||||
deps: ServiceRefsToInstances<TDeps, 'root'>,
|
||||
options: TOpts,
|
||||
): TScope extends 'root'
|
||||
? Promise<TImpl>
|
||||
: Promise<(deps: ServiceRefsToInstances<TDeps>) => Promise<TImpl>>;
|
||||
@@ -166,15 +168,20 @@ export function createServiceFactory<
|
||||
TDeps extends { [name in string]: ServiceRef<unknown> },
|
||||
TOpts extends MaybeOptions = undefined,
|
||||
>(
|
||||
config: ServiceFactoryConfig<TService, TScope, TImpl, TDeps, TOpts>,
|
||||
config: FactoryFunctionConfig<
|
||||
ServiceFactoryConfig<TService, TScope, TImpl, TDeps>,
|
||||
TOpts
|
||||
>,
|
||||
): FactoryFunctionWithOptions<ServiceFactory<TService>, TOpts> {
|
||||
return (options?: TOpts) =>
|
||||
if (typeof config === 'function') {
|
||||
return (opts?: TOpts) => {
|
||||
const c = config(opts!);
|
||||
return { ...c, scope: c.service.scope } as ServiceFactory<TService>;
|
||||
};
|
||||
}
|
||||
return () =>
|
||||
({
|
||||
...config,
|
||||
scope: config.service.scope,
|
||||
service: config.service,
|
||||
deps: config.deps,
|
||||
factory(deps: ServiceRefsToInstances<TDeps, 'root'>) {
|
||||
return config.factory(deps, options!);
|
||||
},
|
||||
} as ServiceFactory<TService>);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user