Skip to content

Commit

Permalink
fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
fwouts committed Aug 20, 2023
1 parent 1e65632 commit 735f4c8
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 28 deletions.
2 changes: 2 additions & 0 deletions core/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -161,11 +161,13 @@ export async function createWorkspace({
frameworkPlugin,
logger,
middlewares,
port,
onFileChanged: (absoluteFilePath) => {
const filePath = path.relative(rootDir, absoluteFilePath);
frameworkPlugin.typeAnalyzer.invalidateCachedTypesForFile(filePath);
},
});
await previewer.start();
return {
url: () => `http://localhost:${port}`,
stop: async () => {
Expand Down
40 changes: 12 additions & 28 deletions core/src/previewer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ export class Previewer {
logger: Logger;
frameworkPlugin: FrameworkPlugin;
middlewares: express.RequestHandler[];
port: number;
onFileChanged?(absoluteFilePath: string): void;
}
) {
Expand All @@ -90,30 +91,23 @@ export class Previewer {
]);
}

async start(
allocatePort: () => Promise<number>,
options: { restarting?: boolean } = {}
) {
let port: number;
async start(options: { restarting?: boolean } = {}) {
const statusBeforeStart = this.status;
switch (statusBeforeStart.kind) {
case "starting":
port = statusBeforeStart.port;
try {
await statusBeforeStart.promise;
} catch (e) {
this.options.logger.error(e);
this.status = {
kind: "stopped",
};
await this.startFromStopped(port);
await this.startFromStopped();
}
break;
case "started":
port = statusBeforeStart.port;
break;
case "stopping":
port = statusBeforeStart.port;
try {
await statusBeforeStart.promise;
} catch (e) {
Expand All @@ -122,25 +116,21 @@ export class Previewer {
kind: "stopped",
};
}
await this.start(async () => port, options);
await this.start(options);
break;
case "stopped":
port = await allocatePort();
await this.startFromStopped(port, options);
await this.startFromStopped(options);
break;
default:
throw assertNever(statusBeforeStart);
}
return port;
}

private async startFromStopped(
port: number,
{ restarting }: { restarting?: boolean } = {}
) {
private async startFromStopped({
restarting,
}: { restarting?: boolean } = {}) {
this.status = {
kind: "starting",
port,
promise: (async () => {
// PostCSS requires the current directory to change because it relies
// on the `import-cwd` package to resolve plugins.
Expand Down Expand Up @@ -255,16 +245,15 @@ export class Previewer {
frameworkPlugin: this.options.frameworkPlugin,
});
this.options.logger.debug(`Starting server`);
const server = await this.appServer.start(port);
const server = await this.appServer.start(this.options.port);
this.options.logger.debug(`Starting Vite manager`);
this.viteManager.start(server, port).catch((e) => {
this.viteManager.start(server, this.options.port).catch((e) => {
this.options.logger.error(`Vite manager failed to start: ${e}`);
this.stop();
});
this.options.logger.debug(`Previewer ready`);
this.status = {
kind: "started",
port,
};
})(),
};
Expand All @@ -273,7 +262,7 @@ export class Previewer {
// doesn't accept connections right away.
for (let i = 0; ; i++) {
try {
await axios.get(`http://localhost:${port}`);
await axios.get(`http://localhost:${this.options.port}`);
break;
} catch (e) {
if (i === 10) {
Expand Down Expand Up @@ -304,7 +293,6 @@ export class Previewer {
}
this.status = {
kind: "stopping",
port: this.status.port,
promise: (async () => {
if (!restarting) {
this.transformingReader.listeners.remove(this.onFileChangeListener);
Expand Down Expand Up @@ -341,7 +329,6 @@ export class Previewer {
FILES_REQUIRING_RESTART.has(path.basename(absoluteFilePath))
) {
if (this.status.kind === "starting" || this.status.kind === "started") {
const port = this.status.port;
// Packages were updated. Restart.
this.options.logger.info(
"New dependencies were detected. Restarting..."
Expand All @@ -350,7 +337,7 @@ export class Previewer {
restarting: true,
})
.then(async () => {
await this.start(async () => port, { restarting: true });
await this.start({ restarting: true });
})
.catch(this.options.logger.error);
}
Expand All @@ -369,16 +356,13 @@ export class Previewer {
type PreviewerStatus =
| {
kind: "starting";
port: number;
promise: Promise<void>;
}
| {
kind: "started";
port: number;
}
| {
kind: "stopping";
port: number;
promise: Promise<void>;
}
| {
Expand Down

0 comments on commit 735f4c8

Please sign in to comment.