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
Add Task args as argument to Task callbacks #4605
base: main
Are you sure you want to change the base?
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
'@lit/task': minor | ||
--- | ||
|
||
Add Task args to StatusRenderer callbacks |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -37,11 +37,11 @@ export const initialState = Symbol(); | |
|
||
export type TaskStatus = (typeof TaskStatus)[keyof typeof TaskStatus]; | ||
|
||
export type StatusRenderer<R> = { | ||
initial?: () => unknown; | ||
pending?: () => unknown; | ||
complete?: (value: R) => unknown; | ||
error?: (error: unknown) => unknown; | ||
export type StatusRenderer<R, D extends ReadonlyArray<unknown>> = { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We call the args type |
||
initial?: (args?: D) => unknown; | ||
pending?: (args?: D) => unknown; | ||
complete?: (value: R, args?: D) => unknown; | ||
error?: (error: unknown, args?: D) => unknown; | ||
}; | ||
|
||
export interface TaskConfig<T extends ReadonlyArray<unknown>, R> { | ||
|
@@ -423,18 +423,26 @@ export class Task< | |
return this._error; | ||
} | ||
|
||
render<T extends StatusRenderer<R>>(renderer: T) { | ||
render<S extends StatusRenderer<R, T>>(renderer: S) { | ||
switch (this.status) { | ||
case TaskStatus.INITIAL: | ||
return renderer.initial?.() as MaybeReturnType<T['initial']>; | ||
return renderer.initial?.(this._previousArgs) as MaybeReturnType< | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
S['initial'] | ||
>; | ||
case TaskStatus.PENDING: | ||
return renderer.pending?.() as MaybeReturnType<T['pending']>; | ||
case TaskStatus.COMPLETE: | ||
return renderer.complete?.(this.value!) as MaybeReturnType< | ||
T['complete'] | ||
return renderer.pending?.(this._previousArgs) as MaybeReturnType< | ||
S['pending'] | ||
>; | ||
case TaskStatus.COMPLETE: | ||
return renderer.complete?.( | ||
this.value!, | ||
this._previousArgs | ||
) as MaybeReturnType<S['complete']>; | ||
case TaskStatus.ERROR: | ||
return renderer.error?.(this.error) as MaybeReturnType<T['error']>; | ||
return renderer.error?.( | ||
this.error, | ||
this._previousArgs | ||
) as MaybeReturnType<S['error']>; | ||
default: | ||
throw new Error(`Unexpected status: ${this.status}`); | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For backwards compatibility: in case someone is using this type directly in an annotation, I think we need a default value for
D
so it can be omitted: