Refactor some Plugin code out of event handlers

This commit is contained in:
Oliver Hamlet
2016-06-04 16:59:41 +01:00
parent d3704b7d65
commit 14ef93be23
4 changed files with 75 additions and 31 deletions
+6 -25
View File
@@ -52,12 +52,7 @@ function updateMasterlist() {
result.plugins.forEach((resultPlugin) => {
const existingPlugin = loot.game.plugins.find(plugin => plugin.name === resultPlugin.name);
if (existingPlugin) {
existingPlugin.isDirty = resultPlugin.isDirty;
existingPlugin.isPriorityGlobal = resultPlugin.isPriorityGlobal;
existingPlugin.masterlist = resultPlugin.masterlist;
existingPlugin.messages = resultPlugin.messages;
existingPlugin.priority = resultPlugin.priority;
existingPlugin.tags = resultPlugin.tags;
existingPlugin.update(resultPlugin);
}
});
@@ -107,8 +102,7 @@ function onSortPlugins() {
item.name === plugin.name
));
if (existingPlugin) {
existingPlugin.crc = plugin.crc;
existingPlugin.isEmpty = plugin.isEmpty;
existingPlugin.update(plugin);
}
});
/* Send discardUnappliedChanges query. Not doing so prevents LOOT's window
@@ -123,8 +117,7 @@ function onSortPlugins() {
result.plugins.forEach((plugin) => {
let existingPlugin = loot.game.plugins.find(item => item.name === plugin.name);
if (existingPlugin) {
existingPlugin.crc = plugin.crc;
existingPlugin.isEmpty = plugin.isEmpty;
existingPlugin.update(plugin);
} else {
existingPlugin = new loot.Plugin(plugin);
}
@@ -190,11 +183,7 @@ function onClearAllMetadata() {
existingPlugin.userlist = undefined;
existingPlugin.editor = undefined;
existingPlugin.priority = plugin.priority;
existingPlugin.isPriorityGlobal = plugin.isPriorityGlobal;
existingPlugin.messages = plugin.messages;
existingPlugin.tags = plugin.tags;
existingPlugin.isDirty = plugin.isDirty;
existingPlugin.update(plugin);
}
});
@@ -384,11 +373,7 @@ function onEditorClose(evt) {
const edits = evt.target.readFromEditor(plugin);
promise = loot.query('editorClosed', edits).then(JSON.parse).then((result) => {
if (result) {
plugin.priority = result.priority;
plugin.isPriorityGlobal = result.isPriorityGlobal;
plugin.messages = result.messages;
plugin.tags = result.tags;
plugin.isDirty = result.isDirty;
plugin.update(result);
plugin.userlist = edits.userlist;
@@ -466,11 +451,7 @@ function onClearMetadata(evt) {
existingPlugin.userlist = undefined;
existingPlugin.editor = undefined;
existingPlugin.priority = plugin.priority;
existingPlugin.isPriorityGlobal = plugin.isPriorityGlobal;
existingPlugin.messages = plugin.messages;
existingPlugin.tags = plugin.tags;
existingPlugin.isDirty = plugin.isDirty;
existingPlugin.update(plugin);
}
loot.Dialog.showNotification(loot.l10n.translate('The user-added metadata for "%s" has been cleared.', evt.target.getName()));
/* Now perform search again. If there is no current search, this won't
+1 -6
View File
@@ -24,12 +24,7 @@ function getConflictingPlugins(pluginName) {
}
const plugin = loot.game.plugins.find(item => item.name === key);
if (plugin) {
plugin.crc = result[key].crc;
plugin.isEmpty = result[key].isEmpty;
plugin.messages = result[key].messages;
plugin.tags = result[key].tags;
plugin.isDirty = result[key].isDirty;
plugin.update(result[key]);
}
}
}
+13
View File
@@ -189,6 +189,19 @@
this._isSearchResult = false;
}
update(plugin) {
if (!plugin) {
return;
}
if (plugin.name !== this.name) {
throw new Error(`Cannot update ${this.name}'s data using data for ${plugin.name}`);
}
Object.getOwnPropertyNames(plugin).forEach((property) => {
this[property] = plugin[property];
});
}
static fromJson(key, value) {
if (value !== null && value.__type === 'Plugin') {
return new Plugin(value);
+55
View File
@@ -275,6 +275,61 @@ describe('Plugin', () => {
});
});
describe('#update()', () => {
let plugin;
const updatedPlugin = {
name: 'test',
foo: 'bar',
crc: 0xDEADBEEF,
};
beforeEach(() => {
plugin = new loot.Plugin({ name: 'test' });
});
it('should do nothing if its argument is undefined', () => {
plugin.update();
plugin.should.deepEqual(new loot.Plugin({ name: 'test' }));
});
it('should throw if the argument has no name property', () => {
should(() => { plugin.update({}); }).throw(Error);
});
it('should throw if the argument\'s name property doesn\'t match the plugin\'s name', () => {
should(() => { plugin.update({ name: 'other test' }); }).throw(Error);
});
it('should set property values for all the given argument\'s properties', () => {
plugin.update(updatedPlugin);
plugin.foo.should.equal(updatedPlugin.foo);
plugin.crc.should.equal(updatedPlugin.crc);
});
it('should not change property values for properties not present in the argument', () => {
plugin.isActive = true;
plugin.update(updatedPlugin);
plugin.foo.should.equal(updatedPlugin.foo);
plugin.crc.should.equal(updatedPlugin.crc);
plugin.isActive.should.be.true();
});
it('should set explicitly undefined values', () => {
plugin.isActive = true;
plugin.update({
name: plugin.name,
isActive: undefined,
});
should(plugin.isActive).be.undefined();
});
});
describe('#fromJson()', () => {
it('should return the value object if the JSON is not of the Plugin type', () => {
const testInputObj = {