Skip to content

Commit 0fe6963

Browse files
authored
use class-based reacgtivity for logs in playground (#1290)
1 parent 5756c13 commit 0fe6963

File tree

7 files changed

+39
-34
lines changed

7 files changed

+39
-34
lines changed

apps/svelte.dev/src/routes/tutorial/[...slug]/OutputRollup.svelte

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<script lang="ts">
22
import { browser } from '$app/environment';
33
import Viewer from '@sveltejs/repl/viewer';
4-
import Console, { type Log } from '@sveltejs/repl/console';
4+
import { Console, type Log } from '@sveltejs/repl/console';
55
import { theme } from '@sveltejs/site-kit/state';
66
import Chrome from './Chrome.svelte';
77
import Loading from './Loading.svelte';

packages/repl/package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,8 @@
4545
"svelte": "./src/lib/Workspace.svelte.ts"
4646
},
4747
"./console": {
48-
"types": "./src/lib/Output/console/Console.svelte",
49-
"svelte": "./src/lib/Output/console/Console.svelte"
48+
"types": "./src/lib/Output/console/index.ts",
49+
"svelte": "./src/lib/Output/console/index.ts"
5050
}
5151
},
5252
"files": [

packages/repl/src/lib/Output/Viewer.svelte

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,15 @@
55
import Message from '../Message.svelte';
66
import PaneWithPanel from './PaneWithPanel.svelte';
77
import ReplProxy from './ReplProxy.js';
8-
import Console, { type Log } from './console/Console.svelte';
8+
import Console from './console/Console.svelte';
99
import getLocationFromStack from './get-location-from-stack';
1010
import srcdoc from './srcdoc/index.html?raw';
1111
import srcdoc_styles from './srcdoc/styles.css?raw';
1212
import ErrorOverlay from './ErrorOverlay.svelte';
1313
import type { CompileError } from 'svelte/compiler';
1414
import type Bundler from '../Bundler.svelte';
1515
import type { BundleResult } from '../public';
16+
import { Log } from './console/Log.svelte';
1617
1718
interface Props {
1819
error: Error | null;
@@ -48,7 +49,7 @@
4849
let context = get_repl_context();
4950
let bundle = $derived((bundler ?? context?.bundler)?.result);
5051
51-
let logs: Log[] = $state.raw([]); // we don't want to proxify the logged values
52+
let logs: Log[] = $state([]);
5253
let log_group_stack: Log[][] = [];
5354
5455
// svelte-ignore state_referenced_locally
@@ -255,19 +256,18 @@
255256
error = e;
256257
}
257258
258-
function push_logs(log: Log) {
259+
function push_logs(data: any) {
260+
const log = new Log(data);
259261
current_log_group.push((last_console_event = log));
260-
logs = [...logs, log];
261262
onLog?.(logs);
262263
}
263264
264-
function group_logs(log: Log) {
265-
log.logs = [];
265+
function group_logs(data: any) {
266+
const log = new Log(data);
266267
current_log_group.push(log);
267268
// TODO: Investigate
268269
log_group_stack.push(current_log_group);
269270
current_log_group = log.logs;
270-
logs = [...logs];
271271
onLog?.(logs);
272272
}
273273
@@ -278,14 +278,12 @@
278278
}
279279
280280
function increment_duplicate_log() {
281-
const last_log = current_log_group[current_log_group.length - 1];
281+
last_console_event.count += 1;
282282
283-
if (last_log) {
284-
last_log.count = (last_log.count || 1) + 1;
285-
logs = [...logs];
283+
if (current_log_group.includes(last_console_event)) {
286284
onLog?.(logs);
287285
} else {
288-
last_console_event.count = 1;
286+
// console was cleared
289287
push_logs(last_console_event);
290288
}
291289
}

packages/repl/src/lib/Output/console/Console.svelte

Lines changed: 1 addition & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,6 @@
1-
<script module lang="ts">
2-
export type Log = {
3-
command: 'info' | 'warn' | 'error' | 'table' | 'group' | 'clear' | 'unclonable';
4-
action?: 'console';
5-
args?: any[];
6-
collapsed?: boolean;
7-
expanded?: boolean;
8-
count?: number;
9-
logs?: Log[];
10-
stack?: Array<{
11-
label?: string;
12-
location?: string;
13-
}>;
14-
data?: any;
15-
columns?: string[];
16-
};
17-
</script>
18-
191
<script lang="ts">
202
import ConsoleLine from './ConsoleLine.svelte';
3+
import type { Log } from './Log.svelte';
214
225
export let logs: Log[];
236
</script>

packages/repl/src/lib/Output/console/ConsoleLine.svelte

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<script lang="ts">
22
import JSONNode from '@sveltejs/svelte-json-tree';
33
import ConsoleTable from './ConsoleTable.svelte';
4-
import type { Log } from './Console.svelte';
4+
import type { Log } from './Log.svelte';
55
66
export let log: Log;
77
export let depth = 0;
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
type Command = 'info' | 'warn' | 'error' | 'table' | 'group' | 'clear' | 'unclonable';
2+
3+
export class Log {
4+
command: Command;
5+
args?: any[];
6+
stack?: Array<{
7+
label?: string;
8+
location?: string;
9+
}>;
10+
data?: any;
11+
columns?: string[];
12+
13+
collapsed = $state(false);
14+
expanded = $state(false);
15+
count = $state(1);
16+
logs = $state([]);
17+
18+
constructor(data: any) {
19+
this.command = data.command;
20+
Object.assign(this, data);
21+
}
22+
}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
export { default as Console } from './Console.svelte';
2+
export { Log } from './Log.svelte';

0 commit comments

Comments
 (0)