@@ -67,6 +67,9 @@ const WebviewPlotInactiveTimeout = 120_000;
67
67
/** Interval in milliseconds at which inactive webview plots are checked. */
68
68
const WebviewPlotInactiveInterval = 1_000 ;
69
69
70
+ /** The key used to store the cached plot thumbnail descriptors */
71
+ const CachedPlotThumbnailDescriptorsKey = 'positron.plots.cachedPlotThumbnailDescriptors' ;
72
+
70
73
/** The key used to store the preferred history policy */
71
74
const HistoryPolicyStorageKey = 'positron.plots.historyPolicy' ;
72
75
@@ -86,12 +89,23 @@ interface DataUri {
86
89
}
87
90
88
91
/**
89
- * PositronPlotsService class .
92
+ * ICachedPlotThumbnailDescriptor interface .
90
93
*/
94
+ interface ICachedPlotThumbnailDescriptor {
95
+ readonly plotClientId : string ;
96
+ readonly thumbnailURI : string ;
97
+ }
98
+
99
+ /**
100
+ * PositronPlotsService class.
101
+ */
91
102
export class PositronPlotsService extends Disposable implements IPositronPlotsService {
92
103
/** Needed for service branding in dependency injector. */
93
104
declare readonly _serviceBrand : undefined ;
94
105
106
+ /** The map of cached plot thumbnail descriptors. */
107
+ private readonly _cachedPlotThumbnailDescriptors = new Map < string , ICachedPlotThumbnailDescriptor > ( ) ;
108
+
95
109
/** The list of Positron plots. */
96
110
private readonly _plots : IPositronPlotClient [ ] = [ ] ;
97
111
@@ -224,8 +238,7 @@ export class PositronPlotsService extends Disposable implements IPositronPlotsSe
224
238
await this . registerWebviewPlotClient ( plotClient ) ;
225
239
} ) ) ;
226
240
227
- // When the storage service is about to save state, store the current history policy
228
- // and storage policy in the workspace storage.
241
+ // When the storage service is about to save state, store policies and cached plot thumbnail descriptors.
229
242
this . _register ( this . _storageService . onWillSaveState ( ( ) => {
230
243
this . _storageService . store (
231
244
HistoryPolicyStorageKey ,
@@ -254,6 +267,45 @@ export class PositronPlotsService extends Disposable implements IPositronPlotsSe
254
267
StorageScope . WORKSPACE ,
255
268
StorageTarget . MACHINE ) ;
256
269
}
270
+
271
+ // Enumerate the plot clients and update the cached plot thumbnail descriptors.
272
+ const keysToDelete : Set < string > = new Set ( this . _cachedPlotThumbnailDescriptors . keys ( ) ) ;
273
+ this . _plots . forEach ( plotClient => {
274
+ keysToDelete . delete ( plotClient . id ) ;
275
+ if ( plotClient instanceof PlotClientInstance ) {
276
+ if ( plotClient . lastRender ?. uri ) {
277
+ this . _cachedPlotThumbnailDescriptors . set ( plotClient . id , {
278
+ plotClientId : plotClient . id ,
279
+ thumbnailURI : plotClient . lastRender . uri
280
+ } ) ;
281
+ }
282
+ } else if ( plotClient instanceof HtmlPlotClient ) {
283
+ if ( plotClient . thumbnailUri ) {
284
+ this . _cachedPlotThumbnailDescriptors . set ( plotClient . id , {
285
+ plotClientId : plotClient . id ,
286
+ thumbnailURI : plotClient . thumbnailUri
287
+ } ) ;
288
+ }
289
+ }
290
+ } ) ;
291
+
292
+ // Delete any cached plot thumbnail descriptors that are no longer valid.
293
+ keysToDelete . forEach ( key => this . _cachedPlotThumbnailDescriptors . delete ( key ) ) ;
294
+
295
+ // Update the cached plot thumbnail descriptors in workspace storage.
296
+ if ( this . _cachedPlotThumbnailDescriptors . size ) {
297
+ this . _storageService . store (
298
+ CachedPlotThumbnailDescriptorsKey ,
299
+ JSON . stringify ( [ ...this . _cachedPlotThumbnailDescriptors . values ( ) ] ) ,
300
+ StorageScope . WORKSPACE ,
301
+ StorageTarget . MACHINE ) ;
302
+ } else {
303
+ this . _storageService . store (
304
+ CachedPlotThumbnailDescriptorsKey ,
305
+ undefined ,
306
+ StorageScope . WORKSPACE ,
307
+ StorageTarget . MACHINE ) ;
308
+ }
257
309
} ) ) ;
258
310
259
311
// Listen for changes to the dark mode configuration
@@ -331,6 +383,22 @@ export class PositronPlotsService extends Disposable implements IPositronPlotsSe
331
383
this . _selectedHistoryPolicy = preferredHistoryPolicy as HistoryPolicy ;
332
384
}
333
385
386
+ // Load the cached plot thumbnail descriptors from workspace storage.
387
+ const cachedPlotThumbnailDescriptorsJSON = this . _storageService . get ( CachedPlotThumbnailDescriptorsKey , StorageScope . WORKSPACE ) ;
388
+ if ( cachedPlotThumbnailDescriptorsJSON ) {
389
+ try {
390
+ // Parse the cached plot thumbnail descriptors.
391
+ const cachedPlotThumbnailDescriptors = JSON . parse ( cachedPlotThumbnailDescriptorsJSON ) as ICachedPlotThumbnailDescriptor [ ] ;
392
+
393
+ // Initialize the cached plot thumbnail descriptors.
394
+ for ( const cachedPlotThumbnailDescriptor of cachedPlotThumbnailDescriptors ) {
395
+ this . _cachedPlotThumbnailDescriptors . set ( cachedPlotThumbnailDescriptor . plotClientId , cachedPlotThumbnailDescriptor ) ;
396
+ }
397
+ } catch ( error ) {
398
+ this . _logService . error ( `Error parsing cached plot thumbnail descriptors: ${ error } ` ) ;
399
+ }
400
+ }
401
+
334
402
// When a plot is selected, update its last selected time.
335
403
this . _register ( this . _onDidSelectPlot . event ( async ( id ) => {
336
404
this . _lastSelectedTimeByPlotId . set ( id , Date . now ( ) ) ;
@@ -427,6 +495,15 @@ export class PositronPlotsService extends Disposable implements IPositronPlotsSe
427
495
return this . _selectedDarkFilterMode ;
428
496
}
429
497
498
+ /**
499
+ * Gets the cached plot thumbnail URI for a given plot ID.
500
+ * @param plotId The plot ID to get the thumbnail URI for.
501
+ * @returns The thumbnail URI for the plot, or undefined if not found.
502
+ */
503
+ getCachedPlotThumbnailURI ( plotId : string ) {
504
+ return this . _cachedPlotThumbnailDescriptors . get ( plotId ) ?. thumbnailURI ;
505
+ }
506
+
430
507
/**
431
508
* Selects a new sizing policy and fires an event indicating that the policy
432
509
* has changed.
0 commit comments