Skip to content
This repository has been archived by the owner on Sep 6, 2021. It is now read-only.

Live Development -- Provide API for enabling/disabling Agents #1303

Merged
merged 6 commits into from
Jul 26, 2012
Merged
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 48 additions & 13 deletions src/LiveDevelopment/LiveDevelopment.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,15 +74,27 @@ define(function LiveDevelopment(require, exports, module) {
"remote": require("LiveDevelopment/Agents/RemoteAgent"),
"network": require("LiveDevelopment/Agents/NetworkAgent"),
"dom": require("LiveDevelopment/Agents/DOMAgent"),
"css": require("LiveDevelopment/Agents/CSSAgent")
/* FUTURE
"css": require("LiveDevelopment/Agents/CSSAgent"),
"script": require("LiveDevelopment/Agents/ScriptAgent"),
"highlight": require("LiveDevelopment/Agents/HighlightAgent"),
"goto": require("LiveDevelopment/Agents/GotoAgent"),
"edit": require("LiveDevelopment/Agents/EditAgent")
*/
};

// Some agents are still experimental, so we don't enable them all by default
// However, extensions can enable them by calling enableAgent().
// This object is used as a set (thus all properties have the value 'true').
// Property names should match property names in the 'agents' object.
var _enabledAgentNames = {
"console": true,
"remote": true,
"network": true,
"dom": true,
"css": true
};
// store the names (matching property names in the 'agent' object) of agents that we've loaded
var _loadedAgentNames = [];

var _htmlDocumentPath; // the path of the html file open for live development
var _liveDocument; // the document open for live editing.
var _relatedDocuments; // CSS and JS documents that are used by the live HTML document
Expand Down Expand Up @@ -221,25 +233,46 @@ define(function LiveDevelopment(require, exports, module) {

/** Unload the agents */
function unloadAgents() {
var i;
for (i in agents) {
if (agents.hasOwnProperty(i) && agents[i].unload) {
agents[i].unload();
}
}
_loadedAgentNames.forEach(function (name) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you still need the check to see if unload() exists before calling it? I notice the check for load() existing is still present below...

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In the loadAgents() function, we're reading from '_enabledAgentNames'. Extension developers can put any string they want into this map (through enableAgent). That means that an agent might not actually exist with some name in _enabledAgentNames.

That's why we need the check in the loadAgents() function.

However, we only add a name to the _loadedAgentNames array if an agent with that name a.) exists and b.) has a 'load' function. All agents that have a load function should have an unload function.

So, when we're in unloadAgents and we do agents[name].unload(), an exception is appropriate if unload is undefined (or if agents[name] is undefined)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

After writing that, it seems like I should probably be checking in enableAgent to make sure that the agent they specify actually exists.

That'll remove the need to check agents.hasOwnProperty(name) in loadAgents. But, we still need to check for load() existing in the agent. It's possible that some agents might not have load/unload.

However, if an agent has 'load', it needs to have 'unload'. So, the unload test still isn't needed.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

change made in 1bce0f3

agents[name].unload();
});
_loadedAgentNames = [];
}

/** Load the agents */
function loadAgents() {
var i, promises = [];
for (i in agents) {
if (agents.hasOwnProperty(i) && agents[i].load) {
promises.push(agents[i].load());
var name, promises = [];
for (name in _enabledAgentNames) {
if (_enabledAgentNames.hasOwnProperty(name) && agents.hasOwnProperty(name) && agents[name].load) {
promises.push(agents[name].load());
_loadedAgentNames.push(name);
}
}
return promises;
}

/** Enable an agent. Takes effect next time a connection is made. Does not affect
* current live development sessions.
*
* @param {string} name of agent to enable
*/
function enableAgent(name) {
if (!_enabledAgentNames.hasOwnProperty(name)) {
_enabledAgentNames[name] = true;
}
}

/** Disable an agent. Takes effect next time a connection is made. Does not affect
* current live development sessions.
*
* @param {string} name of agent to disable
*/
function disableAgent(name) {
if (_enabledAgentNames.hasOwnProperty(name)) {
delete _enabledAgentNames[name];
}
}

/** Update the status
* @param {integer} new status
*/
Expand Down Expand Up @@ -459,6 +492,8 @@ define(function LiveDevelopment(require, exports, module) {
exports.agents = agents;
exports.open = open;
exports.close = close;
exports.enableAgent = enableAgent;
exports.disableAgent = disableAgent;
exports.getLiveDocForPath = getLiveDocForPath;
exports.hideHighlight = hideHighlight;
exports.init = init;
Expand Down