@@ -89,6 +89,16 @@ define(function (require, exports, module) {
8989 */
9090 var _responseDeferreds = { } ;
9191
92+ let _remoteFunctionProvider = null ;
93+
94+ /**
95+ * The callback fn must return a single text string that will be used as remote function script
96+ * @param callbackFn
97+ */
98+ function setCustomRemoteFunctionProvider ( callbackFn ) {
99+ _remoteFunctionProvider = callbackFn ;
100+ }
101+
92102 /**
93103 * Returns an array of the client IDs that are being managed by this live document.
94104 * @return {Array.<number> }
@@ -348,73 +358,6 @@ define(function (require, exports, module) {
348358 _transport . start ( ) ;
349359 }
350360
351- let remoteFunctionsScripts = new Map ( ) ;
352- let remoteFunctionsConstantsScripts = new Map ( ) ;
353- let effectiveRemoteFunctionsScripts = RemoteFunctions ;
354-
355- function _computeRemoteScript ( ) {
356- const remoteScriptText = Array . from ( remoteFunctionsScripts . values ( ) ) . join ( "\n" ) ;
357- const remoteConstantScriptText = Array . from ( remoteFunctionsConstantsScripts . values ( ) ) . join ( "\n" ) ;
358- effectiveRemoteFunctionsScripts = RemoteFunctions
359- . replace ( "// DONT_STRIP_MINIFY:REPLACE_WITH_ADDED_REMOTE_CONSTANT_SCRIPTS" , remoteConstantScriptText )
360- . replace ( "// DONT_STRIP_MINIFY:REPLACE_WITH_ADDED_REMOTE_SCRIPTS" , remoteScriptText ) ;
361- }
362-
363- const JS_RESERVED_WORDS = new Set ( [
364- "break" , "case" , "catch" , "class" , "const" , "continue" , "debugger" , "default" ,
365- "delete" , "do" , "else" , "export" , "extends" , "finally" , "for" , "function" ,
366- "if" , "import" , "in" , "instanceof" , "new" , "return" , "super" , "switch" ,
367- "this" , "throw" , "try" , "typeof" , "var" , "void" , "while" , "with" , "yield" ,
368- "enum" , "await" , "implements" , "interface" , "let" , "package" , "private" ,
369- "protected" , "public" , "static"
370- ] ) ;
371-
372- function isValidFunctionName ( name ) {
373- if ( typeof name !== "string" || ! name . length ) {
374- return false ;
375- }
376-
377- // JS identifier syntax
378- if ( ! / ^ [ $ A - Z _ ] [ 0 - 9 A - Z _ $ ] * $ / i. test ( name ) ) {
379- return false ;
380- }
381-
382- // Reserved words
383- return ! JS_RESERVED_WORDS . has ( name ) ;
384- }
385-
386-
387- function addRemoteFunctionScript ( scriptFunctionName , scriptText ) {
388- if ( remoteFunctionsConstantsScripts . has ( scriptFunctionName ) || remoteFunctionsScripts . has ( scriptFunctionName ) ) {
389- console . error ( `Remote function script ${ scriptFunctionName } already exists. Script wont be added.` ) ;
390- return false ;
391- }
392- if ( scriptFunctionName . length > 100 || ! isValidFunctionName ( scriptFunctionName ) ) {
393- console . error ( `Script name ${ scriptFunctionName } should be a valid function name.` ) ;
394- return false ;
395- }
396- scriptText = `(function ${ scriptFunctionName } (){${ scriptText } })();` ;
397- remoteFunctionsScripts . set ( scriptFunctionName , scriptText ) ;
398- if ( ! RemoteFunctions . includes ( "// DONT_STRIP_MINIFY:REPLACE_WITH_ADDED_REMOTE_SCRIPTS" ) ) {
399- throw new Error ( "RemoteFunctions script is missing the placeholder // REPLACE_WITH_ADDED_REMOTE_SCRIPTS" ) ;
400- }
401- _computeRemoteScript ( ) ;
402- return true ;
403- }
404-
405- function addRemoteFunctionConstantsScript ( scriptName , scriptText ) {
406- if ( remoteFunctionsConstantsScripts . has ( scriptName ) || remoteFunctionsScripts . has ( scriptName ) ) {
407- console . error ( `Remote function script ${ scriptName } already exists. Script wont be added.` ) ;
408- return false ;
409- }
410- remoteFunctionsConstantsScripts . set ( scriptName , scriptText ) ;
411- if ( ! RemoteFunctions . includes ( "// DONT_STRIP_MINIFY:REPLACE_WITH_ADDED_REMOTE_CONSTANT_SCRIPTS" ) ) {
412- throw new Error ( "RemoteFunctions script missing placeholder // REPLACE_WITH_ADDED_REMOTE_CONSTANT_SCRIPTS" ) ;
413- }
414- _computeRemoteScript ( ) ;
415- return true ;
416- }
417-
418361 /**
419362 * Returns a script that should be injected into the HTML that's launched in the
420363 * browser in order to implement remote commands that handle protocol requests.
@@ -426,8 +369,12 @@ define(function (require, exports, module) {
426369 // Inject DocumentObserver into the browser (tracks related documents)
427370 script += DocumentObserver ;
428371 // Inject remote functions into the browser.
429- script += "\nwindow._LD=(" + effectiveRemoteFunctionsScripts +
430- "(" + JSON . stringify ( LiveDevMultiBrowser . getConfig ( ) ) + "))" ;
372+ if ( _remoteFunctionProvider ) {
373+ script += _remoteFunctionProvider ( ) ;
374+ } else {
375+ script += "\nwindow._LD=(" + RemoteFunctions +
376+ "(" + JSON . stringify ( LiveDevMultiBrowser . getConfig ( ) ) + "))" ;
377+ }
431378 return "\n" + script + "\n" ;
432379 }
433380
@@ -565,9 +512,8 @@ define(function (require, exports, module) {
565512 exports . close = close ;
566513 exports . getConnectionIds = getConnectionIds ;
567514 exports . closeAllConnections = closeAllConnections ;
568- exports . addRemoteFunctionScript = addRemoteFunctionScript ;
569- exports . addRemoteFunctionConstantsScript = addRemoteFunctionConstantsScript ;
570515 exports . setLivePreviewMessageHandler = setLivePreviewMessageHandler ;
516+ exports . setCustomRemoteFunctionProvider = setCustomRemoteFunctionProvider ;
571517 exports . LIVE_DEV_REMOTE_SCRIPTS_FILE_NAME = LIVE_DEV_REMOTE_SCRIPTS_FILE_NAME ;
572518 exports . LIVE_DEV_REMOTE_WORKER_SCRIPTS_FILE_NAME = LIVE_DEV_REMOTE_WORKER_SCRIPTS_FILE_NAME ;
573519 exports . EVENT_LIVE_PREVIEW_CLICKED = EVENT_LIVE_PREVIEW_CLICKED ;
0 commit comments