Skip to content
This repository has been archived by the owner on Jun 28, 2022. It is now read-only.

Commit

Permalink
runat-1128988: Add event batching to commands-changed
Browse files Browse the repository at this point in the history
We don't want to light up the remote interface 10x for every module, so
this adds holdFire/resumeFire to addItemsByModule and friends.

Signed-off-by: Joe Walker <[email protected]>
  • Loading branch information
joewalker committed Mar 19, 2015
1 parent 879c05f commit 1a76df2
Showing 1 changed file with 25 additions and 2 deletions.
27 changes: 25 additions & 2 deletions lib/gcli/system.js
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,14 @@ exports.createSystem = function(options) {
},

addItemsByModule: function(names, options) {
var promises = [];

options = options || {};
if (!options.delayedLoad) {
// We could be about to add many commands, just report the change once
this.commands.onCommandsChange.holdFire();

This comment has been minimized.

Copy link
@bgrins

bgrins Mar 19, 2015

Just curious, where does the command actually get added? Searching for commands.add doesn't turn up much: https://github.com/joewalker/gcli/search?utf8=%E2%9C%93&q=%22commands.add%22&type=Code

This comment has been minimized.

Copy link
@joewalker

joewalker Mar 20, 2015

Author Owner

https://github.com/joewalker/gcli/blob/master/lib/gcli/system.js#L70 To paraphrase:

system.addItem = function(item) {
  components[getItemType(item)].add(item);
};
}

if (typeof names === 'string') {
names = [ names ];
}
Expand All @@ -165,20 +172,34 @@ exports.createSystem = function(options) {
pendingChanges = true;
}
else {
loadModule(name).catch(console.error);
promises.push(loadModule(name).catch(console.error));
}
});

if (options.delayedLoad) {
return Promise.resolve();
}
else {
return Promise.all(promises).then(function() {
this.commands.onCommandsChange.resumeFire();
}.bind(this));
}
},

removeItemsByModule: function(name) {
this.commands.onCommandsChange.holdFire();

delete loadableModules[name];
unloadModule(name);

this.commands.onCommandsChange.resumeFire();
},

load: function() {
if (!pendingChanges) {
return Promise.resolve();
}
this.commands.onCommandsChange.holdFire();

// clone loadedModules, so we can remove what is left at the end
var modules = Object.keys(loadedModules).map(function(name) {
Expand All @@ -193,7 +214,9 @@ exports.createSystem = function(options) {
Object.keys(modules).forEach(unloadModule);
pendingChanges = false;

return Promise.all(promises);
return Promise.all(promises).then(function() {
this.commands.onCommandsChange.resumeFire();
}.bind(this));
},

toString: function() {
Expand Down

0 comments on commit 1a76df2

Please sign in to comment.