Skip to content

Commit

Permalink
Strongly type events
Browse files Browse the repository at this point in the history
  • Loading branch information
Gerrit0 committed Sep 4, 2023
1 parent c82821c commit caec4e1
Show file tree
Hide file tree
Showing 8 changed files with 111 additions and 29 deletions.
10 changes: 4 additions & 6 deletions src/lib/application.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,17 +87,18 @@ export interface ApplicationEvents {
@Component({ name: "application", internal: true })
export class Application extends ChildableComponent<
Application,
AbstractComponent<Application>
AbstractComponent<Application>,
ApplicationEvents
> {
/**
* The converter used to create the declaration reflections.
*/
converter: Converter;
converter = new Converter(this);

/**
* The renderer used to generate the documentation output.
*/
renderer: Renderer;
renderer = new Renderer(this);

/**
* The serializer used to generate JSON output.
Expand Down Expand Up @@ -161,9 +162,6 @@ export class Application extends ChildableComponent<
);
}
super(null!); // We own ourselves

this.converter = this.addComponent<Converter>("converter", Converter);
this.renderer = this.addComponent<Renderer>("renderer", Renderer);
}

/**
Expand Down
4 changes: 2 additions & 2 deletions src/lib/converter/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -250,8 +250,8 @@ export class Context {
* @param reflection The triggering reflection.
* @param node The triggering TypeScript node if available.
*/
trigger(name: string, reflection: Reflection, node?: ts.Node) {
this.converter.trigger(name, this, reflection, node);
trigger(name: "createDeclaration", reflection: DeclarationReflection) {
this.converter.trigger(name, this, reflection);
}

/** @internal */
Expand Down
36 changes: 31 additions & 5 deletions src/lib/converter/converter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,15 @@ import type { Application } from "../application";
import {
Comment,
CommentDisplayPart,
DeclarationReflection,
ParameterReflection,
ProjectReflection,
Reflection,
ReflectionKind,
ReflectionSymbolId,
SignatureReflection,
SomeType,
TypeParameterReflection,
} from "../models/index";
import { Context } from "./context";
import { ConverterComponent } from "./components";
Expand Down Expand Up @@ -36,6 +40,31 @@ import {
} from "./comments/linkResolver";
import type { DeclarationReference } from "./comments/declarationReference";

interface ConverterEvents {
begin: [Context];
end: [Context];
createDeclaration: [Context, DeclarationReflection];
createSignature: [
Context,
SignatureReflection,
(
| ts.SignatureDeclaration
| ts.IndexSignatureDeclaration
| ts.JSDocSignature
)?,
ts.Signature?
];
createParameter: [Context, ParameterReflection];
createTypeParameter: [
Context,
TypeParameterReflection,
ts.TypeParameterDeclaration?
];
resolveBegin: [Context];
resolveReflection: [Context, Reflection];
resolveEnd: [Context];
}

/**
* Compiles source files using TypeScript and converts compiler symbols to reflections.
*/
Expand All @@ -46,7 +75,8 @@ import type { DeclarationReference } from "./comments/declarationReference";
})
export class Converter extends ChildableComponent<
Application,
ConverterComponent
ConverterComponent,
ConverterEvents
> {
/** @internal */
@Option("externalPattern")
Expand Down Expand Up @@ -386,10 +416,6 @@ export class Converter extends ChildableComponent<
context.project.comment = symbol
? context.getComment(symbol, context.project.kind)
: context.getFileComment(node);
context.trigger(
Converter.EVENT_CREATE_DECLARATION,
context.project
);
moduleContext = context;
} else {
const reflection = context.createDeclarationReflection(
Expand Down
3 changes: 2 additions & 1 deletion src/lib/converter/factories/index-signature.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,9 @@ export function convertIndexSignature(context: Context, symbol: ts.Symbol) {
context.registerReflection(index, indexSymbol);
context.scope.indexSignature = index;

context.trigger(
context.converter.trigger(
ConverterEvents.CREATE_SIGNATURE,
context,
index,
indexDeclaration,
);
Expand Down
25 changes: 21 additions & 4 deletions src/lib/converter/factories/signature.ts
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,11 @@ function convertParameters(
paramRefl.comment ||= context.getComment(param, paramRefl.kind);

context.registerReflection(paramRefl, param);
context.trigger(ConverterEvents.CREATE_PARAMETER, paramRefl);
context.converter.trigger(
ConverterEvents.CREATE_PARAMETER,
context,
paramRefl
);

let type: ts.Type | ts.TypeNode | undefined;
if (declaration) {
Expand Down Expand Up @@ -228,7 +232,11 @@ export function convertParameterNodes(
paramRefl,
context.getSymbolAtLocation(param)
);
context.trigger(ConverterEvents.CREATE_PARAMETER, paramRefl);
context.converter.trigger(
ConverterEvents.CREATE_PARAMETER,
context,
paramRefl
);

paramRefl.type = context.converter.convertType(
context.withScope(paramRefl),
Expand Down Expand Up @@ -295,7 +303,11 @@ function convertTypeParameters(
}

context.registerReflection(paramRefl, param.getSymbol());
context.trigger(ConverterEvents.CREATE_TYPE_PARAMETER, paramRefl);
context.converter.trigger(
ConverterEvents.CREATE_TYPE_PARAMETER,
context,
paramRefl
);

return paramRefl;
});
Expand Down Expand Up @@ -336,7 +348,12 @@ export function createTypeParamReflection(
paramRefl.comment = context.getJsDocComment(param.parent);
}

context.trigger(ConverterEvents.CREATE_TYPE_PARAMETER, paramRefl, param);
context.converter.trigger(
ConverterEvents.CREATE_TYPE_PARAMETER,
context,
paramRefl,
param
);
return paramRefl;
}

Expand Down
2 changes: 1 addition & 1 deletion src/lib/converter/plugins/TypePlugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ export class TypePlugin extends ConverterComponent {
this.reflections.clear();
}

private onResolve(context: Context, reflection: DeclarationReflection) {
private onResolve(context: Context, reflection: Reflection) {
this.resolve(context.project, reflection);
}

Expand Down
32 changes: 30 additions & 2 deletions src/lib/output/renderer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import * as path from "path";

import type { Application } from "../application";
import type { Theme } from "./theme";
import { RendererEvent, PageEvent, IndexEvent } from "./events";
import { RendererEvent, PageEvent, IndexEvent, MarkdownEvent } from "./events";
import type { ProjectReflection } from "../models/reflections/project";
import type { RenderTemplate } from "./models/UrlMapping";
import { writeFileSync } from "../utils/fs";
Expand Down Expand Up @@ -83,6 +83,16 @@ export interface RendererHooks {
"pageSidebar.end": [DefaultThemeRenderContext];
}

interface RendererEvents {
parseMarkdown: [MarkdownEvent];
includeMarkdown: [MarkdownEvent];
beginPage: [PageEvent<Reflection>];
endPage: [PageEvent<Reflection>];
beginRender: [RendererEvent];
endRender: [RendererEvent];
prepareIndex: [IndexEvent];
}

/**
* The renderer processes a {@link ProjectReflection} using a {@link Theme} instance and writes
* the emitted html documents to a output directory. You can specify which theme should be used
Expand Down Expand Up @@ -118,7 +128,8 @@ export interface RendererHooks {
@Component({ name: "renderer", internal: true, childClass: RendererComponent })
export class Renderer extends ChildableComponent<
Application,
RendererComponent
RendererComponent,
RendererEvents
> {
private themes = new Map<string, new (renderer: Renderer) => Theme>([
["default", DefaultTheme],
Expand Down Expand Up @@ -173,6 +184,8 @@ export class Renderer extends ChildableComponent<
*/
hooks = new EventHooks<RendererHooks, JsxElement>();

markedPlugin: MarkedPlugin;

/** @internal */
@Option("theme")
accessor themeName!: string;
Expand Down Expand Up @@ -207,6 +220,15 @@ export class Renderer extends ChildableComponent<

renderStartTime = -1;

constructor(app: Application) {
super(app);

this.markedPlugin = new MarkedPlugin(this);
new AssetsPlugin(this);
new JavascriptIndexPlugin(this);
new NavigationPlugin(this);
}

/**
* Define a new theme that can be used to render output.
* This API will likely be changing at some point, to allow more easily overriding parts of the theme without
Expand Down Expand Up @@ -397,3 +419,9 @@ export class Renderer extends ChildableComponent<
// HACK: THIS HAS TO STAY DOWN HERE
// if you try to move it up to the top of the file, then you'll run into stuff being used before it has been defined.
import "./plugins";
import {
AssetsPlugin,
JavascriptIndexPlugin,
MarkedPlugin,
NavigationPlugin,
} from "./plugins";
28 changes: 20 additions & 8 deletions src/lib/utils/component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,9 +87,9 @@ export class ComponentEvent {

component: AbstractComponent<ComponentHost>;

static ADDED = "componentAdded";
static readonly ADDED = "componentAdded";

static REMOVED = "componentRemoved";
static readonly REMOVED = "componentRemoved";

constructor(
owner: ComponentHost,
Expand All @@ -106,8 +106,11 @@ export class ComponentEvent {
*
* @template O type of component's owner.
*/
export abstract class AbstractComponent<O extends ComponentHost>
extends EventDispatcher<any>
export abstract class AbstractComponent<
O extends ComponentHost,
E extends Record<keyof E, unknown[]> = {}
>
extends EventDispatcher<E>
implements ComponentHost
{
/**
Expand Down Expand Up @@ -156,6 +159,11 @@ export abstract class AbstractComponent<O extends ComponentHost>
}
}

interface ChildableComponentEvents {
componentAdded: [ComponentEvent];
componentRemoved: [ComponentEvent];
}

/**
* Component that can have child components.
*
Expand All @@ -164,8 +172,9 @@ export abstract class AbstractComponent<O extends ComponentHost>
*/
export abstract class ChildableComponent<
O extends ComponentHost,
C extends Component
> extends AbstractComponent<O> {
C extends Component,
E extends Record<keyof E, unknown[]> = {}
> extends AbstractComponent<O, E & ChildableComponentEvents> {
/**
*
*/
Expand Down Expand Up @@ -223,7 +232,10 @@ export abstract class ChildableComponent<
: componentClass;
const event = new ComponentEvent(this, component);

this.trigger(ComponentEvent.ADDED, event);
(this as ChildableComponent<O, C, {}>).trigger(
ComponentEvent.ADDED,
event
);
this._componentChildren[name] = component;

return component;
Expand All @@ -234,7 +246,7 @@ export abstract class ChildableComponent<
const component = (this._componentChildren || {})[name];
if (component) {
delete this._componentChildren![name];
this.trigger(
(this as ChildableComponent<O, C, {}>).trigger(
ComponentEvent.REMOVED,
new ComponentEvent(this, component)
);
Expand Down

0 comments on commit caec4e1

Please sign in to comment.