Following a discord discussion, eliminate EventHub.mediateDependentC… (#6184)

This commit is contained in:
Ofek
2024-02-24 20:17:27 +02:00
committed by GitHub
parent 5cabe8af72
commit 3a109622db
3 changed files with 53 additions and 56 deletions

View File

@@ -89,26 +89,4 @@ export class EventHub {
}
this.subscriptions = [];
}
public mediateDependentCalls<T1 extends unknown[], T2 extends unknown[] = T1>(
dependent: EventHubCallback<any>,
dependency: EventHubCallback<any>,
): DependencyProxies<T1, T2> {
let hasDependencyExecuted = false;
let lastDependentArguments: unknown[] | null = null;
const dependencyProxy = function (this: unknown, ...args: unknown[]) {
dependency.apply(this, args);
hasDependencyExecuted = true;
if (lastDependentArguments) {
dependent.apply(this, lastDependentArguments);
}
};
const dependentProxy = function (this: unknown, ...args: unknown[]) {
if (hasDependencyExecuted) {
dependent.apply(this, args);
}
lastDependentArguments = args;
};
return {dependencyProxy, dependentProxy};
}
}

View File

@@ -56,9 +56,11 @@ type AstCodeEntry = {
};
export class Ast extends MonacoPane<monaco.editor.IStandaloneCodeEditor, AstState> {
// TODO: eliminate deprecated deltaDecorations monaco API
decorations: DecorationEntry = {linkedCode: []};
prevDecorations: any[] = [];
colours: string[] = [];
colourScheme?: string = undefined;
srcColours?: Record<number, number> = undefined;
astCode: AstCodeEntry[] = [];
linkedFadeTimeoutId?: NodeJS.Timeout = undefined;
@@ -91,11 +93,9 @@ export class Ast extends MonacoPane<monaco.editor.IStandaloneCodeEditor, AstStat
this.container.on('destroy', this.close, this);
const onColoursOnCompile = this.eventHub.mediateDependentCalls(this.onColours, this.onCompileResult);
this.eventHub.on('compileResult', onColoursOnCompile.dependencyProxy, this);
this.eventHub.on('compileResult', this.onCompileResult, this);
this.eventHub.on('compiler', this.onCompiler, this);
this.eventHub.on('colours', onColoursOnCompile.dependentProxy, this);
this.eventHub.on('colours', this.onColours, this);
this.eventHub.on('panesLinkLine', this.onPanesLinkLine, this);
this.eventHub.on('compilerClose', this.onCompilerClose, this);
this.eventHub.on('settingsChange', this.onSettingsChange, this);
@@ -183,6 +183,7 @@ export class Ast extends MonacoPane<monaco.editor.IStandaloneCodeEditor, AstStat
if (result.hasAstOutput) {
this.showAstResults(result.astOutput);
this.tryApplyAstColours();
} else if (compiler.supportsAstView) {
this.showAstResults([{text: '<No output>'}]);
}
@@ -230,27 +231,35 @@ export class Ast extends MonacoPane<monaco.editor.IStandaloneCodeEditor, AstStat
}
}
onColours(id: number, colours: Record<number, number>, scheme: string) {
if (id === this.compilerInfo.compilerId) {
const astColours = {};
for (const [index, code] of this.astCode.entries()) {
if (
code.source &&
code.source.from?.line &&
code.source.to?.line &&
code.source.from.line <= code.source.to.line &&
code.source.to.line < code.source.from.line + 100
) {
for (let i = code.source.from.line; i <= code.source.to.line; ++i) {
if (i - 1 in colours) {
astColours[index] = colours[i - 1];
break;
}
tryApplyAstColours(): void {
if (!this.srcColours || !this.colourScheme || this.astCode.length === 0) return;
const astColours = {};
for (const [index, code] of this.astCode.entries()) {
if (
code.source &&
code.source.from?.line &&
code.source.to?.line &&
code.source.from.line <= code.source.to.line &&
code.source.to.line < code.source.from.line + 100
) {
for (let i = code.source.from.line; i <= code.source.to.line; ++i) {
if (i - 1 in this.srcColours) {
astColours[index] = this.srcColours[i - 1];
break;
}
}
}
colour.applyColours(astColours, scheme, this.editorDecorations);
}
colour.applyColours(astColours, this.colourScheme, this.editorDecorations);
}
onColours(id: number, srcColours: Record<number, number>, colourScheme: string): void {
if (id !== this.compilerInfo.compilerId) return;
this.srcColours = srcColours;
this.colourScheme = colourScheme;
this.tryApplyAstColours();
}
updateDecorations() {

View File

@@ -48,9 +48,13 @@ import {CompilerInfo} from '../compiler.interfaces.js';
export class Ir extends MonacoPane<monaco.editor.IStandaloneCodeEditor, IrState> {
linkedFadeTimeoutId: NodeJS.Timeout | null = null;
irCode: any[] = [];
colours: any[] = [];
srcColours?: Record<number, number | undefined> = undefined;
colourScheme?: string = undefined;
// TODO: eliminate deprecated deltaDecorations monaco API
decorations: any = {};
previousDecorations: string[] = [];
options: Toggles;
filters: Toggles;
lastOptions: LLVMIrBackendOptions = {
@@ -168,15 +172,11 @@ export class Ir extends MonacoPane<monaco.editor.IStandaloneCodeEditor, IrState>
override registerCallbacks(): void {
const onMouseMove = _.throttle(this.onMouseMove.bind(this), 50);
const onDidChangeCursorSelection = _.throttle(this.onDidChangeCursorSelection.bind(this), 500);
const onColoursOnCompile = this.eventHub.mediateDependentCalls(
this.onColours.bind(this),
this.onCompileResult.bind(this),
);
this.paneRenaming.on('renamePane', this.updateState.bind(this));
this.eventHub.on('compileResult', onColoursOnCompile.dependencyProxy, this);
this.eventHub.on('colours', onColoursOnCompile.dependentProxy, this);
this.eventHub.on('compileResult', this.onCompileResult.bind(this));
this.eventHub.on('colours', this.onColours.bind(this));
this.eventHub.on('panesLinkLine', this.onPanesLinkLine.bind(this));
this.editor.onMouseMove(event => onMouseMove(event));
@@ -213,6 +213,7 @@ export class Ir extends MonacoPane<monaco.editor.IStandaloneCodeEditor, IrState>
if (this.compilerInfo.compilerId !== compilerId) return;
if (result.hasIrOutput) {
this.showIrResults(unwrap(result.irOutput).asm);
this.tryApplyIrColours();
} else if (compiler.supportsIrView) {
this.showIrResults([{text: '<No output>'}]);
}
@@ -248,20 +249,29 @@ export class Ir extends MonacoPane<monaco.editor.IStandaloneCodeEditor, IrState>
}
}
onColours(compilerId: number, colours: any, scheme: any): void {
if (compilerId !== this.compilerInfo.compilerId) return;
tryApplyIrColours(): void {
if (!this.srcColours || !this.colourScheme || this.irCode.length === 0) return;
const irColours: Record<number, number> = {};
for (const [index, code] of this.irCode.entries()) {
if (
code.source &&
code.source.file === null &&
code.source.line > 0 &&
colours[code.source.line - 1] !== undefined
this.srcColours[code.source.line - 1] !== undefined
) {
irColours[index] = colours[code.source.line - 1];
irColours[index] = this.srcColours[code.source.line - 1]!;
}
}
applyColours(irColours, scheme, this.editorDecorations);
applyColours(irColours, this.colourScheme, this.editorDecorations);
}
onColours(compilerId: number, srcColours: Record<number, number>, scheme: string): void {
if (compilerId !== this.compilerInfo.compilerId) return;
this.colourScheme = scheme;
this.srcColours = srcColours;
this.tryApplyIrColours();
}
onMouseMove(e: monaco.editor.IEditorMouseEvent): void {