Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions modules/jaspr-code/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,16 @@
"command": "jaspr.doctor",
"title": "Doctor",
"category": "Jaspr"
},
{
"command": "jaspr.hotReload",
"title": "Hot Reload",
"category": "Jaspr"
},
{
"command": "jaspr.hotRestart",
"title": "Hot Restart",
"category": "Jaspr"
}
],
"menus": {
Expand All @@ -77,9 +87,29 @@
},
{
"command": "jaspr.doctor"
},
{
"command": "jaspr.hotReload",
"when": "jaspr:isRunning"
},
{
"command": "jaspr.hotRestart",
"when": "jaspr:isRunning"
}
]
},
"keybindings": [
{
"command": "jaspr.hotReload",
"key": "r",
"when": "inDebugMode && jaspr:isRunning && !editorTextFocus"
},
{
"command": "jaspr.hotRestart",
"key": "shift+r",
"when": "inDebugMode && jaspr:isRunning && !editorTextFocus"
Comment on lines +105 to +110

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should these conditions be inputFocus instead of editorTextFocus, so they don't trigger in other inputs like "Find"?

Suggested change
"when": "inDebugMode && jaspr:isRunning && !editorTextFocus"
},
{
"command": "jaspr.hotRestart",
"key": "shift+r",
"when": "inDebugMode && jaspr:isRunning && !editorTextFocus"
"when": "inDebugMode && jaspr:isRunning && !inputFocus"
},
{
"command": "jaspr.hotRestart",
"key": "shift+r",
"when": "inDebugMode && jaspr:isRunning && !inputFocus"

}
],
"debuggers": [
{
"type": "jaspr",
Expand Down
35 changes: 35 additions & 0 deletions modules/jaspr-code/src/debug.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,3 +110,38 @@ export class DynamicDebugConfigProvider
return results;
}
}

export class JasprDebugAdapterTrackerFactory
implements vscode.DebugAdapterTrackerFactory
{
createDebugAdapterTracker(
session: vscode.DebugSession
): vscode.ProviderResult<vscode.DebugAdapterTracker> {
const processId = session.configuration._processId;
if (!processId) {
return undefined;
}
const daemon = JasprServeDaemon.getDaemonByProcessId(processId);
if (!daemon) {
return undefined;
}

return {
onWillReceiveMessage(message: any) {
if (message?.type === "request") {
if (message.command === "hotReload") {
daemon.reload();
Comment on lines +132 to +133

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This does seem accurate.

} else if (
message.command === "restart" ||
message.command === "hotRestart" ||
((message.command === "disconnect" ||
message.command === "terminate") &&
Boolean(message.arguments?.restart))
) {
daemon.restart();
}
}
},
};
}
}
31 changes: 31 additions & 0 deletions modules/jaspr-code/src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@ import * as vscode from "vscode";
import {
DynamicDebugConfigProvider,
InitialLaunchJsonDebugConfigProvider,
JasprDebugAdapterTrackerFactory,
JasprDebugConfigurationProvider,
} from "./debug";
import { JasprServeDaemon } from "./jaspr/serve_daemon";

import { createJasprProject, handleNewProjects } from "./create";
import { jasprClean, jasprDoctor, jasprServe } from "./commands";
Expand Down Expand Up @@ -61,6 +63,35 @@ export async function activate(context: vscode.ExtensionContext) {
vscode.commands.registerCommand("jaspr.doctor", jasprDoctor)
);

context.subscriptions.push(
vscode.debug.registerDebugAdapterTrackerFactory(
"dart",
new JasprDebugAdapterTrackerFactory()
)
);

context.subscriptions.push(
vscode.commands.registerCommand("jaspr.hotReload", async () => {
const daemon = JasprServeDaemon.getActiveDaemon();
if (daemon) {
daemon.reload();
} else {
await vscode.commands.executeCommand("dart.hotReload");
}
})
);

context.subscriptions.push(
vscode.commands.registerCommand("jaspr.hotRestart", async () => {
const daemon = JasprServeDaemon.getActiveDaemon();
if (daemon) {
daemon.restart();
} else {
await vscode.commands.executeCommand("workbench.action.debug.restart");
}
})
);

context.subscriptions.push(
vscode.commands.registerCommand("jaspr.serve", jasprServe(context))
);
Expand Down
97 changes: 97 additions & 0 deletions modules/jaspr-code/src/jaspr/serve_daemon.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,40 @@ export class JasprServeDaemon implements vscode.Disposable {
private randomId: number = 0;
private runName: string = "";

private static _activeDaemons: Map<number, JasprServeDaemon> = new Map();
public static activeDaemon: JasprServeDaemon | undefined;

public static getDaemonByProcessId(
processId: number
): JasprServeDaemon | undefined {
return this._activeDaemons.get(processId);
}

public static getActiveDaemon(): JasprServeDaemon | undefined {
if (this.activeDaemon && this.activeDaemon.status === "running") {
return this.activeDaemon;
}
const activeSession = vscode.debug.activeDebugSession;
if (activeSession?.configuration?._processId) {
const daemon = this.getDaemonByProcessId(
activeSession.configuration._processId
);
if (daemon && daemon.status === "running") {
return daemon;
}
}
Comment on lines +45 to +56

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems valid.

for (const daemon of this._activeDaemons.values()) {
if (daemon.status === "running") {
return daemon;
}
}
return undefined;
}

private nextRequestId: number = 1;
private lastReloadTime: number = 0;
private lastRestartTime: number = 0;

async start(
context: vscode.ExtensionContext,
folder?: vscode.WorkspaceFolder,
Expand All @@ -46,6 +80,10 @@ export class JasprServeDaemon implements vscode.Disposable {
this.randomId = Math.floor(Math.random() * 1000000);
this.runName = debugConfiguration?.name ?? "Jaspr";

JasprServeDaemon.activeDaemon = this;
JasprServeDaemon._activeDaemons.set(this.randomId, this);
void vscode.commands.executeCommand("setContext", "jaspr:isRunning", true);

this.statusBarItem = await vscode.window.createStatusBarItem(
vscode.StatusBarAlignment.Left,
2
Expand Down Expand Up @@ -114,6 +152,15 @@ export class JasprServeDaemon implements vscode.Disposable {
}
})
);

this._disposables.push(
vscode.debug.onDidTerminateDebugSession((session) => {
const index = this.sessions.indexOf(session);
if (index !== -1) {
this.sessions.splice(index, 1);
}
})
);
}

async _startTerminal(context: vscode.ExtensionContext) {
Expand All @@ -137,6 +184,15 @@ export class JasprServeDaemon implements vscode.Disposable {
if (data.length === 1 && data.charCodeAt(0) === 3) {
// Ctrl+C
this.stop();
return;
}

if (data === "r") {
this.reload();
} else if (data === "R") {
this.restart();
} else if (data === "q") {
this.stop();
}
},
open: () => resolve(terminal),
Expand Down Expand Up @@ -198,7 +254,41 @@ export class JasprServeDaemon implements vscode.Disposable {
});
}

public reload(): void {
const now = Date.now();
if (now - this.lastReloadTime < 250) {
return;
}
this.lastReloadTime = now;
this.sendRequest("client.restart", { fullRestart: false });

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems valid. Would be unfortunate to drop support for earlier versions already though. Perhaps can use and check the protocolVersion from the CLI's DaemonDomain?

}

public restart(): void {
const now = Date.now();
if (now - this.lastRestartTime < 250) {
return;
}
this.lastRestartTime = now;
this.sendRequest("client.restart", { fullRestart: true });
}

public sendRequest(method: string, params?: any): void {
if (this.status !== "running" || !this.process) {
return;
}
Comment on lines +276 to +278
const id = `${++this.nextRequestId}`;
const request = [{ method, params, id }];
this.process.stdin.write(JSON.stringify(request) + "\n");
}

async stop(): Promise<void> {
if (JasprServeDaemon.activeDaemon === this) {
JasprServeDaemon.activeDaemon = undefined;
}
JasprServeDaemon._activeDaemons.delete(this.randomId);
if (JasprServeDaemon._activeDaemons.size === 0) {
void vscode.commands.executeCommand("setContext", "jaspr:isRunning", false);
}
this.process?.stdin.write('[{"method":"daemon.shutdown", "id": "0"}]\n');
this.statusBarCloseItem?.hide();
const sess = this.sessions;
Expand All @@ -210,6 +300,13 @@ export class JasprServeDaemon implements vscode.Disposable {
}

async dispose() {
if (JasprServeDaemon.activeDaemon === this) {
JasprServeDaemon.activeDaemon = undefined;
}
JasprServeDaemon._activeDaemons.delete(this.randomId);
if (JasprServeDaemon._activeDaemons.size === 0) {
void vscode.commands.executeCommand("setContext", "jaspr:isRunning", false);
}
this.stop();

this.statusBarItem?.dispose();
Expand Down
7 changes: 6 additions & 1 deletion packages/jaspr_cli/lib/src/commands/daemon_command.dart
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,11 @@ class DaemonCommand extends DevCommand with DaemonHelper {

@override
void handleDevWorkflow(DevWorkflow workflow) {
daemon.registerDomain(ClientDomain(daemon, workflow));
daemon.registerDomain(ClientDomain(
daemon,
workflow,
onReload: triggerReload,
onRestart: triggerRestart,
));
}
}
56 changes: 34 additions & 22 deletions packages/jaspr_cli/lib/src/commands/dev_command.dart
Original file line number Diff line number Diff line change
Expand Up @@ -495,32 +495,44 @@ abstract class DevCommand extends BaseCommand with ProxyHelper {
}
}

Future<void> triggerReload() async {
if (!canHotReload) {
await triggerRestart();
return;
}
if (workflow.isBuilding) {
if (workflow.queuedAction == QueuedAction.restart) {
logger.write('Build in progress. Restart already queued.', tag: Tag.client);
} else if (workflow.queuedAction == QueuedAction.reload) {
logger.write('Build in progress. Reload already queued.', tag: Tag.client);
} else {
workflow.queueAction(QueuedAction.reload);
logger.write('Build in progress. Queued reload...', tag: Tag.client);
}
} else {
await _performReload();
}
}

Future<void> triggerRestart() async {
if (workflow.isBuilding) {
if (workflow.queuedAction == QueuedAction.restart) {
logger.write('Build in progress. Restart already queued.', tag: Tag.client);
} else {
workflow.queueAction(QueuedAction.restart);
logger.write('Build in progress. Queued restart...', tag: Tag.client);
}
} else {
await _performRestart();
}
}

Future<void> _handleKey(String key) async {
switch (key) {
case 'r' when canHotReload:
if (workflow.isBuilding) {
if (workflow.queuedAction == QueuedAction.restart) {
logger.write('Build in progress. Restart already queued.', tag: Tag.client);
} else if (workflow.queuedAction == QueuedAction.reload) {
logger.write('Build in progress. Reload already queued.', tag: Tag.client);
} else {
workflow.queueAction(QueuedAction.reload);
logger.write('Build in progress. Queued reload...', tag: Tag.client);
}
} else {
await _performReload();
}
await triggerReload();
case 'R':
if (workflow.isBuilding) {
if (workflow.queuedAction == QueuedAction.restart) {
logger.write('Build in progress. Restart already queued.', tag: Tag.client);
} else {
workflow.queueAction(QueuedAction.restart);
logger.write('Build in progress. Queued restart...', tag: Tag.client);
}
} else {
await _performRestart();
}
await triggerRestart();
case 'l':
await workflow.chrome.launch(port);
case 'd':
Expand Down
Loading
Loading