|
14 | 14 | * SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-only WITH Classpath-exception-2.0
|
15 | 15 | ********************************************************************************/
|
16 | 16 |
|
17 |
| -import { inject, injectable } from 'inversify'; |
18 |
| -import { MessageService, UNTITLED_SCHEME, URI } from '../common'; |
| 17 | +import type { ApplicationShell } from './shell'; |
| 18 | +import { injectable } from 'inversify'; |
| 19 | +import { UNTITLED_SCHEME, URI, Disposable, DisposableCollection } from '../common'; |
19 | 20 | import { Navigatable, NavigatableWidget } from './navigatable-types';
|
20 |
| -import { Saveable, SaveableSource, SaveOptions } from './saveable'; |
| 21 | +import { AutoSaveMode, Saveable, SaveableSource, SaveOptions, SaveReason } from './saveable'; |
21 | 22 | import { Widget } from './widgets';
|
| 23 | +import { FrontendApplicationContribution } from './frontend-application-contribution'; |
| 24 | +import { FrontendApplication } from './frontend-application'; |
| 25 | +import throttle = require('lodash.throttle'); |
22 | 26 |
|
23 | 27 | @injectable()
|
24 |
| -export class SaveResourceService { |
25 |
| - @inject(MessageService) protected readonly messageService: MessageService; |
| 28 | +export class SaveResourceService implements FrontendApplicationContribution { |
| 29 | + |
| 30 | + protected saveThrottles = new Map<Saveable, AutoSaveThrottle>(); |
| 31 | + protected saveMode: AutoSaveMode = 'off'; |
| 32 | + protected saveDelay = 1000; |
| 33 | + protected shell: ApplicationShell; |
| 34 | + |
| 35 | + get autoSave(): AutoSaveMode { |
| 36 | + return this.saveMode; |
| 37 | + } |
| 38 | + |
| 39 | + set autoSave(value: AutoSaveMode) { |
| 40 | + this.updateAutoSaveMode(value); |
| 41 | + } |
| 42 | + |
| 43 | + get autoSaveDelay(): number { |
| 44 | + return this.saveDelay; |
| 45 | + } |
| 46 | + |
| 47 | + set autoSaveDelay(value: number) { |
| 48 | + this.updateAutoSaveDelay(value); |
| 49 | + } |
| 50 | + |
| 51 | + onDidInitializeLayout(app: FrontendApplication): void { |
| 52 | + this.shell = app.shell; |
| 53 | + // Register restored editors first |
| 54 | + for (const widget of this.shell.widgets) { |
| 55 | + const saveable = Saveable.get(widget); |
| 56 | + if (saveable) { |
| 57 | + this.registerSaveable(widget, saveable); |
| 58 | + } |
| 59 | + } |
| 60 | + this.shell.onDidAddWidget(e => { |
| 61 | + const saveable = Saveable.get(e); |
| 62 | + if (saveable) { |
| 63 | + this.registerSaveable(e, saveable); |
| 64 | + } |
| 65 | + }); |
| 66 | + this.shell.onDidChangeCurrentWidget(e => { |
| 67 | + if (this.saveMode === 'onFocusChange') { |
| 68 | + const widget = e.oldValue; |
| 69 | + const saveable = Saveable.get(widget); |
| 70 | + if (saveable && widget && this.shouldAutoSave(widget, saveable)) { |
| 71 | + saveable.save({ |
| 72 | + saveReason: SaveReason.FocusChange |
| 73 | + }); |
| 74 | + } |
| 75 | + } |
| 76 | + }); |
| 77 | + this.shell.onDidRemoveWidget(e => { |
| 78 | + const saveable = Saveable.get(e); |
| 79 | + if (saveable) { |
| 80 | + this.saveThrottles.get(saveable)?.dispose(); |
| 81 | + this.saveThrottles.delete(saveable); |
| 82 | + } |
| 83 | + }); |
| 84 | + } |
| 85 | + |
| 86 | + protected updateAutoSaveMode(mode: AutoSaveMode): void { |
| 87 | + this.saveMode = mode; |
| 88 | + for (const saveThrottle of this.saveThrottles.values()) { |
| 89 | + saveThrottle.autoSave = mode; |
| 90 | + } |
| 91 | + if (mode === 'onFocusChange') { |
| 92 | + // If the new mode is onFocusChange, we need to save all dirty documents that are not focused |
| 93 | + const widgets = this.shell.widgets; |
| 94 | + for (const widget of widgets) { |
| 95 | + const saveable = Saveable.get(widget); |
| 96 | + if (saveable && widget !== this.shell.currentWidget && this.shouldAutoSave(widget, saveable)) { |
| 97 | + saveable.save({ |
| 98 | + saveReason: SaveReason.FocusChange |
| 99 | + }); |
| 100 | + } |
| 101 | + } |
| 102 | + } |
| 103 | + } |
| 104 | + |
| 105 | + protected updateAutoSaveDelay(delay: number): void { |
| 106 | + this.saveDelay = delay; |
| 107 | + for (const saveThrottle of this.saveThrottles.values()) { |
| 108 | + saveThrottle.autoSaveDelay = delay; |
| 109 | + } |
| 110 | + } |
| 111 | + |
| 112 | + registerSaveable(widget: Widget, saveable: Saveable): Disposable { |
| 113 | + const saveThrottle = new AutoSaveThrottle( |
| 114 | + saveable, |
| 115 | + () => { |
| 116 | + if (this.saveMode === 'afterDelay' && this.shouldAutoSave(widget, saveable)) { |
| 117 | + saveable.save({ |
| 118 | + saveReason: SaveReason.AfterDelay |
| 119 | + }); |
| 120 | + } |
| 121 | + }, |
| 122 | + this.addBlurListener(widget, saveable) |
| 123 | + ); |
| 124 | + saveThrottle.autoSave = this.saveMode; |
| 125 | + saveThrottle.autoSaveDelay = this.saveDelay; |
| 126 | + this.saveThrottles.set(saveable, saveThrottle); |
| 127 | + return saveThrottle; |
| 128 | + } |
| 129 | + |
| 130 | + protected addBlurListener(widget: Widget, saveable: Saveable): Disposable { |
| 131 | + const document = widget.node.ownerDocument; |
| 132 | + const listener = (() => { |
| 133 | + if (this.saveMode === 'onWindowChange' && !this.windowHasFocus(document) && this.shouldAutoSave(widget, saveable)) { |
| 134 | + saveable.save({ |
| 135 | + saveReason: SaveReason.FocusChange |
| 136 | + }); |
| 137 | + } |
| 138 | + }).bind(this); |
| 139 | + document.addEventListener('blur', listener); |
| 140 | + return Disposable.create(() => { |
| 141 | + document.removeEventListener('blur', listener); |
| 142 | + }); |
| 143 | + } |
| 144 | + |
| 145 | + protected windowHasFocus(document: Document): boolean { |
| 146 | + if (document.visibilityState === 'hidden') { |
| 147 | + return false; |
| 148 | + } else if (document.hasFocus()) { |
| 149 | + return true; |
| 150 | + } |
| 151 | + // TODO: Add support for iframes |
| 152 | + return false; |
| 153 | + } |
| 154 | + |
| 155 | + protected shouldAutoSave(widget: Widget, saveable: Saveable): boolean { |
| 156 | + const uri = NavigatableWidget.getUri(widget); |
| 157 | + if (uri?.scheme === UNTITLED_SCHEME) { |
| 158 | + // Never auto-save untitled documents |
| 159 | + return false; |
| 160 | + } else { |
| 161 | + return saveable.dirty; |
| 162 | + } |
| 163 | + } |
26 | 164 |
|
27 | 165 | /**
|
28 | 166 | * Indicate if the document can be saved ('Save' command should be disable if not).
|
@@ -58,3 +196,66 @@ export class SaveResourceService {
|
58 | 196 | return Promise.reject('Unsupported: The base SaveResourceService does not support saveAs action.');
|
59 | 197 | }
|
60 | 198 | }
|
| 199 | + |
| 200 | +export class AutoSaveThrottle implements Disposable { |
| 201 | + |
| 202 | + private _saveable: Saveable; |
| 203 | + private _cb: () => void; |
| 204 | + private _disposable: DisposableCollection; |
| 205 | + private _throttle?: ReturnType<typeof throttle>; |
| 206 | + private _mode: AutoSaveMode = 'off'; |
| 207 | + private _autoSaveDelay = 1000; |
| 208 | + |
| 209 | + get autoSave(): AutoSaveMode { |
| 210 | + return this._mode; |
| 211 | + } |
| 212 | + |
| 213 | + set autoSave(value: AutoSaveMode) { |
| 214 | + this._mode = value; |
| 215 | + this.throttledSave(); |
| 216 | + } |
| 217 | + |
| 218 | + get autoSaveDelay(): number { |
| 219 | + return this._autoSaveDelay; |
| 220 | + } |
| 221 | + |
| 222 | + set autoSaveDelay(value: number) { |
| 223 | + this._autoSaveDelay = value; |
| 224 | + // Explicitly delete the throttle to recreate it with the new delay |
| 225 | + this._throttle?.cancel(); |
| 226 | + this._throttle = undefined; |
| 227 | + this.throttledSave(); |
| 228 | + } |
| 229 | + |
| 230 | + constructor(saveable: Saveable, cb: () => void, ...disposables: Disposable[]) { |
| 231 | + this._cb = cb; |
| 232 | + this._saveable = saveable; |
| 233 | + this._disposable = new DisposableCollection( |
| 234 | + ...disposables, |
| 235 | + saveable.onContentChanged(() => { |
| 236 | + this.throttledSave(); |
| 237 | + }), |
| 238 | + saveable.onDirtyChanged(() => { |
| 239 | + this.throttledSave(); |
| 240 | + }) |
| 241 | + ); |
| 242 | + } |
| 243 | + |
| 244 | + protected throttledSave(): void { |
| 245 | + this._throttle?.cancel(); |
| 246 | + if (this._mode === 'afterDelay' && this._saveable.dirty) { |
| 247 | + if (!this._throttle) { |
| 248 | + this._throttle = throttle(() => this._cb(), this._autoSaveDelay, { |
| 249 | + leading: false, |
| 250 | + trailing: true |
| 251 | + }); |
| 252 | + } |
| 253 | + this._throttle(); |
| 254 | + } |
| 255 | + } |
| 256 | + |
| 257 | + dispose(): void { |
| 258 | + this._disposable.dispose(); |
| 259 | + } |
| 260 | + |
| 261 | +} |
0 commit comments