-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathMVMServer.ts
More file actions
114 lines (96 loc) · 4.5 KB
/
MVMServer.ts
File metadata and controls
114 lines (96 loc) · 4.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
// Copyright 2024-2026 The MathWorks, Inc.
import NotificationService, { Notification } from '../notifications/NotificationService'
import MVM, { IMVM, MatlabMVMConnectionState, EvalRequest, EvalResponse, FEvalRequest, FEvalResponse, BreakpointRequest, PromptState } from './impl/MVM'
/**
* Provides an interface for sending evals and fevals and listening to the results.
*/
export default class MVMServer {
private readonly _mvm: MVM;
private readonly _notificationService: typeof NotificationService;
constructor (mvm: MVM, notificationService: typeof NotificationService) {
this._mvm = mvm;
this._notificationService = notificationService;
this._setupListeners();
// Set up connection notification listeners
this._notificationService.registerNotificationListener(Notification.MVMEvalRequest, this._doEval.bind(this));
this._notificationService.registerNotificationListener(Notification.MVMFevalRequest, this._doFeval.bind(this));
this._notificationService.registerNotificationListener(Notification.MVMInterruptRequest, this._doInterrupt.bind(this));
this._notificationService.registerNotificationListener(Notification.MVMSetBreakpointRequest, this._doSetBreakpoint.bind(this));
this._notificationService.registerNotificationListener(Notification.MVMClearBreakpointRequest, this._doClearBreakpoint.bind(this));
this._notificationService.registerNotificationListener(Notification.MVMUnpauseRequest, this._doUnpause.bind(this));
}
private _setupListeners (): void {
this._mvm.on(IMVM.Events.stateChange, this._handleMvmStateChange.bind(this))
this._mvm.on(IMVM.Events.output, this._handleOutput.bind(this));
this._mvm.on(IMVM.Events.clc, this._handleClc.bind(this));
this._mvm.on(IMVM.Events.promptChange, this._handlePromptChange.bind(this));
}
private _handleMvmStateChange (state: MatlabMVMConnectionState, release?: string): void {
this._notificationService.sendNotification(Notification.MVMStateChange, { state, release });
}
private _doEval (data: EvalRequest): void {
const requestId = data.requestId;
if (requestId === undefined) {
return;
}
void this._mvm.eval(data.command, data.isUserEval, data.capabilitiesToRemove)?.then(() => {
this._notificationService.sendNotification(Notification.MVMEvalComplete, {
requestId
} as EvalResponse)
});
}
private _doFeval (data: FEvalRequest): void {
const requestId = data.requestId;
if (requestId === undefined) {
return;
}
void this._mvm.feval(data.functionName, data.nargout, data.args, data.isUserEval, data.capabilitiesToRemove)?.then((result: unknown) => {
this._notificationService.sendNotification(Notification.MVMFevalComplete, {
requestId,
result
} as FEvalResponse);
});
}
private _doSetBreakpoint (data: BreakpointRequest): void {
const requestId = data.requestId;
if (requestId === undefined) {
return;
}
void this._mvm.setBreakpoint(data.fileName, data.lineNumber, data.condition, data.anonymousIndex)?.then((result: unknown) => {
this._notificationService.sendNotification(Notification.MVMSetBreakpointComplete, {
requestId,
result
});
});
}
private _doClearBreakpoint (data: BreakpointRequest): void {
const requestId = data.requestId;
if (requestId === undefined) {
return;
}
void this._mvm.clearBreakpoint(data.fileName, data.lineNumber, data.condition, data.anonymousIndex)?.then((result: unknown) => {
this._notificationService.sendNotification(Notification.MVMClearBreakpointComplete, {
requestId,
result
});
});
}
private _doInterrupt (): void {
this._mvm.interrupt();
}
private _doUnpause (): void {
this._mvm.unpause();
}
private _handleOutput (data: unknown): void {
this._notificationService.sendNotification(Notification.MVMText, data)
}
private _handleClc (): void {
this._notificationService.sendNotification(Notification.MVMClc);
}
private _handlePromptChange (state: PromptState, isIdle: boolean): void {
this._notificationService.sendNotification(Notification.MVMPromptChange, {
state,
isIdle
});
}
}