Skip to content

Commit d05cd13

Browse files
committed
fix: code review
1 parent c7829e4 commit d05cd13

File tree

4 files changed

+68
-45
lines changed

4 files changed

+68
-45
lines changed

packages/runtime/src/store/previews.ts

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ export class PreviewsStore {
3434
portInfo.origin = url;
3535

3636
if (this._previewsLayout.length === 0) {
37-
this.previews.set([new PreviewInfo(portInfo)]);
37+
this.previews.set([new PreviewInfo({}, portInfo)]);
3838
} else {
3939
this._previewsLayout = [...this._previewsLayout];
4040
this.previews.set(this._previewsLayout);
@@ -57,17 +57,16 @@ export class PreviewsStore {
5757
// if the schema is `true`, we just use the default empty array
5858
const previews = config === true ? [] : config ?? [];
5959

60-
const previewInfos = previews.map((preview) => {
61-
const info = new PreviewInfo(preview);
62-
const portInfo = this._availablePreviews.get(info.port);
60+
const previewInfos = previews.map((previewConfig) => {
61+
const preview = PreviewInfo.parse(previewConfig);
62+
let portInfo = this._availablePreviews.get(preview.port);
6363

6464
if (!portInfo) {
65-
this._availablePreviews.set(info.port, info.portInfo);
66-
} else {
67-
info.portInfo = portInfo;
65+
portInfo = new PortInfo(preview.port);
66+
this._availablePreviews.set(preview.port, portInfo);
6867
}
6968

70-
return info;
69+
return new PreviewInfo(preview, portInfo);
7170
});
7271

7372
let areDifferent = previewInfos.length != this._previewsLayout.length;

packages/runtime/src/webcontainer/port-info.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,6 @@ export class PortInfo {
22
constructor(
33
readonly port: number,
44
public origin?: string,
5-
public ready?: boolean,
5+
public ready: boolean = false,
66
) {}
77
}

packages/runtime/src/webcontainer/preview-info.spec.ts

Lines changed: 32 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,105 +1,118 @@
11
import { describe, it, expect } from 'vitest';
22
import { PreviewInfo } from './preview-info.js';
3+
import { PortInfo } from './port-info.js';
34

45
describe('PreviewInfo', () => {
56
it('should accept a number for port', () => {
6-
const previewInfo = new PreviewInfo(3000);
7+
const previewInfo = PreviewInfo.parse(3000);
78

89
expect(previewInfo.port).toBe(3000);
910
expect(previewInfo.title).toBe(undefined);
1011
expect(previewInfo.pathname).toBe(undefined);
1112
});
1213

1314
it('should accept a string for port and pathname', () => {
14-
const previewInfo = new PreviewInfo('3000/some/nested/path');
15+
const previewInfo = PreviewInfo.parse('3000/some/nested/path');
1516

1617
expect(previewInfo.port).toBe(3000);
1718
expect(previewInfo.pathname).toBe('some/nested/path');
1819
expect(previewInfo.title).toBe(undefined);
1920
});
2021

2122
it('should accept a tuple of [port, title]', () => {
22-
const previewInfo = new PreviewInfo([3000, 'Local server']);
23+
const previewInfo = PreviewInfo.parse([3000, 'Local server']);
2324

2425
expect(previewInfo.port).toBe(3000);
2526
expect(previewInfo.title).toBe('Local server');
2627
expect(previewInfo.pathname).toBe(undefined);
2728
});
2829

2930
it('should accept a tuple of [port, title, pathname]', () => {
30-
const previewInfo = new PreviewInfo([3000, 'Local server', '/docs']);
31+
const previewInfo = PreviewInfo.parse([3000, 'Local server', '/docs']);
3132

3233
expect(previewInfo.port).toBe(3000);
3334
expect(previewInfo.title).toBe('Local server');
3435
expect(previewInfo.pathname).toBe('/docs');
3536
});
3637

3738
it('should accept an object with { port, title }', () => {
38-
const previewInfo = new PreviewInfo({ port: 3000, title: 'Local server' });
39+
const previewInfo = PreviewInfo.parse({ port: 3000, title: 'Local server' });
3940

4041
expect(previewInfo.port).toBe(3000);
4142
expect(previewInfo.title).toBe('Local server');
4243
expect(previewInfo.pathname).toBe(undefined);
4344
});
4445

4546
it('should accept an object with { port, title, pathname }', () => {
46-
const previewInfo = new PreviewInfo({ port: 3000, title: 'Local server', pathname: '/docs' });
47+
const previewInfo = PreviewInfo.parse({ port: 3000, title: 'Local server', pathname: '/docs' });
4748

4849
expect(previewInfo.port).toBe(3000);
4950
expect(previewInfo.title).toBe('Local server');
5051
expect(previewInfo.pathname).toBe('/docs');
5152
});
5253

5354
it('should not be ready by default', () => {
54-
const previewInfo = new PreviewInfo(3000);
55+
const previewInfo = new PreviewInfo({}, new PortInfo(3000));
5556

5657
expect(previewInfo.ready).toBe(false);
5758
});
5859

5960
it('should be ready if explicitly set', () => {
60-
const previewInfo = new PreviewInfo(3000, true);
61+
const previewInfo = new PreviewInfo({}, new PortInfo(3000, undefined, true));
6162

6263
expect(previewInfo.ready).toBe(true);
6364
});
6465

6566
it('should not be ready if explicitly set', () => {
66-
const previewInfo = new PreviewInfo(3000, false);
67+
const previewInfo = new PreviewInfo({}, new PortInfo(3000, undefined, false));
6768

6869
expect(previewInfo.ready).toBe(false);
6970
});
7071

7172
it('should have a url with a custom pathname and baseUrl', () => {
72-
const previewInfo = new PreviewInfo('3000/foo');
73+
const parsed = PreviewInfo.parse('3000/foo');
74+
const previewInfo = new PreviewInfo(parsed, new PortInfo(parsed.port));
7375
previewInfo.portInfo.origin = 'https://example.com';
7476

7577
expect(previewInfo.url).toBe('https://example.com/foo');
7678
});
7779

7880
it('should be equal to another preview info with the same port and title', () => {
79-
const a = new PreviewInfo(3000);
80-
const b = new PreviewInfo(3000);
81+
const a = new PreviewInfo({}, new PortInfo(3000));
82+
const b = new PreviewInfo({}, new PortInfo(3000));
8183

8284
expect(PreviewInfo.equals(a, b)).toBe(true);
8385
});
8486

8587
it('should not be equal to another preview info with a different port', () => {
86-
const a = new PreviewInfo(3000);
87-
const b = new PreviewInfo(4000);
88+
const a = new PreviewInfo({}, new PortInfo(3000));
89+
const b = new PreviewInfo({}, new PortInfo(4000));
8890

8991
expect(PreviewInfo.equals(a, b)).toBe(false);
9092
});
9193

9294
it('should not be equal to another preview info with a different title', () => {
93-
const a = new PreviewInfo([3000, 'Local server']);
94-
const b = new PreviewInfo([3000, 'Remote server']);
95+
const parsed = {
96+
a: PreviewInfo.parse([3000, 'Local server']),
97+
b: PreviewInfo.parse([3000, 'Remote server']),
98+
};
99+
100+
const a = new PreviewInfo(parsed.a, new PortInfo(parsed.a.port));
101+
const b = new PreviewInfo(parsed.b, new PortInfo(parsed.b.port));
95102

96103
expect(PreviewInfo.equals(a, b)).toBe(false);
97104
});
98105

99106
it('should not be equal to another preview info with a different pathname', () => {
100-
const a = new PreviewInfo(3000);
101-
const b = new PreviewInfo('3000/b');
102-
const c = new PreviewInfo('3000/c');
107+
const parsed = {
108+
a: PreviewInfo.parse(3000),
109+
b: PreviewInfo.parse('3000/b'),
110+
c: PreviewInfo.parse('3000/c'),
111+
};
112+
113+
const a = new PreviewInfo(parsed.a, new PortInfo(parsed.a.port));
114+
const b = new PreviewInfo(parsed.b, new PortInfo(parsed.b.port));
115+
const c = new PreviewInfo(parsed.c, new PortInfo(parsed.c.port));
103116

104117
expect(PreviewInfo.equals(a, b)).toBe(false);
105118
expect(PreviewInfo.equals(b, c)).toBe(false);

packages/runtime/src/webcontainer/preview-info.ts

Lines changed: 28 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,7 @@ import type { PreviewSchema } from '@tutorialkit/types';
22
import { PortInfo } from './port-info.js';
33

44
export class PreviewInfo {
5-
portInfo: PortInfo;
6-
5+
readonly portInfo: PortInfo;
76
title?: string;
87
pathname?: string;
98

@@ -27,29 +26,41 @@ export class PreviewInfo {
2726
return this.portInfo.ready;
2827
}
2928

30-
constructor(preview: PortInfo | Exclude<PreviewSchema, boolean>[0], ready?: boolean) {
31-
if (preview instanceof PortInfo) {
32-
this.portInfo = preview;
33-
} else if (typeof preview === 'number') {
34-
this.portInfo = new PortInfo(preview);
29+
constructor(preview: Omit<Preview, 'port'>, portInfo: PortInfo) {
30+
this.title = preview.title;
31+
this.pathname = preview.pathname;
32+
this.portInfo = portInfo;
33+
}
34+
35+
static parse(preview: Exclude<PreviewSchema, boolean>[0]): Preview {
36+
if (typeof preview === 'number') {
37+
return {
38+
port: preview,
39+
};
3540
} else if (typeof preview === 'string') {
3641
const [port, ...rest] = preview.split('/');
37-
this.portInfo = new PortInfo(parseInt(port));
38-
this.pathname = rest.join('/');
42+
return {
43+
port: parseInt(port),
44+
pathname: rest.join('/'),
45+
};
3946
} else if (Array.isArray(preview)) {
40-
this.portInfo = new PortInfo(preview[0]);
41-
this.title = preview[1];
42-
this.pathname = preview[2];
47+
return {
48+
port: preview[0],
49+
title: preview[1],
50+
pathname: preview[2],
51+
};
4352
} else {
44-
this.portInfo = new PortInfo(preview.port);
45-
this.title = preview.title;
46-
this.pathname = preview.pathname;
53+
return preview;
4754
}
48-
49-
this.portInfo.ready ||= !!ready;
5055
}
5156

5257
static equals(a: PreviewInfo, b: PreviewInfo) {
5358
return a.portInfo.port === b.portInfo.port && a.pathname === b.pathname && a.title === b.title;
5459
}
5560
}
61+
62+
interface Preview {
63+
port: number;
64+
pathname?: string;
65+
title?: string;
66+
}

0 commit comments

Comments
 (0)