Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve Types for $delegate and $resolve function #1567

Open
wants to merge 6 commits into
base: v4/latest
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions framework/src/BaseDelegateComponent.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import {UnknownObject} from '@jovotech/common';
import {BaseComponent, ComponentData} from './index';

export type ExtractDelegatedEventData<
T extends BaseDelegateComponent<Record<string, unknown>>,
KEY extends keyof T['__resolve'],
> = T extends BaseDelegateComponent<infer RESOLVE> ? RESOLVE[KEY] : never;

type IsTuple<T> = T extends [any, ...any] ? true : false

export abstract class BaseDelegateComponent<
RESOLVE extends Record<string, any>,
DATA extends ComponentData = ComponentData,
CONFIG extends UnknownObject = UnknownObject,
> extends BaseComponent<DATA, CONFIG> {
// used to permit the ExtractDelegatedEventData work.
// If you find a better way that not require "declare", be my guest! :D
declare __resolve: RESOLVE;

override async $resolve<ARGS extends RESOLVE[KEY], KEY extends keyof RESOLVE = keyof RESOLVE>(
eventName: Extract<KEY, string>,
...eventArgs: ARGS extends Array<unknown> ? (IsTuple<ARGS> extends true ? ARGS : [ARGS]) : [ARGS]
): Promise<void> {
// because of the JovoProxy class, this implementation of the $resolve will not be called.
// But it's ok, we need only types work.
return super.$resolve(eventName as string, ...eventArgs);
}
}
11 changes: 9 additions & 2 deletions framework/src/Jovo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import util from 'util';
import { App, AppConfig } from './App';
import { HandleRequest } from './HandleRequest';
import {
BaseComponent,
BaseComponent, BaseDelegateComponent,
BaseOutput,
ComponentConfig,
ComponentConstructor,
Expand Down Expand Up @@ -354,6 +354,12 @@ export abstract class Jovo<
});
}

async $delegate<COMPONENT extends BaseDelegateComponent<any>>(
component: ComponentConstructor<COMPONENT> | string,
options: DelegateOptions<ComponentConfig<COMPONENT>, COMPONENT extends BaseDelegateComponent<infer RESOLVE, any, any>
? keyof RESOLVE
: never>,
): Promise<void>
async $delegate<COMPONENT extends BaseComponent>(
constructor: ComponentConstructor<COMPONENT>,
options: DelegateOptions<ComponentConfig<COMPONENT>>,
Expand Down Expand Up @@ -431,7 +437,8 @@ export abstract class Jovo<
}

// TODO determine whether an error should be thrown if $resolve is called from a context outside a delegation
async $resolve<ARGS extends unknown[]>(eventName: string, ...eventArgs: ARGS): Promise<void> {
// TODO Move the implementation to the BaseDelegatedComponent. So also the previously TODO can be removed.
async $resolve<ARGS extends [any]>(eventName: string, ...eventArgs: ARGS): Promise<void> {
if (!this.$state) {
return;
}
Expand Down
1 change: 1 addition & 0 deletions framework/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ export {
export * from './App';
export * from './AsyncJovo';
export * from './BaseComponent';
export * from './BaseDelegateComponent';
export * from './BaseOutput';
export * from './ComponentPlugin';
export * from './ComponentTree';
Expand Down