Skip to content

Commit 72a7205

Browse files
committed
fix: only one close request when closing viewer
Fixes #3834. Signed-off-by: Max <[email protected]>
1 parent defeed3 commit 72a7205

File tree

3 files changed

+32
-30
lines changed

3 files changed

+32
-30
lines changed

cypress/e2e/sync.spec.js

Lines changed: 28 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ import { randUser } from '../utils/index.js'
2424

2525
const user = randUser()
2626

27-
describe('yjs document state', () => {
27+
describe('Save', () => {
2828
before(() => {
2929
cy.createUser(user)
3030
})
@@ -33,31 +33,38 @@ describe('yjs document state', () => {
3333
cy.login(user)
3434
cy.uploadTestFile('test.md')
3535
cy.visit('/apps/files')
36-
cy.intercept({ method: 'POST', url: '**/apps/text/session/sync' }).as('sync')
36+
cy.intercept({ method: 'POST', url: '**/apps/text/session/sync' }, (req) => {
37+
if (req.body.autosaveContent) {
38+
req.alias = 'save'
39+
}
40+
}).as('sync')
3741
cy.openTestFile()
3842
cy.getContent().find('h2').should('contain', 'Hello world')
39-
cy.wait('@sync', { timeout: 7000 })
4043
cy.getContent().type('* Saving the doc saves the doc state{enter}')
41-
cy.wait('@sync', { timeout: 7000 })
4244
})
4345

4446
it('saves the actual file and document state', () => {
4547
cy.getContent().type('{ctrl+s}')
46-
cy.wait('@sync').its('request.body')
47-
.should('have.property', 'autosaveContent')
48+
cy.wait('@save').its('request.body')
49+
.should('have.property', 'documentState')
4850
.should('not.be.empty')
51+
cy.testName()
52+
.then(name => cy.downloadFile(`/${name}.md`))
53+
.its('data')
54+
.should('include', 'saves the doc state')
55+
cy.closeFile()
56+
})
57+
58+
it('saves on close', () => {
4959
cy.closeFile()
60+
cy.wait('@save')
5061
cy.testName()
5162
.then(name => cy.downloadFile(`/${name}.md`))
5263
.its('data')
5364
.should('include', 'saves the doc state')
5465
})
5566

5667
it('passes the doc content from one session to the next', () => {
57-
cy.getContent().type('{ctrl+s}')
58-
cy.wait('@sync').its('request.body')
59-
.should('have.property', 'documentState')
60-
.should('not.be.empty')
6168
cy.closeFile()
6269
cy.intercept({ method: 'PUT', url: '**/apps/text/session/create' })
6370
.as('create')
@@ -66,10 +73,20 @@ describe('yjs document state', () => {
6673
.its('response.body')
6774
.should('have.property', 'content')
6875
.should('not.be.empty')
69-
cy.wait('@sync').its('request.body').should('have.property', 'version')
7076
cy.getContent().find('h2').should('contain', 'Hello world')
7177
cy.getContent().find('li').should('contain', 'Saving the doc saves the doc state')
7278
cy.getContent().type('recovered')
7379
cy.closeFile()
7480
})
81+
82+
// regression test #3834
83+
it('close triggers one close request', () => {
84+
cy.closeFile()
85+
// Wait to make sure there is enough time for all close requests to run
86+
// eslint-disable-next-line cypress/no-unnecessary-waiting
87+
cy.wait(100)
88+
// reuse @close intercepted in closeFile()
89+
cy.get('@close.all').should('have.length', 1)
90+
})
91+
7592
})

src/components/Editor.vue

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -333,7 +333,6 @@ export default {
333333
window.removeEventListener('afterprint', this.preparePrinting)
334334
}
335335
this.$providers.forEach(p => p.destroy())
336-
this.close()
337336
},
338337
methods: {
339338
...mapActions('text', [

src/services/SyncService.js

Lines changed: 4 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -250,25 +250,11 @@ class SyncService {
250250
return this.save({ manualSave: false })
251251
}
252252

253-
close() {
253+
async close() {
254254
this.backend.disconnect()
255-
let closed = false
256-
return new Promise((resolve, reject) => {
257-
this.on('save', () => {
258-
this._close().then(() => {
259-
closed = true
260-
resolve()
261-
}).catch(() => resolve())
262-
})
263-
setTimeout(() => {
264-
if (!closed) {
265-
this._close().then(() => {
266-
resolve()
267-
}).catch(() => resolve())
268-
}
269-
}, 2000)
270-
this.save()
271-
})
255+
const timeout = new Promise((resolve) => setTimeout(resolve, 2000))
256+
await Promise.any([timeout, this.save()])
257+
return this._close()
272258
}
273259

274260
_close() {

0 commit comments

Comments
 (0)