Skip to content

Commit

Permalink
Merge pull request #496 from hwshim/master
Browse files Browse the repository at this point in the history
BUGFIX] Reloading file updated from another ide
  • Loading branch information
hwshim committed Sep 23, 2015
2 parents a2ea509 + 5712b36 commit e74d6d2
Show file tree
Hide file tree
Showing 8 changed files with 126 additions and 137 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,16 @@ define([
return false;
},

/**
* @param {Function} callback
*/
reload: function(callback) {
logger.info('reload(callback)');
var file = this.getPersistence();
file.setFlag(Persistence.READ, false);
this.getData(callback);
},

/**
* @param {Function} callback
*/
Expand All @@ -106,13 +116,16 @@ define([
var that = this;
var file = this.getPersistence();
if (file.getFlag(Persistence.READ) === false) {
this.emit(DataSource.LOAD_START, this);
fsCache.readFile(file.getPath(), function(error, data) {
if (error) {
notify.error('Failed to read file "' + file.getPath() + '" (' + error + ')');
that.emit(DataSource.LOAD_FAIL, that);
} else {
logger.info('data arrived');
file.setContents(data);
file.setFlag(Persistence.READ, true);
that.emit(DataSource.LOAD_COMPLETE, data);
callback(file.getContents());
}
});
Expand All @@ -139,7 +152,7 @@ define([
file.setFlag(Persistence.READ, true);
callback(file.getContents());
that.emit(DataSource.AFTER_SAVE);
topic.publish('data-source/written', that);
topic.publish('data-source/written', that.getId());
}
});
},
Expand Down
36 changes: 35 additions & 1 deletion common/src/webida/plugins/editors/DataSourceHandler.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,9 +77,12 @@ define([
* @private
*/
_subscribe: function() {
//open
//on deleted
this.subscribed.push(topic.subscribe('workspace.nodes.deleting', this._onNodesDeleted.bind(this)));
this.subscribed.push(topic.subscribe('fs.cache.node.deleted', this._checkCase.bind(this)));

//on content changed
this.subscribed.push(topic.subscribe('data-source/content-change', this._onContentChange.bind(this)));
},

/**
Expand Down Expand Up @@ -210,6 +213,14 @@ define([
}
},

// ************* On Content Changed ************* //

_onContentChange: function(dataSourceId) {
var dsRegistry = workbench.getDataSourceRegistry();
var dataSource = dsRegistry.getDataSourceById(dataSourceId);
_askReload(dataSource);
},

/**
* @private
*/
Expand Down Expand Up @@ -238,5 +249,28 @@ define([
});
}

function _askReload(dataSource) {
var dataSourceId = dataSource.getId();
var persistence = dataSource.getPersistence();
require(['popup-dialog'], function(PopupDialog) {
// @formatter:off
PopupDialog.yesno({
title: "Reload '" + persistence.getName() + "'?",
message: "'" + persistence.getPath() + "' has been changed. "
+ 'Reload the editor(s) for the resource?',
type: 'warning'
}).then(function() {
var page = workbench.getCurrentPage();
var registry = page.getPartRegistry();
var parts = registry.getPartsByDataSource(dataSource);
parts.forEach(function(part) {
part.resetModel();
});
}, function() {
});
// @formatter:on
});
}

return DataSourceHandler;
});
48 changes: 4 additions & 44 deletions common/src/webida/plugins/editors/plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,49 +24,37 @@
define([
'dojo/topic',
'external/lodash/lodash.min',
'external/URIjs/src/URI',
'webida-lib/app',
'webida-lib/util/path',
'webida-lib/util/arrays/BubblingArray',
'webida-lib/util/logger/logger-client',
'webida-lib/util/notify',
'webida-lib/plugin-manager-0.1',
'webida-lib/plugins/workbench/plugin',
'webida-lib/plugins/workbench/ui/CompatibleTabPartContainer',
'webida-lib/plugins/workbench/ui/EditorPart',
'webida-lib/plugins/workbench/ui/LayoutPane',
'webida-lib/plugins/workbench/ui/PartContainer',
'webida-lib/plugins/workbench/ui/Workbench',
'webida-lib/widgets/views/view',
'webida-lib/widgets/views/viewmanager',
'webida-lib/widgets/views/viewFocusController',
'external/async/dist/async.min',
'plugins/webida.notification/notification-message',
'plugins/webida.workspace.model.file/FileDataSource', //TODO : temp for 7.21
'./DataSourceHandler',
'./LifecycleManager'
], function (
topic,
_,
URI,
ide,
pathUtil,
BubblingArray,
Logger,
notify,
pm,
workbench,
CompatibleTabPartContainer,
EditorPart,
LayoutPane,
PartContainer,
Workbench,
View,
vm,
ViewFocusController,
async,
toastr,
FileDataSource,
DataSourceHandler,
LifecycleManager
) {
Expand All @@ -90,6 +78,7 @@ define([
});

topic.subscribe('fs.cache.file.invalidated', function(fsURL, path) {
logger.info('fs.cache.file.invalidated arrived');
var file = editors.getFile(path);
if (file) {
if (file === editors.currentFile) {
Expand All @@ -102,14 +91,7 @@ define([

topic.subscribe('fs.cache.file.set', function(fsUrl, target, reason) {
if (reason === 'refreshed') {
var file = editors.getFile(target);
if (file) {
if (file === editors.currentFile) {
_.defer(askAndReload.bind(null, file));
} else {
file.toAskAndReload = true;
}
}
topic.publish('data-source/content-change', target);
}
});

Expand All @@ -121,7 +103,7 @@ define([
//Compatibility
topic.subscribe('current-part-changed', function(oldPart, newPart) {

console.log('current-part-changed');
logger.info('current-part-changed arrived');

if (oldPart !== newPart) {

Expand All @@ -141,7 +123,7 @@ define([
key: 'E'
});

/////////////////////// TODO refactor
//------------ TODO refactor ---------------

var file = newPart.getDataSource().getPersistence();

Expand All @@ -156,10 +138,6 @@ define([
fsCache.refreshFileContents(file.path);
}

if (file.toAskAndReload) {
file.toAskAndReload = false;
_.defer(askAndReload.bind(null, file));
}
topic.publish('editors.selected', file.path, file);
}
} else {
Expand All @@ -175,24 +153,6 @@ define([

var fsCache = ide.getFSCache();
var asked = [];
function askAndReload(file) {
require(['popup-dialog'], function(PopupDialog) {
if (asked.indexOf(file) === -1) {
console.assert(asked.length === 0, 'assertion fail: only one file is asked to reload at any point of time');
asked.push(file);
PopupDialog.yesno({
title: 'Reload File',
message: file.path + '<br><br>' + 'File \'' + file.name + '\' has changed. <br>' + 'Do you want to reload?',
type: 'warning'
}).then(function() {
asked.pop();
topic.publish('editor/open', file.getPersistenceId());
}, function() {
asked.pop();
});
}
});
}

/** @module editors */
var editors = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -235,16 +235,6 @@ define([
*/
getDataSource: function() {
return this.dataSource;
},

/**
* For the EditorPart, saved data could be retrived
* from dataSource. So setSavedData() is not required.
* @override
* @return {string}
*/
getSavedData: function() {
return this.getDataSource().getPersistence().getContents();
}
});

Expand Down
22 changes: 18 additions & 4 deletions common/src/webida/plugins/workbench/ui/DataSource.js
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,13 @@ define([
throw new Error('getData(callback) should be implemented by ' + this.constructor.name);
},

/**
* @param {Function} callback
*/
reload: function(callback) {
throw new Error('reload(callback) should be implemented by ' + this.constructor.name);
},

/**
* @return {string} title
*/
Expand Down Expand Up @@ -142,14 +149,21 @@ define([

/**
* Emit this event when DataSource's
* getContents() start to load the contents
* getContents() starts to load the contents
* @constant {string}
*/
DataSource.LOAD_START = 'loadStart';

/**
* Emit this event when DataSource's
* getContents() complete to load the contents
* getContents() fails to load the contents
* @constant {string}
*/
DataSource.LOAD_FAIL = 'loadFail';

/**
* Emit this event when DataSource's
* getContents() completes to load the contents
* @constant {string}
*/
DataSource.LOAD_COMPLETE = 'loadComplete';
Expand All @@ -168,12 +182,12 @@ define([
*/
DataSource.AFTER_SAVE = 'afterSave';

/**
/**
* Emit this event when error occured
* during setData(contents, callback)
* @constant {string}
*/
DataSource.SAVE_FAIL = 'saveFail';
DataSource.SAVE_FAIL = 'saveFail';

/**
* This event is emitted when setId(newId) is called
Expand Down
7 changes: 4 additions & 3 deletions common/src/webida/plugins/workbench/ui/EditorModelManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -108,10 +108,11 @@ define([
* Resets the given EditorModel to its last saved state.
*/
resetModel: function() {
logger.info('resetModel()');
logger.info('resetModel()');
var model = this.getModel();
logger.info('this.getSavedData() = ', this.getSavedData());
model.createContents(this.getSavedData());
this.getDataSource().reload(function(data) {
model.createContents(data);
});
},
});

Expand Down
15 changes: 0 additions & 15 deletions common/src/webida/plugins/workbench/ui/PartModelManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -88,21 +88,6 @@ define([
return this.model;
},

/**
* @param {Object} data
*/
setSavedData: function(data) {
logger.info('setSavedData(' + data + ')');
this.savedData = data;
},

/**
* @return {Object}
*/
getSavedData: function() {
return this.savedData;
},

/**
* @private
*/
Expand Down
Loading

0 comments on commit e74d6d2

Please sign in to comment.