-
Notifications
You must be signed in to change notification settings - Fork 46
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(vscode): add a command to reset Preview.js and clear cache
This addresses the VS Code implementation for #1488.
- Loading branch information
Showing
9 changed files
with
224 additions
and
116 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,33 +1,29 @@ | ||
import type { AnalyzeFileResponse, Client } from "@previewjs/daemon/client"; | ||
import type vscode from "vscode"; | ||
import type { createWorkspaceGetter } from "./workspaces"; | ||
import { WorkspaceGetter } from "./workspaces"; | ||
|
||
export function createComponentDetector( | ||
previewjsInitPromise: Promise<Client | null>, | ||
getWorkspaceId: ReturnType<typeof createWorkspaceGetter> | ||
) { | ||
async function getComponents( | ||
client: Client, | ||
getWorkspaceId: WorkspaceGetter | ||
): ComponentDetector { | ||
return async function ( | ||
document?: vscode.TextDocument | ||
): Promise<AnalyzeFileResponse["components"]> { | ||
if (!document || !document.fileName) { | ||
return []; | ||
} | ||
const previewjsClient = await previewjsInitPromise; | ||
if (!previewjsClient) { | ||
return []; | ||
} | ||
const workspaceId = await getWorkspaceId(previewjsClient, document); | ||
const workspaceId = await getWorkspaceId(document); | ||
if (!workspaceId) { | ||
return []; | ||
} | ||
const { components } = await previewjsClient.analyzeFile({ | ||
const { components } = await client.analyzeFile({ | ||
workspaceId, | ||
absoluteFilePath: document.fileName, | ||
}); | ||
return components; | ||
} | ||
|
||
return { | ||
getComponents, | ||
}; | ||
} | ||
|
||
export type ComponentDetector = ( | ||
document?: vscode.TextDocument | ||
) => Promise<AnalyzeFileResponse["components"]>; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,45 +1,38 @@ | ||
import type { Client } from "@previewjs/daemon/client"; | ||
import { exclusivePromiseRunner } from "exclusive-promises"; | ||
|
||
let currentPreview: | ||
| { | ||
workspaceId: string; | ||
url: string; | ||
} | ||
| undefined = undefined; | ||
import { PreviewJsState } from "./state"; | ||
|
||
const locking = exclusivePromiseRunner(); | ||
export async function ensurePreviewServerStarted( | ||
client: Client, | ||
state: PreviewJsState, | ||
workspaceId: string | ||
) { | ||
return locking(async () => { | ||
if (currentPreview?.workspaceId !== workspaceId) { | ||
if (currentPreview) { | ||
await client.stopPreview({ | ||
workspaceId: currentPreview.workspaceId, | ||
if (state.currentPreview?.workspaceId !== workspaceId) { | ||
if (state.currentPreview) { | ||
await state.client.stopPreview({ | ||
workspaceId: state.currentPreview.workspaceId, | ||
}); | ||
} | ||
const { url } = await client.startPreview({ | ||
const { url } = await state.client.startPreview({ | ||
workspaceId, | ||
}); | ||
currentPreview = { | ||
state.currentPreview = { | ||
workspaceId, | ||
url, | ||
}; | ||
} | ||
return currentPreview; | ||
return state.currentPreview; | ||
}); | ||
} | ||
|
||
export async function ensurePreviewServerStopped(client: Client) { | ||
export async function ensurePreviewServerStopped(state: PreviewJsState) { | ||
return locking(async () => { | ||
if (!currentPreview) { | ||
if (!state.currentPreview) { | ||
return; | ||
} | ||
await client.stopPreview({ | ||
workspaceId: currentPreview.workspaceId, | ||
await state.client.stopPreview({ | ||
workspaceId: state.currentPreview.workspaceId, | ||
}); | ||
currentPreview = undefined; | ||
state.currentPreview = null; | ||
}); | ||
} |
Oops, something went wrong.