Skip to content

Commit

Permalink
Use Any for Application, ApplicationV2, and FormApplication
Browse files Browse the repository at this point in the history
  • Loading branch information
LukeAbby committed Jan 2, 2025
1 parent a703503 commit eaf564f
Show file tree
Hide file tree
Showing 27 changed files with 162 additions and 153 deletions.
2 changes: 1 addition & 1 deletion src/foundry/client-esm/applications/_module.d.mts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ export * as ui from "./ui/_module.mjs";
/**
* A registry of currently rendered ApplicationV2 instances.
*/
export const instances: Map<string, foundry.applications.api.ApplicationV2>;
export const instances: Map<string, foundry.applications.api.ApplicationV2.Any>;

/**
* Parse an HTML string, returning a processed HTMLElement or HTMLCollection.
Expand Down
2 changes: 0 additions & 2 deletions src/foundry/client-esm/applications/_types.d.mts
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
import type ApplicationV2 from "./api/application.d.mts";

export {};

// After seeing that none of these types add anything or are even exported a
// very reasonable question may be: Why on earth does this file exist?
//
Expand Down
2 changes: 1 addition & 1 deletion src/foundry/client-esm/applications/api/application.d.mts
Original file line number Diff line number Diff line change
Expand Up @@ -438,7 +438,7 @@ declare class ApplicationV2<
* Iterate over the inheritance chain of this Application.
* The chain includes this Application itself and all parents until the base application is encountered.
*/
static inheritanceChain(): Generator<typeof ApplicationV2>;
static inheritanceChain(): Generator<ApplicationV2.AnyConstructor>;

/**
* Initialize configuration options for the Application instance.
Expand Down
251 changes: 130 additions & 121 deletions src/foundry/client-esm/applications/api/handlebars-application.d.mts
Original file line number Diff line number Diff line change
@@ -1,132 +1,12 @@
import type { DeepPartial, Mixin } from "../../../../utils/index.d.mts";
import type ApplicationV2 from "./application.d.mts";

declare namespace HandlebarsApplication {
// Note(LukeAbby): `unknown` is returned in the false case instead of `never` because otherwise errors will crop up at usage sites like "any cannot be assigned to `never`".
type ConfigurationFor<Instance extends HandlebarsApplication> =
Instance extends ApplicationV2.Internal.Instance<infer Configuration, infer _1, infer _2> ? Configuration : unknown;

type RenderOptionsFor<Instance extends HandlebarsApplication> =
Instance extends ApplicationV2.Internal.Instance<infer _1, infer RenderOptions, infer _2> ? RenderOptions : object;

type RenderContextFor<Instance extends HandlebarsApplication> =
Instance extends ApplicationV2.Internal.Instance<infer _1, infer _2, infer RenderContext> ? RenderContext : object;
}

/**
* The mixed application class augmented with [Handlebars](https://handlebarsjs.com) template rendering behavior.
*/
declare class HandlebarsApplication {
/** @privateRemarks All mixin classses should accept anything for its constructor. */
constructor(...args: any[]);

/**
* Configure a registry of template parts which are supported for this application for partial rendering.
* @defaultValue `{}`
*/
static PARTS: Record<string, HandlebarsApplicationMixin.HandlebarsTemplatePart>;

/**
* A record of all rendered template parts.
* @defaultValue `{}`
*/
get parts(): Record<string, HTMLElement>;

protected _configureRenderOptions(options: DeepPartial<HandlebarsApplicationMixin.HandlebarsRenderOptions>): void;

protected _preFirstRender(
context: DeepPartial<HandlebarsApplication.RenderContextFor<this>>,
options: DeepPartial<HandlebarsApplication.RenderOptionsFor<this>>,
): Promise<void>;

/**
* Render each configured application part using Handlebars templates.
* @param context - Context data for the render operation
* @param options - Options which configure application rendering behavior
* @returns A single rendered HTMLElement for each requested part
*/
protected _renderHTML(
context: HandlebarsApplication.RenderContextFor<this>,
options: DeepPartial<HandlebarsApplication.RenderOptionsFor<this>>,
): Promise<Record<string, HTMLElement>>;

/**
* Prepare context that is specific to only a single rendered part.
*
* It is recommended to augment or mutate the shared context so that downstream methods like _onRender have
* visibility into the data that was used for rendering. It is acceptable to return a different context object
* rather than mutating the shared context at the expense of this transparency.
*
* @param partId - The part being rendered
* @param context - Shared context provided by _prepareContext
* @param options - Options which configure application rendering behavior
* @returns Context data for a specific part
*/
protected _preparePartContext(
partId: string,
context: HandlebarsApplication.RenderContextFor<this>,
options: DeepPartial<HandlebarsApplicationMixin.HandlebarsRenderOptions>,
): Promise<HandlebarsApplication.RenderContextFor<this>>;

/**
* Replace the HTML of the application with the result provided by Handlebars rendering.
* @param result - The result from Handlebars template rendering
* @param content - The content element into which the rendered result must be inserted
* @param options - Options which configure application rendering behavior
*/
protected _replaceHTML(
result: Record<string, HTMLElement>,
content: HTMLElement,
options: DeepPartial<HandlebarsApplication.RenderOptionsFor<this>>,
): void;

/**
* Prepare data used to synchronize the state of a template part.
* @param partId - The id of the part being rendered
* @param newElement - The new rendered HTML element for the part
* @param priorElement - The prior rendered HTML element for the part
* @param state - A state object which is used to synchronize after replacement
*/
protected _preSyncPartState(
partId: string,
newElement: HTMLElement,
priorElement: HTMLElement,
state: HandlebarsApplicationMixin.PartState,
): void;

/**
* Synchronize the state of a template part after it has been rendered and replaced in the DOM.
* @param partId - The id of the part being rendered
* @param newElement - The new rendered HTML element for the part
* @param priorElement - The prior rendered HTML element for the part
* @param state - A state object which is used to synchronize after replacement
*/
protected _syncPartState(
partId: string,
newElement: HTMLElement,
priorElement: HTMLElement,
state: HandlebarsApplicationMixin.PartState,
): void;

/**
* Attach event listeners to rendered template parts.
* @param partId - The id of the part being rendered
* @param htmlElement - The rendered HTML element for the part
* @param options - Rendering options passed to the render method
*/
protected _attachPartListeners(
partId: string,
htmlElement: HTMLElement,
options: DeepPartial<HandlebarsApplicationMixin.HandlebarsRenderOptions>,
): void;
}

/**
* Augment an Application class with [Handlebars](https://handlebarsjs.com) template rendering behavior.
*/
declare function HandlebarsApplicationMixin<BaseClass extends ApplicationV2.Internal.Constructor>(
BaseApplication: BaseClass,
): Mixin<typeof HandlebarsApplication, BaseClass>;
): Mixin<typeof HandlebarsApplicationMixin.HandlebarsApplication, BaseClass>;

declare namespace HandlebarsApplicationMixin {
interface PartState {
Expand Down Expand Up @@ -175,6 +55,135 @@ declare namespace HandlebarsApplicationMixin {
*/
forms?: Record<string, ApplicationV2.FormConfiguration>;
}

namespace HandlebarsApplication {
// Note(LukeAbby): `unknown` is returned in the false case instead of `never` because otherwise errors will crop up at usage sites like "any cannot be assigned to `never`".
type ConfigurationFor<Instance extends HandlebarsApplication> =
Instance extends ApplicationV2.Internal.Instance<infer Configuration, infer _1, infer _2>
? Configuration
: unknown;

type RenderOptionsFor<Instance extends HandlebarsApplication> =
Instance extends ApplicationV2.Internal.Instance<infer _1, infer RenderOptions, infer _2>
? RenderOptions
: object;

type RenderContextFor<Instance extends HandlebarsApplication> =
Instance extends ApplicationV2.Internal.Instance<infer _1, infer _2, infer RenderContext>
? RenderContext
: object;
}

/**
* The mixed application class augmented with [Handlebars](https://handlebarsjs.com) template rendering behavior.
*
* @remarks This does NOT exist at runtime. This is only here to be used as a type when relevant as well as to avoid
* issues with anonymous mixin classes.
*/
class HandlebarsApplication {
/** @privateRemarks All mixin classses should accept anything for its constructor. */
constructor(...args: any[]);

/**
* Configure a registry of template parts which are supported for this application for partial rendering.
* @defaultValue `{}`
*/
static PARTS: Record<string, HandlebarsApplicationMixin.HandlebarsTemplatePart>;

/**
* A record of all rendered template parts.
* @defaultValue `{}`
*/
get parts(): Record<string, HTMLElement>;

protected _configureRenderOptions(options: DeepPartial<HandlebarsApplicationMixin.HandlebarsRenderOptions>): void;

protected _preFirstRender(
context: DeepPartial<HandlebarsApplication.RenderContextFor<this>>,
options: DeepPartial<HandlebarsApplication.RenderOptionsFor<this>>,
): Promise<void>;

/**
* Render each configured application part using Handlebars templates.
* @param context - Context data for the render operation
* @param options - Options which configure application rendering behavior
* @returns A single rendered HTMLElement for each requested part
*/
protected _renderHTML(
context: HandlebarsApplication.RenderContextFor<this>,
options: DeepPartial<HandlebarsApplication.RenderOptionsFor<this>>,
): Promise<Record<string, HTMLElement>>;

/**
* Prepare context that is specific to only a single rendered part.
*
* It is recommended to augment or mutate the shared context so that downstream methods like _onRender have
* visibility into the data that was used for rendering. It is acceptable to return a different context object
* rather than mutating the shared context at the expense of this transparency.
*
* @param partId - The part being rendered
* @param context - Shared context provided by _prepareContext
* @param options - Options which configure application rendering behavior
* @returns Context data for a specific part
*/
protected _preparePartContext(
partId: string,
context: HandlebarsApplication.RenderContextFor<this>,
options: DeepPartial<HandlebarsApplicationMixin.HandlebarsRenderOptions>,
): Promise<HandlebarsApplication.RenderContextFor<this>>;

/**
* Replace the HTML of the application with the result provided by Handlebars rendering.
* @param result - The result from Handlebars template rendering
* @param content - The content element into which the rendered result must be inserted
* @param options - Options which configure application rendering behavior
*/
protected _replaceHTML(
result: Record<string, HTMLElement>,
content: HTMLElement,
options: DeepPartial<HandlebarsApplication.RenderOptionsFor<this>>,
): void;

/**
* Prepare data used to synchronize the state of a template part.
* @param partId - The id of the part being rendered
* @param newElement - The new rendered HTML element for the part
* @param priorElement - The prior rendered HTML element for the part
* @param state - A state object which is used to synchronize after replacement
*/
protected _preSyncPartState(
partId: string,
newElement: HTMLElement,
priorElement: HTMLElement,
state: HandlebarsApplicationMixin.PartState,
): void;

/**
* Synchronize the state of a template part after it has been rendered and replaced in the DOM.
* @param partId - The id of the part being rendered
* @param newElement - The new rendered HTML element for the part
* @param priorElement - The prior rendered HTML element for the part
* @param state - A state object which is used to synchronize after replacement
*/
protected _syncPartState(
partId: string,
newElement: HTMLElement,
priorElement: HTMLElement,
state: HandlebarsApplicationMixin.PartState,
): void;

/**
* Attach event listeners to rendered template parts.
* @param partId - The id of the part being rendered
* @param htmlElement - The rendered HTML element for the part
* @param options - Rendering options passed to the render method
*/
protected _attachPartListeners(
partId: string,
htmlElement: HTMLElement,
options: DeepPartial<HandlebarsApplicationMixin.HandlebarsRenderOptions>,
): void;
}
}

export default HandlebarsApplicationMixin;
4 changes: 2 additions & 2 deletions src/foundry/client/apps/app.d.mts
Original file line number Diff line number Diff line change
Expand Up @@ -310,7 +310,7 @@ declare global {
* Return the inheritance chain for this Application class up to (and including) it's base Application class.
* @internal
*/
protected static _getInheritanceChain(): (typeof Application)[];
protected static _getInheritanceChain(): Application.AnyConstructor[];

/**
* Call all hooks for all applications in the inheritance chain.
Expand Down Expand Up @@ -593,7 +593,7 @@ declare global {
/**
* @see {@link Application.RENDER_STATES}
*/
type RenderState = ValueOf<(typeof Application)["RENDER_STATES"]>;
type RenderState = ValueOf<typeof Application.RENDER_STATES>;
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/foundry/client/apps/forms/grid-config.d.mts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ declare global {
* })
* ```
*/
static override get defaultOptions(): (typeof FormApplication)["defaultOptions"];
static override get defaultOptions(): typeof FormApplication.defaultOptions;

protected override _render(force?: boolean, options?: Application.RenderOptions<Options>): Promise<void>;

Expand Down
2 changes: 1 addition & 1 deletion src/foundry/client/apps/hud/hotbar.d.mts
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ declare global {
* @param uuid - The Document UUID to display
* @remarks Returns a ui notification number if doc could not be found
*/
static toggleDocumentSheet(uuid: string): Promise<void> | Application | number;
static toggleDocumentSheet(uuid: string): Promise<void> | Application.Any | number;
}

namespace Hotbar {
Expand Down
2 changes: 1 addition & 1 deletion src/foundry/client/apps/sidebar/apps/client-settings.d.mts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ declare global {
* })
* ```
*/
static override get defaultOptions(): (typeof FormApplication)["defaultOptions"];
static override get defaultOptions(): typeof FormApplication.defaultOptions;

override _prepareCategoryData(): SettingsConfig.CategoryData;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ declare global {
* Handle button click to reset default settings
* @param event - The initial button click event
*/
protected _onResetDefaults(event: JQuery.ClickEvent): Promise<Application>;
protected _onResetDefaults(event: JQuery.ClickEvent): Promise<Application.Any>;

protected override _onSubmit(
event: Event,
Expand Down
2 changes: 1 addition & 1 deletion src/foundry/client/apps/sidebar/tabs/settings.d.mts
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ declare global {
* return options;
* ```
*/
static override get defaultOptions(): (typeof Application)["defaultOptions"];
static override get defaultOptions(): typeof Application.defaultOptions;

override getData(options?: Partial<Options>): Promise<{ src: string }>;

Expand Down
6 changes: 3 additions & 3 deletions src/foundry/client/data/abstract/client-document.d.mts
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,13 @@ declare class InternalClientDocument<BaseDocument extends Document.Internal.Inst
* @see {@link Document#render}
* @defaultValue `{}`
*/
readonly apps: Record<string, Application | foundry.applications.api.ApplicationV2>;
readonly apps: Record<string, Application.Any | foundry.applications.api.ApplicationV2.Any>;

/**
* A cached reference to the FormApplication instance used to configure this Document.
* @defaultValue `null`
*/
protected readonly _sheet: FormApplication | null; // TODO: Replace with InstanceType<ConfiguredSheetClass<T>> once the circular reference problem has been solved
protected readonly _sheet: FormApplication.Any | null; // TODO: Replace with InstanceType<ConfiguredSheetClass<T>> once the circular reference problem has been solved

static name: "ClientDocumentMixin";

Expand Down Expand Up @@ -78,7 +78,7 @@ declare class InternalClientDocument<BaseDocument extends Document.Internal.Inst
/**
* Lazily obtain a FormApplication instance used to configure this Document, or null if no sheet is available.
*/
get sheet(): FormApplication | foundry.applications.api.ApplicationV2 | null;
get sheet(): FormApplication.Any | foundry.applications.api.ApplicationV2.Any | null;

/**
* A boolean indicator for whether or not the current game User has at least limited visibility for this Document.
Expand Down
2 changes: 1 addition & 1 deletion src/foundry/client/data/abstract/document-collection.d.mts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ declare global {
* An Array of application references which will be automatically updated when the collection content changes
* @defaultValue `[]`
*/
apps: Application[];
apps: Application.Any[];

/**
* Initialize the DocumentCollection by constructing any initially provided Document instances
Expand Down
Loading

0 comments on commit eaf564f

Please sign in to comment.