-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathGraphicsPrewarmService.ts
More file actions
88 lines (77 loc) · 3.1 KB
/
GraphicsPrewarmService.ts
File metadata and controls
88 lines (77 loc) · 3.1 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
// Copyright 2025-2026 The MathWorks, Inc.
import Logger from '../logging/Logger';
import MVM, { IMVM, MatlabMVMConnectionState } from '../mvm/impl/MVM';
import { ConfigurationManager } from './ConfigurationManager';
/**
* Handles prewarming MATLAB graphics to boost the performance of the first figure window when
* connecting to MATLAB R2025a and later.
*/
export default class GraphicsPrewarmService {
hasPrewarmed = false
constructor (private readonly mvm: MVM, private readonly configurationManager: ConfigurationManager) {
// Listen to MATLAB state changes to trigger prewarm when ready
this.mvm.on(IMVM.Events.stateChange, state => this.handleMvmStateChange(state))
// Listen to changes to the prewarmGraphics configuration
this.configurationManager.addSettingCallback(
'prewarmGraphics',
configuration => this.handleSettingChanged(configuration.prewarmGraphics)
)
}
/**
* Reacts to changes in the MVM state.
*
* @param state The MVM state
*/
private async handleMvmStateChange (state: MatlabMVMConnectionState): Promise<void> {
if (state === MatlabMVMConnectionState.CONNECTED) {
await this.handleMatlabConnectionReady()
} else if (state === MatlabMVMConnectionState.DISCONNECTED) {
// Reset hasPrewarmed flag when MATLAB is disconnected
this.hasPrewarmed = false
}
}
/**
* Reacts to the MATLAB connection becoming ready. If the graphics prewarm is
* enabled and has not yet been performed, this will trigger the graphics prewarm.
*/
private async handleMatlabConnectionReady (): Promise<void> {
if (this.hasPrewarmed) {
// Return early if already prewarmed
return
}
const prewarmGraphics = (await this.configurationManager.getConfiguration()).prewarmGraphics
if (prewarmGraphics) {
this.prewarmGraphics()
}
}
/**
* Reacts to changes in the `prewarmGraphics` setting.
*
* @param newValue The new value of the `prewarmGraphics` setting
*/
private handleSettingChanged (newValue: boolean): void {
if (newValue && !this.hasPrewarmed) {
this.prewarmGraphics()
}
}
/**
* For MATLAB R2025a and later, prewarms the graphics by loading the MATLAB desktop in the background.
* This boosts the rendering performance for the first figure window by doing this work early.
*
* For MATLAB releases earlier than R2025a, this is a no-op.
*/
private prewarmGraphics (): void {
const matlabRelease = this.mvm.getMatlabRelease()
if (matlabRelease == null || matlabRelease === '') {
// Not currently connected to MATLAB or unable
// to determine release number - no-op
return
}
if (matlabRelease >= 'R2025a') {
// This is only needed in R2025a and later
this.mvm.feval('matlab.desktop.internal.webdesktop', 0, ['-hidden'])
Logger.log('Prewarming graphics')
}
this.hasPrewarmed = true
}
}