Skip to content

Commit 4251f43

Browse files
committed
Add types for monaco
1 parent 4a2ff2a commit 4251f43

3 files changed

Lines changed: 47 additions & 5 deletions

File tree

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "app",
3-
"version": "1.1.1",
3+
"version": "1.1.2",
44
"scripts": {
55
"ng": "ng",
66
"dev": "ng serve --host 127.0.0.1",

src/app/modules/worker/pages/worker-edit/editor.libs.ts

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,9 +87,43 @@ declare var env: Environment;`;
8787

8888
export const scheduledLib = `
8989
interface ScheduledEvent {
90-
waitUntil: (handler: Promise<any>) => void;
9190
scheduledTime: number;
91+
waitUntil(promise: Promise<any>): void;
9292
}
9393
9494
declare function addEventListener(type: 'scheduled', listener: (event: ScheduledEvent) => void);
9595
`;
96+
97+
export const executionContextLib = `
98+
interface ExecutionContext {
99+
waitUntil(promise: Promise<any>): void;
100+
passThroughOnException(): void;
101+
}
102+
`;
103+
104+
export function createEnvType(values: EnvValue[]) {
105+
if (!values.length) {
106+
return `type Env = {};`;
107+
}
108+
109+
const members = values.map((v) => {
110+
switch (v.type) {
111+
case 'assets':
112+
return `readonly ${v.key}: BindingAssets`;
113+
case 'storage':
114+
return `readonly ${v.key}: BindingStorage`;
115+
case 'kv':
116+
return `readonly ${v.key}: BindingKV`;
117+
case 'database':
118+
return `readonly ${v.key}: BindingDatabase`;
119+
default:
120+
return `readonly ${v.key}: string`;
121+
}
122+
});
123+
124+
// Note: Binding interfaces are defined in createEnvironmentLib which is added first
125+
return `
126+
type Env = {
127+
${members.join(';\n ')}
128+
};`;
129+
}

src/app/modules/worker/pages/worker-edit/worker-edit.page.ts

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ const log = logger.getLogger('WorkerEditPage');
1717
// Monaco
1818
import { MonacoEditorConstructionOptions } from '@materia-ui/ngx-monaco-editor';
1919
import { FormControl } from '@angular/forms';
20-
import { createEnvironmentLib, scheduledLib } from './editor.libs';
20+
import { createEnvironmentLib, createEnvType, scheduledLib, executionContextLib } from './editor.libs';
2121
import { CommonModule } from '@angular/common';
2222
import { SharedModule } from '~/app/shared/shared.module';
2323
import { IframeComponent } from './components/iframe/iframe.component';
@@ -172,6 +172,10 @@ export default class WorkerEditPage implements OnInit, OnDestroy {
172172
monaco.languages.typescript.typescriptDefaults.addExtraLib(scheduledLib);
173173
monaco.languages.typescript.javascriptDefaults.addExtraLib(scheduledLib);
174174

175+
// ExecutionContext lib (for ES Modules ctx parameter)
176+
monaco.languages.typescript.typescriptDefaults.addExtraLib(executionContextLib);
177+
monaco.languages.typescript.javascriptDefaults.addExtraLib(executionContextLib);
178+
175179
// On save (CTRL + S)
176180
editor.addCommand(monaco.KeyMod.CtrlCmd | monaco.KeyCode.KeyS, () => this.updateWorker());
177181

@@ -211,11 +215,15 @@ export default class WorkerEditPage implements OnInit, OnDestroy {
211215
private environmentLibs: monaco.IDisposable[] = [];
212216
private setEnvironmentLib(values: readonly IEnvironmentValue[]) {
213217
log.debug('set env', values);
214-
const lib = createEnvironmentLib(values.map((v) => ({ key: v.key, type: v.type })));
218+
const envValues = values.map((v) => ({ key: v.key, type: v.type }));
219+
const lib = createEnvironmentLib(envValues);
220+
const envType = createEnvType(envValues);
215221
this.environmentLibs.map((lib) => lib.dispose());
216222
this.environmentLibs = [
217223
monaco.languages.typescript.typescriptDefaults.addExtraLib(lib),
218-
monaco.languages.typescript.javascriptDefaults.addExtraLib(lib)
224+
monaco.languages.typescript.javascriptDefaults.addExtraLib(lib),
225+
monaco.languages.typescript.typescriptDefaults.addExtraLib(envType),
226+
monaco.languages.typescript.javascriptDefaults.addExtraLib(envType)
219227
];
220228
}
221229

0 commit comments

Comments
 (0)