Skip to content

Commit

Permalink
feat(app): mark file as unsaved if user cancels update
Browse files Browse the repository at this point in the history
This commit ensures we mark a file as unsaved
if the user cancels a file update request.

It also ensures that we cache the lastModified last
checked (and handled by the user) so that we don't
re-execute the check every time the tab gets re-focused.

Closes #1188
  • Loading branch information
nikku committed Feb 26, 2019
1 parent 5c42572 commit a559126
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 15 deletions.
17 changes: 13 additions & 4 deletions client/src/app/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@ export class App extends PureComponent {

// skip new file
if (this.isUnsaved(tab) || typeof tabLastModified === 'undefined') {
return;
return tab;
}

const {
Expand All @@ -221,17 +221,24 @@ export class App extends PureComponent {

// skip unchanged
if (!(lastModified > tabLastModified)) {
return;
return tab;
}

const answer = await this.showDialog(getContentChangedDialog());

if (answer === 'ok') {
const updatedFile = await fileSystem.readFile(file.path);
const updatedFile = await fileSystem.readFile(file);

return this.updateTab(tab, {
file: updatedFile
});
} else {
return this.updateTab(tab, {
file: {
...file,
lastModified
}
}, this.setUnsaved(tab, true));
}
}

Expand All @@ -240,8 +247,9 @@ export class App extends PureComponent {
*
* @param {Tab} tab
* @param {Object} newAttrs
* @param {Object} [newState={}]
*/
updateTab(tab, newAttrs) {
updateTab(tab, newAttrs, newState={}) {

if (newAttrs.id && newAttrs.id !== tab.id) {
throw new Error('must not change tab.id');
Expand Down Expand Up @@ -276,6 +284,7 @@ export class App extends PureComponent {
}

return {
...newState,
activeTab: updatedActiveTab,
tabs: updatedTabs
};
Expand Down
37 changes: 26 additions & 11 deletions client/src/app/__tests__/AppSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -1611,18 +1611,25 @@ describe('<App>', function() {

const tab = openedTabs[0];

updateFileStats(tab.file, {
lastModified: new Date().getMilliseconds()
}, fileSystem);
const lastModified = new Date().getMilliseconds();

updateFileStats(tab.file, { lastModified }, fileSystem);

// when
await app.checkFileChanged(tab);
const updatedTab = await app.checkFileChanged(tab);

// then
expect(showSpy).to.have.been.called;
expect(readFileSpy).to.have.been.called;

expect(app.findOpenTab(file1).file.contents).to.eql(NEW_FILE_CONTENTS);
expect(updatedTab).to.eql(app.findOpenTab(file1));

expect(updatedTab.file.contents).to.eql(NEW_FILE_CONTENTS);

// TODO(nikku): fix test suite and properly pass last modified
// expect(updatedTab.file.lastModified).to.eql(lastModified);

expect(app.isUnsaved(updatedTab)).to.be.false;
});


Expand Down Expand Up @@ -1651,11 +1658,14 @@ describe('<App>', function() {
}, fileSystem);

// when
await app.checkFileChanged(tab);
const updatedTab = await app.checkFileChanged(tab);

// then
expect(showSpy).to.not.have.been.called;
expect(readFileSpy).to.not.have.been.called;

expect(app.isUnsaved(updatedTab)).to.be.false;

});


Expand Down Expand Up @@ -1706,19 +1716,24 @@ describe('<App>', function() {

const tab = openedTabs[0];

updateFileStats(tab.file, {
lastModified: new Date().getMilliseconds()
}, fileSystem);
const lastModified = new Date().getMilliseconds();

updateFileStats(tab.file, { lastModified }, fileSystem);

const oldTabContents = tab.file.contents;

// when
await app.checkFileChanged(tab);
const updatedTab = await app.checkFileChanged(tab);

// then
expect(showSpy).to.have.been.called;
expect(readFileSpy).to.not.have.been.called;
expect(tab.file.contents).to.equal(oldTabContents);
expect(updatedTab).to.eql(app.findOpenTab(file1));

expect(updatedTab.file.contents).to.equal(oldTabContents);
expect(updatedTab.file.lastModified).to.equal(lastModified);

expect(app.isUnsaved(updatedTab)).to.be.true;
});


Expand Down

0 comments on commit a559126

Please sign in to comment.