From 593e0babba48bfc40995c73f4a48f1e7fc8499a6 Mon Sep 17 00:00:00 2001 From: Joe Pea Date: Tue, 14 Apr 2020 02:42:46 -0700 Subject: [PATCH] WIP add TypeScript (no transpilation, only type checking and improved intellisense in VS Code or TypeScript-capable editors) --- .editorconfig | 12 ++++++++++++ tsconfig.json | 16 ++++++++++++++++ wasmaudioworklet/app.js | 4 ++-- wasmaudioworklet/audioworkletnode.js | 4 ++++ wasmaudioworklet/audioworkletpolyfill.js | 16 ++++++++++++++-- 5 files changed, 48 insertions(+), 4 deletions(-) create mode 100644 .editorconfig create mode 100644 tsconfig.json diff --git a/.editorconfig b/.editorconfig new file mode 100644 index 00000000..8290e619 --- /dev/null +++ b/.editorconfig @@ -0,0 +1,12 @@ +# EditorConfig is awesome: http://EditorConfig.org + +# top-most EditorConfig file +root = true + +[*] +charset = utf-8 +indent_style = space +indent_size = 4 +end_of_line = lf +insert_final_newline = true +trim_trailing_whitespace = true diff --git a/tsconfig.json b/tsconfig.json new file mode 100644 index 00000000..8e41a3bf --- /dev/null +++ b/tsconfig.json @@ -0,0 +1,16 @@ +{ + "compilerOptions": { + // Use TypeScript only for type checking. + "noEmit": true, + + // Run TypeScript on JS files. + "checkJs": true, + "allowJs": true, + + // Strict settings to enforce the most type-safe code. + "strict": true, + "noUnusedLocals": true, + "noUnusedParameters": true + }, + "include": ["./wasmaudioworklet/**/*"] +} diff --git a/wasmaudioworklet/app.js b/wasmaudioworklet/app.js index c386c969..073d419c 100644 --- a/wasmaudioworklet/app.js +++ b/wasmaudioworklet/app.js @@ -4,11 +4,11 @@ import { initVisualizer } from './visualizer/80sgrid.js'; import { initEditor } from './editorcontroller.js'; customElements.define('app-javascriptmusic', - class extends HTMLElement { + class JavaScriptMusic extends HTMLElement { constructor() { super(); - const shadowRoot = this.attachShadow({mode: 'open'}); + this.shadowRoot = this.attachShadow({mode: 'open'}); this.init(); } diff --git a/wasmaudioworklet/audioworkletnode.js b/wasmaudioworklet/audioworkletnode.js index 98a7aae6..a30e7f2e 100644 --- a/wasmaudioworklet/audioworkletnode.js +++ b/wasmaudioworklet/audioworkletnode.js @@ -2,7 +2,11 @@ import { stopVideoRecording, startVideoRecording } from './screenrecorder/screen // The code in the main global scope. +/** + * @param {Node} componentRoot + */ export function initAudioWorkletNode(componentRoot) { + /** @type {AudioWorkletNode} */ let audioworkletnode; let playing = false; window.recordedmidi = []; diff --git a/wasmaudioworklet/audioworkletpolyfill.js b/wasmaudioworklet/audioworkletpolyfill.js index 3b699f18..6f546aab 100644 --- a/wasmaudioworklet/audioworkletpolyfill.js +++ b/wasmaudioworklet/audioworkletpolyfill.js @@ -6,12 +6,21 @@ */ if(typeof AudioContext !== 'function') { - window.AudioContext = window.webkitAudioContext; + /** @type {any} */ + const win = window + window.AudioContext = win.webkitAudioContext; } if(typeof AudioWorkletNode !== 'function') { console.log('No audioworklet support - using polyfill'); - window.AudioWorkletNode = function(context, processorName) { + + window.AudioWorkletNode = /** @type {AudioWorkletNode} */(AudioWorkletNodePolyfill) + + /** + * @param {any} context + * @param {any} processorName + */ + function AudioWorkletNodePolyfill(context, processorName) { let connected = false; return { context: context, @@ -114,3 +123,6 @@ if(typeof AudioWorkletNode !== 'function') { window.audioWorkletProcessors[name] = new processorClass(); } } + +// For TypeScript to recognize the file as a "module". +export {} \ No newline at end of file