Skip to content

Commit 254b1f1

Browse files
committed
feat: add remote function script support in live preview for extensibility
1 parent aef47cb commit 254b1f1

File tree

2 files changed

+22
-1
lines changed

2 files changed

+22
-1
lines changed

src/LiveDevelopment/BrowserScripts/RemoteFunctions.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5189,8 +5189,12 @@ function RemoteFunctions(config = {}) {
51895189
}
51905190

51915191
registerHandlers();
5192+
let customReturns = {};
5193+
// the below code comment is replaced by added scripts for extensibility
5194+
// REPLACE_WITH_ADDED_REMOTE_SCRIPTS
51925195

51935196
return {
5197+
...customReturns,
51945198
"DOMEditHandler" : DOMEditHandler,
51955199
"hideHighlight" : hideHighlight,
51965200
"highlight" : highlight,

src/LiveDevelopment/MultiBrowserImpl/protocol/LiveDevProtocol.js

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -331,6 +331,21 @@ define(function (require, exports, module) {
331331
_transport.start();
332332
}
333333

334+
let remoteFunctionsScripts = new Map();
335+
let effectiveRemoteFunctionsScripts = RemoteFunctions;
336+
function addRemoteFunctionScript(scriptName, scriptText) {
337+
if(remoteFunctionsScripts.has(scriptName)){
338+
console.error(`Remote function script ${scriptName} already exists. Script wont be added.`);
339+
return false;
340+
}
341+
remoteFunctionsScripts.set(scriptName, scriptText);
342+
if(!RemoteFunctions.includes("// REPLACE_WITH_ADDED_REMOTE_SCRIPTS")){
343+
throw new Error("RemoteFunctions script is missing the placeholder // REPLACE_WITH_ADDED_REMOTE_SCRIPTS");
344+
}
345+
effectiveRemoteFunctionsScripts = RemoteFunctions.replace("// REPLACE_WITH_ADDED_REMOTE_SCRIPTS",
346+
Array.from(remoteFunctionsScripts.values()).join("\n"));
347+
return true;
348+
}
334349

335350
/**
336351
* Returns a script that should be injected into the HTML that's launched in the
@@ -343,7 +358,8 @@ define(function (require, exports, module) {
343358
// Inject DocumentObserver into the browser (tracks related documents)
344359
script += DocumentObserver;
345360
// Inject remote functions into the browser.
346-
script += "\nwindow._LD=(" + RemoteFunctions + "(" + JSON.stringify(LiveDevMultiBrowser.config) + "))";
361+
script += "\nwindow._LD=(" + effectiveRemoteFunctionsScripts +
362+
"(" + JSON.stringify(LiveDevMultiBrowser.config) + "))";
347363
return "\n" + script + "\n";
348364
}
349365

@@ -481,6 +497,7 @@ define(function (require, exports, module) {
481497
exports.close = close;
482498
exports.getConnectionIds = getConnectionIds;
483499
exports.closeAllConnections = closeAllConnections;
500+
exports.addRemoteFunctionScript = addRemoteFunctionScript;
484501
exports.LIVE_DEV_REMOTE_SCRIPTS_FILE_NAME = LIVE_DEV_REMOTE_SCRIPTS_FILE_NAME;
485502
exports.LIVE_DEV_REMOTE_WORKER_SCRIPTS_FILE_NAME = LIVE_DEV_REMOTE_WORKER_SCRIPTS_FILE_NAME;
486503
exports.EVENT_LIVE_PREVIEW_CLICKED = EVENT_LIVE_PREVIEW_CLICKED;

0 commit comments

Comments
 (0)