Skip to content

Commit 72e31a7

Browse files
committed
Modernize code to ES2025
1 parent 29657b1 commit 72e31a7

9 files changed

Lines changed: 117 additions & 104 deletions

File tree

src/lib/components/dialog-manager/dialog-store.svelte.ts

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -27,19 +27,19 @@ export function removeDialog(id: number): void {
2727
export function createDialog<R>(
2828
contextFactory: (resolve: (result: R) => void) => DialogContext<R>
2929
): Promise<R> {
30-
return new Promise((resolve) => {
31-
const id = ++dialogCount;
32-
let resolved = false;
30+
const { promise, resolve } = Promise.withResolvers<R>();
31+
const id = ++dialogCount;
32+
let resolved = false;
3333

34-
const wrappedResolve = (result: R) => {
35-
if (resolved) return;
36-
resolved = true;
37-
setTimeout(() => removeDialog(id), 150);
38-
resolve(result);
39-
};
34+
const wrappedResolve = (result: R) => {
35+
if (resolved) return;
36+
resolved = true;
37+
setTimeout(() => removeDialog(id), 150);
38+
resolve(result);
39+
};
4040

41-
dialogs.set(id, contextFactory(wrappedResolve) as DialogContext);
42-
});
41+
dialogs.set(id, contextFactory(wrappedResolve) as DialogContext);
42+
return promise;
4343
}
4444

4545
/**

src/routes/(app)/home/+page.svelte

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,13 @@
1111
1212
registerBreadcrumbs(() => [{ label: 'Home', href: '/home' }]);
1313
14-
let shockerCount = $derived(
15-
Array.from(ownHubs).reduce((sum, [, hub]) => sum + hub.shockers.length, 0)
16-
);
14+
let shockerCount = $derived(ownHubs.values().reduce((sum, hub) => sum + hub.shockers.length, 0));
1715
let hubCount = $derived(ownHubs.size);
1816
let onlineHubCount = $derived(
19-
Array.from(onlineHubs).filter(([, state]) => state.isOnline).length
17+
onlineHubs
18+
.values()
19+
.filter((state) => state.isOnline)
20+
.toArray().length
2021
);
2122
2223
onMount(refreshOwnHubs);

src/routes/(app)/hubs/+page.svelte

Lines changed: 23 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -21,26 +21,29 @@
2121
const isMobile = new IsMobile();
2222
2323
let data = $derived.by<Hub[]>(() => {
24-
return Array.from(ownHubs).map(([, hub]) => {
25-
const onlineState = onlineHubs.get(hub.id);
26-
return {
27-
id: hub.id,
28-
name: hub.name,
29-
is_online: onlineState?.isOnline ?? false,
30-
firmware_version: onlineState?.firmwareVersion ?? null,
31-
shockers: hub.shockers.map((shocker) => {
32-
return {
33-
id: shocker.id,
34-
rf_id: shocker.rfId,
35-
model: shocker.model,
36-
name: shocker.name,
37-
is_paused: shocker.isPaused,
38-
created_at: shocker.createdOn,
39-
};
40-
}),
41-
created_at: hub.createdOn,
42-
};
43-
});
24+
return ownHubs
25+
.values()
26+
.map((hub) => {
27+
const onlineState = onlineHubs.get(hub.id);
28+
return {
29+
id: hub.id,
30+
name: hub.name,
31+
is_online: onlineState?.isOnline ?? false,
32+
firmware_version: onlineState?.firmwareVersion ?? null,
33+
shockers: hub.shockers.map((shocker) => {
34+
return {
35+
id: shocker.id,
36+
rf_id: shocker.rfId,
37+
model: shocker.model,
38+
name: shocker.name,
39+
is_paused: shocker.isPaused,
40+
created_at: shocker.createdOn,
41+
};
42+
}),
43+
created_at: hub.createdOn,
44+
};
45+
})
46+
.toArray();
4447
});
4548
4649
async function openCreateHubDialog() {

src/routes/(app)/shares/public/[shareId=guid]/edit/dialog-add-shocker.svelte

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,14 @@
66
import { ownHubs } from '$lib/state/hubs-state.svelte';
77
88
let availableShockers = $derived(
9-
Array.from(ownHubs)
10-
.flatMap(([, hub]) => hub.shockers)
9+
ownHubs
10+
.values()
11+
.flatMap((hub) => hub.shockers)
1112
.map((shocker) => ({
1213
value: shocker.id,
1314
label: shocker.name,
1415
}))
16+
.toArray()
1517
);
1618
1719
interface Props {

src/routes/(app)/shares/user/dialog-share-code-create.svelte

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,14 @@
1212
import { refreshOutgoingInvites } from '$lib/state/user-shares-state.svelte';
1313
1414
let availableShockers = $derived(
15-
Array.from(ownHubs)
16-
.flatMap(([, hub]) => hub.shockers)
15+
ownHubs
16+
.values()
17+
.flatMap((hub) => hub.shockers)
1718
.map((shocker) => ({
1819
value: shocker.id,
1920
label: shocker.name,
2021
}))
22+
.toArray()
2123
);
2224
2325
interface Props {

src/routes/(app)/shockers/own/+page.svelte

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,12 @@
2525
2626
registerBreadcrumbs(() => [{ label: 'Shockers' }]);
2727
28-
let shockers = $derived(Array.from(ownHubs).flatMap(([, hub]) => hub.shockers));
28+
let shockers = $derived(
29+
ownHubs
30+
.values()
31+
.flatMap((hub) => hub.shockers)
32+
.toArray()
33+
);
2934
3035
let moduleType = $state<ModuleType>(ModuleType.ClassicControlModule);
3136
let loading = $state(true);
@@ -49,7 +54,7 @@
4954
}
5055
5156
async function openAddShockerDialog() {
52-
const hubs = Array.from(ownHubs);
57+
const hubs = ownHubs.entries().toArray();
5358
if (hubs.length === 0) {
5459
toast.error('You need to create a hub before adding shockers.');
5560
return;

src/routes/flashtool/FlashManager.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,9 @@ async function setupESPLoader(
3737
}
3838

3939
function sleep(ms: number): Promise<void> {
40-
return new Promise((f) => setTimeout(f, ms));
40+
const { promise, resolve } = Promise.withResolvers<void>();
41+
setTimeout(resolve, ms);
42+
return promise;
4143
}
4244

4345
function appendBuffer(buffer: Uint8Array | null, data: Uint8Array): Uint8Array {

tsconfig.json

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,11 @@
11
{
22
"extends": "./.svelte-kit/tsconfig.json",
33
"compilerOptions": {
4-
"target": "es2024",
4+
"target": "es2025",
55
"rewriteRelativeImportExtensions": true,
6-
"allowJs": true,
76
"checkJs": true,
8-
"esModuleInterop": true,
9-
"forceConsistentCasingInFileNames": true,
10-
"resolveJsonModule": true,
117
"skipLibCheck": true,
128
"sourceMap": true,
13-
"strict": true,
149
"moduleResolution": "bundler"
1510
}
1611
// Path aliases are handled by https://svelte.dev/docs/kit/configuration#alias

vite.config.ts

Lines changed: 59 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import net from 'node:net';
88
import os from 'node:os';
99
import { env } from 'node:process';
1010
import license from 'rollup-plugin-license';
11-
import { type Plugin, type PluginOption, defineConfig, loadEnv } from 'vite';
11+
import { type Plugin, type PluginOption, type UserConfig, defineConfig, loadEnv } from 'vite';
1212
import devtoolsJson from 'vite-plugin-devtools-json';
1313
import mkcert from 'vite-plugin-mkcert';
1414

@@ -157,55 +157,55 @@ async function getServerConfig(mode: string, useLocalRedirect: boolean) {
157157
}
158158

159159
async function ensurePortBindable(host: string, port: number): Promise<void> {
160-
return new Promise((resolve) => {
161-
const server = net.createServer();
162-
server.once('error', (err: NodeJS.ErrnoException) => {
163-
if (err.code === 'EACCES') {
164-
const platform = os.platform();
165-
let fix: string;
166-
167-
if (platform === 'linux') {
168-
fix = [
169-
`Node.js needs permission to serve HTTPS on port ${port}.\n`,
170-
'Option 1 (Recommended): Set up a reverse proxy',
171-
' Use Nginx or Caddy to proxy traffic to your Node server.',
172-
' This is more secure and follows best practices.\n',
173-
'Option 2 (Quick fix): Grant Node.js permission to bind to privileged ports',
174-
chalk.blue.bold(` sudo setcap 'cap_net_bind_service=+ep' $(which node)\n`),
175-
chalk.yellow.bold(
176-
' ⚠️ Security note: This allows Node to bind to ANY port below 1024.'
177-
),
178-
chalk.yellow.bold(' Only use this in trusted development environments.'),
179-
].join('\n');
180-
} else if (platform === 'darwin') {
181-
fix = [
182-
`Node.js needs permission to serve HTTPS on port ${port}.\n`,
183-
'Fix: Run the dev server with sudo, or set up a reverse proxy.',
184-
].join('\n');
185-
} else {
186-
fix = `Node.js does not have permission to bind to port ${port}.\nTry running with elevated privileges or use a reverse proxy.`;
187-
}
188-
189-
console.log(
190-
boxen(fix, {
191-
padding: 1,
192-
margin: 1,
193-
borderStyle: 'round',
194-
borderColor: 'yellow',
195-
title: `Port ${port} Permission Denied`,
196-
titleAlignment: 'center',
197-
})
198-
);
199-
process.exit(1);
160+
const { promise, resolve } = Promise.withResolvers<void>();
161+
const server = net.createServer();
162+
server.once('error', (err: NodeJS.ErrnoException) => {
163+
if (err.code === 'EACCES') {
164+
const platform = os.platform();
165+
let fix: string;
166+
167+
if (platform === 'linux') {
168+
fix = [
169+
`Node.js needs permission to serve HTTPS on port ${port}.\n`,
170+
'Option 1 (Recommended): Set up a reverse proxy',
171+
' Use Nginx or Caddy to proxy traffic to your Node server.',
172+
' This is more secure and follows best practices.\n',
173+
'Option 2 (Quick fix): Grant Node.js permission to bind to privileged ports',
174+
chalk.blue.bold(` sudo setcap 'cap_net_bind_service=+ep' $(which node)\n`),
175+
chalk.yellow.bold(
176+
' ⚠️ Security note: This allows Node to bind to ANY port below 1024.'
177+
),
178+
chalk.yellow.bold(' Only use this in trusted development environments.'),
179+
].join('\n');
180+
} else if (platform === 'darwin') {
181+
fix = [
182+
`Node.js needs permission to serve HTTPS on port ${port}.\n`,
183+
'Fix: Run the dev server with sudo, or set up a reverse proxy.',
184+
].join('\n');
185+
} else {
186+
fix = `Node.js does not have permission to bind to port ${port}.\nTry running with elevated privileges or use a reverse proxy.`;
200187
}
201-
// For other errors (e.g. EADDRINUSE), let Vite handle them
202-
resolve();
203-
});
204-
server.once('listening', () => {
205-
server.close(() => resolve());
206-
});
207-
server.listen(port, host);
188+
189+
console.log(
190+
boxen(fix, {
191+
padding: 1,
192+
margin: 1,
193+
borderStyle: 'round',
194+
borderColor: 'yellow',
195+
title: `Port ${port} Permission Denied`,
196+
titleAlignment: 'center',
197+
})
198+
);
199+
process.exit(1);
200+
}
201+
// For other errors (e.g. EADDRINUSE), let Vite handle them
202+
resolve();
203+
});
204+
server.once('listening', () => {
205+
server.close(() => resolve());
208206
});
207+
server.listen(port, host);
208+
return promise;
209209
}
210210

211211
export default defineConfig(async ({ command, mode, isPreview }) => {
@@ -215,17 +215,20 @@ export default defineConfig(async ({ command, mode, isPreview }) => {
215215
// If we are running locally, ensure that local.{PUBLIC_SITE_URL} resolves to localhost, and then use mkcert to generate a certificate
216216
const useLocalRedirect = isLocalServe && !isProduction && !isTruthy(env.CI);
217217

218-
return defineConfig({
218+
return {
219219
build: {
220-
target: 'es2024',
220+
rolldownOptions: {
221+
output: {
222+
legalComments: 'none',
223+
},
224+
treeshake:
225+
mode === 'production'
226+
? { manualPureFunctions: ['console.log', 'console.debug', 'console.trace'] }
227+
: undefined,
228+
},
221229
},
222230
plugins: getPlugins(useLocalRedirect),
223231
server: await getServerConfig(mode, useLocalRedirect),
224232
test: { include: ['src/**/*.{test,spec}.{js,ts}'] },
225-
esbuild: {
226-
legalComments: 'none',
227-
drop: mode === 'production' ? ['debugger'] : [],
228-
pure: mode === 'production' ? ['console.log', 'console.debug', 'console.trace'] : [],
229-
},
230-
});
233+
} satisfies UserConfig;
231234
});

0 commit comments

Comments
 (0)