function _close(clientId) {
delete _connections[clientId];
exports.trigger("ConnectionClose", {
clientId: clientId
});
}
function _connect(clientId, url) {
// add new connections
// TODO: check URL
_connections[clientId] = true;
exports.trigger("ConnectionConnect", {
clientId: clientId,
url: url
});
}
function _receive(clientId, msgStr) {
var msg = JSON.parse(msgStr),
event = msg.method || "event",
deferred;
if (msg.id) {
deferred = _responseDeferreds[msg.id];
if (deferred) {
delete _responseDeferreds[msg.id];
if (msg.error) {
deferred.reject(msg);
} else {
deferred.resolve(msg);
}
}
} else if (msg.tagId) {
var editor = EditorManager.getActiveEditor(),
position = HTMLInstrumentation.getPositionFromTagId(editor, parseInt(msg.tagId, 10));
if (position) {
editor.setCursorPos(position.line, position.ch, true);
}
} else {
// enrich received message with clientId
msg.clientId = clientId;
exports.trigger(event, msg);
}
}
function _send(msg, clients) {
var id = _nextMsgId++,
result = new $.Deferred();
// broadcast if there are no specific clients
clients = clients || getConnectionIds();
msg.id = id;
_responseDeferreds[id] = result;
_transport.send(clients, JSON.stringify(msg));
return result.promise();
}
Closes the connection to the given client. Proxies to the transport.
function close(clientId) {
_transport.close(clientId);
}
function closeAllConnections() {
getConnectionIds().forEach(function (clientId) {
close(clientId);
});
_connections = {};
}
EventDispatcher.makeEventDispatcher(exports);
// public API
exports.setTransport = setTransport;
exports.getRemoteScript = getRemoteScript;
exports.evaluate = evaluate;
exports.setStylesheetText = setStylesheetText;
exports.getStylesheetText = getStylesheetText;
exports.reload = reload;
exports.navigate = navigate;
exports.close = close;
exports.getConnectionIds = getConnectionIds;
exports.closeAllConnections = closeAllConnections;
});
Protocol method. Evaluates the given script in the browser (in global context), and returns a promise that will be fulfilled with the result of the script, if any.
function evaluate(script, clients) {
return _send(
{
method: "Runtime.evaluate",
params: {
expression: script
}
},
clients
);
}
Returns an array of the client IDs that are being managed by this live document.
function getConnectionIds() {
return Object.keys(_connections);
}
Returns a script that should be injected into the HTML that's launched in the browser in order to implement remote commands that handle protocol requests. Includes the <script> tags.
function getRemoteFunctionsScript() {
var script = "";
// Inject DocumentObserver into the browser (tracks related documents)
script += DocumentObserver;
// Inject remote functions into the browser.
script += "window._LD=(" + RemoteFunctions + "(" + JSON.stringify(LiveDevMultiBrowser.config) + "))";
return "<script>\n" + script + "</script>\n";
}
Returns a script that should be injected into the HTML that's launched in the browser in order to handle protocol requests. Includes the <script> tags. This script will also include the script required by the transport, if any.
function getRemoteScript() {
var transportScript = _transport.getRemoteScript() || "";
var remoteFunctionsScript = getRemoteFunctionsScript() || "";
return transportScript +
"<script>\n" + LiveDevProtocolRemote + "</script>\n" +
remoteFunctionsScript;
}
Protocol method. Rretrieves the content of a given stylesheet (for unit testing)
function getStylesheetText(url, clients) {
return _send(
{
method: "CSS.getStylesheetText",
params: {
url: url
}
},
clients
);
}
Protocol method. Navigates current page to the given URL.
function navigate(url, clients) {
return _send(
{
method: "Page.navigate",
params: {
url: url
}
},
clients
);
}
Protocol method. Reloads the page that is currently loaded into the browser, optionally ignoring cache.
function reload(ignoreCache, clients) {
return _send(
{
method: "Page.reload",
params: {
ignoreCache: true
}
},
clients
);
}
Protocol method. Reloads a CSS styleseet in the browser (by replacing its text) given its url.
function setStylesheetText(url, text, clients) {
return _send(
{
method: "CSS.setStylesheetText",
params: {
url: url,
text: text
}
}
);
}
Sets the transport that should be used by the protocol. See LiveDevelopment.setTransport()
for more detail on the transport.
function setTransport(transport) {
if (_transport) {
_transport.off(".livedev");
}
_transport = transport;
_transport
.on("connect.livedev", function (event, msg) {
_connect(msg[0], msg[1]);
})
.on("message.livedev", function (event, msg) {
_receive(msg[0], msg[1]);
})
.on("close.livedev", function (event, msg) {
_close(msg[0]);
});
_transport.start();
}