@@ -36,24 +36,18 @@ export default class AutomaticsManager {
36
36
this . plugin . settings . differentIntervalCommitAndPush &&
37
37
this . plugin . settings . autoPushInterval > 0
38
38
) {
39
- const now = new Date ( ) ;
40
-
41
- const diff =
42
- this . plugin . settings . autoPushInterval -
43
- Math . round (
44
- ( now . getTime ( ) - lastAutos . push . getTime ( ) ) / 1000 / 60
45
- ) ;
46
- this . startAutoPush ( diff <= 0 ? 0 : diff ) ;
39
+ const diff = this . diff (
40
+ this . plugin . settings . autoPushInterval ,
41
+ lastAutos . push
42
+ ) ;
43
+ this . startAutoPush ( diff ) ;
47
44
}
48
45
if ( this . plugin . settings . autoPullInterval > 0 ) {
49
- const now = new Date ( ) ;
50
-
51
- const diff =
52
- this . plugin . settings . autoPullInterval -
53
- Math . round (
54
- ( now . getTime ( ) - lastAutos . pull . getTime ( ) ) / 1000 / 60
55
- ) ;
56
- this . startAutoPull ( diff <= 0 ? 0 : diff ) ;
46
+ const diff = this . diff (
47
+ this . plugin . settings . autoPullInterval ,
48
+ lastAutos . pull
49
+ ) ;
50
+ this . startAutoPull ( diff ) ;
57
51
}
58
52
}
59
53
@@ -96,37 +90,30 @@ export default class AutomaticsManager {
96
90
}
97
91
98
92
/**
99
- * Starts the debouncer and timer with the correct remaining time.
93
+ * Starts the auto commit- and-sync with the correct remaining time.
100
94
*
101
95
* Additionally, if `setLastSaveToLastCommit` is enabled, the last auto commit-and-sync
102
96
* is set to the last commit time.
103
- *
104
- * Should be called when the latest commit time is changed. E.g. after a commit or pull.
105
97
*/
106
- async setUpAutoCommitAndSync ( ) {
98
+ private async setUpAutoCommitAndSync ( ) {
107
99
if ( this . plugin . settings . setLastSaveToLastCommit ) {
108
100
this . clearAutoCommitAndSync ( ) ;
109
101
const lastCommitDate =
110
102
await this . plugin . gitManager . getLastCommitTime ( ) ;
111
103
if ( lastCommitDate ) {
112
- this . plugin . localStorage . setLastAutoBackup (
113
- lastCommitDate . toString ( )
114
- ) ;
104
+ this . saveLastAuto ( lastCommitDate , "backup" ) ;
115
105
}
116
106
}
117
107
118
108
if ( ! this . timeoutIDCommitAndSync && ! this . plugin . autoCommitDebouncer ) {
119
109
const lastAutos = this . loadLastAuto ( ) ;
120
110
121
111
if ( this . plugin . settings . autoSaveInterval > 0 ) {
122
- const now = new Date ( ) ;
123
-
124
- const diff =
125
- this . plugin . settings . autoSaveInterval -
126
- Math . round (
127
- ( now . getTime ( ) - lastAutos . backup . getTime ( ) ) / 1000 / 60
128
- ) ;
129
- this . startAutoCommitAndSync ( diff <= 0 ? 0 : diff ) ;
112
+ const diff = this . diff (
113
+ this . plugin . settings . autoSaveInterval ,
114
+ lastAutos . backup
115
+ ) ;
116
+ this . startAutoCommitAndSync ( diff ) ;
130
117
}
131
118
}
132
119
}
@@ -153,19 +140,42 @@ export default class AutomaticsManager {
153
140
}
154
141
}
155
142
156
- // this.plugin is used for both auto commit-and-sync and commit only
143
+ // This is used for both auto commit-and-sync and commit only
157
144
private doAutoCommitAndSync ( ) : void {
158
145
this . plugin . promiseQueue . addTask (
159
- ( ) => {
146
+ async ( ) => {
147
+ // Re-check if the auto commit should run now or be postponed,
148
+ // because the last commit time has changed
149
+ if ( this . plugin . settings . setLastSaveToLastCommit ) {
150
+ const lastCommitDate =
151
+ await this . plugin . gitManager . getLastCommitTime ( ) ;
152
+ if ( lastCommitDate ) {
153
+ this . saveLastAuto ( lastCommitDate , "backup" ) ;
154
+ const diff = this . diff (
155
+ this . plugin . settings . autoSaveInterval ,
156
+ lastCommitDate
157
+ ) ;
158
+ if ( diff > 0 ) {
159
+ this . startAutoCommitAndSync ( diff ) ;
160
+ // Return false to mark the next iteration
161
+ // already being scheduled
162
+ return false ;
163
+ }
164
+ }
165
+ }
160
166
if ( this . plugin . settings . differentIntervalCommitAndPush ) {
161
- return this . plugin . commit ( { fromAuto : true } ) ;
167
+ await this . plugin . commit ( { fromAuto : true } ) ;
162
168
} else {
163
- return this . plugin . commitAndSync ( true ) ;
169
+ await this . plugin . commitAndSync ( true ) ;
164
170
}
171
+ return true ;
165
172
} ,
166
- ( ) => {
167
- this . saveLastAuto ( new Date ( ) , "backup" ) ;
168
- this . startAutoCommitAndSync ( ) ;
173
+ ( schedule ) => {
174
+ // Don't schedule if the next iteration is already scheduled
175
+ if ( schedule !== false ) {
176
+ this . saveLastAuto ( new Date ( ) , "backup" ) ;
177
+ this . startAutoCommitAndSync ( ) ;
178
+ }
169
179
}
170
180
) ;
171
181
}
@@ -238,4 +248,17 @@ export default class AutomaticsManager {
238
248
}
239
249
return false ;
240
250
}
251
+
252
+ /**
253
+ * Calculates the minutes until the next auto action. >= 0
254
+ *
255
+ * This is done by the difference between the setting and the time since the last auto action, but at least 0.
256
+ */
257
+ private diff ( setting : number , lastAuto : Date ) {
258
+ const now = new Date ( ) ;
259
+ const diff =
260
+ setting -
261
+ Math . round ( ( now . getTime ( ) - lastAuto . getTime ( ) ) / 1000 / 60 ) ;
262
+ return Math . max ( 0 , diff ) ;
263
+ }
241
264
}
0 commit comments