forked from jupyterlab/jupyterlab
/
savehandler.ts
172 lines (153 loc) · 3.93 KB
/
savehandler.ts
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
// Copyright (c) Jupyter Development Team.
// Distributed under the terms of the Modified BSD License.
import { IDisposable } from '@phosphor/disposable';
import { Signal } from '@phosphor/signaling';
import { DocumentRegistry } from '@jupyterlab/docregistry';
/**
* A class that manages the auto saving of a document.
*
* #### Notes
* Implements https://github.com/ipython/ipython/wiki/IPEP-15:-Autosaving-the-IPython-Notebook.
*/
export class SaveHandler implements IDisposable {
/**
* Construct a new save handler.
*/
constructor(options: SaveHandler.IOptions) {
this._context = options.context;
let interval = options.saveInterval || 120;
this._minInterval = interval * 1000;
this._interval = this._minInterval;
// Restart the timer when the contents model is updated.
this._context.fileChanged.connect(this._setTimer, this);
this._context.disposed.connect(this.dispose, this);
}
/**
* The save interval used by the timer (in seconds).
*/
get saveInterval(): number {
return this._interval / 1000;
}
set saveInterval(value: number) {
this._minInterval = this._interval = value * 1000;
if (this._isActive) {
this._setTimer();
}
}
/**
* Get whether the handler is active.
*/
get isActive(): boolean {
return this._isActive;
}
/**
* Get whether the save handler is disposed.
*/
get isDisposed(): boolean {
return this._isDisposed;
}
/**
* Dispose of the resources used by the save handler.
*/
dispose(): void {
if (this.isDisposed) {
return;
}
this._isDisposed = true;
clearTimeout(this._autosaveTimer);
Signal.clearData(this);
}
/**
* Start the autosaver.
*/
start(): void {
this._isActive = true;
this._setTimer();
}
/**
* Stop the autosaver.
*/
stop(): void {
this._isActive = false;
clearTimeout(this._autosaveTimer);
}
/**
* Set the timer.
*/
private _setTimer(): void {
clearTimeout(this._autosaveTimer);
if (!this._isActive) {
return;
}
this._autosaveTimer = window.setTimeout(() => {
this._save();
}, this._interval);
}
/**
* Handle an autosave timeout.
*/
private _save(): void {
let context = this._context;
// Trigger the next update.
this._setTimer();
if (!context) {
return;
}
// Bail if the model is not dirty or the file is not writable, or the dialog
// is already showing.
let writable = context.contentsModel && context.contentsModel.writable;
if (!writable || !context.model.dirty || this._inDialog) {
return;
}
let start = new Date().getTime();
context
.save()
.then(() => {
if (this.isDisposed) {
return;
}
let duration = new Date().getTime() - start;
// New save interval: higher of 10x save duration or min interval.
this._interval = Math.max(
this._multiplier * duration,
this._minInterval
);
// Restart the update to pick up the new interval.
this._setTimer();
})
.catch(err => {
// If the user canceled the save, do nothing.
if (err.message === 'Cancel') {
return;
}
// Otherwise, log the error.
console.error('Error in Auto-Save', err.message);
});
}
private _autosaveTimer = -1;
private _minInterval = -1;
private _interval = -1;
private _context: DocumentRegistry.Context;
private _isActive = false;
private _inDialog = false;
private _isDisposed = false;
private _multiplier = 10;
}
/**
* A namespace for `SaveHandler` statics.
*/
export namespace SaveHandler {
/**
* The options used to create a save handler.
*/
export interface IOptions {
/**
* The context asssociated with the file.
*/
context: DocumentRegistry.Context;
/**
* The minimum save interval in seconds (default is two minutes).
*/
saveInterval?: number;
}
}