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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
|
/*
* @copyright Copyright (c) 2019 Julius Härtl <jus@bitgrid.net>
*
* @author Julius Härtl <jus@bitgrid.net>
*
* @license GNU AGPL version 3 or any later version
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
import axios from '@nextcloud/axios'
import { endpointUrl } from '../helpers'
import { SyncService, ERROR_TYPE } from './SyncService'
import { sendableSteps } from 'prosemirror-collab'
/**
* Minimum inverval to refetch the document changes
*
* @type {number} time in ms
*/
const FETCH_INTERVAL = 300
/**
* Maximum interval between refetches of document state if multiple users have joined
*
* @type {number} time in ms
*/
const FETCH_INTERVAL_MAX = 5000
/**
* Interval to check for changes when there is only one user joined
*
* @type {number} time in ms
*/
const FETCH_INTERVAL_SINGLE_EDITOR = 5000
/**
* Interval to fetch for changes when a browser window is considered invisible by the
* page visibility API https://developer.mozilla.org/de/docs/Web/API/Page_Visibility_API
*
* @type {number} time in ms
*/
const FETCH_INTERVAL_INVISIBLE = 60000
const MIN_PUSH_RETRY = 500
const MAX_PUSH_RETRY = 10000
/* Timeout after that a PUSH_FAILURE error is emitted */
const WARNING_PUSH_RETRY = 5000
/* Maximum number of retries for fetching before emitting a connection error */
const MAX_RETRY_FETCH_COUNT = 5
/**
* Timeout for sessions to be marked as disconnected
* Make sure that this is higher than any FETCH_INTERVAL_ values
*/
const COLLABORATOR_DISCONNECT_TIME = FETCH_INTERVAL_INVISIBLE * 1.5
class PollingBackend {
constructor(authority) {
/** @type {SyncService} */
this._authority = authority
this.fetchInterval = FETCH_INTERVAL
this.retryTime = MIN_PUSH_RETRY
this.lock = false
this.fetchRetryCounter = 0
}
connect() {
this.initialLoadingFinished = false
this.fetcher = setInterval(this._fetchSteps.bind(this), 50)
document.addEventListener('visibilitychange', this.visibilitychange.bind(this))
}
_isPublic() {
return !!this._authority.options.shareToken
}
forceSave() {
this._forcedSave = true
this.fetchSteps()
}
save() {
this._manualSave = true
this.fetchSteps()
}
fetchSteps() {
this._fetchSteps()
}
/**
* This method is only called though the timer
*/
_fetchSteps() {
if (this.lock || !this.fetcher) {
return
}
this.lock = true
let autosaveContent
if (this._forcedSave || this._manualSave
|| (!sendableSteps(this._authority.state)
&& (this._authority._getVersion() !== this._authority.document.lastSavedVersion))
) {
autosaveContent = this._authority._getContent()
}
axios.post(endpointUrl('session/sync', this._isPublic()), {
documentId: this._authority.document.id,
sessionId: this._authority.session.id,
sessionToken: this._authority.session.token,
version: this._authority._getVersion(),
autosaveContent,
force: !!this._forcedSave,
manualSave: !!this._manualSave,
token: this._authority.options.shareToken,
filePath: this._authority.options.filePath,
}).then(this._handleResponse.bind(this), this._handleError.bind(this))
this._manualSave = false
this._forcedSave = false
}
_handleResponse(response) {
this.fetchRetryCounter = 0
if (this._authority.document.lastSavedVersion < response.data.document.lastSavedVersion) {
console.debug('Saved document', response.data.document)
this._authority.emit('save', { document: response.data.document, sessions: response.data.sessions })
}
this._authority.emit('change', { document: response.data.document, sessions: response.data.sessions })
this._authority.document = response.data.document
this._authority.sessions = response.data.sessions
if (response.data.steps.length === 0) {
if (!this.initialLoadingFinished) {
this.initialLoadingFinished = true
}
if (this._authority.checkIdle()) {
return
}
this.lock = false
if (response.data.sessions.filter((session) => session.lastContact > Date.now() / 1000 - COLLABORATOR_DISCONNECT_TIME).length < 2) {
this.maximumRefetchTimer()
} else {
this.increaseRefetchTimer()
}
this._authority.emit('stateChange', { dirty: false })
this._authority.emit('stateChange', { initialLoading: true })
return
}
this._authority._receiveSteps(response.data)
this.lock = false
this._forcedSave = false
if (this.initialLoadingFinished) {
this.resetRefetchTimer()
}
}
_handleError(e) {
this.lock = false
if (!e.response || e.code === 'ECONNABORTED') {
if (this.fetchRetryCounter++ >= MAX_RETRY_FETCH_COUNT) {
console.error('[PollingBackend:fetchSteps] Network error when fetching steps, emitting CONNECTION_FAILED')
this._authority.emit('error', ERROR_TYPE.CONNECTION_FAILED, { retry: false })
} else {
console.error(`[PollingBackend:fetchSteps] Network error when fetching steps, retry ${this.fetchRetryCounter}`)
}
} else if (e.response.status === 409 && e.response.data.document.currentVersion === this._authority.document.currentVersion) {
// Only emit conflict event if we have synced until the latest version
console.error('Conflict during file save, please resolve')
this._authority.emit('error', ERROR_TYPE.SAVE_COLLISSION, {
outsideChange: e.response.data.outsideChange,
})
} else if (e.response.status === 403) {
this._authority.emit('error', ERROR_TYPE.SOURCE_NOT_FOUND, {})
this.disconnect()
} else if (e.response.status === 404) {
this._authority.emit('error', ERROR_TYPE.SOURCE_NOT_FOUND, {})
this.disconnect()
} else if (e.response.status === 503) {
this.increaseRefetchTimer()
this._authority.emit('error', ERROR_TYPE.CONNECTION_FAILED, { retry: false })
console.error('Failed to fetch steps due to unavailable service', e)
} else {
this.disconnect()
this._authority.emit('error', ERROR_TYPE.CONNECTION_FAILED, { retry: false })
console.error('Failed to fetch steps due to other reason', e)
}
}
sendSteps(_sendable) {
this._authority.emit('stateChange', { dirty: true })
if (this.lock) {
setTimeout(() => {
this._authority.sendSteps()
}, 100)
return
}
this.lock = true
const sendable = (typeof _sendable === 'function') ? _sendable() : _sendable
const steps = sendable.steps
axios.post(endpointUrl('session/push', !!this._authority.options.shareToken), {
documentId: this._authority.document.id,
sessionId: this._authority.session.id,
sessionToken: this._authority.session.token,
steps: steps.map(s => s.toJSON ? s.toJSON() : s) || [],
version: sendable.version,
token: this._authority.options.shareToken,
filePath: this._authority.options.filePath,
}).then((response) => {
this.carefulRetryReset()
this.lock = false
this.fetchSteps()
}).catch(({ response, code }) => {
console.error('failed to apply steps due to collission, retrying')
this.lock = false
if (!response || code === 'ECONNABORTED') {
this._authority.emit('error', ERROR_TYPE.CONNECTION_FAILED, {})
return
}
const { status, data } = response
if (status === 403) {
if (!data.document) {
// either the session is invalid or the document is read only.
console.error('failed to write to document - not allowed')
}
// Only emit conflict event if we have synced until the latest version
if (data.document?.currentVersion === this._authority.document.currentVersion) {
this._authority.emit('error', ERROR_TYPE.PUSH_FAILURE, {})
OC.Notification.showTemporary('Changes could not be sent yet')
}
}
this.fetchSteps()
this.carefulRetry()
})
}
disconnect() {
clearInterval(this.fetcher)
this.fetcher = 0
document.removeEventListener('visibilitychange', this.visibilitychange.bind(this))
}
resetRefetchTimer() {
if (this.fetcher === 0) {
return
}
this.fetchInterval = FETCH_INTERVAL
clearInterval(this.fetcher)
this.fetcher = setInterval(this._fetchSteps.bind(this), this.fetchInterval)
}
increaseRefetchTimer() {
if (this.fetcher === 0) {
return
}
this.fetchInterval = Math.min(this.fetchInterval * 2, FETCH_INTERVAL_MAX)
clearInterval(this.fetcher)
this.fetcher = setInterval(this._fetchSteps.bind(this), this.fetchInterval)
}
maximumRefetchTimer() {
if (this.fetcher === 0) {
return
}
this.fetchInterval = FETCH_INTERVAL_SINGLE_EDITOR
clearInterval(this.fetcher)
this.fetcher = setInterval(this._fetchSteps.bind(this), this.fetchInterval)
}
visibilitychange() {
if (this.fetcher === 0) {
return
}
if (document.visibilityState === 'hidden') {
this.fetchInterval = FETCH_INTERVAL_INVISIBLE
clearInterval(this.fetcher)
this.fetcher = setInterval(this._fetchSteps.bind(this), this.fetchInterval)
} else {
this.resetRefetchTimer()
}
}
carefulRetry() {
const newRetry = this.retryTime ? Math.min(this.retryTime * 2, MAX_PUSH_RETRY) : MIN_PUSH_RETRY
if (newRetry > WARNING_PUSH_RETRY && this.retryTime < WARNING_PUSH_RETRY) {
OC.Notification.showTemporary('Changes could not be sent yet')
this._authority.emit('error', ERROR_TYPE.PUSH_FAILURE, {})
}
this.retryTime = newRetry
}
carefulRetryReset() {
this.retryTime = MIN_PUSH_RETRY
}
}
export default PollingBackend
|