Move to TypeScript (#3830)

This moves our JS file to use TS instead, which allows us to use a proper linter to check the code.
All related files where moved out from the root in a dedicated folder to avoid polluting the Rust environment.
This commit is contained in:
daxpedda 2024-07-27 18:26:52 +02:00 committed by GitHub
parent 7b0104b54c
commit 7892e86731
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
13 changed files with 120 additions and 13 deletions

View file

@ -0,0 +1,19 @@
{
"module": {
"type": "es6"
},
"isModule": true,
"minify": true,
"jsc": {
"parser": {
"syntax": "typescript"
},
"target": "es2022",
"minify": {
"compress": {
"unused": true
},
"mangle": true
}
}
}

View file

@ -0,0 +1,32 @@
import eslint from '@eslint/js'
import tseslint from 'typescript-eslint'
import globals from 'globals'
export default tseslint.config(
{
ignores: ['**/*.min.js', 'eslint.config.mjs'],
},
eslint.configs.recommended,
...tseslint.configs.strictTypeChecked,
...tseslint.configs.stylisticTypeChecked,
{
languageOptions: {
parserOptions: {
ecmaVersion: 'latest',
project: ['tsconfig.json'],
sourceType: 'module',
},
globals: {
...globals.browser,
},
},
rules: {
'@typescript-eslint/no-confusing-void-expression': [
'error',
{
ignoreArrowShorthand: true,
},
],
},
}
)

View file

@ -0,0 +1,9 @@
{
"devDependencies": {
"@eslint/js": "^9",
"@types/eslint__js": "^8",
"eslint": "^8",
"typescript": "^5",
"typescript-eslint": "^7"
}
}

View file

@ -0,0 +1,12 @@
declare global {
// eslint-disable-next-line no-var
var scheduler: Scheduler
}
export interface Scheduler {
postTask<T>(callback: () => T | PromiseLike<T>, options?: SchedulerPostTaskOptions): Promise<T>
}
export interface SchedulerPostTaskOptions {
delay?: number
}

View file

@ -0,0 +1,14 @@
{
"compilerOptions": {
"target": "ESNext",
"allowUnreachableCode": false,
"allowUnusedLabels": false,
"exactOptionalPropertyTypes": true,
"noImplicitOverride": true,
"noPropertyAccessFromIndexSignature": true,
"noUncheckedIndexedAccess": true,
"noUnusedLocals": true,
"noUnusedParameters": true,
"strict": true,
}
}

View file

@ -0,0 +1 @@
onmessage=e=>{let[s,a]=e.data,l=()=>s.postMessage(void 0);"scheduler"in globalThis?globalThis.scheduler.postTask(l,{delay:a}):setTimeout(l,a)};

View file

@ -0,0 +1,10 @@
onmessage = (event) => {
const [port, timeout] = event.data as [MessagePort, number]
const f = () => port.postMessage(undefined)
if ('scheduler' in globalThis) {
void globalThis.scheduler.postTask(f, { delay: timeout })
} else {
setTimeout(f, timeout)
}
}

View file

@ -175,7 +175,7 @@ impl Schedule {
F: 'static + FnMut(),
{
thread_local! {
static URL: ScriptUrl = ScriptUrl::new(include_str!("worker.min.js"));
static URL: ScriptUrl = ScriptUrl::new(include_str!("../script/worker.min.js"));
static WORKER: Worker = URL.with(|url| Worker::new(&url.0)).expect("`new Worker()` is not expected to fail with a local script");
}

View file

@ -1,10 +0,0 @@
onmessage = event => {
const [port, timeout] = event.data
const f = () => port.postMessage(undefined)
if ('scheduler' in this) {
scheduler.postTask(f, { delay: timeout })
} else {
setTimeout(f, timeout)
}
}

View file

@ -1 +0,0 @@
onmessage=e=>{let[s,t]=e.data,a=()=>s.postMessage(void 0);"scheduler"in this?scheduler.postTask(a,{delay:t}):setTimeout(a,t)};