-
Notifications
You must be signed in to change notification settings - Fork 7.6k
Live Development -- Provide API for enabling/disabling Agents #1303
Changes from 2 commits
3282802
bd8dc7d
e67c7d2
2223087
5c72190
1bce0f3
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -74,15 +74,24 @@ 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() | ||
var _enabledAgents = { | ||
"console": true, | ||
"remote": true, | ||
"network": true, | ||
"dom": true, | ||
"css": true | ||
}; | ||
var _loadedAgents = []; | ||
|
||
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 | ||
|
@@ -221,25 +230,48 @@ define(function LiveDevelopment(require, exports, module) { | |
|
||
/** Unload the agents */ | ||
function unloadAgents() { | ||
var i; | ||
for (i in agents) { | ||
_loadedAgents.forEach(function (i) { | ||
if (agents.hasOwnProperty(i) && agents[i].unload) { | ||
agents[i].unload(); | ||
} | ||
} | ||
}); | ||
_loadedAgents = []; | ||
} | ||
|
||
/** Load the agents */ | ||
function loadAgents() { | ||
var i, promises = []; | ||
for (i in agents) { | ||
if (agents.hasOwnProperty(i) && agents[i].load) { | ||
for (i in _enabledAgents) { | ||
if (_enabledAgents.hasOwnProperty(i) && agents.hasOwnProperty(i) && agents[i].load) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This assumes the indices in '_enabledAgents' and 'agents' match perfectly. So the set of enabled agents must always be grouped in a contiguous block at the top of 'agents.' Can we guarantee that's always true? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Oops, strike that too... these are maps, not arrays. Could you rename 'i' to something more clear, like 'agentName'? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I changed _enabledAgents to _enabledAgentNames, and the iteration variable to 'name'. I also added a comment to make this more clear. |
||
promises.push(agents[i].load()); | ||
_loadedAgents.push(i); | ||
} | ||
} | ||
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 (!_enabledAgents.hasOwnProperty(name)) { | ||
_enabledAgents[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 (_enabledAgents.hasOwnProperty(name)) { | ||
delete _enabledAgents[name]; | ||
} | ||
} | ||
|
||
/** Update the status | ||
* @param {integer} new status | ||
*/ | ||
|
@@ -459,6 +491,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; | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The first arg here is the value of
_loadedAgents[i]
, noti
itself. As-is I think this code will never try to unload anything.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oops, never mind... now that I've read below I see _loadedAgents is actually an array of agent names (the keys of 'agents'), not the agents themselves. Could we clarify that either by renaming this array or at least by adding docs where it's declared? Also, similar to below let's not use 'i' here for something that's not actually an index.
(Alternatively, you could change _loadedAgents to be an array of the actual agent objects... that seems fine to me too).
Also, the hasOwnProperty() check can go away now that you're using forEach().
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Changed name of _loadedAgents to _loadedAgentNames to make this more clear. Also added a comment.
And, I removed the hasOwnProperty check.