Skip to content

Commit 75b3336

Browse files
authored
more js -> ts conversion (#48)
* convert some .js files to .ts * more * fix
1 parent 07c399e commit 75b3336

22 files changed

+308
-427
lines changed

packages/repl/src/lib/Bundler.js

Lines changed: 0 additions & 72 deletions
This file was deleted.

packages/repl/src/lib/Bundler.ts

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
import type { File } from './types';
2+
import Worker from './workers/bundler/index.js?worker';
3+
import type { BundleMessageData } from './workers/workers';
4+
5+
const workers = new Map();
6+
7+
let uid = 1;
8+
9+
export default class Bundler {
10+
worker: Worker;
11+
handlers: Map<number, (data: BundleMessageData) => void>;
12+
13+
constructor({
14+
packages_url,
15+
svelte_url,
16+
onstatus
17+
}: {
18+
packages_url: string;
19+
svelte_url: string;
20+
onstatus: (val: string | null) => void;
21+
}) {
22+
const hash = `${packages_url}:${svelte_url}`;
23+
24+
if (!workers.has(hash)) {
25+
const worker = new Worker();
26+
worker.postMessage({ type: 'init', packages_url, svelte_url });
27+
workers.set(hash, worker);
28+
}
29+
30+
this.worker = workers.get(hash);
31+
32+
this.handlers = new Map();
33+
34+
this.worker.addEventListener('message', (event: MessageEvent<BundleMessageData>) => {
35+
const handler = this.handlers.get(event.data.uid);
36+
37+
if (handler) {
38+
// if no handler, was meant for a different REPL
39+
if (event.data.type === 'status') {
40+
onstatus(event.data.message);
41+
return;
42+
}
43+
44+
onstatus(null);
45+
handler(event.data);
46+
this.handlers.delete(event.data.uid);
47+
}
48+
});
49+
}
50+
51+
bundle(files: File[]) {
52+
return new Promise((fulfil) => {
53+
this.handlers.set(uid, fulfil);
54+
55+
this.worker.postMessage({
56+
uid,
57+
type: 'bundle',
58+
files
59+
});
60+
61+
uid += 1;
62+
});
63+
}
64+
65+
destroy() {
66+
this.worker.terminate();
67+
}
68+
}

packages/repl/src/lib/Output/Compiler.js renamed to packages/repl/src/lib/Output/Compiler.ts

Lines changed: 15 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,17 @@
1+
import type { File } from '$lib/types';
2+
import type { CompileOptions } from 'svelte/compiler';
13
import Worker from '../workers/compiler/index.js?worker';
4+
import type { CompilerOutput, MigrateOutput } from '$lib/workers/workers';
25

36
const workers = new Map();
47

58
let uid = 1;
69

710
export default class Compiler {
8-
/** @type {Worker} */
9-
worker;
11+
worker: Worker;
12+
handlers: Map<number, (...arg: any) => void> = new Map();
1013

11-
/** @type {Map<number, (...arg: any) => void>} */
12-
handlers = new Map();
13-
14-
/** @param {string} svelte_url */
15-
constructor(svelte_url) {
14+
constructor(svelte_url: string) {
1615
if (!workers.has(svelte_url)) {
1716
const worker = new Worker();
1817
worker.postMessage({ type: 'init', svelte_url });
@@ -21,30 +20,18 @@ export default class Compiler {
2120

2221
this.worker = workers.get(svelte_url);
2322

24-
this.worker.addEventListener(
25-
'message',
26-
/**
27-
* @param {MessageEvent<any>} event
28-
*/
29-
(event) => {
30-
const handler = this.handlers.get(event.data.id);
23+
this.worker.addEventListener('message', (event: MessageEvent<any>) => {
24+
const handler = this.handlers.get(event.data.id);
3125

32-
if (handler) {
33-
// if no handler, was meant for a different REPL
34-
handler(event.data.result);
35-
this.handlers.delete(event.data.id);
36-
}
26+
if (handler) {
27+
// if no handler, was meant for a different REPL
28+
handler(event.data.result);
29+
this.handlers.delete(event.data.id);
3730
}
38-
);
31+
});
3932
}
4033

41-
/**
42-
* @param {import('$lib/types').File} file
43-
* @param {import('svelte/compiler').CompileOptions} options
44-
* @param {boolean} return_ast
45-
* @returns {Promise<import('$lib/workers/workers').CompilerOutput>}
46-
*/
47-
compile(file, options, return_ast) {
34+
compile(file: File, options: CompileOptions, return_ast: boolean): Promise<CompilerOutput> {
4835
return new Promise((fulfil) => {
4936
const id = uid++;
5037

@@ -69,11 +56,7 @@ export default class Compiler {
6956
});
7057
}
7158

72-
/**
73-
* @param {import('$lib/types').File} file
74-
* @returns {Promise<import('$lib/workers/workers').MigrateOutput>}
75-
*/
76-
migrate(file) {
59+
migrate(file: File): Promise<MigrateOutput> {
7760
return new Promise((fulfil) => {
7861
const id = uid++;
7962

packages/repl/src/lib/Output/ReplProxy.js renamed to packages/repl/src/lib/Output/ReplProxy.ts

Lines changed: 17 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,14 @@
1+
import type { Handlers } from './proxy';
2+
13
let uid = 1;
24

35
export default class ReplProxy {
4-
/** @type {HTMLIFrameElement} */
5-
iframe;
6-
7-
/** @type {import("./proxy").Handlers} */
8-
handlers;
9-
10-
/** @type {Map<number, { resolve: (value: any) => void, reject: (value: any) => void }>} */
11-
pending_cmds = new Map();
6+
iframe: HTMLIFrameElement;
7+
handlers: Handlers;
8+
pending_cmds: Map<number, { resolve: (value: any) => void; reject: (value: any) => void }> =
9+
new Map();
1210

13-
/** @param {MessageEvent<any>} event */
14-
handle_event = (event) => {
11+
handle_event = (event: MessageEvent<any>) => {
1512
if (event.source !== this.iframe.contentWindow) return;
1613

1714
const { action, args } = event.data;
@@ -31,11 +28,7 @@ export default class ReplProxy {
3128
}
3229
};
3330

34-
/**
35-
* @param {HTMLIFrameElement} iframe
36-
* @param {import("./proxy").Handlers} handlers
37-
*/
38-
constructor(iframe, handlers) {
31+
constructor(iframe: HTMLIFrameElement, handlers: Handlers) {
3932
this.iframe = iframe;
4033
this.handlers = handlers;
4134

@@ -46,11 +39,7 @@ export default class ReplProxy {
4639
window.removeEventListener('message', this.handle_event);
4740
}
4841

49-
/**
50-
* @param {string} action
51-
* @param {any} args
52-
*/
53-
iframe_command(action, args) {
42+
iframe_command(action: string, args: any) {
5443
return new Promise((resolve, reject) => {
5544
const cmd_id = uid++;
5645

@@ -60,10 +49,13 @@ export default class ReplProxy {
6049
});
6150
}
6251

63-
/**
64-
* @param {{ action: string; cmd_id: number; message: string; stack: any; args: any; }} cmd_data
65-
*/
66-
handle_command_message(cmd_data) {
52+
handle_command_message(cmd_data: {
53+
action: string;
54+
cmd_id: number;
55+
message: string;
56+
stack: any;
57+
args: any;
58+
}) {
6759
let action = cmd_data.action;
6860
let id = cmd_data.cmd_id;
6961
let handler = this.pending_cmds.get(id);
@@ -85,8 +77,7 @@ export default class ReplProxy {
8577
}
8678
}
8779

88-
/** @param {string} script */
89-
eval(script) {
80+
eval(script: string) {
9081
return this.iframe_command('eval', { script });
9182
}
9283

packages/repl/src/lib/Output/get-location-from-stack.js renamed to packages/repl/src/lib/Output/get-location-from-stack.ts

Lines changed: 5 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,7 @@
1-
import { decode } from '@jridgewell/sourcemap-codec';
2-
3-
/**
4-
* @param {string} stack
5-
* @param {import('@jridgewell/sourcemap-codec').SourceMapMappings} map
6-
* @returns
7-
*/
8-
export default function getLocationFromStack(stack, map) {
1+
import type { StartOrEnd } from '$lib/types';
2+
import { decode, type SourceMapMappings } from '@jridgewell/sourcemap-codec';
3+
4+
export default function getLocationFromStack(stack: string, map: SourceMapMappings) {
95
if (!stack) return;
106
const last = stack.split('\n')[1];
117
const match = /<anonymous>:(\d+):(\d+)\)$/.exec(last);
@@ -18,13 +14,7 @@ export default function getLocationFromStack(stack, map) {
1814
return trace({ line, column }, map);
1915
}
2016

21-
/**
22-
*
23-
* @param {Omit<import('$lib/types').StartOrEnd, 'character'>} loc
24-
* @param {*} map
25-
* @returns
26-
*/
27-
function trace(loc, map) {
17+
function trace(loc: Omit<StartOrEnd, 'character'>, map) {
2818
const mappings = decode(map.mappings);
2919
const segments = mappings[loc.line - 1];
3020

packages/repl/src/lib/Repl.svelte

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
import { set_repl_context } from './context.js';
1313
import { get_full_filename } from './utils.js';
1414
import Compiler from './Output/Compiler.js';
15-
import type { File, MessageDetails, ReplContext } from './types.js';
15+
import type { Bundle, File, MessageDetails, ReplContext } from './types.js';
1616
import type { CompileOptions } from 'svelte/compiler';
1717
import type { CompilerOutput } from './workers/workers.js';
1818
@@ -129,7 +129,7 @@
129129
resolver = resolve;
130130
});
131131
const result = await $bundler?.bundle($files);
132-
if (result && token === current_token) $bundle = result;
132+
if (result && token === current_token) $bundle = result as Bundle;
133133
resolver();
134134
}
135135

0 commit comments

Comments
 (0)